##// END OF EJS Templates
Pass parameters with :params in controller tests....
Jean-Philippe Lang -
r15282:c364e6ee9b2e
parent child
Show More
@@ -1,596 +1,642
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 UsersControllerTest < Redmine::ControllerTest
21 21 include Redmine::I18n
22 22
23 23 fixtures :users, :email_addresses, :projects, :members, :member_roles, :roles,
24 24 :custom_fields, :custom_values, :groups_users,
25 25 :auth_sources,
26 26 :enabled_modules,
27 27 :issues, :issue_statuses,
28 28 :trackers
29 29
30 30 def setup
31 31 User.current = nil
32 32 @request.session[:user_id] = 1 # admin
33 33 end
34 34
35 35 def test_index
36 36 get :index
37 37 assert_response :success
38 38 assert_template 'index'
39 39 assert_not_nil assigns(:users)
40 40 # active users only
41 41 assert_nil assigns(:users).detect {|u| !u.active?}
42 42 end
43 43
44 44 def test_index_with_status_filter
45 get :index, :status => 3
45 get :index, :params => {:status => 3}
46 46 assert_response :success
47 47 assert_template 'index'
48 48 assert_not_nil assigns(:users)
49 49 assert_equal [3], assigns(:users).map(&:status).uniq
50 50 end
51 51
52 52 def test_index_with_name_filter
53 get :index, :name => 'john'
53 get :index, :params => {:name => 'john'}
54 54 assert_response :success
55 55 assert_template 'index'
56 56 users = assigns(:users)
57 57 assert_not_nil users
58 58 assert_equal 1, users.size
59 59 assert_equal 'John', users.first.firstname
60 60 end
61 61
62 62 def test_index_with_group_filter
63 get :index, :group_id => '10'
63 get :index, :params => {:group_id => '10'}
64 64 assert_response :success
65 65 assert_template 'index'
66 66 users = assigns(:users)
67 67 assert users.any?
68 68 assert_equal([], (users - Group.find(10).users))
69 69 assert_select 'select[name=group_id]' do
70 70 assert_select 'option[value="10"][selected=selected]'
71 71 end
72 72 end
73 73
74 74 def test_show
75 75 @request.session[:user_id] = nil
76 get :show, :id => 2
76 get :show, :params => {:id => 2}
77 77 assert_response :success
78 78 assert_template 'show'
79 79 assert_not_nil assigns(:user)
80 80
81 81 assert_select 'li', :text => /Phone number/
82 82 end
83 83
84 84 def test_show_should_not_display_hidden_custom_fields
85 85 @request.session[:user_id] = nil
86 86 UserCustomField.find_by_name('Phone number').update_attribute :visible, false
87 get :show, :id => 2
87 get :show, :params => {:id => 2}
88 88 assert_response :success
89 89 assert_template 'show'
90 90 assert_not_nil assigns(:user)
91 91
92 92 assert_select 'li', :text => /Phone number/, :count => 0
93 93 end
94 94
95 95 def test_show_should_not_fail_when_custom_values_are_nil
96 96 user = User.find(2)
97 97
98 98 # Create a custom field to illustrate the issue
99 99 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
100 100 custom_value = user.custom_values.build(:custom_field => custom_field).save!
101 101
102 get :show, :id => 2
102 get :show, :params => {:id => 2}
103 103 assert_response :success
104 104 end
105 105
106 106 def test_show_inactive
107 107 @request.session[:user_id] = nil
108 get :show, :id => 5
108 get :show, :params => {:id => 5}
109 109 assert_response 404
110 110 end
111 111
112 112 def test_show_inactive_by_admin
113 113 @request.session[:user_id] = 1
114 get :show, :id => 5
114 get :show, :params => {:id => 5}
115 115 assert_response 200
116 116 assert_not_nil assigns(:user)
117 117 end
118 118
119 119 def test_show_user_who_is_not_visible_should_return_404
120 120 Role.anonymous.update! :users_visibility => 'members_of_visible_projects'
121 121 user = User.generate!
122 122
123 123 @request.session[:user_id] = nil
124 get :show, :id => user.id
124 get :show, :params => {:id => user.id}
125 125 assert_response 404
126 126 end
127 127
128 128 def test_show_displays_memberships_based_on_project_visibility
129 129 @request.session[:user_id] = 1
130 get :show, :id => 2
130 get :show, :params => {:id => 2}
131 131 assert_response :success
132 132 memberships = assigns(:memberships)
133 133 assert_not_nil memberships
134 134 project_ids = memberships.map(&:project_id)
135 135 assert project_ids.include?(2) #private project admin can see
136 136 end
137 137
138 138 def test_show_current_should_require_authentication
139 139 @request.session[:user_id] = nil
140 get :show, :id => 'current'
140 get :show, :params => {:id => 'current'}
141 141 assert_response 302
142 142 end
143 143
144 144 def test_show_current
145 145 @request.session[:user_id] = 2
146 get :show, :id => 'current'
146 get :show, :params => {:id => 'current'}
147 147 assert_response :success
148 148 assert_template 'show'
149 149 assert_equal User.find(2), assigns(:user)
150 150 end
151 151
152 152 def test_new
153 153 get :new
154 154 assert_response :success
155 155 assert_template :new
156 156 assert assigns(:user)
157 157 end
158 158
159 159 def test_create
160 160 Setting.bcc_recipients = '1'
161 161
162 162 assert_difference 'User.count' do
163 163 assert_difference 'ActionMailer::Base.deliveries.size' do
164 post :create,
164 post :create, :params => {
165 165 :user => {
166 166 :firstname => 'John',
167 167 :lastname => 'Doe',
168 168 :login => 'jdoe',
169 169 :password => 'secret123',
170 170 :password_confirmation => 'secret123',
171 171 :mail => 'jdoe@gmail.com',
172 172 :mail_notification => 'none'
173 173 },
174 174 :send_information => '1'
175 }
175 176 end
176 177 end
177 178
178 179 user = User.order('id DESC').first
179 180 assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
180 181
181 182 assert_equal 'John', user.firstname
182 183 assert_equal 'Doe', user.lastname
183 184 assert_equal 'jdoe', user.login
184 185 assert_equal 'jdoe@gmail.com', user.mail
185 186 assert_equal 'none', user.mail_notification
186 187 assert user.check_password?('secret123')
187 188
188 189 mail = ActionMailer::Base.deliveries.last
189 190 assert_not_nil mail
190 191 assert_equal [user.mail], mail.bcc
191 192 assert_mail_body_match 'secret', mail
192 193 end
193 194
194 195 def test_create_with_preferences
195 196 assert_difference 'User.count' do
196 post :create,
197 post :create, :params => {
197 198 :user => {
198 199 :firstname => 'John',
199 200 :lastname => 'Doe',
200 201 :login => 'jdoe',
201 202 :password => 'secret123',
202 203 :password_confirmation => 'secret123',
203 204 :mail => 'jdoe@gmail.com',
204 205 :mail_notification => 'none'
205 206 },
206 207 :pref => {
207 208 'hide_mail' => '1',
208 209 'time_zone' => 'Paris',
209 210 'comments_sorting' => 'desc',
210 211 'warn_on_leaving_unsaved' => '0'
211 212 }
213 }
212 214 end
213 215 user = User.order('id DESC').first
214 216 assert_equal 'jdoe', user.login
215 217 assert_equal true, user.pref.hide_mail
216 218 assert_equal 'Paris', user.pref.time_zone
217 219 assert_equal 'desc', user.pref[:comments_sorting]
218 220 assert_equal '0', user.pref[:warn_on_leaving_unsaved]
219 221 end
220 222
221 223 def test_create_with_generate_password_should_email_the_password
222 224 assert_difference 'User.count' do
223 post :create, :user => {
224 :login => 'randompass',
225 :firstname => 'Random',
226 :lastname => 'Pass',
227 :mail => 'randompass@example.net',
228 :language => 'en',
229 :generate_password => '1',
230 :password => '',
231 :password_confirmation => ''
232 }, :send_information => 1
225 post :create, :params => {
226 :user => {
227 :login => 'randompass',
228 :firstname => 'Random',
229 :lastname => 'Pass',
230 :mail => 'randompass@example.net',
231 :language => 'en',
232 :generate_password => '1',
233 :password => '',
234 :password_confirmation => ''
235 },
236 :send_information => 1
237 }
233 238 end
234 239 user = User.order('id DESC').first
235 240 assert_equal 'randompass', user.login
236 241
237 242 mail = ActionMailer::Base.deliveries.last
238 243 assert_not_nil mail
239 244 m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
240 245 assert m
241 246 password = m[1]
242 247 assert user.check_password?(password)
243 248 end
244 249
245 250 def test_create_and_continue
246 post :create, :user => {
251 post :create, :params => {
252 :user => {
247 253 :login => 'randompass',
248 254 :firstname => 'Random',
249 255 :lastname => 'Pass',
250 256 :mail => 'randompass@example.net',
251 257 :generate_password => '1'
252 }, :continue => '1'
258 },
259 :continue => '1'
260 }
253 261 assert_redirected_to '/users/new?user%5Bgenerate_password%5D=1'
254 262 end
255 263
256 264 def test_create_with_failure
257 265 assert_no_difference 'User.count' do
258 post :create, :user => {}
266 post :create, :params => {:user => {}}
259 267 end
260 268 assert_response :success
261 269 assert_template 'new'
262 270 end
263 271
264 272 def test_create_with_failure_sould_preserve_preference
265 273 assert_no_difference 'User.count' do
266 post :create,
274 post :create, :params => {
267 275 :user => {},
268 276 :pref => {
269 277 'no_self_notified' => '1',
270 278 'hide_mail' => '1',
271 279 'time_zone' => 'Paris',
272 280 'comments_sorting' => 'desc',
273 281 'warn_on_leaving_unsaved' => '0'
274 282 }
283 }
275 284 end
276 285 assert_response :success
277 286 assert_template 'new'
278 287
279 288 assert_select 'select#pref_time_zone option[selected=selected]', :text => /Paris/
280 289 assert_select 'input#pref_no_self_notified[value="1"][checked=checked]'
281 290 end
282 291
283 292 def test_create_admin_should_send_security_notification
284 293 ActionMailer::Base.deliveries.clear
285 post :create,
294 post :create, :params => {
286 295 :user => {
287 296 :firstname => 'Edgar',
288 297 :lastname => 'Schmoe',
289 298 :login => 'eschmoe',
290 299 :password => 'secret123',
291 300 :password_confirmation => 'secret123',
292 301 :mail => 'eschmoe@example.foo',
293 302 :admin => '1'
294 303 }
304 }
295 305
296 306 assert_not_nil (mail = ActionMailer::Base.deliveries.last)
297 307 assert_mail_body_match '0.0.0.0', mail
298 308 assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: 'eschmoe'), mail
299 309 assert_select_email do
300 310 assert_select 'a[href^=?]', 'http://localhost:3000/users', :text => 'Users'
301 311 end
302 312
303 313 # All admins should receive this
304 314 User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
305 315 assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
306 316 end
307 317 end
308 318
309 319 def test_create_non_admin_should_not_send_security_notification
310 320 ActionMailer::Base.deliveries.clear
311 post :create,
321 post :create, :params => {
312 322 :user => {
313 323 :firstname => 'Edgar',
314 324 :lastname => 'Schmoe',
315 325 :login => 'eschmoe',
316 326 :password => 'secret123',
317 327 :password_confirmation => 'secret123',
318 328 :mail => 'eschmoe@example.foo',
319 329 :admin => '0'
320 330 }
331 }
321 332 assert_nil ActionMailer::Base.deliveries.last
322 333 end
323 334
324 335
325 336 def test_edit
326 get :edit, :id => 2
337 get :edit, :params => {:id => 2}
327 338 assert_response :success
328 339 assert_template 'edit'
329 340 assert_equal User.find(2), assigns(:user)
330 341 end
331 342
332 343 def test_edit_registered_user
333 344 assert User.find(2).register!
334 345
335 get :edit, :id => 2
346 get :edit, :params => {:id => 2}
336 347 assert_response :success
337 348 assert_select 'a', :text => 'Activate'
338 349 end
339 350
340 351 def test_update
341 352 ActionMailer::Base.deliveries.clear
342 put :update, :id => 2,
343 :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'},
344 :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
353 put :update, :params => {
354 :id => 2,
355 :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'},
356 :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
357 }
345 358 user = User.find(2)
346 359 assert_equal 'Changed', user.firstname
347 360 assert_equal 'only_assigned', user.mail_notification
348 361 assert_equal true, user.pref[:hide_mail]
349 362 assert_equal 'desc', user.pref[:comments_sorting]
350 363 assert ActionMailer::Base.deliveries.empty?
351 364 end
352 365
353 366 def test_update_with_failure
354 367 assert_no_difference 'User.count' do
355 put :update, :id => 2, :user => {:firstname => ''}
368 put :update, :params => {
369 :id => 2,
370 :user => {:firstname => ''}
371 }
356 372 end
357 373 assert_response :success
358 374 assert_template 'edit'
359 375 end
360 376
361 377 def test_update_with_group_ids_should_assign_groups
362 put :update, :id => 2, :user => {:group_ids => ['10']}
378 put :update, :params => {
379 :id => 2,
380 :user => {:group_ids => ['10']}
381 }
363 382 user = User.find(2)
364 383 assert_equal [10], user.group_ids
365 384 end
366 385
367 386 def test_update_with_activation_should_send_a_notification
368 387 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
369 388 u.login = 'foo'
370 389 u.status = User::STATUS_REGISTERED
371 390 u.save!
372 391 ActionMailer::Base.deliveries.clear
373 392 Setting.bcc_recipients = '1'
374 393
375 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
394 put :update, :params => {
395 :id => u.id,
396 :user => {:status => User::STATUS_ACTIVE}
397 }
376 398 assert u.reload.active?
377 399 mail = ActionMailer::Base.deliveries.last
378 400 assert_not_nil mail
379 401 assert_equal ['foo.bar@somenet.foo'], mail.bcc
380 402 assert_mail_body_match ll('fr', :notice_account_activated), mail
381 403 end
382 404
383 405 def test_update_with_password_change_should_send_a_notification
384 406 ActionMailer::Base.deliveries.clear
385 407 Setting.bcc_recipients = '1'
386 408
387 put :update, :id => 2, :user => {:password => 'newpass123', :password_confirmation => 'newpass123'}, :send_information => '1'
409 put :update, :params => {
410 :id => 2,
411 :user => {:password => 'newpass123', :password_confirmation => 'newpass123'},
412 :send_information => '1'
413 }
388 414 u = User.find(2)
389 415 assert u.check_password?('newpass123')
390 416
391 417 mail = ActionMailer::Base.deliveries.last
392 418 assert_not_nil mail
393 419 assert_equal [u.mail], mail.bcc
394 420 assert_mail_body_match 'newpass123', mail
395 421 end
396 422
397 423 def test_update_with_generate_password_should_email_the_password
398 424 ActionMailer::Base.deliveries.clear
399 425 Setting.bcc_recipients = '1'
400 426
401 put :update, :id => 2, :user => {
402 :generate_password => '1',
403 :password => '',
404 :password_confirmation => ''
405 }, :send_information => '1'
427 put :update, :params => {
428 :id => 2,
429 :user => {
430 :generate_password => '1',
431 :password => '',
432 :password_confirmation => ''
433 },
434 :send_information => '1'
435 }
406 436
407 437 mail = ActionMailer::Base.deliveries.last
408 438 assert_not_nil mail
409 439 m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
410 440 assert m
411 441 password = m[1]
412 442 assert User.find(2).check_password?(password)
413 443 end
414 444
415 445 def test_update_without_generate_password_should_not_change_password
416 put :update, :id => 2, :user => {
417 :firstname => 'changed',
418 :generate_password => '0',
419 :password => '',
420 :password_confirmation => ''
421 }, :send_information => '1'
446 put :update, :params => {
447 :id => 2, :user => {
448 :firstname => 'changed',
449 :generate_password => '0',
450 :password => '',
451 :password_confirmation => ''
452 },
453 :send_information => '1'
454 }
422 455
423 456 user = User.find(2)
424 457 assert_equal 'changed', user.firstname
425 458 assert user.check_password?('jsmith')
426 459 end
427 460
428 461 def test_update_user_switchin_from_auth_source_to_password_authentication
429 462 # Configure as auth source
430 463 u = User.find(2)
431 464 u.auth_source = AuthSource.find(1)
432 465 u.save!
433 466
434 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'}
467 put :update, :params => {
468 :id => u.id,
469 :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'}
470 }
435 471
436 472 assert_equal nil, u.reload.auth_source
437 473 assert u.check_password?('newpass123')
438 474 end
439 475
440 476 def test_update_notified_project
441 get :edit, :id => 2
477 get :edit, :params => {:id => 2}
442 478 assert_response :success
443 479 assert_template 'edit'
444 480 u = User.find(2)
445 481 assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort
446 482 assert_equal [1, 2, 5], u.notified_projects_ids.sort
447 483 assert_select 'input[name=?][value=?]', 'user[notified_project_ids][]', '1'
448 484 assert_equal 'all', u.mail_notification
449 put :update, :id => 2,
450 :user => {
451 :mail_notification => 'selected',
452 :notified_project_ids => [1, 2]
453 }
485 put :update, :params => {
486 :id => 2,
487 :user => {
488 :mail_notification => 'selected',
489 :notified_project_ids => [1, 2]
490 }
491 }
454 492 u = User.find(2)
455 493 assert_equal 'selected', u.mail_notification
456 494 assert_equal [1, 2], u.notified_projects_ids.sort
457 495 end
458 496
459 497 def test_update_status_should_not_update_attributes
460 498 user = User.find(2)
461 499 user.pref[:no_self_notified] = '1'
462 500 user.pref.save
463 501
464 put :update, :id => 2, :user => {:status => 3}
502 put :update, :params => {
503 :id => 2,
504 :user => {:status => 3}
505 }
465 506 assert_response 302
466 507 user = User.find(2)
467 508 assert_equal 3, user.status
468 509 assert_equal '1', user.pref[:no_self_notified]
469 510 end
470 511
471 512 def test_update_assign_admin_should_send_security_notification
472 513 ActionMailer::Base.deliveries.clear
473 put :update, :id => 2, :user => {
474 :admin => 1
514 put :update, :params => {
515 :id => 2,
516 :user => {:admin => 1}
475 517 }
476 518
477 519 assert_not_nil (mail = ActionMailer::Base.deliveries.last)
478 520 assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: User.find(2).login), mail
479 521
480 522 # All admins should receive this
481 523 User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
482 524 assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
483 525 end
484 526 end
485 527
486 528 def test_update_unassign_admin_should_send_security_notification
487 529 user = User.find(2)
488 530 user.admin = true
489 531 user.save!
490 532
491 533 ActionMailer::Base.deliveries.clear
492 put :update, :id => user.id, :user => {
493 :admin => 0
534 put :update, :params => {
535 :id => user.id,
536 :user => {:admin => 0}
494 537 }
495 538
496 539 assert_not_nil (mail = ActionMailer::Base.deliveries.last)
497 540 assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: user.login), mail
498 541
499 542 # All admins should receive this
500 543 User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
501 544 assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
502 545 end
503 546 end
504 547
505 548 def test_update_lock_admin_should_send_security_notification
506 549 user = User.find(2)
507 550 user.admin = true
508 551 user.save!
509 552
510 553 ActionMailer::Base.deliveries.clear
511 put :update, :id => 2, :user => {
512 :status => Principal::STATUS_LOCKED
554 put :update, :params => {
555 :id => 2,
556 :user => {:status => Principal::STATUS_LOCKED}
513 557 }
514 558
515 559 assert_not_nil (mail = ActionMailer::Base.deliveries.last)
516 560 assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: User.find(2).login), mail
517 561
518 562 # All admins should receive this
519 563 User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
520 564 assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
521 565 end
522 566
523 567 # if user is already locked, destroying should not send a second mail
524 568 # (for active admins see furtherbelow)
525 569 ActionMailer::Base.deliveries.clear
526 delete :destroy, :id => 1
570 delete :destroy, :params => {:id => 1}
527 571 assert_nil ActionMailer::Base.deliveries.last
528 572
529 573 end
530 574
531 575 def test_update_unlock_admin_should_send_security_notification
532 576 user = User.find(5) # already locked
533 577 user.admin = true
534 578 user.save!
535 579 ActionMailer::Base.deliveries.clear
536 put :update, :id => user.id, :user => {
537 :status => Principal::STATUS_ACTIVE
580 put :update, :params => {
581 :id => user.id,
582 :user => {:status => Principal::STATUS_ACTIVE}
538 583 }
539 584
540 585 assert_not_nil (mail = ActionMailer::Base.deliveries.last)
541 586 assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: user.login), mail
542 587
543 588 # All admins should receive this
544 589 User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
545 590 assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
546 591 end
547 592 end
548 593
549 594 def test_update_admin_unrelated_property_should_not_send_security_notification
550 595 ActionMailer::Base.deliveries.clear
551 put :update, :id => 1, :user => {
552 :firstname => 'Jimmy'
596 put :update, :params => {
597 :id => 1,
598 :user => {:firstname => 'Jimmy'}
553 599 }
554 600 assert_nil ActionMailer::Base.deliveries.last
555 601 end
556 602
557 603 def test_destroy
558 604 assert_difference 'User.count', -1 do
559 delete :destroy, :id => 2
605 delete :destroy, :params => {:id => 2}
560 606 end
561 607 assert_redirected_to '/users'
562 608 assert_nil User.find_by_id(2)
563 609 end
564 610
565 611 def test_destroy_should_be_denied_for_non_admin_users
566 612 @request.session[:user_id] = 3
567 613
568 614 assert_no_difference 'User.count' do
569 get :destroy, :id => 2
615 get :destroy, :params => {:id => 2}
570 616 end
571 617 assert_response 403
572 618 end
573 619
574 620 def test_destroy_should_redirect_to_back_url_param
575 621 assert_difference 'User.count', -1 do
576 delete :destroy, :id => 2, :back_url => '/users?name=foo'
622 delete :destroy, :params => {:id => 2, :back_url => '/users?name=foo'}
577 623 end
578 624 assert_redirected_to '/users?name=foo'
579 625 end
580 626
581 627 def test_destroy_active_admin_should_send_security_notification
582 628 user = User.find(2)
583 629 user.admin = true
584 630 user.save!
585 631 ActionMailer::Base.deliveries.clear
586 delete :destroy, :id => user.id
632 delete :destroy, :params => {:id => user.id}
587 633
588 634 assert_not_nil (mail = ActionMailer::Base.deliveries.last)
589 635 assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: user.login), mail
590 636
591 637 # All admins should receive this
592 638 User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
593 639 assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
594 640 end
595 641 end
596 642 end
@@ -1,366 +1,374
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 if ENV["COVERAGE"]
19 19 require 'simplecov'
20 20 require File.expand_path(File.dirname(__FILE__) + "/coverage/html_formatter")
21 21 SimpleCov.formatter = Redmine::Coverage::HtmlFormatter
22 22 SimpleCov.start 'rails'
23 23 end
24 24
25 25 $redmine_test_ldap_server = ENV['REDMINE_TEST_LDAP_SERVER'] || '127.0.0.1'
26 26
27 27 ENV["RAILS_ENV"] = "test"
28 28 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
29 29 require 'rails/test_help'
30 30 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
31 31
32 32 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
33 33 include ObjectHelpers
34 34
35 35 require 'net/ldap'
36 36 require 'mocha/setup'
37 37
38 38 Redmine::SudoMode.disable!
39 39
40 40 class ActionView::TestCase
41 41 helper :application
42 42 include ApplicationHelper
43 43 end
44 44
45 45 class ActiveSupport::TestCase
46 46 include ActionDispatch::TestProcess
47 47
48 48 self.use_transactional_fixtures = true
49 49 self.use_instantiated_fixtures = false
50 50
51 51 def uploaded_test_file(name, mime)
52 52 fixture_file_upload("files/#{name}", mime, true)
53 53 end
54 54
55 55 # Mock out a file
56 56 def self.mock_file
57 57 file = 'a_file.png'
58 58 file.stubs(:size).returns(32)
59 59 file.stubs(:original_filename).returns('a_file.png')
60 60 file.stubs(:content_type).returns('image/png')
61 61 file.stubs(:read).returns(false)
62 62 file
63 63 end
64 64
65 65 def mock_file
66 66 self.class.mock_file
67 67 end
68 68
69 69 def mock_file_with_options(options={})
70 70 file = ''
71 71 file.stubs(:size).returns(32)
72 72 original_filename = options[:original_filename] || nil
73 73 file.stubs(:original_filename).returns(original_filename)
74 74 content_type = options[:content_type] || nil
75 75 file.stubs(:content_type).returns(content_type)
76 76 file.stubs(:read).returns(false)
77 77 file
78 78 end
79 79
80 80 # Use a temporary directory for attachment related tests
81 81 def set_tmp_attachments_directory
82 82 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
83 83 unless File.directory?("#{Rails.root}/tmp/test/attachments")
84 84 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
85 85 end
86 86 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
87 87 end
88 88
89 89 def set_fixtures_attachments_directory
90 90 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
91 91 end
92 92
93 93 def with_settings(options, &block)
94 94 saved_settings = options.keys.inject({}) do |h, k|
95 95 h[k] = case Setting[k]
96 96 when Symbol, false, true, nil
97 97 Setting[k]
98 98 else
99 99 Setting[k].dup
100 100 end
101 101 h
102 102 end
103 103 options.each {|k, v| Setting[k] = v}
104 104 yield
105 105 ensure
106 106 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
107 107 end
108 108
109 109 # Yields the block with user as the current user
110 110 def with_current_user(user, &block)
111 111 saved_user = User.current
112 112 User.current = user
113 113 yield
114 114 ensure
115 115 User.current = saved_user
116 116 end
117 117
118 118 def with_locale(locale, &block)
119 119 saved_localed = ::I18n.locale
120 120 ::I18n.locale = locale
121 121 yield
122 122 ensure
123 123 ::I18n.locale = saved_localed
124 124 end
125 125
126 126 def self.ldap_configured?
127 127 @test_ldap = Net::LDAP.new(:host => $redmine_test_ldap_server, :port => 389)
128 128 return @test_ldap.bind
129 129 rescue Exception => e
130 130 # LDAP is not listening
131 131 return nil
132 132 end
133 133
134 134 def self.convert_installed?
135 135 Redmine::Thumbnail.convert_available?
136 136 end
137 137
138 138 def convert_installed?
139 139 self.class.convert_installed?
140 140 end
141 141
142 142 # Returns the path to the test +vendor+ repository
143 143 def self.repository_path(vendor)
144 144 path = Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
145 145 # Unlike ruby, JRuby returns Rails.root with backslashes under Windows
146 146 path.tr("\\", "/")
147 147 end
148 148
149 149 # Returns the url of the subversion test repository
150 150 def self.subversion_repository_url
151 151 path = repository_path('subversion')
152 152 path = '/' + path unless path.starts_with?('/')
153 153 "file://#{path}"
154 154 end
155 155
156 156 # Returns true if the +vendor+ test repository is configured
157 157 def self.repository_configured?(vendor)
158 158 File.directory?(repository_path(vendor))
159 159 end
160 160
161 161 def repository_path_hash(arr)
162 162 hs = {}
163 163 hs[:path] = arr.join("/")
164 164 hs[:param] = arr.join("/")
165 165 hs
166 166 end
167 167
168 168 def sqlite?
169 169 ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
170 170 end
171 171
172 172 def mysql?
173 173 ActiveRecord::Base.connection.adapter_name =~ /mysql/i
174 174 end
175 175
176 176 def postgresql?
177 177 ActiveRecord::Base.connection.adapter_name =~ /postgresql/i
178 178 end
179 179
180 180 def quoted_date(date)
181 181 date = Date.parse(date) if date.is_a?(String)
182 182 ActiveRecord::Base.connection.quoted_date(date)
183 183 end
184 184
185 185 # Asserts that a new record for the given class is created
186 186 # and returns it
187 187 def new_record(klass, &block)
188 188 new_records(klass, 1, &block).first
189 189 end
190 190
191 191 # Asserts that count new records for the given class are created
192 192 # and returns them as an array order by object id
193 193 def new_records(klass, count, &block)
194 194 assert_difference "#{klass}.count", count do
195 195 yield
196 196 end
197 197 klass.order(:id => :desc).limit(count).to_a.reverse
198 198 end
199 199
200 200 def assert_save(object)
201 201 saved = object.save
202 202 message = "#{object.class} could not be saved"
203 203 errors = object.errors.full_messages.map {|m| "- #{m}"}
204 204 message << ":\n#{errors.join("\n")}" if errors.any?
205 205 assert_equal true, saved, message
206 206 end
207 207
208 208 def assert_select_error(arg)
209 209 assert_select '#errorExplanation', :text => arg
210 210 end
211 211
212 212 def assert_include(expected, s, message=nil)
213 213 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
214 214 end
215 215
216 216 def assert_not_include(expected, s, message=nil)
217 217 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
218 218 end
219 219
220 220 def assert_select_in(text, *args, &block)
221 221 d = Nokogiri::HTML(CGI::unescapeHTML(String.new(text))).root
222 222 assert_select(d, *args, &block)
223 223 end
224 224
225 225 def assert_select_email(*args, &block)
226 226 email = ActionMailer::Base.deliveries.last
227 227 assert_not_nil email
228 228 html_body = email.parts.detect {|part| part.content_type.include?('text/html')}.try(&:body)
229 229 assert_not_nil html_body
230 230 assert_select_in html_body.encoded, *args, &block
231 231 end
232 232
233 233 def assert_mail_body_match(expected, mail, message=nil)
234 234 if expected.is_a?(String)
235 235 assert_include expected, mail_body(mail), message
236 236 else
237 237 assert_match expected, mail_body(mail), message
238 238 end
239 239 end
240 240
241 241 def assert_mail_body_no_match(expected, mail, message=nil)
242 242 if expected.is_a?(String)
243 243 assert_not_include expected, mail_body(mail), message
244 244 else
245 245 assert_no_match expected, mail_body(mail), message
246 246 end
247 247 end
248 248
249 249 def mail_body(mail)
250 250 mail.parts.first.body.encoded
251 251 end
252 252
253 253 # Returns the lft value for a new root issue
254 254 def new_issue_lft
255 255 1
256 256 end
257 257 end
258 258
259 259 module Redmine
260 260 class RoutingTest < ActionDispatch::IntegrationTest
261 261 def should_route(arg)
262 262 arg = arg.dup
263 263 request = arg.keys.detect {|key| key.is_a?(String)}
264 264 raise ArgumentError unless request
265 265 options = arg.slice!(request)
266 266
267 267 raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/
268 268 method, path = $1.downcase.to_sym, $2
269 269
270 270 raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/
271 271 controller, action = $1, $2
272 272
273 273 assert_routing(
274 274 {:method => method, :path => path},
275 275 options.merge(:controller => controller, :action => action)
276 276 )
277 277 end
278 278 end
279 279
280 280 class ControllerTest < ActionController::TestCase
281 def process(method, path, parameters={}, options={})
282 if parameters.key?(:params)
283 raise ArgumentError if options.present?
284 super method, path, parameters[:params], parameters.except(:params)
285 else
286 super
287 end
288 end
281 289 end
282 290
283 291 class IntegrationTest < ActionDispatch::IntegrationTest
284 292 def log_user(login, password)
285 293 User.anonymous
286 294 get "/login"
287 295 assert_equal nil, session[:user_id]
288 296 assert_response :success
289 297 assert_template "account/login"
290 298 post "/login", :username => login, :password => password
291 299 assert_equal login, User.find(session[:user_id]).login
292 300 end
293 301
294 302 def credentials(user, password=nil)
295 303 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
296 304 end
297 305 end
298 306
299 307 module ApiTest
300 308 API_FORMATS = %w(json xml).freeze
301 309
302 310 # Base class for API tests
303 311 class Base < Redmine::IntegrationTest
304 312 def setup
305 313 Setting.rest_api_enabled = '1'
306 314 end
307 315
308 316 def teardown
309 317 Setting.rest_api_enabled = '0'
310 318 end
311 319
312 320 # Uploads content using the XML API and returns the attachment token
313 321 def xml_upload(content, credentials)
314 322 upload('xml', content, credentials)
315 323 end
316 324
317 325 # Uploads content using the JSON API and returns the attachment token
318 326 def json_upload(content, credentials)
319 327 upload('json', content, credentials)
320 328 end
321 329
322 330 def upload(format, content, credentials)
323 331 set_tmp_attachments_directory
324 332 assert_difference 'Attachment.count' do
325 333 post "/uploads.#{format}", content, {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials)
326 334 assert_response :created
327 335 end
328 336 data = response_data
329 337 assert_kind_of Hash, data['upload']
330 338 token = data['upload']['token']
331 339 assert_not_nil token
332 340 token
333 341 end
334 342
335 343 # Parses the response body based on its content type
336 344 def response_data
337 345 unless response.content_type.to_s =~ /^application\/(.+)/
338 346 raise "Unexpected response type: #{response.content_type}"
339 347 end
340 348 format = $1
341 349 case format
342 350 when 'xml'
343 351 Hash.from_xml(response.body)
344 352 when 'json'
345 353 ActiveSupport::JSON.decode(response.body)
346 354 else
347 355 raise "Unknown response format: #{format}"
348 356 end
349 357 end
350 358 end
351 359
352 360 class Routing < Redmine::RoutingTest
353 361 def should_route(arg)
354 362 arg = arg.dup
355 363 request = arg.keys.detect {|key| key.is_a?(String)}
356 364 raise ArgumentError unless request
357 365 options = arg.slice!(request)
358 366
359 367 API_FORMATS.each do |format|
360 368 format_request = request.sub /$/, ".#{format}"
361 369 super options.merge(format_request => arg[request], :format => format)
362 370 end
363 371 end
364 372 end
365 373 end
366 374 end
General Comments 0
You need to be logged in to leave comments. Login now