| 1 |
require File.dirname(__FILE__) + '/../spec_helper' |
| 2 |
include OpenIdAuthentication |
| 3 |
|
| 4 |
describe SessionsController do |
| 5 |
|
| 6 |
def auth_token(token) |
| 7 |
CGI::Cookie.new('name' => 'auth_token', 'value' => token) |
| 8 |
end |
| 9 |
|
| 10 |
def cookie_for(user) |
| 11 |
auth_token users(user).remember_token |
| 12 |
end |
| 13 |
|
| 14 |
it "should login and redirect" do |
| 15 |
controller.stub!(:using_open_id?).and_return(false) |
| 16 |
post :create, :email => "johan@johansorensen.com", :password => "test" |
| 17 |
session[:user_id].should_not be(nil) |
| 18 |
response.should be_redirect |
| 19 |
end |
| 20 |
|
| 21 |
it "should login with openid and redirect" do |
| 22 |
identity_url = "http://patcito.myopenid.com" |
| 23 |
controller.stub!(:using_open_id?).and_return(true) |
| 24 |
controller.stub!(:successful?).and_return(false) |
| 25 |
controller.stub!(:authenticate_with_open_id).and_yield(Result[:successful],identity_url,registration={'nickname'=>"patcito",'email'=>"patcito@gmail.com",'fullname'=>'Patrick Aljord'}) |
| 26 |
post :create, :openid_url => identity_url |
| 27 |
session[:user_id].should_not be(nil) |
| 28 |
response.should be_redirect |
| 29 |
end |
| 30 |
|
| 31 |
it "should fail login and not redirect" do |
| 32 |
controller.stub!(:using_open_id?).and_return(false) |
| 33 |
post :create, :email => 'johan@johansorensen.com', :password => 'bad password' |
| 34 |
session[:user_id].should be(nil) |
| 35 |
response.should be_success |
| 36 |
end |
| 37 |
|
| 38 |
it "should logout" do |
| 39 |
login_as :johan |
| 40 |
get :destroy |
| 41 |
session[:user_id].should be(nil) |
| 42 |
response.should be_redirect |
| 43 |
end |
| 44 |
|
| 45 |
it "should remember me" do |
| 46 |
controller.stub!(:using_open_id?).and_return(false) |
| 47 |
post :create, :email => 'johan@johansorensen.com', :password => 'test', :remember_me => "1" |
| 48 |
response.cookies["auth_token"].should_not be(nil) |
| 49 |
end |
| 50 |
|
| 51 |
it "should should not remember me" do |
| 52 |
controller.stub!(:using_open_id?).and_return(false) |
| 53 |
post :create, :email => 'johan@johansorensen.com', :password => 'test', :remember_me => "0" |
| 54 |
response.cookies["auth_token"].should be(nil) |
| 55 |
end |
| 56 |
|
| 57 |
it "should delete token on logout" do |
| 58 |
login_as :johan |
| 59 |
get :destroy |
| 60 |
response.cookies["auth_token"].should == [] |
| 61 |
end |
| 62 |
|
| 63 |
it "should login with cookie" do |
| 64 |
users(:johan).remember_me |
| 65 |
request.cookies["auth_token"] = cookie_for(:johan) |
| 66 |
get :new |
| 67 |
controller.send(:logged_in?).should be(true) |
| 68 |
end |
| 69 |
|
| 70 |
it "should fail when trying to login with with expired cookie" do |
| 71 |
users(:johan).remember_me |
| 72 |
users(:johan).update_attribute :remember_token_expires_at, 5.minutes.ago.utc |
| 73 |
request.cookies["auth_token"] = cookie_for(:johan) |
| 74 |
get :new |
| 75 |
controller.send(:logged_in?).should be(false) |
| 76 |
end |
| 77 |
|
| 78 |
it "should fail cookie login" do |
| 79 |
users(:johan).remember_me |
| 80 |
@request.cookies["auth_token"] = auth_token('invalid_auth_token') |
| 81 |
get :new |
| 82 |
@controller.send(:logged_in?).should be(false) |
| 83 |
end |
| 84 |
|
| 85 |
it "should set current user to the session user_id" do |
| 86 |
session[:user_id] = users(:johan).id |
| 87 |
get :new |
| 88 |
controller.send(:current_user).should == users(:johan) |
| 89 |
end |
| 90 |
|
| 91 |
it "should show flash when invalid credentials are passed" do |
| 92 |
controller.stub!(:using_open_id?).and_return(false) |
| 93 |
post :create, :email => "invalid", :password => "also invalid" |
| 94 |
|
| 95 |
|
| 96 |
end |
| 97 |
end |