asciinema.org/spec/controllers/sessions_controller_spec.rb

103 lines
2.2 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe SessionsController do
describe "#create" do
subject { get :create, :provider => 'twitter' }
shared_examples_for 'successful path' do
it "creates the session" do
subject
expect(controller).to have_received(:current_user=).with(user)
end
it "sets the flash message" do
subject
expect(flash[:notice]).to eq('Logged in!')
end
it "redirects to the root url" do
expect(subject).to redirect_to(root_url)
end
end
before do
request.env['asciiio.user'] = user
allow(controller).to receive(:current_user=)
end
context "when user was persisted" do
let(:user) { mock_model(User) }
it_behaves_like 'successful path'
end
2012-03-05 23:25:21 +00:00
context "when user doesn't exist" do
let(:user_attributes) { {
:uid => '1234',
:provider => 'github',
:avatar_url => 'http://foo'
} }
2012-03-05 23:25:21 +00:00
let(:user) { mock_model(User, user_attributes).as_new_record }
2012-03-05 23:25:21 +00:00
context "and creation succeeds" do
before do
allow(user).to receive(:save) { true }
2012-03-05 23:25:21 +00:00
end
it_behaves_like 'successful path'
end
context "and creation fails" do
2012-03-05 23:25:21 +00:00
before do
allow(user).to receive(:save) { false }
2012-03-05 23:25:21 +00:00
end
it "stores uid and provider in session " do
subject
2012-03-10 13:58:10 +00:00
expect(session[:new_user]).to eq({
:uid => '1234', :provider => 'github', :avatar_url => 'http://foo'
})
2012-03-10 13:58:10 +00:00
end
it "renders users/new" do
expect(subject).to render_template('users/new')
2012-03-05 23:25:21 +00:00
end
end
end
end
describe "#destroy" do
before do
session[:user_id] = "123"
2012-02-26 19:09:07 +00:00
get :destroy
end
it "should destroy session" do
session[:user_id].should be_nil
@controller.current_user.should be_nil
end
it "should redirects to root_url" do
2012-07-29 17:43:03 +00:00
flash[:notice].should == "Logged out!"
should redirect_to(root_url)
end
end
describe "#failure" do
before do
2013-05-27 21:44:12 +00:00
get :failure
end
2013-05-27 21:44:12 +00:00
it "should redirect to root_url and set error message" do
flash[:alert].should =~ /Authentication failed/
should redirect_to(root_url)
end
end
end