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