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
|
2012-02-25 22:43:17 +00:00
|
|
|
let(:provider) { "twitter" }
|
|
|
|
let(:uid) { 1234 }
|
2012-03-05 23:25:21 +00:00
|
|
|
let(:nickname) { "mrFoo" }
|
2012-02-25 22:43:17 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
OmniAuth.config.mock_auth[:twitter] = {
|
|
|
|
"provider" => provider,
|
2012-03-05 23:25:21 +00:00
|
|
|
"uid" => uid,
|
2012-03-13 21:18:13 +00:00
|
|
|
"info" => { "nickname" => nickname }
|
2012-02-25 22:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
|
|
|
|
end
|
|
|
|
|
|
|
|
context "user exists" do
|
|
|
|
before do
|
2012-12-01 17:45:06 +00:00
|
|
|
FactoryGirl.create(:user, :provider => provider, :uid => uid)
|
2012-12-01 18:18:34 +00:00
|
|
|
get :create, :provider => provider
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should create session" do
|
|
|
|
session[:user_id].should_not be_nil
|
2012-02-26 19:10:09 +00:00
|
|
|
@controller.current_user.should_not be_nil
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should redirects user to root url" do
|
2012-07-29 17:43:03 +00:00
|
|
|
flash[:notice].should == "Logged in!"
|
2012-02-25 22:43:17 +00:00
|
|
|
should redirect_to(root_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "user doesn't exist" do
|
|
|
|
let(:auth) { request.env["omniauth.auth"] }
|
2012-03-05 23:25:21 +00:00
|
|
|
let(:user) { stub("user", :id => 1, :persisted? => true) }
|
2012-02-25 22:43:17 +00:00
|
|
|
|
2012-12-01 18:18:34 +00:00
|
|
|
context "when nickname is not taken" do
|
2012-03-05 23:25:21 +00:00
|
|
|
it "should call create_with_omniauth" do
|
|
|
|
User.should_receive(:create_with_omniauth).
|
|
|
|
with(auth).
|
|
|
|
and_return(user)
|
2012-02-25 22:43:17 +00:00
|
|
|
|
2012-12-01 18:18:34 +00:00
|
|
|
get :create, :provider => provider
|
2012-03-05 23:25:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should login user" do
|
|
|
|
User.stub(:create_with_omniauth).and_return(user)
|
|
|
|
|
2012-12-01 18:18:34 +00:00
|
|
|
get :create, :provider => provider
|
2012-03-05 23:25:21 +00:00
|
|
|
|
|
|
|
session[:user_id].should_not be_nil
|
|
|
|
end
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|
|
|
|
|
2012-03-05 23:25:21 +00:00
|
|
|
context "when nicknamne is taken" do
|
|
|
|
let(:not_saved_user) {
|
2012-03-13 21:18:13 +00:00
|
|
|
stub_model(User,
|
2012-03-10 13:58:10 +00:00
|
|
|
:persisted? => false,
|
|
|
|
:valid? => false,
|
|
|
|
:uid => uid,
|
|
|
|
:provider => provider
|
|
|
|
)
|
2012-03-05 23:25:21 +00:00
|
|
|
}
|
2012-02-25 22:43:17 +00:00
|
|
|
|
2012-03-05 23:25:21 +00:00
|
|
|
before do
|
|
|
|
User.stub(:create_with_omniauth).and_return(not_saved_user)
|
|
|
|
end
|
2012-02-25 22:43:17 +00:00
|
|
|
|
2012-03-10 13:58:10 +00:00
|
|
|
it "puts uid and provider in session " do
|
2012-12-01 18:18:34 +00:00
|
|
|
get :create, :provider => provider
|
2012-03-10 13:58:10 +00:00
|
|
|
|
2012-04-09 13:56:26 +00:00
|
|
|
session[:new_user][:uid].should == uid
|
|
|
|
session[:new_user][:provider].should == provider
|
2012-03-10 13:58:10 +00:00
|
|
|
end
|
|
|
|
|
2012-03-05 23:25:21 +00:00
|
|
|
it "renders user/new" do
|
2012-12-01 18:18:34 +00:00
|
|
|
get :create, :provider => provider
|
2012-03-05 23:25:21 +00:00
|
|
|
should render_template('users/new')
|
|
|
|
end
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|
2012-03-05 23:25:21 +00:00
|
|
|
|
2012-02-25 22:43:17 +00:00
|
|
|
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
|
|
|
|
let(:message) { "something went wrong" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
get :failure, :message => message
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redirects to root_url and set error message" do
|
2012-02-26 17:40:51 +00:00
|
|
|
flash[:alert].should == message
|
2012-02-26 17:13:46 +00:00
|
|
|
should redirect_to(root_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|