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 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
OmniAuth.config.mock_auth[:twitter] = {
|
|
|
|
"provider" => provider,
|
|
|
|
"uid" => uid
|
|
|
|
}
|
|
|
|
|
|
|
|
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
|
|
|
|
end
|
|
|
|
|
|
|
|
context "user exists" do
|
|
|
|
before do
|
|
|
|
Factory(:user, :provider => provider, :uid => uid)
|
|
|
|
post :create
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should create session" do
|
|
|
|
session[:user_id].should_not be_nil
|
2012-02-26 17:13:46 +00:00
|
|
|
assigns[:current_user].should_not be_nil
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should redirects user to root url" do
|
2012-02-26 17:13:46 +00:00
|
|
|
flash[:notice].should == "Signed 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"] }
|
|
|
|
let(:user) { stub("user", :id => 1) }
|
|
|
|
|
|
|
|
it "should call create_with_omniauth" do
|
|
|
|
User.should_receive(:create_with_omniauth).
|
|
|
|
with(auth).
|
|
|
|
and_return(user)
|
|
|
|
|
|
|
|
post :create
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should login user" do
|
|
|
|
User.stub(:create_with_omniauth).and_return(user)
|
|
|
|
|
|
|
|
post :create
|
|
|
|
|
|
|
|
session[:user_id].should_not be_nil
|
|
|
|
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"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should destroy session" do
|
|
|
|
delete :destroy
|
|
|
|
session[:user_id].should be_nil
|
2012-02-26 17:13:46 +00:00
|
|
|
assigns[: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
|
|
|
|
delete :destroy
|
|
|
|
flash[:notice].should == "Signed out!"
|
|
|
|
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
|
|
|
|
flash[:error].should == message
|
|
|
|
should redirect_to(root_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|