Chris@1494
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
|
Chris@1494
|
3 #
|
Chris@1494
|
4 # This program is free software; you can redistribute it and/or
|
Chris@1494
|
5 # modify it under the terms of the GNU General Public License
|
Chris@1494
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@1494
|
7 # of the License, or (at your option) any later version.
|
Chris@1494
|
8 #
|
Chris@1494
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@1494
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@1494
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@1494
|
12 # GNU General Public License for more details.
|
Chris@1494
|
13 #
|
Chris@1494
|
14 # You should have received a copy of the GNU General Public License
|
Chris@1494
|
15 # along with this program; if not, write to the Free Software
|
Chris@1494
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@1494
|
17
|
Chris@1494
|
18 require File.expand_path('../../test_helper', __FILE__)
|
Chris@1494
|
19
|
Chris@1494
|
20 class AccountControllerTest < ActionController::TestCase
|
Chris@1494
|
21 fixtures :users, :roles
|
Chris@1494
|
22
|
Chris@1494
|
23 def setup
|
Chris@1494
|
24 User.current = nil
|
Chris@1494
|
25 end
|
Chris@1494
|
26
|
Chris@1494
|
27 def test_get_login
|
Chris@1494
|
28 get :login
|
Chris@1494
|
29 assert_response :success
|
Chris@1494
|
30 assert_template 'login'
|
Chris@1494
|
31
|
Chris@1494
|
32 assert_select 'input[name=username]'
|
Chris@1494
|
33 assert_select 'input[name=password]'
|
Chris@1494
|
34 end
|
Chris@1494
|
35
|
Chris@1494
|
36 def test_get_login_while_logged_in_should_redirect_to_home
|
Chris@1494
|
37 @request.session[:user_id] = 2
|
Chris@1494
|
38
|
Chris@1494
|
39 get :login
|
Chris@1494
|
40 assert_redirected_to '/'
|
Chris@1494
|
41 assert_equal 2, @request.session[:user_id]
|
Chris@1494
|
42 end
|
Chris@1494
|
43
|
Chris@1494
|
44 def test_login_should_redirect_to_back_url_param
|
Chris@1494
|
45 # request.uri is "test.host" in test environment
|
Chris@1494
|
46 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1'
|
Chris@1494
|
47 assert_redirected_to '/issues/show/1'
|
Chris@1494
|
48 end
|
Chris@1494
|
49
|
Chris@1494
|
50 def test_login_should_not_redirect_to_another_host
|
Chris@1494
|
51 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake'
|
Chris@1494
|
52 assert_redirected_to '/my/page'
|
Chris@1494
|
53 end
|
Chris@1494
|
54
|
Chris@1494
|
55 def test_login_with_wrong_password
|
Chris@1494
|
56 post :login, :username => 'admin', :password => 'bad'
|
Chris@1494
|
57 assert_response :success
|
Chris@1494
|
58 assert_template 'login'
|
Chris@1494
|
59
|
Chris@1494
|
60 assert_select 'div.flash.error', :text => /Invalid user or password/
|
Chris@1494
|
61 assert_select 'input[name=username][value=admin]'
|
Chris@1494
|
62 assert_select 'input[name=password]'
|
Chris@1494
|
63 assert_select 'input[name=password][value]', 0
|
Chris@1494
|
64 end
|
Chris@1494
|
65
|
Chris@1494
|
66 def test_login_with_locked_account_should_fail
|
Chris@1494
|
67 User.find(2).update_attribute :status, User::STATUS_LOCKED
|
Chris@1494
|
68
|
Chris@1494
|
69 post :login, :username => 'jsmith', :password => 'jsmith'
|
Chris@1494
|
70 assert_redirected_to '/login'
|
Chris@1494
|
71 assert_include 'locked', flash[:error]
|
Chris@1494
|
72 assert_nil @request.session[:user_id]
|
Chris@1494
|
73 end
|
Chris@1494
|
74
|
Chris@1494
|
75 def test_login_as_registered_user_with_manual_activation_should_inform_user
|
Chris@1494
|
76 User.find(2).update_attribute :status, User::STATUS_REGISTERED
|
Chris@1494
|
77
|
Chris@1494
|
78 with_settings :self_registration => '2', :default_language => 'en' do
|
Chris@1494
|
79 post :login, :username => 'jsmith', :password => 'jsmith'
|
Chris@1494
|
80 assert_redirected_to '/login'
|
Chris@1494
|
81 assert_include 'pending administrator approval', flash[:error]
|
Chris@1494
|
82 end
|
Chris@1494
|
83 end
|
Chris@1494
|
84
|
Chris@1494
|
85 def test_login_as_registered_user_with_email_activation_should_propose_new_activation_email
|
Chris@1494
|
86 User.find(2).update_attribute :status, User::STATUS_REGISTERED
|
Chris@1494
|
87
|
Chris@1494
|
88 with_settings :self_registration => '1', :default_language => 'en' do
|
Chris@1494
|
89 post :login, :username => 'jsmith', :password => 'jsmith'
|
Chris@1494
|
90 assert_redirected_to '/login'
|
Chris@1494
|
91 assert_equal 2, @request.session[:registered_user_id]
|
Chris@1494
|
92 assert_include 'new activation email', flash[:error]
|
Chris@1494
|
93 end
|
Chris@1494
|
94 end
|
Chris@1494
|
95
|
Chris@1494
|
96 def test_login_should_rescue_auth_source_exception
|
Chris@1494
|
97 source = AuthSource.create!(:name => 'Test')
|
Chris@1494
|
98 User.find(2).update_attribute :auth_source_id, source.id
|
Chris@1494
|
99 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
|
Chris@1494
|
100
|
Chris@1494
|
101 post :login, :username => 'jsmith', :password => 'jsmith'
|
Chris@1494
|
102 assert_response 500
|
Chris@1494
|
103 assert_error_tag :content => /Something wrong/
|
Chris@1494
|
104 end
|
Chris@1494
|
105
|
Chris@1494
|
106 def test_login_should_reset_session
|
Chris@1494
|
107 @controller.expects(:reset_session).once
|
Chris@1494
|
108
|
Chris@1494
|
109 post :login, :username => 'jsmith', :password => 'jsmith'
|
Chris@1494
|
110 assert_response 302
|
Chris@1494
|
111 end
|
Chris@1494
|
112
|
Chris@1494
|
113 def test_get_logout_should_not_logout
|
Chris@1494
|
114 @request.session[:user_id] = 2
|
Chris@1494
|
115 get :logout
|
Chris@1494
|
116 assert_response :success
|
Chris@1494
|
117 assert_template 'logout'
|
Chris@1494
|
118
|
Chris@1494
|
119 assert_equal 2, @request.session[:user_id]
|
Chris@1494
|
120 end
|
Chris@1494
|
121
|
Chris@1494
|
122 def test_get_logout_with_anonymous_should_redirect
|
Chris@1494
|
123 get :logout
|
Chris@1494
|
124 assert_redirected_to '/'
|
Chris@1494
|
125 end
|
Chris@1494
|
126
|
Chris@1494
|
127 def test_logout
|
Chris@1494
|
128 @request.session[:user_id] = 2
|
Chris@1494
|
129 post :logout
|
Chris@1494
|
130 assert_redirected_to '/'
|
Chris@1494
|
131 assert_nil @request.session[:user_id]
|
Chris@1494
|
132 end
|
Chris@1494
|
133
|
Chris@1494
|
134 def test_logout_should_reset_session
|
Chris@1494
|
135 @controller.expects(:reset_session).once
|
Chris@1494
|
136
|
Chris@1494
|
137 @request.session[:user_id] = 2
|
Chris@1494
|
138 post :logout
|
Chris@1494
|
139 assert_response 302
|
Chris@1494
|
140 end
|
Chris@1494
|
141
|
Chris@1494
|
142 def test_get_register_with_registration_on
|
Chris@1494
|
143 with_settings :self_registration => '3' do
|
Chris@1494
|
144 get :register
|
Chris@1494
|
145 assert_response :success
|
Chris@1494
|
146 assert_template 'register'
|
Chris@1494
|
147 assert_not_nil assigns(:user)
|
Chris@1494
|
148
|
Chris@1494
|
149 assert_select 'input[name=?]', 'user[password]'
|
Chris@1494
|
150 assert_select 'input[name=?]', 'user[password_confirmation]'
|
Chris@1494
|
151 end
|
Chris@1494
|
152 end
|
Chris@1494
|
153
|
Chris@1494
|
154 def test_get_register_should_detect_user_language
|
Chris@1494
|
155 with_settings :self_registration => '3' do
|
Chris@1494
|
156 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
|
Chris@1494
|
157 get :register
|
Chris@1494
|
158 assert_response :success
|
Chris@1494
|
159 assert_not_nil assigns(:user)
|
Chris@1494
|
160 assert_equal 'fr', assigns(:user).language
|
Chris@1494
|
161 assert_select 'select[name=?]', 'user[language]' do
|
Chris@1494
|
162 assert_select 'option[value=fr][selected=selected]'
|
Chris@1494
|
163 end
|
Chris@1494
|
164 end
|
Chris@1494
|
165 end
|
Chris@1494
|
166
|
Chris@1494
|
167 def test_get_register_with_registration_off_should_redirect
|
Chris@1494
|
168 with_settings :self_registration => '0' do
|
Chris@1494
|
169 get :register
|
Chris@1494
|
170 assert_redirected_to '/'
|
Chris@1494
|
171 end
|
Chris@1494
|
172 end
|
Chris@1494
|
173
|
Chris@1494
|
174 # See integration/account_test.rb for the full test
|
Chris@1494
|
175 def test_post_register_with_registration_on
|
Chris@1494
|
176 with_settings :self_registration => '3' do
|
Chris@1494
|
177 assert_difference 'User.count' do
|
Chris@1494
|
178 post :register, :user => {
|
Chris@1494
|
179 :login => 'register',
|
Chris@1494
|
180 :password => 'secret123',
|
Chris@1494
|
181 :password_confirmation => 'secret123',
|
Chris@1494
|
182 :firstname => 'John',
|
Chris@1494
|
183 :lastname => 'Doe',
|
Chris@1494
|
184 :mail => 'register@example.com'
|
Chris@1494
|
185 }
|
Chris@1494
|
186 assert_redirected_to '/my/account'
|
Chris@1494
|
187 end
|
Chris@1494
|
188 user = User.first(:order => 'id DESC')
|
Chris@1494
|
189 assert_equal 'register', user.login
|
Chris@1494
|
190 assert_equal 'John', user.firstname
|
Chris@1494
|
191 assert_equal 'Doe', user.lastname
|
Chris@1494
|
192 assert_equal 'register@example.com', user.mail
|
Chris@1494
|
193 assert user.check_password?('secret123')
|
Chris@1494
|
194 assert user.active?
|
Chris@1494
|
195 end
|
Chris@1494
|
196 end
|
Chris@1494
|
197
|
Chris@1494
|
198 def test_post_register_with_registration_off_should_redirect
|
Chris@1494
|
199 with_settings :self_registration => '0' do
|
Chris@1494
|
200 assert_no_difference 'User.count' do
|
Chris@1494
|
201 post :register, :user => {
|
Chris@1494
|
202 :login => 'register',
|
Chris@1494
|
203 :password => 'test',
|
Chris@1494
|
204 :password_confirmation => 'test',
|
Chris@1494
|
205 :firstname => 'John',
|
Chris@1494
|
206 :lastname => 'Doe',
|
Chris@1494
|
207 :mail => 'register@example.com'
|
Chris@1494
|
208 }
|
Chris@1494
|
209 assert_redirected_to '/'
|
Chris@1494
|
210 end
|
Chris@1494
|
211 end
|
Chris@1494
|
212 end
|
Chris@1494
|
213
|
Chris@1494
|
214 def test_get_lost_password_should_display_lost_password_form
|
Chris@1494
|
215 get :lost_password
|
Chris@1494
|
216 assert_response :success
|
Chris@1494
|
217 assert_select 'input[name=mail]'
|
Chris@1494
|
218 end
|
Chris@1494
|
219
|
Chris@1494
|
220 def test_lost_password_for_active_user_should_create_a_token
|
Chris@1494
|
221 Token.delete_all
|
Chris@1494
|
222 ActionMailer::Base.deliveries.clear
|
Chris@1494
|
223 assert_difference 'ActionMailer::Base.deliveries.size' do
|
Chris@1494
|
224 assert_difference 'Token.count' do
|
Chris@1494
|
225 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
|
Chris@1494
|
226 post :lost_password, :mail => 'JSmith@somenet.foo'
|
Chris@1494
|
227 assert_redirected_to '/login'
|
Chris@1494
|
228 end
|
Chris@1494
|
229 end
|
Chris@1494
|
230 end
|
Chris@1494
|
231
|
Chris@1494
|
232 token = Token.order('id DESC').first
|
Chris@1494
|
233 assert_equal User.find(2), token.user
|
Chris@1494
|
234 assert_equal 'recovery', token.action
|
Chris@1494
|
235
|
Chris@1494
|
236 assert_select_email do
|
Chris@1494
|
237 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
|
Chris@1494
|
238 end
|
Chris@1494
|
239 end
|
Chris@1494
|
240
|
Chris@1494
|
241 def test_lost_password_for_unknown_user_should_fail
|
Chris@1494
|
242 Token.delete_all
|
Chris@1494
|
243 assert_no_difference 'Token.count' do
|
Chris@1494
|
244 post :lost_password, :mail => 'invalid@somenet.foo'
|
Chris@1494
|
245 assert_response :success
|
Chris@1494
|
246 end
|
Chris@1494
|
247 end
|
Chris@1494
|
248
|
Chris@1494
|
249 def test_lost_password_for_non_active_user_should_fail
|
Chris@1494
|
250 Token.delete_all
|
Chris@1494
|
251 assert User.find(2).lock!
|
Chris@1494
|
252
|
Chris@1494
|
253 assert_no_difference 'Token.count' do
|
Chris@1494
|
254 post :lost_password, :mail => 'JSmith@somenet.foo'
|
Chris@1494
|
255 assert_redirected_to '/account/lost_password'
|
Chris@1494
|
256 end
|
Chris@1494
|
257 end
|
Chris@1494
|
258
|
Chris@1494
|
259 def test_lost_password_for_user_who_cannot_change_password_should_fail
|
Chris@1494
|
260 User.any_instance.stubs(:change_password_allowed?).returns(false)
|
Chris@1494
|
261
|
Chris@1494
|
262 assert_no_difference 'Token.count' do
|
Chris@1494
|
263 post :lost_password, :mail => 'JSmith@somenet.foo'
|
Chris@1494
|
264 assert_response :success
|
Chris@1494
|
265 end
|
Chris@1494
|
266 end
|
Chris@1494
|
267
|
Chris@1494
|
268 def test_get_lost_password_with_token_should_display_the_password_recovery_form
|
Chris@1494
|
269 user = User.find(2)
|
Chris@1494
|
270 token = Token.create!(:action => 'recovery', :user => user)
|
Chris@1494
|
271
|
Chris@1494
|
272 get :lost_password, :token => token.value
|
Chris@1494
|
273 assert_response :success
|
Chris@1494
|
274 assert_template 'password_recovery'
|
Chris@1494
|
275
|
Chris@1494
|
276 assert_select 'input[type=hidden][name=token][value=?]', token.value
|
Chris@1494
|
277 end
|
Chris@1494
|
278
|
Chris@1494
|
279 def test_get_lost_password_with_invalid_token_should_redirect
|
Chris@1494
|
280 get :lost_password, :token => "abcdef"
|
Chris@1494
|
281 assert_redirected_to '/'
|
Chris@1494
|
282 end
|
Chris@1494
|
283
|
Chris@1494
|
284 def test_post_lost_password_with_token_should_change_the_user_password
|
Chris@1494
|
285 user = User.find(2)
|
Chris@1494
|
286 token = Token.create!(:action => 'recovery', :user => user)
|
Chris@1494
|
287
|
Chris@1494
|
288 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
|
Chris@1494
|
289 assert_redirected_to '/login'
|
Chris@1494
|
290 user.reload
|
Chris@1494
|
291 assert user.check_password?('newpass123')
|
Chris@1494
|
292 assert_nil Token.find_by_id(token.id), "Token was not deleted"
|
Chris@1494
|
293 end
|
Chris@1494
|
294
|
Chris@1494
|
295 def test_post_lost_password_with_token_for_non_active_user_should_fail
|
Chris@1494
|
296 user = User.find(2)
|
Chris@1494
|
297 token = Token.create!(:action => 'recovery', :user => user)
|
Chris@1494
|
298 user.lock!
|
Chris@1494
|
299
|
Chris@1494
|
300 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
|
Chris@1494
|
301 assert_redirected_to '/'
|
Chris@1494
|
302 assert ! user.check_password?('newpass123')
|
Chris@1494
|
303 end
|
Chris@1494
|
304
|
Chris@1494
|
305 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form
|
Chris@1494
|
306 user = User.find(2)
|
Chris@1494
|
307 token = Token.create!(:action => 'recovery', :user => user)
|
Chris@1494
|
308
|
Chris@1494
|
309 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass'
|
Chris@1494
|
310 assert_response :success
|
Chris@1494
|
311 assert_template 'password_recovery'
|
Chris@1494
|
312 assert_not_nil Token.find_by_id(token.id), "Token was deleted"
|
Chris@1494
|
313
|
Chris@1494
|
314 assert_select 'input[type=hidden][name=token][value=?]', token.value
|
Chris@1494
|
315 end
|
Chris@1494
|
316
|
Chris@1494
|
317 def test_post_lost_password_with_invalid_token_should_redirect
|
Chris@1494
|
318 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass'
|
Chris@1494
|
319 assert_redirected_to '/'
|
Chris@1494
|
320 end
|
Chris@1494
|
321
|
Chris@1494
|
322 def test_activation_email_should_send_an_activation_email
|
Chris@1494
|
323 User.find(2).update_attribute :status, User::STATUS_REGISTERED
|
Chris@1494
|
324 @request.session[:registered_user_id] = 2
|
Chris@1494
|
325
|
Chris@1494
|
326 with_settings :self_registration => '1' do
|
Chris@1494
|
327 assert_difference 'ActionMailer::Base.deliveries.size' do
|
Chris@1494
|
328 get :activation_email
|
Chris@1494
|
329 assert_redirected_to '/login'
|
Chris@1494
|
330 end
|
Chris@1494
|
331 end
|
Chris@1494
|
332 end
|
Chris@1494
|
333
|
Chris@1494
|
334 def test_activation_email_without_session_data_should_fail
|
Chris@1494
|
335 User.find(2).update_attribute :status, User::STATUS_REGISTERED
|
Chris@1494
|
336
|
Chris@1494
|
337 with_settings :self_registration => '1' do
|
Chris@1494
|
338 assert_no_difference 'ActionMailer::Base.deliveries.size' do
|
Chris@1494
|
339 get :activation_email
|
Chris@1494
|
340 assert_redirected_to '/'
|
Chris@1494
|
341 end
|
Chris@1494
|
342 end
|
Chris@1494
|
343 end
|
Chris@1494
|
344 end
|