root/spec/controllers/sessions_controller_spec.rb

Download in other formats: Raw | Text
Revisions
Dimitrij Denissenko
Dimitrij Denissenko
May 10 2009 * 15:23
(over 1 year ago)

Revision f2cdead0bed5206d98a951201bbdc2a256e6de39

Use Rails' default 'root' route

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe SessionsController do
  before do
    @user = mock_model(User, :time_zone => 'London')
    User.stub!(:current).and_return(@user)
  end


  describe 'GET /new (/login)' do    

    describe 'if not logged-in' do

      before do
        @user.should_receive(:public?).and_return(true)        
      end
      
      def do_get
        get :new
      end
      
      it_should_successfully_render_template('new')
    end

    describe 'if logged-in' do
      before do
        @user.should_receive(:public?).and_return(false)
      end
      
      def do_get
        get :new        
      end
      
      it_should_successfully_render_template('logged_in')
    end    
  end


  describe 'POST /create' do

    describe 'with invalid user-params' do

      def do_post
        post :create, :user => 'abcd'
      end

      it 'should redirect-to the login screen' do
        do_post
        response.should redirect_to(login_path)
      end

      it 'should reset the the session' do        
        session[:user_id] = 1
        do_post
        session[:user_id].should be_nil        
      end

    end


    describe 'with correct user-credentials' do

      before do
        User.stub!(:authenticate).and_return(@user)
      end

      def do_post
        post :create, :user => {:username => 'myaccount', :password => 'passW0rd'}        
      end

      it 'should authenticate user' do    
        User.should_receive(:authenticate).with('username' => "myaccount", 'password' => "passW0rd").and_return(@user)      
        do_post
      end

      it 'should confirm success' do    
        do_post
        flash[:notice].should_not be_blank
      end

      it 'should set session' do
        session[:user_id] = 1
        do_post
        session[:user_id].should == @user.id        
      end

      describe 'if back-to location is set' do
        it 'should redirect to it' do
          session[:back_to] = '/projects'
          do_post
          response.should redirect_to(projects_path)
        end
      end

      describe 'if no back-to location is set' do
        it 'should redirect to home' do
          do_post
          response.should redirect_to(root_path)
        end
      end

    end

    
    describe 'with incorrect user-credentials' do
      before do
        User.should_receive(:authenticate).and_return(nil)
      end

      def do_post
        post :create, :user => {:username => 'invalid', :password => 'wrong'}
      end
      
      it 'should confirm wrong credentials' do
        do_post
        flash[:error].should_not be_blank
      end

      it 'should redirect to login' do
        do_post
        response.should be_redirect
        response.should redirect_to(login_path)
      end    
    end
  end


  describe 'GET /destroy (/logout)' do    
    fixtures :users

    def do_get
      get :destroy      
    end
    
    it 'should reset session' do
      controller.should_receive(:reset_session)
      do_get
    end
    
    it_should_successfully_render_template('destroy')
  end


  describe 'GET /secure' do  
    it 'should not be allowed' do
      RetroCM[:general][:user_management].stub!(:[]).with(:secure_auth).and_return(true)
      get :secure
      response.code.to_i.should == 400      
    end
  end


  describe 'XHR-GET /secure' do  
    it 'should not be allowed' do
      RetroCM[:general][:user_management].stub!(:[]).with(:secure_auth).and_return(true)
      xhr :get, :secure
      response.code.to_i.should == 400      
    end
  end


  describe 'XHR-POST /secure' do    
    before do
      RetroCM[:general][:user_management].stub!(:[]).with(:secure_auth).and_return(true)      
    end

    describe 'without a username parameter' do  
      it 'should not be allowed' do
        xhr :post, :secure
        response.code.to_i.should == 400      
      end
    end

    describe 'when secure authentication is disabled' do  
      it 'should return nothing' do
        RetroCM[:general][:user_management].stub!(:[]).with(:secure_auth).and_return(false)
        xhr :post, :secure, :username => 'myaccount'
        response.should be_success
        response.body.should be_blank
      end
    end

    describe 'with a valid username parameter' do  
      before do
        User.should_receive(:find_by_username).with('myaccount').and_return(@user)
        @user.should_receive(:salt).and_return('QwErTyU')
        xhr :post, :secure, :username => 'myaccount'
      end

      it 'should return the user salt' do
        response.should be_success
        response.body.should == 'QwErTyU'
      end
    end

    describe 'with a invalid username parameter' do  
      before do
        User.should_receive(:find_by_username).with('invalid').and_return(nil)
        xhr :post, :secure, :username => 'invalid'
      end

      it 'should return nothing' do
        response.should be_success
        response.body.should be_blank
      end
    end
  end

end