asciinema.org/spec/controllers/sessions_controller_spec.rb

120 lines
2.7 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe SessionsController do
describe "#create" do
let(:provider) { "twitter" }
let(:uid) { 1234 }
2012-03-05 23:25:21 +00:00
let(:nickname) { "mrFoo" }
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 }
}
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
@controller.current_user.should_not be_nil
end
it "should redirects user to root url" do
2012-07-29 17:43:03 +00:00
flash[:notice].should == "Logged in!"
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-03-05 23:25:21 +00:00
context "when nicknamne is not taken" do
it "should call create_with_omniauth" do
User.should_receive(:create_with_omniauth).
with(auth).
and_return(user)
2012-03-05 23:25:21 +00:00
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
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-03-05 23:25:21 +00:00
before do
User.stub(:create_with_omniauth).and_return(not_saved_user)
end
2012-03-10 13:58:10 +00:00
it "puts uid and provider in session " do
post :create
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
post :create
should render_template('users/new')
end
end
2012-03-05 23:25:21 +00:00
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
let(:message) { "something went wrong" }
before do
get :failure, :message => message
end
it "should redirects to root_url and set error message" do
flash[:alert].should == message
should redirect_to(root_url)
end
end
end