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 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class UsersControllerTest < Redmine::ControllerTest
  20. include Redmine::I18n
  21. fixtures :users, :user_preferences, :email_addresses, :projects, :members, :member_roles, :roles,
  22. :custom_fields, :custom_values, :groups_users,
  23. :auth_sources,
  24. :enabled_modules,
  25. :issues, :issue_statuses,
  26. :trackers
  27. def setup
  28. User.current = nil
  29. @request.session[:user_id] = 1 # admin
  30. end
  31. def test_index
  32. get :index
  33. assert_response :success
  34. assert_select 'table.users'
  35. assert_select 'tr.user.active'
  36. assert_select 'tr.user.locked', 0
  37. end
  38. def test_index_with_status_filter
  39. get :index, :params => {:status => 3}
  40. assert_response :success
  41. assert_select 'tr.user.active', 0
  42. assert_select 'tr.user.locked'
  43. end
  44. def test_index_with_name_filter
  45. get :index, :params => {:name => 'john'}
  46. assert_response :success
  47. assert_select 'tr.user td.username', :text => 'jsmith'
  48. assert_select 'tr.user', 1
  49. end
  50. def test_index_with_group_filter
  51. get :index, :params => {:group_id => '10'}
  52. assert_response :success
  53. assert_select 'tr.user', Group.find(10).users.count
  54. assert_select 'select[name=group_id]' do
  55. assert_select 'option[value="10"][selected=selected]'
  56. end
  57. end
  58. def test_index_csv
  59. with_settings :default_language => 'en' do
  60. user = User.logged.status(1).first
  61. user.update(passwd_changed_on: Time.current.last_month, twofa_scheme: 'totp')
  62. get :index, params: {format: 'csv'}
  63. assert_response :success
  64. assert_equal User.logged.status(1).count, response.body.chomp.split("\n").size - 1
  65. assert_include format_time(user.updated_on), response.body.split("\n").second
  66. assert_include format_time(user.passwd_changed_on), response.body.split("\n").second
  67. # status
  68. assert_include 'active', response.body.split("\n").second
  69. assert_not_include 'locked', response.body.split("\n").second
  70. # twofa_scheme
  71. assert_include 'Authenticator app', response.body.split("\n").second
  72. assert_include 'disabled', response.body.split("\n").third
  73. assert_equal 'text/csv; header=present', @response.media_type
  74. end
  75. end
  76. def test_index_csv_with_custom_field_columns
  77. float_custom_field = UserCustomField.generate!(:name => 'float field', :field_format => 'float')
  78. date_custom_field = UserCustomField.generate!(:name => 'date field', :field_format => 'date')
  79. user = User.last
  80. user.custom_field_values = {float_custom_field.id.to_s => 2.1, date_custom_field.id.to_s => '2020-01-10'}
  81. user.save
  82. User.find(@request.session[:user_id]).update(:language => nil)
  83. with_settings :default_language => 'fr' do
  84. get :index, :params => {:name => user.lastname, :format => 'csv'}
  85. assert_response :success
  86. assert_include 'float field;date field', response.body
  87. assert_include '2,10;10/01/2020', response.body
  88. assert_equal 'text/csv; header=present', @response.media_type
  89. end
  90. end
  91. def test_index_csv_with_status_filter
  92. with_settings :default_language => 'en' do
  93. get :index, :params => {:status => 3, :format => 'csv'}
  94. assert_response :success
  95. assert_equal User.logged.status(3).count, response.body.chomp.split("\n").size - 1
  96. assert_include 'locked', response.body
  97. assert_not_include 'active', response.body
  98. assert_equal 'text/csv; header=present', @response.media_type
  99. end
  100. end
  101. def test_index_csv_with_name_filter
  102. get :index, :params => {:name => 'John', :format => 'csv'}
  103. assert_response :success
  104. assert_equal User.logged.like('John').count, response.body.chomp.split("\n").size - 1
  105. assert_include 'John', response.body
  106. assert_equal 'text/csv; header=present', @response.media_type
  107. end
  108. def test_index_csv_with_group_filter
  109. get :index, :params => {:group_id => '10', :format => 'csv'}
  110. assert_response :success
  111. assert_equal Group.find(10).users.count, response.body.chomp.split("\n").size - 1
  112. assert_equal 'text/csv; header=present', @response.media_type
  113. end
  114. def test_show
  115. @request.session[:user_id] = nil
  116. get :show, :params => {:id => 2}
  117. assert_response :success
  118. assert_select 'h2', :text => /John Smith/
  119. # groups block should not be rendeder for users which are not part of any group
  120. assert_select 'div#groups', 0
  121. end
  122. def test_show_should_display_visible_custom_fields
  123. @request.session[:user_id] = nil
  124. UserCustomField.find_by_name('Phone number').update_attribute :visible, true
  125. get :show, :params => {:id => 2}
  126. assert_response :success
  127. assert_select 'li.cf_4.string_cf', :text => /Phone number/
  128. end
  129. def test_show_should_not_display_hidden_custom_fields
  130. @request.session[:user_id] = nil
  131. UserCustomField.find_by_name('Phone number').update_attribute :visible, false
  132. get :show, :params => {:id => 2}
  133. assert_response :success
  134. assert_select 'li', :text => /Phone number/, :count => 0
  135. end
  136. def test_show_should_not_fail_when_custom_values_are_nil
  137. user = User.find(2)
  138. # Create a custom field to illustrate the issue
  139. custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
  140. custom_value = user.custom_values.build(:custom_field => custom_field).save!
  141. get :show, :params => {:id => 2}
  142. assert_response :success
  143. end
  144. def test_show_inactive
  145. @request.session[:user_id] = nil
  146. get :show, :params => {:id => 5}
  147. assert_response 404
  148. end
  149. def test_show_inactive_by_admin
  150. @request.session[:user_id] = 1
  151. get :show, :params => {:id => 5}
  152. assert_response 200
  153. assert_select 'h2', :text => /Dave2 Lopper2/
  154. end
  155. def test_show_user_who_is_not_visible_should_return_404
  156. Role.anonymous.update! :users_visibility => 'members_of_visible_projects'
  157. user = User.generate!
  158. @request.session[:user_id] = nil
  159. get :show, :params => {:id => user.id}
  160. assert_response 404
  161. end
  162. def test_show_displays_memberships_based_on_project_visibility
  163. @request.session[:user_id] = 1
  164. get :show, :params => {:id => 2}
  165. assert_response :success
  166. assert_select 'table.list.projects>tbody' do
  167. assert_select 'tr:nth-of-type(1)' do
  168. assert_select 'td:nth-of-type(1)>span>a', :text => 'eCookbook'
  169. assert_select 'td:nth-of-type(2)', :text => 'Manager'
  170. end
  171. assert_select 'tr:nth-of-type(2)' do
  172. assert_select 'td:nth-of-type(1)>span>a', :text => 'Private child of eCookbook'
  173. assert_select 'td:nth-of-type(2)', :text => 'Manager'
  174. end
  175. assert_select 'tr:nth-of-type(3)' do
  176. assert_select 'td:nth-of-type(1)>span>a', :text => 'OnlineStore'
  177. assert_select 'td:nth-of-type(2)', :text => 'Developer'
  178. end
  179. end
  180. end
  181. def test_show_current_should_require_authentication
  182. @request.session[:user_id] = nil
  183. get :show, :params => {:id => 'current'}
  184. assert_response 302
  185. end
  186. def test_show_current
  187. @request.session[:user_id] = 2
  188. get :show, :params => {:id => 'current'}
  189. assert_response :success
  190. assert_select 'h2', :text => /John Smith/
  191. end
  192. def test_show_issues_counts
  193. @request.session[:user_id] = 2
  194. get :show, :params => {:id => 2}
  195. assert_select 'table.list.issue-report>tbody' do
  196. assert_select 'tr:nth-of-type(1)' do
  197. assert_select 'td:nth-of-type(1)>a', :text => 'Assigned issues'
  198. assert_select 'td:nth-of-type(2)>a', :text => '1' # open
  199. assert_select 'td:nth-of-type(3)>a', :text => '0' # closed
  200. assert_select 'td:nth-of-type(4)>a', :text => '1' # total
  201. end
  202. assert_select 'tr:nth-of-type(2)' do
  203. assert_select 'td:nth-of-type(1)>a', :text => 'Reported issues'
  204. assert_select 'td:nth-of-type(2)>a', :text => '11' # open
  205. assert_select 'td:nth-of-type(3)>a', :text => '2' # closed
  206. assert_select 'td:nth-of-type(4)>a', :text => '13' # total
  207. end
  208. end
  209. end
  210. def test_show_user_should_list_user_groups
  211. @request.session[:user_id] = 1
  212. get :show, :params => {:id => 8}
  213. assert_select 'div#groups', 1 do
  214. assert_select 'h3', :text => 'Groups'
  215. assert_select 'li', 2
  216. assert_select 'a[href=?]', '/groups/10/edit', :text => 'A Team'
  217. assert_select 'a[href=?]', '/groups/11/edit', :text => 'B Team'
  218. end
  219. end
  220. def test_show_should_list_all_emails
  221. EmailAddress.create!(user_id: 3, address: 'dlopper@example.net')
  222. EmailAddress.create!(user_id: 3, address: 'dlopper@example.org')
  223. @request.session[:user_id] = 1
  224. get :show, params: {id: 3}
  225. assert_select 'li', text: /Email:/ do
  226. assert_select 'a:nth-of-type(1)', text: 'dlopper@somenet.foo'
  227. assert_select 'a:nth-of-type(2)', text: 'dlopper@example.net'
  228. assert_select 'a:nth-of-type(3)', text: 'dlopper@example.org'
  229. end
  230. end
  231. def test_new
  232. get :new
  233. assert_response :success
  234. assert_select 'input[name=?]', 'user[login]'
  235. assert_select 'label[for=?]>span.required', 'user_password', 1
  236. end
  237. def test_create
  238. assert_difference 'User.count' do
  239. assert_difference 'ActionMailer::Base.deliveries.size' do
  240. post :create, :params => {
  241. :user => {
  242. :firstname => 'John',
  243. :lastname => 'Doe',
  244. :login => 'jdoe',
  245. :password => 'secret123',
  246. :password_confirmation => 'secret123',
  247. :mail => 'jdoe@gmail.com',
  248. :mail_notification => 'none'
  249. },
  250. :send_information => '1'
  251. }
  252. end
  253. end
  254. user = User.order('id DESC').first
  255. assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
  256. assert_equal 'John', user.firstname
  257. assert_equal 'Doe', user.lastname
  258. assert_equal 'jdoe', user.login
  259. assert_equal 'jdoe@gmail.com', user.mail
  260. assert_equal 'none', user.mail_notification
  261. assert user.check_password?('secret123')
  262. mail = ActionMailer::Base.deliveries.last
  263. assert_not_nil mail
  264. assert_equal [user.mail], mail.to
  265. assert_mail_body_match 'secret', mail
  266. end
  267. def test_create_with_preferences
  268. assert_difference 'User.count' do
  269. post :create, :params => {
  270. :user => {
  271. :firstname => 'John',
  272. :lastname => 'Doe',
  273. :login => 'jdoe',
  274. :password => 'secret123',
  275. :password_confirmation => 'secret123',
  276. :mail => 'jdoe@gmail.com',
  277. :mail_notification => 'none'
  278. },
  279. :pref => {
  280. 'hide_mail' => '1',
  281. 'time_zone' => 'Paris',
  282. 'comments_sorting' => 'desc',
  283. 'warn_on_leaving_unsaved' => '0',
  284. 'textarea_font' => 'proportional',
  285. 'history_default_tab' => 'history'
  286. }
  287. }
  288. end
  289. user = User.order('id DESC').first
  290. assert_equal 'jdoe', user.login
  291. assert_equal true, user.pref.hide_mail
  292. assert_equal 'Paris', user.pref.time_zone
  293. assert_equal 'desc', user.pref[:comments_sorting]
  294. assert_equal '0', user.pref[:warn_on_leaving_unsaved]
  295. assert_equal 'proportional', user.pref[:textarea_font]
  296. assert_equal 'history', user.pref[:history_default_tab]
  297. end
  298. def test_create_with_generate_password_should_email_the_password
  299. assert_difference 'User.count' do
  300. post :create, :params => {
  301. :user => {
  302. :login => 'randompass',
  303. :firstname => 'Random',
  304. :lastname => 'Pass',
  305. :mail => 'randompass@example.net',
  306. :language => 'en',
  307. :generate_password => '1',
  308. :password => '',
  309. :password_confirmation => ''
  310. },
  311. :send_information => 1
  312. }
  313. end
  314. user = User.order('id DESC').first
  315. assert_equal 'randompass', user.login
  316. mail = ActionMailer::Base.deliveries.last
  317. assert_not_nil mail
  318. m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
  319. assert m
  320. password = m[1]
  321. assert user.check_password?(password)
  322. end
  323. def test_create_and_continue
  324. post :create, :params => {
  325. :user => {
  326. :login => 'randompass',
  327. :firstname => 'Random',
  328. :lastname => 'Pass',
  329. :mail => 'randompass@example.net',
  330. :generate_password => '1'
  331. },
  332. :continue => '1'
  333. }
  334. assert_redirected_to '/users/new?user%5Bgenerate_password%5D=1'
  335. end
  336. def test_create_with_failure
  337. assert_no_difference 'User.count' do
  338. post :create, :params => {:user => {:login => 'foo'}}
  339. end
  340. assert_response :success
  341. assert_select_error /Email cannot be blank/
  342. end
  343. def test_create_with_failure_sould_preserve_preference
  344. assert_no_difference 'User.count' do
  345. post :create, :params => {
  346. :user => {
  347. :login => 'foo'
  348. },
  349. :pref => {
  350. 'no_self_notified' => '1',
  351. 'hide_mail' => '1',
  352. 'time_zone' => 'Paris',
  353. 'comments_sorting' => 'desc',
  354. 'warn_on_leaving_unsaved' => '0'
  355. }
  356. }
  357. end
  358. assert_response :success
  359. assert_select 'select#pref_time_zone option[selected=selected]', :text => /Paris/
  360. assert_select 'input#pref_no_self_notified[value="1"][checked=checked]'
  361. end
  362. def test_create_admin_should_send_security_notification
  363. ActionMailer::Base.deliveries.clear
  364. post :create, :params => {
  365. :user => {
  366. :firstname => 'Edgar',
  367. :lastname => 'Schmoe',
  368. :login => 'eschmoe',
  369. :password => 'secret123',
  370. :password_confirmation => 'secret123',
  371. :mail => 'eschmoe@example.foo',
  372. :admin => '1'
  373. }
  374. }
  375. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  376. assert_mail_body_match '0.0.0.0', mail
  377. assert_mail_body_match(
  378. I18n.t(
  379. :mail_body_security_notification_add,
  380. field: I18n.t(:field_admin),
  381. value: 'eschmoe'
  382. ),
  383. mail
  384. )
  385. assert_select_email do
  386. assert_select 'a[href^=?]', 'http://localhost:3000/users', :text => 'Users'
  387. end
  388. # All admins should receive this
  389. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  390. assert_not_nil(
  391. ActionMailer::Base.deliveries.detect do |mail|
  392. [mail.to].flatten.include?(admin.mail)
  393. end
  394. )
  395. end
  396. end
  397. def test_create_non_admin_should_not_send_security_notification
  398. ActionMailer::Base.deliveries.clear
  399. post :create, :params => {
  400. :user => {
  401. :firstname => 'Edgar',
  402. :lastname => 'Schmoe',
  403. :login => 'eschmoe',
  404. :password => 'secret123',
  405. :password_confirmation => 'secret123',
  406. :mail => 'eschmoe@example.foo',
  407. :admin => '0'
  408. }
  409. }
  410. assert_nil ActionMailer::Base.deliveries.last
  411. end
  412. def test_edit
  413. with_settings :gravatar_enabled => '1' do
  414. get :edit, :params => {:id => 2}
  415. end
  416. assert_response :success
  417. assert_select 'h2>a+img.gravatar'
  418. assert_select 'input[name=?][value=?]', 'user[login]', 'jsmith'
  419. assert_select 'label[for=?]>span.required', 'user_password', 0
  420. end
  421. def test_edit_registered_user
  422. assert User.find(2).register!
  423. get :edit, :params => {:id => 2}
  424. assert_response :success
  425. assert_select 'a', :text => 'Activate'
  426. end
  427. def test_edit_should_be_denied_for_anonymous
  428. assert User.find(6).anonymous?
  429. get :edit, :params => {:id => 6}
  430. assert_response 404
  431. end
  432. def test_edit_user_with_full_text_formatting_custom_field_should_not_fail
  433. field = UserCustomField.find(4)
  434. field.update_attribute :text_formatting, 'full'
  435. get :edit, :params => {:id => 2}
  436. assert_response :success
  437. end
  438. def test_update
  439. ActionMailer::Base.deliveries.clear
  440. put :update, :params => {
  441. :id => 2,
  442. :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'},
  443. :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
  444. }
  445. user = User.find(2)
  446. assert_equal 'Changed', user.firstname
  447. assert_equal 'only_assigned', user.mail_notification
  448. assert_equal true, user.pref[:hide_mail]
  449. assert_equal 'desc', user.pref[:comments_sorting]
  450. assert ActionMailer::Base.deliveries.empty?
  451. end
  452. def test_update_with_failure
  453. assert_no_difference 'User.count' do
  454. put :update, :params => {
  455. :id => 2,
  456. :user => {:firstname => ''}
  457. }
  458. end
  459. assert_response :success
  460. assert_select_error /First name cannot be blank/
  461. end
  462. def test_update_with_group_ids_should_assign_groups
  463. put :update, :params => {
  464. :id => 2,
  465. :user => {:group_ids => ['10']}
  466. }
  467. user = User.find(2)
  468. assert_equal [10], user.group_ids
  469. end
  470. def test_update_with_activation_should_send_a_notification
  471. u = User.new(:firstname => 'Foo', :lastname => 'Bar',
  472. :mail => 'foo.bar@somenet.foo', :language => 'fr')
  473. u.login = 'foo'
  474. u.status = User::STATUS_REGISTERED
  475. u.save!
  476. ActionMailer::Base.deliveries.clear
  477. put(
  478. :update,
  479. :params => {
  480. :id => u.id,
  481. :user => {:status => User::STATUS_ACTIVE}
  482. }
  483. )
  484. assert u.reload.active?
  485. mail = ActionMailer::Base.deliveries.last
  486. assert_not_nil mail
  487. assert_equal ['foo.bar@somenet.foo'], mail.to
  488. assert_mail_body_match ll('fr', :notice_account_activated), mail
  489. end
  490. def test_update_with_password_change_should_send_a_notification
  491. ActionMailer::Base.deliveries.clear
  492. put(
  493. :update,
  494. :params => {
  495. :id => 2,
  496. :user => {
  497. :password => 'newpass123',
  498. :password_confirmation => 'newpass123'
  499. },
  500. :send_information => '1'
  501. }
  502. )
  503. u = User.find(2)
  504. assert u.check_password?('newpass123')
  505. mail = ActionMailer::Base.deliveries.last
  506. assert_not_nil mail
  507. assert_equal [u.mail], mail.to
  508. assert_mail_body_match 'newpass123', mail
  509. end
  510. def test_update_with_password_change_by_admin_should_send_a_security_notification
  511. ActionMailer::Base.deliveries.clear
  512. user = User.find_by(login: 'jsmith')
  513. put :update, :params => {
  514. :id => user.id,
  515. :user => {:password => 'newpass123', :password_confirmation => 'newpass123'}
  516. }
  517. assert_equal 1, ActionMailer::Base.deliveries.size
  518. mail = ActionMailer::Base.deliveries.last
  519. assert_equal [user.mail], mail.to
  520. assert_match 'Security notification', mail.subject
  521. assert_mail_body_match 'Your password has been changed.', mail
  522. end
  523. def test_update_with_generate_password_should_email_the_password
  524. ActionMailer::Base.deliveries.clear
  525. put(
  526. :update,
  527. :params => {
  528. :id => 2,
  529. :user => {
  530. :generate_password => '1',
  531. :password => '',
  532. :password_confirmation => ''
  533. },
  534. :send_information => '1'
  535. }
  536. )
  537. mail = ActionMailer::Base.deliveries.last
  538. assert_not_nil mail
  539. u = User.find(2)
  540. assert_equal [u.mail], mail.to
  541. m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
  542. assert m
  543. password = m[1]
  544. assert u.check_password?(password)
  545. end
  546. def test_update_without_generate_password_should_not_change_password
  547. put :update, :params => {
  548. :id => 2, :user => {
  549. :firstname => 'changed',
  550. :generate_password => '0',
  551. :password => '',
  552. :password_confirmation => ''
  553. },
  554. :send_information => '1'
  555. }
  556. user = User.find(2)
  557. assert_equal 'changed', user.firstname
  558. assert user.check_password?('jsmith')
  559. end
  560. def test_update_user_switchin_from_auth_source_to_password_authentication
  561. # Configure as auth source
  562. u = User.find(2)
  563. u.auth_source = AuthSource.find(1)
  564. u.save!
  565. put :update, :params => {
  566. :id => u.id,
  567. :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'}
  568. }
  569. assert_nil u.reload.auth_source
  570. assert u.check_password?('newpass123')
  571. end
  572. def test_update_notified_project
  573. get :edit, :params => {:id => 2}
  574. assert_response :success
  575. u = User.find(2)
  576. assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort
  577. assert_equal [1, 2, 5], u.notified_projects_ids.sort
  578. assert_select 'input[name=?][value=?]', 'user[notified_project_ids][]', '1'
  579. assert_equal 'all', u.mail_notification
  580. put :update, :params => {
  581. :id => 2,
  582. :user => {
  583. :mail_notification => 'selected',
  584. :notified_project_ids => [1, 2]
  585. }
  586. }
  587. u = User.find(2)
  588. assert_equal 'selected', u.mail_notification
  589. assert_equal [1, 2], u.notified_projects_ids.sort
  590. end
  591. def test_update_status_should_not_update_attributes
  592. user = User.find(2)
  593. user.pref[:no_self_notified] = '1'
  594. user.pref.save
  595. put :update, :params => {
  596. :id => 2,
  597. :user => {:status => 3}
  598. }
  599. assert_response 302
  600. user = User.find(2)
  601. assert_equal 3, user.status
  602. assert_equal '1', user.pref[:no_self_notified]
  603. end
  604. def test_update_assign_admin_should_send_security_notification
  605. ActionMailer::Base.deliveries.clear
  606. put :update, :params => {
  607. :id => 2,
  608. :user => {:admin => 1}
  609. }
  610. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  611. assert_mail_body_match(
  612. I18n.t(
  613. :mail_body_security_notification_add,
  614. field: I18n.t(:field_admin),
  615. value: User.find(2).login
  616. ),
  617. mail
  618. )
  619. # All admins should receive this
  620. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  621. assert_not_nil(
  622. ActionMailer::Base.deliveries.detect do |mail|
  623. [mail.to].flatten.include?(admin.mail)
  624. end
  625. )
  626. end
  627. end
  628. def test_update_unassign_admin_should_send_security_notification
  629. user = User.find(2)
  630. user.admin = true
  631. user.save!
  632. ActionMailer::Base.deliveries.clear
  633. put :update, :params => {
  634. :id => user.id,
  635. :user => {:admin => 0}
  636. }
  637. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  638. assert_mail_body_match(
  639. I18n.t(
  640. :mail_body_security_notification_remove,
  641. field: I18n.t(:field_admin),
  642. value: user.login
  643. ),
  644. mail
  645. )
  646. # All admins should receive this
  647. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  648. assert_not_nil(
  649. ActionMailer::Base.deliveries.detect do |mail|
  650. [mail.to].flatten.include?(admin.mail)
  651. end
  652. )
  653. end
  654. end
  655. def test_update_lock_admin_should_send_security_notification
  656. user = User.find(2)
  657. user.admin = true
  658. user.save!
  659. ActionMailer::Base.deliveries.clear
  660. put :update, :params => {
  661. :id => 2,
  662. :user => {:status => Principal::STATUS_LOCKED}
  663. }
  664. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  665. assert_mail_body_match(
  666. I18n.t(
  667. :mail_body_security_notification_remove,
  668. field: I18n.t(:field_admin),
  669. value: User.find(2).login
  670. ),
  671. mail
  672. )
  673. # All admins should receive this
  674. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  675. assert_not_nil(
  676. ActionMailer::Base.deliveries.detect do |mail|
  677. [mail.to].flatten.include?(admin.mail)
  678. end
  679. )
  680. end
  681. # if user is already locked, destroying should not send a second mail
  682. # (for active admins see furtherbelow)
  683. ActionMailer::Base.deliveries.clear
  684. delete :destroy, :params => {:id => 1, :confirm => User.find(1).login}
  685. assert_nil ActionMailer::Base.deliveries.last
  686. end
  687. def test_update_unlock_admin_should_send_security_notification
  688. user = User.find(5) # already locked
  689. user.admin = true
  690. user.save!
  691. ActionMailer::Base.deliveries.clear
  692. put :update, :params => {
  693. :id => user.id,
  694. :user => {:status => Principal::STATUS_ACTIVE}
  695. }
  696. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  697. assert_mail_body_match(
  698. I18n.t(
  699. :mail_body_security_notification_add,
  700. field: I18n.t(:field_admin),
  701. value: user.login
  702. ),
  703. mail
  704. )
  705. # All admins should receive this
  706. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  707. assert_not_nil(
  708. ActionMailer::Base.deliveries.detect do |mail|
  709. [mail.to].flatten.include?(admin.mail)
  710. end
  711. )
  712. end
  713. end
  714. def test_update_admin_unrelated_property_should_not_send_security_notification
  715. ActionMailer::Base.deliveries.clear
  716. put :update, :params => {
  717. :id => 1,
  718. :user => {:firstname => 'Jimmy'}
  719. }
  720. assert_nil ActionMailer::Base.deliveries.last
  721. end
  722. def test_update_should_be_denied_for_anonymous
  723. assert User.find(6).anonymous?
  724. put :update, :params => {:id => 6}
  725. assert_response 404
  726. end
  727. def test_update_with_blank_email_should_not_raise_exception
  728. assert_no_difference 'User.count' do
  729. with_settings :gravatar_enabled => '1' do
  730. put :update, :params => {
  731. :id => 2,
  732. :user => {:mail => ''}
  733. }
  734. end
  735. end
  736. assert_response :success
  737. assert_select_error /Email cannot be blank/
  738. end
  739. def test_destroy
  740. assert_difference 'User.count', -1 do
  741. delete :destroy, :params => {:id => 2, :confirm => User.find(2).login}
  742. end
  743. assert_redirected_to '/users'
  744. assert_nil User.find_by_id(2)
  745. end
  746. def test_destroy_with_lock_param_should_lock_instead
  747. assert_no_difference 'User.count' do
  748. delete :destroy, :params => {:id => 2, :lock => 'lock'}
  749. end
  750. assert_redirected_to '/users'
  751. assert User.find_by_id(2).locked?
  752. end
  753. def test_destroy_should_require_confirmation
  754. assert_no_difference 'User.count' do
  755. delete :destroy, :params => {:id => 2}
  756. end
  757. assert_response :success
  758. assert_select '.warning', :text => /Are you sure you want to delete this user/
  759. end
  760. def test_destroy_should_require_correct_confirmation
  761. assert_no_difference 'User.count' do
  762. delete :destroy, :params => {:id => 2, :confirm => 'wrong'}
  763. end
  764. assert_response :success
  765. assert_select '.warning', :text => /Are you sure you want to delete this user/
  766. end
  767. def test_destroy_should_be_denied_for_non_admin_users
  768. @request.session[:user_id] = 3
  769. assert_no_difference 'User.count' do
  770. delete :destroy, :params => {:id => 2, :confirm => User.find(2).login}
  771. end
  772. assert_response 403
  773. end
  774. def test_destroy_should_be_denied_for_anonymous
  775. assert User.find(6).anonymous?
  776. assert_no_difference 'User.count' do
  777. delete :destroy, :params => {:id => 6, :confirm => User.find(6).login}
  778. end
  779. assert_response 404
  780. end
  781. def test_destroy_should_redirect_to_back_url_param
  782. assert_difference 'User.count', -1 do
  783. delete :destroy, :params => {:id => 2,
  784. :confirm => User.find(2).login,
  785. :back_url => '/users?name=foo'}
  786. end
  787. assert_redirected_to '/users?name=foo'
  788. end
  789. def test_destroy_active_admin_should_send_security_notification
  790. user = User.find(2)
  791. user.admin = true
  792. user.save!
  793. ActionMailer::Base.deliveries.clear
  794. delete :destroy, :params => {:id => user.id, :confirm => user.login}
  795. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  796. assert_mail_body_match(
  797. I18n.t(
  798. :mail_body_security_notification_remove,
  799. field: I18n.t(:field_admin),
  800. value: user.login
  801. ),
  802. mail
  803. )
  804. # All admins should receive this
  805. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  806. assert_not_nil(
  807. ActionMailer::Base.deliveries.detect do |mail|
  808. [mail.to].flatten.include?(admin.mail)
  809. end
  810. )
  811. end
  812. end
  813. def test_destroy_without_unsubscribe_is_denied
  814. user = User.find(2)
  815. user.update(admin: true) # Create other admin so self can be deleted
  816. @request.session[:user_id] = user.id
  817. with_settings unsubscribe: 0 do
  818. assert_no_difference 'User.count' do
  819. delete :destroy, params: {id: user.id}
  820. end
  821. assert_response 422
  822. end
  823. end
  824. def test_destroy_last_admin_is_denied
  825. user = User.find(1)
  826. @request.session[:user_id] = user.id
  827. with_settings unsubscribe: 1 do
  828. assert_no_difference 'User.count' do
  829. delete :destroy, params: {id: user.id}
  830. end
  831. assert_response 422
  832. end
  833. end
  834. end