You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users_controller_test.rb 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class UsersControllerTest < Redmine::ControllerTest
  19. include Redmine::I18n
  20. fixtures :users, :email_addresses, :projects, :members, :member_roles, :roles,
  21. :custom_fields, :custom_values, :groups_users,
  22. :auth_sources,
  23. :enabled_modules,
  24. :issues, :issue_statuses,
  25. :trackers
  26. def setup
  27. User.current = nil
  28. @request.session[:user_id] = 1 # admin
  29. end
  30. def test_index
  31. get :index
  32. assert_response :success
  33. assert_select 'table.users'
  34. assert_select 'tr.user.active'
  35. assert_select 'tr.user.locked', 0
  36. end
  37. def test_index_with_status_filter
  38. get :index, :params => {:status => 3}
  39. assert_response :success
  40. assert_select 'tr.user.active', 0
  41. assert_select 'tr.user.locked'
  42. end
  43. def test_index_with_name_filter
  44. get :index, :params => {:name => 'john'}
  45. assert_response :success
  46. assert_select 'tr.user td.username', :text => 'jsmith'
  47. assert_select 'tr.user', 1
  48. end
  49. def test_index_with_group_filter
  50. get :index, :params => {:group_id => '10'}
  51. assert_response :success
  52. assert_select 'tr.user', Group.find(10).users.count
  53. assert_select 'select[name=group_id]' do
  54. assert_select 'option[value="10"][selected=selected]'
  55. end
  56. end
  57. def test_index_csv
  58. with_settings :default_language => 'en' do
  59. get :index, :params => { :format => 'csv' }
  60. assert_response :success
  61. assert_equal User.logged.status(1).count, response.body.chomp.split("\n").size - 1
  62. assert_include 'active', response.body
  63. assert_not_include 'locked', response.body
  64. assert_equal 'text/csv; header=present', @response.content_type
  65. end
  66. end
  67. def test_index_csv_with_status_filter
  68. with_settings :default_language => 'en' do
  69. get :index, :params => { :status => 3, :format => 'csv' }
  70. assert_response :success
  71. assert_equal User.logged.status(3).count, response.body.chomp.split("\n").size - 1
  72. assert_include 'locked', response.body
  73. assert_not_include 'active', response.body
  74. assert_equal 'text/csv; header=present', @response.content_type
  75. end
  76. end
  77. def test_index_csv_with_name_filter
  78. get :index, :params => {:name => 'John', :format => 'csv'}
  79. assert_response :success
  80. assert_equal User.logged.like('John').count, response.body.chomp.split("\n").size - 1
  81. assert_include 'John', response.body
  82. assert_equal 'text/csv; header=present', @response.content_type
  83. end
  84. def test_index_csv_with_group_filter
  85. get :index, :params => {:group_id => '10', :format => 'csv'}
  86. assert_response :success
  87. assert_equal Group.find(10).users.count, response.body.chomp.split("\n").size - 1
  88. assert_equal 'text/csv; header=present', @response.content_type
  89. end
  90. def test_show
  91. @request.session[:user_id] = nil
  92. get :show, :params => {:id => 2}
  93. assert_response :success
  94. assert_select 'h2', :text => /John Smith/
  95. end
  96. def test_show_should_display_visible_custom_fields
  97. @request.session[:user_id] = nil
  98. UserCustomField.find_by_name('Phone number').update_attribute :visible, true
  99. get :show, :params => {:id => 2}
  100. assert_response :success
  101. assert_select 'li', :text => /Phone number/
  102. end
  103. def test_show_should_not_display_hidden_custom_fields
  104. @request.session[:user_id] = nil
  105. UserCustomField.find_by_name('Phone number').update_attribute :visible, false
  106. get :show, :params => {:id => 2}
  107. assert_response :success
  108. assert_select 'li', :text => /Phone number/, :count => 0
  109. end
  110. def test_show_should_not_fail_when_custom_values_are_nil
  111. user = User.find(2)
  112. # Create a custom field to illustrate the issue
  113. custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
  114. custom_value = user.custom_values.build(:custom_field => custom_field).save!
  115. get :show, :params => {:id => 2}
  116. assert_response :success
  117. end
  118. def test_show_inactive
  119. @request.session[:user_id] = nil
  120. get :show, :params => {:id => 5}
  121. assert_response 404
  122. end
  123. def test_show_inactive_by_admin
  124. @request.session[:user_id] = 1
  125. get :show, :params => {:id => 5}
  126. assert_response 200
  127. assert_select 'h2', :text => /Dave2 Lopper2/
  128. end
  129. def test_show_user_who_is_not_visible_should_return_404
  130. Role.anonymous.update! :users_visibility => 'members_of_visible_projects'
  131. user = User.generate!
  132. @request.session[:user_id] = nil
  133. get :show, :params => {:id => user.id}
  134. assert_response 404
  135. end
  136. def test_show_displays_memberships_based_on_project_visibility
  137. @request.session[:user_id] = 1
  138. get :show, :params => {:id => 2}
  139. assert_response :success
  140. # membership of private project admin can see
  141. assert_select 'li a', :text => "OnlineStore"
  142. end
  143. def test_show_current_should_require_authentication
  144. @request.session[:user_id] = nil
  145. get :show, :params => {:id => 'current'}
  146. assert_response 302
  147. end
  148. def test_show_current
  149. @request.session[:user_id] = 2
  150. get :show, :params => {:id => 'current'}
  151. assert_response :success
  152. assert_select 'h2', :text => /John Smith/
  153. end
  154. def test_new
  155. get :new
  156. assert_response :success
  157. assert_select 'input[name=?]', 'user[login]'
  158. end
  159. def test_create
  160. Setting.bcc_recipients = '1'
  161. assert_difference 'User.count' do
  162. assert_difference 'ActionMailer::Base.deliveries.size' do
  163. post :create, :params => {
  164. :user => {
  165. :firstname => 'John',
  166. :lastname => 'Doe',
  167. :login => 'jdoe',
  168. :password => 'secret123',
  169. :password_confirmation => 'secret123',
  170. :mail => 'jdoe@gmail.com',
  171. :mail_notification => 'none'
  172. },
  173. :send_information => '1'
  174. }
  175. end
  176. end
  177. user = User.order('id DESC').first
  178. assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
  179. assert_equal 'John', user.firstname
  180. assert_equal 'Doe', user.lastname
  181. assert_equal 'jdoe', user.login
  182. assert_equal 'jdoe@gmail.com', user.mail
  183. assert_equal 'none', user.mail_notification
  184. assert user.check_password?('secret123')
  185. mail = ActionMailer::Base.deliveries.last
  186. assert_not_nil mail
  187. assert_equal [user.mail], mail.bcc
  188. assert_mail_body_match 'secret', mail
  189. end
  190. def test_create_with_preferences
  191. assert_difference 'User.count' do
  192. post :create, :params => {
  193. :user => {
  194. :firstname => 'John',
  195. :lastname => 'Doe',
  196. :login => 'jdoe',
  197. :password => 'secret123',
  198. :password_confirmation => 'secret123',
  199. :mail => 'jdoe@gmail.com',
  200. :mail_notification => 'none'
  201. },
  202. :pref => {
  203. 'hide_mail' => '1',
  204. 'time_zone' => 'Paris',
  205. 'comments_sorting' => 'desc',
  206. 'warn_on_leaving_unsaved' => '0',
  207. 'textarea_font' => 'proportional'
  208. }
  209. }
  210. end
  211. user = User.order('id DESC').first
  212. assert_equal 'jdoe', user.login
  213. assert_equal true, user.pref.hide_mail
  214. assert_equal 'Paris', user.pref.time_zone
  215. assert_equal 'desc', user.pref[:comments_sorting]
  216. assert_equal '0', user.pref[:warn_on_leaving_unsaved]
  217. assert_equal 'proportional', user.pref[:textarea_font]
  218. end
  219. def test_create_with_generate_password_should_email_the_password
  220. assert_difference 'User.count' do
  221. post :create, :params => {
  222. :user => {
  223. :login => 'randompass',
  224. :firstname => 'Random',
  225. :lastname => 'Pass',
  226. :mail => 'randompass@example.net',
  227. :language => 'en',
  228. :generate_password => '1',
  229. :password => '',
  230. :password_confirmation => ''
  231. },
  232. :send_information => 1
  233. }
  234. end
  235. user = User.order('id DESC').first
  236. assert_equal 'randompass', user.login
  237. mail = ActionMailer::Base.deliveries.last
  238. assert_not_nil mail
  239. m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
  240. assert m
  241. password = m[1]
  242. assert user.check_password?(password)
  243. end
  244. def test_create_and_continue
  245. post :create, :params => {
  246. :user => {
  247. :login => 'randompass',
  248. :firstname => 'Random',
  249. :lastname => 'Pass',
  250. :mail => 'randompass@example.net',
  251. :generate_password => '1'
  252. },
  253. :continue => '1'
  254. }
  255. assert_redirected_to '/users/new?user%5Bgenerate_password%5D=1'
  256. end
  257. def test_create_with_failure
  258. assert_no_difference 'User.count' do
  259. post :create, :params => {:user => {:login => 'foo'}}
  260. end
  261. assert_response :success
  262. assert_select_error /Email cannot be blank/
  263. end
  264. def test_create_with_failure_sould_preserve_preference
  265. assert_no_difference 'User.count' do
  266. post :create, :params => {
  267. :user => {
  268. :login => 'foo'
  269. },
  270. :pref => {
  271. 'no_self_notified' => '1',
  272. 'hide_mail' => '1',
  273. 'time_zone' => 'Paris',
  274. 'comments_sorting' => 'desc',
  275. 'warn_on_leaving_unsaved' => '0'
  276. }
  277. }
  278. end
  279. assert_response :success
  280. assert_select 'select#pref_time_zone option[selected=selected]', :text => /Paris/
  281. assert_select 'input#pref_no_self_notified[value="1"][checked=checked]'
  282. end
  283. def test_create_admin_should_send_security_notification
  284. ActionMailer::Base.deliveries.clear
  285. post :create, :params => {
  286. :user => {
  287. :firstname => 'Edgar',
  288. :lastname => 'Schmoe',
  289. :login => 'eschmoe',
  290. :password => 'secret123',
  291. :password_confirmation => 'secret123',
  292. :mail => 'eschmoe@example.foo',
  293. :admin => '1'
  294. }
  295. }
  296. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  297. assert_mail_body_match '0.0.0.0', mail
  298. assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: 'eschmoe'), mail
  299. assert_select_email do
  300. assert_select 'a[href^=?]', 'http://localhost:3000/users', :text => 'Users'
  301. end
  302. # All admins should receive this
  303. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  304. assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
  305. end
  306. end
  307. def test_create_non_admin_should_not_send_security_notification
  308. ActionMailer::Base.deliveries.clear
  309. post :create, :params => {
  310. :user => {
  311. :firstname => 'Edgar',
  312. :lastname => 'Schmoe',
  313. :login => 'eschmoe',
  314. :password => 'secret123',
  315. :password_confirmation => 'secret123',
  316. :mail => 'eschmoe@example.foo',
  317. :admin => '0'
  318. }
  319. }
  320. assert_nil ActionMailer::Base.deliveries.last
  321. end
  322. def test_edit
  323. get :edit, :params => {:id => 2}
  324. assert_response :success
  325. assert_select 'input[name=?][value=?]', 'user[login]', 'jsmith'
  326. end
  327. def test_edit_registered_user
  328. assert User.find(2).register!
  329. get :edit, :params => {:id => 2}
  330. assert_response :success
  331. assert_select 'a', :text => 'Activate'
  332. end
  333. def test_edit_should_be_denied_for_anonymous
  334. assert User.find(6).anonymous?
  335. get :edit, :params => {:id => 6}
  336. assert_response 404
  337. end
  338. def test_update
  339. ActionMailer::Base.deliveries.clear
  340. put :update, :params => {
  341. :id => 2,
  342. :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'},
  343. :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
  344. }
  345. user = User.find(2)
  346. assert_equal 'Changed', user.firstname
  347. assert_equal 'only_assigned', user.mail_notification
  348. assert_equal true, user.pref[:hide_mail]
  349. assert_equal 'desc', user.pref[:comments_sorting]
  350. assert ActionMailer::Base.deliveries.empty?
  351. end
  352. def test_update_with_failure
  353. assert_no_difference 'User.count' do
  354. put :update, :params => {
  355. :id => 2,
  356. :user => {:firstname => ''}
  357. }
  358. end
  359. assert_response :success
  360. assert_select_error /First name cannot be blank/
  361. end
  362. def test_update_with_group_ids_should_assign_groups
  363. put :update, :params => {
  364. :id => 2,
  365. :user => {:group_ids => ['10']}
  366. }
  367. user = User.find(2)
  368. assert_equal [10], user.group_ids
  369. end
  370. def test_update_with_activation_should_send_a_notification
  371. u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
  372. u.login = 'foo'
  373. u.status = User::STATUS_REGISTERED
  374. u.save!
  375. ActionMailer::Base.deliveries.clear
  376. Setting.bcc_recipients = '1'
  377. put :update, :params => {
  378. :id => u.id,
  379. :user => {:status => User::STATUS_ACTIVE}
  380. }
  381. assert u.reload.active?
  382. mail = ActionMailer::Base.deliveries.last
  383. assert_not_nil mail
  384. assert_equal ['foo.bar@somenet.foo'], mail.bcc
  385. assert_mail_body_match ll('fr', :notice_account_activated), mail
  386. end
  387. def test_update_with_password_change_should_send_a_notification
  388. ActionMailer::Base.deliveries.clear
  389. Setting.bcc_recipients = '1'
  390. put :update, :params => {
  391. :id => 2,
  392. :user => {:password => 'newpass123', :password_confirmation => 'newpass123'},
  393. :send_information => '1'
  394. }
  395. u = User.find(2)
  396. assert u.check_password?('newpass123')
  397. mail = ActionMailer::Base.deliveries.last
  398. assert_not_nil mail
  399. assert_equal [u.mail], mail.bcc
  400. assert_mail_body_match 'newpass123', mail
  401. end
  402. def test_update_with_generate_password_should_email_the_password
  403. ActionMailer::Base.deliveries.clear
  404. Setting.bcc_recipients = '1'
  405. put :update, :params => {
  406. :id => 2,
  407. :user => {
  408. :generate_password => '1',
  409. :password => '',
  410. :password_confirmation => ''
  411. },
  412. :send_information => '1'
  413. }
  414. mail = ActionMailer::Base.deliveries.last
  415. assert_not_nil mail
  416. m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
  417. assert m
  418. password = m[1]
  419. assert User.find(2).check_password?(password)
  420. end
  421. def test_update_without_generate_password_should_not_change_password
  422. put :update, :params => {
  423. :id => 2, :user => {
  424. :firstname => 'changed',
  425. :generate_password => '0',
  426. :password => '',
  427. :password_confirmation => ''
  428. },
  429. :send_information => '1'
  430. }
  431. user = User.find(2)
  432. assert_equal 'changed', user.firstname
  433. assert user.check_password?('jsmith')
  434. end
  435. def test_update_user_switchin_from_auth_source_to_password_authentication
  436. # Configure as auth source
  437. u = User.find(2)
  438. u.auth_source = AuthSource.find(1)
  439. u.save!
  440. put :update, :params => {
  441. :id => u.id,
  442. :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'}
  443. }
  444. assert_nil u.reload.auth_source
  445. assert u.check_password?('newpass123')
  446. end
  447. def test_update_notified_project
  448. get :edit, :params => {:id => 2}
  449. assert_response :success
  450. u = User.find(2)
  451. assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort
  452. assert_equal [1, 2, 5], u.notified_projects_ids.sort
  453. assert_select 'input[name=?][value=?]', 'user[notified_project_ids][]', '1'
  454. assert_equal 'all', u.mail_notification
  455. put :update, :params => {
  456. :id => 2,
  457. :user => {
  458. :mail_notification => 'selected',
  459. :notified_project_ids => [1, 2]
  460. }
  461. }
  462. u = User.find(2)
  463. assert_equal 'selected', u.mail_notification
  464. assert_equal [1, 2], u.notified_projects_ids.sort
  465. end
  466. def test_update_status_should_not_update_attributes
  467. user = User.find(2)
  468. user.pref[:no_self_notified] = '1'
  469. user.pref.save
  470. put :update, :params => {
  471. :id => 2,
  472. :user => {:status => 3}
  473. }
  474. assert_response 302
  475. user = User.find(2)
  476. assert_equal 3, user.status
  477. assert_equal '1', user.pref[:no_self_notified]
  478. end
  479. def test_update_assign_admin_should_send_security_notification
  480. ActionMailer::Base.deliveries.clear
  481. put :update, :params => {
  482. :id => 2,
  483. :user => {:admin => 1}
  484. }
  485. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  486. assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: User.find(2).login), mail
  487. # All admins should receive this
  488. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  489. assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
  490. end
  491. end
  492. def test_update_unassign_admin_should_send_security_notification
  493. user = User.find(2)
  494. user.admin = true
  495. user.save!
  496. ActionMailer::Base.deliveries.clear
  497. put :update, :params => {
  498. :id => user.id,
  499. :user => {:admin => 0}
  500. }
  501. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  502. assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: user.login), mail
  503. # All admins should receive this
  504. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  505. assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
  506. end
  507. end
  508. def test_update_lock_admin_should_send_security_notification
  509. user = User.find(2)
  510. user.admin = true
  511. user.save!
  512. ActionMailer::Base.deliveries.clear
  513. put :update, :params => {
  514. :id => 2,
  515. :user => {:status => Principal::STATUS_LOCKED}
  516. }
  517. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  518. assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: User.find(2).login), mail
  519. # All admins should receive this
  520. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  521. assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
  522. end
  523. # if user is already locked, destroying should not send a second mail
  524. # (for active admins see furtherbelow)
  525. ActionMailer::Base.deliveries.clear
  526. delete :destroy, :params => {:id => 1}
  527. assert_nil ActionMailer::Base.deliveries.last
  528. end
  529. def test_update_unlock_admin_should_send_security_notification
  530. user = User.find(5) # already locked
  531. user.admin = true
  532. user.save!
  533. ActionMailer::Base.deliveries.clear
  534. put :update, :params => {
  535. :id => user.id,
  536. :user => {:status => Principal::STATUS_ACTIVE}
  537. }
  538. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  539. assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_admin), value: user.login), mail
  540. # All admins should receive this
  541. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  542. assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
  543. end
  544. end
  545. def test_update_admin_unrelated_property_should_not_send_security_notification
  546. ActionMailer::Base.deliveries.clear
  547. put :update, :params => {
  548. :id => 1,
  549. :user => {:firstname => 'Jimmy'}
  550. }
  551. assert_nil ActionMailer::Base.deliveries.last
  552. end
  553. def test_update_should_be_denied_for_anonymous
  554. assert User.find(6).anonymous?
  555. put :update, :params => {:id => 6}
  556. assert_response 404
  557. end
  558. def test_destroy
  559. assert_difference 'User.count', -1 do
  560. delete :destroy, :params => {:id => 2}
  561. end
  562. assert_redirected_to '/users'
  563. assert_nil User.find_by_id(2)
  564. end
  565. def test_destroy_should_be_denied_for_non_admin_users
  566. @request.session[:user_id] = 3
  567. assert_no_difference 'User.count' do
  568. get :destroy, :params => {:id => 2}
  569. end
  570. assert_response 403
  571. end
  572. def test_destroy_should_be_denied_for_anonymous
  573. assert User.find(6).anonymous?
  574. assert_no_difference 'User.count' do
  575. put :destroy, :params => {:id => 6}
  576. end
  577. assert_response 404
  578. end
  579. def test_destroy_should_redirect_to_back_url_param
  580. assert_difference 'User.count', -1 do
  581. delete :destroy, :params => {:id => 2, :back_url => '/users?name=foo'}
  582. end
  583. assert_redirected_to '/users?name=foo'
  584. end
  585. def test_destroy_active_admin_should_send_security_notification
  586. user = User.find(2)
  587. user.admin = true
  588. user.save!
  589. ActionMailer::Base.deliveries.clear
  590. delete :destroy, :params => {:id => user.id}
  591. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  592. assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_admin), value: user.login), mail
  593. # All admins should receive this
  594. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  595. assert_not_nil ActionMailer::Base.deliveries.detect{|mail| [mail.bcc, mail.cc].flatten.include?(admin.mail) }
  596. end
  597. end
  598. end