~matteo-collina/+junk/gipieffe

« back to all changes in this revision

Viewing changes to spec/controllers/authenticated_system_spec.rb

  • Committer: Matteo Collina
  • Date: 2008-11-02 09:46:31 UTC
  • Revision ID: matteo.collina@gmail.com-20081102094631-h9w18le32hew28xp
 * Generated login system by the restful-authentication-i18n plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.dirname(__FILE__) + '/../spec_helper'
 
2
 
 
3
# Be sure to include AuthenticatedTestHelper in spec/spec_helper.rb instead
 
4
# Then, you can remove it from this and the units test.
 
5
include AuthenticatedTestHelper
 
6
include AuthenticatedSystem
 
7
def action_name() end
 
8
 
 
9
describe SessionsController do
 
10
  fixtures :associates
 
11
  
 
12
  before do
 
13
    # FIXME -- sessions controller not testing xml logins 
 
14
    stub!(:authenticate_with_http_basic).and_return nil
 
15
  end    
 
16
  describe "logout_killing_session!" do
 
17
    before do
 
18
      login_as :quentin
 
19
      stub!(:reset_session)
 
20
    end
 
21
    it 'resets the session'         do should_receive(:reset_session);         logout_killing_session! end
 
22
    it 'kills my auth_token cookie' do should_receive(:kill_remember_cookie!); logout_killing_session! end
 
23
    it 'nils the current associate'      do logout_killing_session!; current_associate.should be_nil end
 
24
    it 'kills :associate_id session' do
 
25
      session.stub!(:[]=)
 
26
      session.should_receive(:[]=).with(:associate_id, nil).at_least(:once)
 
27
      logout_killing_session!
 
28
    end
 
29
    it 'forgets me' do    
 
30
      current_associate.remember_me
 
31
      current_associate.remember_token.should_not be_nil; current_associate.remember_token_expires_at.should_not be_nil
 
32
      Associate.find(1).remember_token.should_not be_nil; Associate.find(1).remember_token_expires_at.should_not be_nil
 
33
      logout_killing_session!
 
34
      Associate.find(1).remember_token.should     be_nil; Associate.find(1).remember_token_expires_at.should     be_nil
 
35
    end
 
36
  end
 
37
 
 
38
  describe "logout_keeping_session!" do
 
39
    before do
 
40
      login_as :quentin
 
41
      stub!(:reset_session)
 
42
    end
 
43
    it 'does not reset the session' do should_not_receive(:reset_session);   logout_keeping_session! end
 
44
    it 'kills my auth_token cookie' do should_receive(:kill_remember_cookie!); logout_keeping_session! end
 
45
    it 'nils the current associate'      do logout_keeping_session!; current_associate.should be_nil end
 
46
    it 'kills :associate_id session' do
 
47
      session.stub!(:[]=)
 
48
      session.should_receive(:[]=).with(:associate_id, nil).at_least(:once)
 
49
      logout_keeping_session!
 
50
    end
 
51
    it 'forgets me' do    
 
52
      current_associate.remember_me
 
53
      current_associate.remember_token.should_not be_nil; current_associate.remember_token_expires_at.should_not be_nil
 
54
      Associate.find(1).remember_token.should_not be_nil; Associate.find(1).remember_token_expires_at.should_not be_nil
 
55
      logout_keeping_session!
 
56
      Associate.find(1).remember_token.should     be_nil; Associate.find(1).remember_token_expires_at.should     be_nil
 
57
    end
 
58
  end
 
59
  
 
60
  describe 'When logged out' do 
 
61
    it "should not be authorized?" do
 
62
      authorized?().should be_false
 
63
    end    
 
64
  end
 
65
 
 
66
  #
 
67
  # Cookie Login
 
68
  #
 
69
  describe "Logging in by cookie" do
 
70
    def set_remember_token token, time
 
71
      @associate[:remember_token]            = token; 
 
72
      @associate[:remember_token_expires_at] = time
 
73
      @associate.save!
 
74
    end    
 
75
    before do 
 
76
      @associate = Associate.find(:first); 
 
77
      set_remember_token 'hello!', 5.minutes.from_now
 
78
    end    
 
79
    it 'logs in with cookie' do
 
80
      stub!(:cookies).and_return({ :auth_token => 'hello!' })
 
81
      logged_in?.should be_true
 
82
    end
 
83
    
 
84
    it 'fails cookie login with bad cookie' do
 
85
      should_receive(:cookies).at_least(:once).and_return({ :auth_token => 'i_haxxor_joo' })
 
86
      logged_in?.should_not be_true
 
87
    end
 
88
    
 
89
    it 'fails cookie login with no cookie' do
 
90
      set_remember_token nil, nil
 
91
      should_receive(:cookies).at_least(:once).and_return({ })
 
92
      logged_in?.should_not be_true
 
93
    end
 
94
    
 
95
    it 'fails expired cookie login' do
 
96
      set_remember_token 'hello!', 5.minutes.ago
 
97
      stub!(:cookies).and_return({ :auth_token => 'hello!' })
 
98
      logged_in?.should_not be_true
 
99
    end
 
100
  end
 
101
  
 
102
end