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