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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  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. active = User.active.first
  35. locked = User.where(status: User::STATUS_LOCKED).first
  36. assert_select 'table.users'
  37. assert_select "tr#user-#{active.id}"
  38. assert_select "tr#user-#{locked.id}", 0
  39. end
  40. def test_index_with_status_filter
  41. get :index, params: { set_filter: 1, f: ['status'], op: {status: '='}, v: {status: [3]} }
  42. assert_response :success
  43. assert_select "tr.user", User.where(status: 3).count
  44. end
  45. def test_index_with_firstname_filter
  46. get :index, params: { set_filter: 1, f: ['firstname'], op: {firstname: '~'}, v: {firstname: ['john']} }
  47. assert_response :success
  48. assert_select 'tr.user td.login', text: 'jsmith'
  49. assert_select 'tr.user', 1
  50. end
  51. def test_index_with_group_filter
  52. get :index, params: {
  53. set_filter: 1,
  54. f: ['is_member_of_group'], op: {is_member_of_group: '='}, v: {is_member_of_group: ['10']}
  55. }
  56. assert_response :success
  57. assert_select 'tr.user', Group.find(10).users.count
  58. end
  59. def test_index_should_not_show_2fa_filter_and_column_if_disabled
  60. with_settings twofa: "0" do
  61. get :index
  62. assert_response :success
  63. assert_select "select#add_filter_select" do
  64. assert_select "option[value=twofa_scheme]", 0
  65. end
  66. assert_select "select#available_c" do
  67. assert_select "option[value=twofa_scheme]", 0
  68. end
  69. end
  70. end
  71. def test_index_filter_by_twofa_yes
  72. with_settings twofa: "1" do
  73. user = User.find(1)
  74. user.twofa_totp_key = "AVYA3RARZ3GY3VWT7MIEJ72I5TTJRO3X"
  75. user.twofa_scheme = "totp"
  76. user.save
  77. get :index, params: { set_filter: 1, f: ['twofa_scheme'], op: {twofa_scheme: '*'} }
  78. assert_response :success
  79. assert_select 'tr#user-1', 1
  80. assert_select 'tr.user', 1
  81. assert_select "select#add_filter_select" do
  82. assert_select "option[value=twofa_scheme]"
  83. end
  84. assert_select "select#available_c" do
  85. assert_select "option[value=twofa_scheme]"
  86. end
  87. end
  88. end
  89. def test_index_filter_by_twofa_scheme
  90. with_settings twofa: "1" do
  91. user = User.find(1)
  92. user.twofa_totp_key = "AVYA3RARZ3GY3VWT7MIEJ72I5TTJRO3X"
  93. user.twofa_scheme = "totp"
  94. user.save
  95. get :index, params: {
  96. set_filter: 1,
  97. f: ['twofa_scheme'], op: {twofa_scheme: '='}, v: {twofa_scheme: ['totp']}
  98. }
  99. assert_response :success
  100. assert_select 'tr#user-1', 1
  101. assert_select "select#add_filter_select" do
  102. assert_select "option[value=twofa_scheme]"
  103. end
  104. assert_select "select#available_c" do
  105. assert_select "option[value=twofa_scheme]"
  106. end
  107. end
  108. end
  109. def test_index_filter_by_twofa_no
  110. with_settings twofa: "1" do
  111. user = User.find(1)
  112. user.twofa_totp_key = "AVYA3RARZ3GY3VWT7MIEJ72I5TTJRO3X"
  113. user.twofa_scheme = "totp"
  114. user.save
  115. get :index, params: { set_filter: 1, f: ['twofa_scheme'], op: {twofa_scheme: '!*'} }
  116. assert_response :success
  117. assert_select 'tr#user-1', 0
  118. assert_select 'tr.user'
  119. end
  120. end
  121. def test_index_filter_by_auth_source_none
  122. user = User.find(1)
  123. user.update_column :auth_source_id, 1
  124. get :index, params: {
  125. set_filter: 1,
  126. f: ['auth_source_id'], op: {auth_source_id: '!*'}
  127. }
  128. assert_response :success
  129. assert_select 'tr.user'
  130. assert_select 'tr#user-1', 0
  131. end
  132. def test_index_filter_by_auth_source
  133. user = User.find(1)
  134. user.update_column :auth_source_id, 1
  135. get :index, params: {
  136. set_filter: 1,
  137. f: ['auth_source_id'], op: {auth_source_id: '='}, v: {auth_source_id: ['1']}
  138. }
  139. assert_response :success
  140. assert_select 'tr#user-1', 1
  141. assert_select "select#add_filter_select" do
  142. assert_select "option[value=auth_source_id]"
  143. end
  144. assert_select "select#available_c" do
  145. assert_select "option[value='auth_source.name']"
  146. end
  147. end
  148. def test_index_with_auth_source_column
  149. user = User.find(1)
  150. user.update_column :auth_source_id, 1
  151. get :index, params: {
  152. set_filter: 1,
  153. f: ['auth_source_id'], op: {auth_source_id: '='}, v: {auth_source_id: ['1']},
  154. c: %w(login firstname lastname mail auth_source.name)
  155. }
  156. assert_response :success
  157. assert_select 'tr#user-1', 1
  158. end
  159. def test_index_csv
  160. with_settings :default_language => 'en' do
  161. user = User.logged.status(1).first
  162. user.update(passwd_changed_on: Time.current.last_month, twofa_scheme: 'totp')
  163. get :index, params: {format: 'csv', c: ['updated_on', 'status', 'passwd_changed_on', 'twofa_scheme']}
  164. assert_response :success
  165. assert_equal User.logged.status(1).count, response.body.chomp.split("\n").size - 1
  166. assert_include format_time(user.updated_on), response.body.split("\n").second
  167. assert_include format_time(user.passwd_changed_on), response.body.split("\n").second
  168. # status
  169. assert_include 'active', response.body.split("\n").second
  170. assert_not_include 'locked', response.body.split("\n").second
  171. # twofa_scheme
  172. assert_include 'Authenticator app', response.body.split("\n").second
  173. assert_include 'disabled', response.body.split("\n").third
  174. assert_equal 'text/csv; header=present', @response.media_type
  175. end
  176. end
  177. def test_index_csv_with_custom_field_columns
  178. float_custom_field = UserCustomField.generate!(:name => 'float field', :field_format => 'float')
  179. date_custom_field = UserCustomField.generate!(:name => 'date field', :field_format => 'date')
  180. user = User.last
  181. user.custom_field_values = {float_custom_field.id.to_s => 2.1, date_custom_field.id.to_s => '2020-01-10'}
  182. user.save
  183. User.find(@request.session[:user_id]).update(:language => nil)
  184. with_settings :default_language => 'fr' do
  185. get :index, params: {
  186. c: ["cf_#{float_custom_field.id}", "cf_#{date_custom_field.id}"],
  187. f: ["name"],
  188. op: { name: "~" },
  189. v: { name: [user.lastname] },
  190. format: 'csv'
  191. }
  192. assert_response :success
  193. assert_include 'float field;date field', response.body
  194. assert_include '2,10;10/01/2020', response.body
  195. assert_equal 'text/csv; header=present', @response.media_type
  196. end
  197. end
  198. def test_index_csv_with_status_filter
  199. with_settings :default_language => 'en' do
  200. get :index, :params => {
  201. :set_filter => '1',
  202. :f => [:status], :op => { :status => '=' }, :v => { :status => [3] },
  203. :c => [:login, :status],
  204. :format => 'csv'
  205. }
  206. assert_response :success
  207. assert_equal User.logged.status(3).count, response.body.chomp.split("\n").size - 1
  208. assert_include 'locked', response.body
  209. assert_not_include 'active', response.body
  210. assert_equal 'text/csv; header=present', @response.media_type
  211. end
  212. end
  213. def test_index_csv_with_name_filter
  214. get :index, :params => {
  215. :set_filter => '1',
  216. :f => [:firstname], :op => { :firstname => '~' }, :v => { :firstname => ['John'] },
  217. :c => [:login, :firstname, :status],
  218. :format => 'csv'
  219. }
  220. assert_response :success
  221. assert_equal User.logged.like('John').count, response.body.chomp.split("\n").size - 1
  222. assert_include 'John', response.body
  223. assert_equal 'text/csv; header=present', @response.media_type
  224. end
  225. def test_index_csv_with_group_filter
  226. get :index, :params => {
  227. :set_filter => '1',
  228. :f => [:is_member_of_group], :op => { :is_member_of_group => '=' }, :v => { :is_member_of_group => [10] },
  229. :c => [:login, :status],
  230. :format => 'csv'
  231. }
  232. assert_response :success
  233. assert_equal Group.find(10).users.count, response.body.chomp.split("\n").size - 1
  234. assert_equal 'text/csv; header=present', @response.media_type
  235. end
  236. def test_index_csv_filename_without_query_id_param
  237. get :index, :params => {:format => 'csv'}
  238. assert_response :success
  239. assert_match /users.csv/, @response.headers['Content-Disposition']
  240. end
  241. def test_index_csv_filename_with_query_id_param
  242. query = UserQuery.create!(:name => 'My Query Name', :visibility => UserQuery::VISIBILITY_PUBLIC)
  243. get :index, :params => {:query_id => query.id, :format => 'csv'}
  244. assert_response :success
  245. assert_match /my_query_name\.csv/, @response.headers['Content-Disposition']
  246. end
  247. def test_show
  248. @request.session[:user_id] = nil
  249. get :show, :params => {:id => 2}
  250. assert_response :success
  251. assert_select 'h2', :text => /John Smith/
  252. # groups block should not be rendeder for users which are not part of any group
  253. assert_select 'div#groups', 0
  254. end
  255. def test_show_should_display_visible_custom_fields
  256. @request.session[:user_id] = nil
  257. UserCustomField.find_by_name('Phone number').update_attribute :visible, true
  258. get :show, :params => {:id => 2}
  259. assert_response :success
  260. assert_select 'li.cf_4.string_cf', :text => /Phone number/
  261. end
  262. def test_show_should_not_display_hidden_custom_fields
  263. @request.session[:user_id] = nil
  264. UserCustomField.find_by_name('Phone number').update_attribute :visible, false
  265. get :show, :params => {:id => 2}
  266. assert_response :success
  267. assert_select 'li', :text => /Phone number/, :count => 0
  268. end
  269. def test_show_should_not_fail_when_custom_values_are_nil
  270. user = User.find(2)
  271. # Create a custom field to illustrate the issue
  272. custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
  273. custom_value = user.custom_values.build(:custom_field => custom_field).save!
  274. get :show, :params => {:id => 2}
  275. assert_response :success
  276. end
  277. def test_show_inactive
  278. @request.session[:user_id] = nil
  279. get :show, :params => {:id => 5}
  280. assert_response 404
  281. end
  282. def test_show_inactive_by_admin
  283. @request.session[:user_id] = 1
  284. get :show, :params => {:id => 5}
  285. assert_response 200
  286. assert_select 'h2', :text => /Dave2 Lopper2/
  287. end
  288. def test_show_user_who_is_not_visible_should_return_404
  289. Role.anonymous.update! :users_visibility => 'members_of_visible_projects'
  290. user = User.generate!
  291. @request.session[:user_id] = nil
  292. get :show, :params => {:id => user.id}
  293. assert_response 404
  294. end
  295. def test_show_displays_memberships_based_on_project_visibility
  296. @request.session[:user_id] = 1
  297. get :show, :params => {:id => 2}
  298. assert_response :success
  299. assert_select 'table.list.projects>tbody' do
  300. assert_select 'tr:nth-of-type(1)' do
  301. assert_select 'td:nth-of-type(1)>span>a', :text => 'eCookbook'
  302. assert_select 'td:nth-of-type(2)', :text => 'Manager'
  303. end
  304. assert_select 'tr:nth-of-type(2)' do
  305. assert_select 'td:nth-of-type(1)>span>a', :text => 'Private child of eCookbook'
  306. assert_select 'td:nth-of-type(2)', :text => 'Manager'
  307. end
  308. assert_select 'tr:nth-of-type(3)' do
  309. assert_select 'td:nth-of-type(1)>span>a', :text => 'OnlineStore'
  310. assert_select 'td:nth-of-type(2)', :text => 'Developer'
  311. end
  312. end
  313. end
  314. def test_show_current_should_require_authentication
  315. @request.session[:user_id] = nil
  316. get :show, :params => {:id => 'current'}
  317. assert_response 302
  318. end
  319. def test_show_current
  320. @request.session[:user_id] = 2
  321. get :show, :params => {:id => 'current'}
  322. assert_response :success
  323. assert_select 'h2', :text => /John Smith/
  324. end
  325. def test_show_issues_counts
  326. @request.session[:user_id] = 2
  327. get :show, :params => {:id => 2}
  328. assert_select 'table.list.issue-report>tbody' do
  329. assert_select 'tr:nth-of-type(1)' do
  330. assert_select 'td:nth-of-type(1)>a', :text => 'Assigned issues'
  331. assert_select 'td:nth-of-type(2)>a', :text => '1' # open
  332. assert_select 'td:nth-of-type(3)>a', :text => '0' # closed
  333. assert_select 'td:nth-of-type(4)>a', :text => '1' # total
  334. end
  335. assert_select 'tr:nth-of-type(2)' do
  336. assert_select 'td:nth-of-type(1)>a', :text => 'Reported issues'
  337. assert_select 'td:nth-of-type(2)>a', :text => '11' # open
  338. assert_select 'td:nth-of-type(3)>a', :text => '2' # closed
  339. assert_select 'td:nth-of-type(4)>a', :text => '13' # total
  340. end
  341. end
  342. end
  343. def test_show_user_should_list_user_groups
  344. @request.session[:user_id] = 1
  345. get :show, :params => {:id => 8}
  346. assert_select 'div#groups', 1 do
  347. assert_select 'h3', :text => 'Groups'
  348. assert_select 'li', 2
  349. assert_select 'a[href=?]', '/groups/10/edit', :text => 'A Team'
  350. assert_select 'a[href=?]', '/groups/11/edit', :text => 'B Team'
  351. end
  352. end
  353. def test_show_should_list_all_emails
  354. EmailAddress.create!(user_id: 3, address: 'dlopper@example.net')
  355. EmailAddress.create!(user_id: 3, address: 'dlopper@example.org')
  356. @request.session[:user_id] = 1
  357. get :show, params: {id: 3}
  358. assert_select 'li', text: /Email:/ do
  359. assert_select 'a:nth-of-type(1)', text: 'dlopper@somenet.foo'
  360. assert_select 'a:nth-of-type(2)', text: 'dlopper@example.net'
  361. assert_select 'a:nth-of-type(3)', text: 'dlopper@example.org'
  362. end
  363. end
  364. def test_new
  365. get :new
  366. assert_response :success
  367. assert_select 'input[name=?]', 'user[login]'
  368. assert_select 'label[for=?]>span.required', 'user_password', 1
  369. end
  370. def test_create
  371. assert_difference 'User.count' do
  372. assert_difference 'ActionMailer::Base.deliveries.size' do
  373. post :create, :params => {
  374. :user => {
  375. :firstname => 'John',
  376. :lastname => 'Doe',
  377. :login => 'jdoe',
  378. :password => 'secret123',
  379. :password_confirmation => 'secret123',
  380. :mail => 'jdoe@gmail.com',
  381. :mail_notification => 'none'
  382. },
  383. :send_information => '1'
  384. }
  385. end
  386. end
  387. user = User.order('id DESC').first
  388. assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
  389. assert_equal 'John', user.firstname
  390. assert_equal 'Doe', user.lastname
  391. assert_equal 'jdoe', user.login
  392. assert_equal 'jdoe@gmail.com', user.mail
  393. assert_equal 'none', user.mail_notification
  394. assert user.check_password?('secret123')
  395. mail = ActionMailer::Base.deliveries.last
  396. assert_not_nil mail
  397. assert_equal [user.mail], mail.to
  398. assert_mail_body_match 'secret', mail
  399. end
  400. def test_create_with_preferences
  401. assert_difference 'User.count' do
  402. post :create, :params => {
  403. :user => {
  404. :firstname => 'John',
  405. :lastname => 'Doe',
  406. :login => 'jdoe',
  407. :password => 'secret123',
  408. :password_confirmation => 'secret123',
  409. :mail => 'jdoe@gmail.com',
  410. :mail_notification => 'none'
  411. },
  412. :pref => {
  413. 'hide_mail' => '1',
  414. 'time_zone' => 'Paris',
  415. 'comments_sorting' => 'desc',
  416. 'warn_on_leaving_unsaved' => '0',
  417. 'textarea_font' => 'proportional',
  418. 'history_default_tab' => 'history'
  419. }
  420. }
  421. end
  422. user = User.order('id DESC').first
  423. assert_equal 'jdoe', user.login
  424. assert_equal true, user.pref.hide_mail
  425. assert_equal 'Paris', user.pref.time_zone
  426. assert_equal 'desc', user.pref[:comments_sorting]
  427. assert_equal '0', user.pref[:warn_on_leaving_unsaved]
  428. assert_equal 'proportional', user.pref[:textarea_font]
  429. assert_equal 'history', user.pref[:history_default_tab]
  430. end
  431. def test_create_with_generate_password_should_email_the_password
  432. assert_difference 'User.count' do
  433. post :create, :params => {
  434. :user => {
  435. :login => 'randompass',
  436. :firstname => 'Random',
  437. :lastname => 'Pass',
  438. :mail => 'randompass@example.net',
  439. :language => 'en',
  440. :generate_password => '1',
  441. :password => '',
  442. :password_confirmation => ''
  443. },
  444. :send_information => 1
  445. }
  446. end
  447. user = User.order('id DESC').first
  448. assert_equal 'randompass', user.login
  449. mail = ActionMailer::Base.deliveries.last
  450. assert_not_nil mail
  451. m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
  452. assert m
  453. password = m[1]
  454. assert user.check_password?(password)
  455. end
  456. def test_create_and_continue
  457. post :create, :params => {
  458. :user => {
  459. :login => 'randompass',
  460. :firstname => 'Random',
  461. :lastname => 'Pass',
  462. :mail => 'randompass@example.net',
  463. :generate_password => '1'
  464. },
  465. :continue => '1'
  466. }
  467. assert_redirected_to '/users/new?user%5Bgenerate_password%5D=1'
  468. end
  469. def test_create_with_failure
  470. assert_no_difference 'User.count' do
  471. post :create, :params => {:user => {:login => 'foo'}}
  472. end
  473. assert_response :success
  474. assert_select_error /Email cannot be blank/
  475. end
  476. def test_create_with_failure_sould_preserve_preference
  477. assert_no_difference 'User.count' do
  478. post :create, :params => {
  479. :user => {
  480. :login => 'foo'
  481. },
  482. :pref => {
  483. 'no_self_notified' => '1',
  484. 'hide_mail' => '1',
  485. 'time_zone' => 'Paris',
  486. 'comments_sorting' => 'desc',
  487. 'warn_on_leaving_unsaved' => '0'
  488. }
  489. }
  490. end
  491. assert_response :success
  492. assert_select 'select#pref_time_zone option[selected=selected]', :text => /Paris/
  493. assert_select 'input#pref_no_self_notified[value="1"][checked=checked]'
  494. end
  495. def test_create_admin_should_send_security_notification
  496. ActionMailer::Base.deliveries.clear
  497. post :create, :params => {
  498. :user => {
  499. :firstname => 'Edgar',
  500. :lastname => 'Schmoe',
  501. :login => 'eschmoe',
  502. :password => 'secret123',
  503. :password_confirmation => 'secret123',
  504. :mail => 'eschmoe@example.foo',
  505. :admin => '1'
  506. }
  507. }
  508. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  509. assert_mail_body_match '0.0.0.0', mail
  510. assert_mail_body_match(
  511. I18n.t(
  512. :mail_body_security_notification_add,
  513. field: I18n.t(:field_admin),
  514. value: 'eschmoe'
  515. ),
  516. mail
  517. )
  518. assert_select_email do
  519. assert_select 'a[href^=?]', 'http://localhost:3000/users', :text => 'Users'
  520. end
  521. # All admins should receive this
  522. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  523. assert_not_nil(
  524. ActionMailer::Base.deliveries.detect do |mail|
  525. [mail.to].flatten.include?(admin.mail)
  526. end
  527. )
  528. end
  529. end
  530. def test_create_non_admin_should_not_send_security_notification
  531. ActionMailer::Base.deliveries.clear
  532. post :create, :params => {
  533. :user => {
  534. :firstname => 'Edgar',
  535. :lastname => 'Schmoe',
  536. :login => 'eschmoe',
  537. :password => 'secret123',
  538. :password_confirmation => 'secret123',
  539. :mail => 'eschmoe@example.foo',
  540. :admin => '0'
  541. }
  542. }
  543. assert_nil ActionMailer::Base.deliveries.last
  544. end
  545. def test_edit
  546. with_settings :gravatar_enabled => '1' do
  547. get :edit, :params => {:id => 2}
  548. end
  549. assert_response :success
  550. assert_select 'h2>a+img.gravatar'
  551. assert_select 'input[name=?][value=?]', 'user[login]', 'jsmith'
  552. assert_select 'label[for=?]>span.required', 'user_password', 0
  553. end
  554. def test_edit_registered_user
  555. assert User.find(2).register!
  556. get :edit, :params => {:id => 2}
  557. assert_response :success
  558. assert_select 'a', :text => 'Activate'
  559. end
  560. def test_edit_should_be_denied_for_anonymous
  561. assert User.find(6).anonymous?
  562. get :edit, :params => {:id => 6}
  563. assert_response 404
  564. end
  565. def test_edit_user_with_full_text_formatting_custom_field_should_not_fail
  566. field = UserCustomField.find(4)
  567. field.update_attribute :text_formatting, 'full'
  568. get :edit, :params => {:id => 2}
  569. assert_response :success
  570. end
  571. def test_update
  572. ActionMailer::Base.deliveries.clear
  573. put :update, :params => {
  574. :id => 2,
  575. :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'},
  576. :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
  577. }
  578. user = User.find(2)
  579. assert_equal 'Changed', user.firstname
  580. assert_equal 'only_assigned', user.mail_notification
  581. assert_equal true, user.pref[:hide_mail]
  582. assert_equal 'desc', user.pref[:comments_sorting]
  583. assert ActionMailer::Base.deliveries.empty?
  584. end
  585. def test_update_with_failure
  586. assert_no_difference 'User.count' do
  587. put :update, :params => {
  588. :id => 2,
  589. :user => {:firstname => ''}
  590. }
  591. end
  592. assert_response :success
  593. assert_select_error /First name cannot be blank/
  594. end
  595. def test_update_with_group_ids_should_assign_groups
  596. put :update, :params => {
  597. :id => 2,
  598. :user => {:group_ids => ['10']}
  599. }
  600. user = User.find(2)
  601. assert_equal [10], user.group_ids
  602. end
  603. def test_update_with_activation_should_send_a_notification
  604. u = User.new(:firstname => 'Foo', :lastname => 'Bar',
  605. :mail => 'foo.bar@somenet.foo', :language => 'fr')
  606. u.login = 'foo'
  607. u.status = User::STATUS_REGISTERED
  608. u.save!
  609. ActionMailer::Base.deliveries.clear
  610. put(
  611. :update,
  612. :params => {
  613. :id => u.id,
  614. :user => {:status => User::STATUS_ACTIVE}
  615. }
  616. )
  617. assert u.reload.active?
  618. mail = ActionMailer::Base.deliveries.last
  619. assert_not_nil mail
  620. assert_equal ['foo.bar@somenet.foo'], mail.to
  621. assert_mail_body_match ll('fr', :notice_account_activated), mail
  622. end
  623. def test_update_with_password_change_should_send_a_notification
  624. ActionMailer::Base.deliveries.clear
  625. put(
  626. :update,
  627. :params => {
  628. :id => 2,
  629. :user => {
  630. :password => 'newpass123',
  631. :password_confirmation => 'newpass123'
  632. },
  633. :send_information => '1'
  634. }
  635. )
  636. u = User.find(2)
  637. assert u.check_password?('newpass123')
  638. mail = ActionMailer::Base.deliveries.last
  639. assert_not_nil mail
  640. assert_equal [u.mail], mail.to
  641. assert_mail_body_match 'newpass123', mail
  642. end
  643. def test_update_with_password_change_by_admin_should_send_a_security_notification
  644. ActionMailer::Base.deliveries.clear
  645. user = User.find_by(login: 'jsmith')
  646. put :update, :params => {
  647. :id => user.id,
  648. :user => {:password => 'newpass123', :password_confirmation => 'newpass123'}
  649. }
  650. assert_equal 1, ActionMailer::Base.deliveries.size
  651. mail = ActionMailer::Base.deliveries.last
  652. assert_equal [user.mail], mail.to
  653. assert_match 'Security notification', mail.subject
  654. assert_mail_body_match 'Your password has been changed.', mail
  655. end
  656. def test_update_with_generate_password_should_email_the_password
  657. ActionMailer::Base.deliveries.clear
  658. put(
  659. :update,
  660. :params => {
  661. :id => 2,
  662. :user => {
  663. :generate_password => '1',
  664. :password => '',
  665. :password_confirmation => ''
  666. },
  667. :send_information => '1'
  668. }
  669. )
  670. mail = ActionMailer::Base.deliveries.last
  671. assert_not_nil mail
  672. u = User.find(2)
  673. assert_equal [u.mail], mail.to
  674. m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
  675. assert m
  676. password = m[1]
  677. assert u.check_password?(password)
  678. end
  679. def test_update_without_generate_password_should_not_change_password
  680. put(
  681. :update,
  682. :params => {
  683. :id => 2,
  684. :user => {
  685. :firstname => 'changed',
  686. :generate_password => '0',
  687. :password => '',
  688. :password_confirmation => ''
  689. },
  690. :send_information => '1'
  691. }
  692. )
  693. user = User.find(2)
  694. assert_equal 'changed', user.firstname
  695. assert user.check_password?('jsmith')
  696. end
  697. def test_update_user_switchin_from_auth_source_to_password_authentication
  698. # Configure as auth source
  699. u = User.find(2)
  700. u.auth_source = AuthSource.find(1)
  701. u.save!
  702. put :update, :params => {
  703. :id => u.id,
  704. :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'}
  705. }
  706. assert_nil u.reload.auth_source
  707. assert u.check_password?('newpass123')
  708. end
  709. def test_update_notified_project
  710. get :edit, :params => {:id => 2}
  711. assert_response :success
  712. u = User.find(2)
  713. assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort
  714. assert_equal [1, 2, 5], u.notified_projects_ids.sort
  715. assert_select 'input[name=?][value=?]', 'user[notified_project_ids][]', '1'
  716. assert_equal 'all', u.mail_notification
  717. put :update, :params => {
  718. :id => 2,
  719. :user => {
  720. :mail_notification => 'selected',
  721. :notified_project_ids => [1, 2]
  722. }
  723. }
  724. u = User.find(2)
  725. assert_equal 'selected', u.mail_notification
  726. assert_equal [1, 2], u.notified_projects_ids.sort
  727. end
  728. def test_update_status_should_not_update_attributes
  729. user = User.find(2)
  730. user.pref[:no_self_notified] = '1'
  731. user.pref.save
  732. put :update, :params => {
  733. :id => 2,
  734. :user => {:status => 3}
  735. }
  736. assert_response 302
  737. user = User.find(2)
  738. assert_equal 3, user.status
  739. assert_equal '1', user.pref[:no_self_notified]
  740. end
  741. def test_update_assign_admin_should_send_security_notification
  742. ActionMailer::Base.deliveries.clear
  743. put :update, :params => {
  744. :id => 2,
  745. :user => {:admin => 1}
  746. }
  747. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  748. assert_mail_body_match(
  749. I18n.t(
  750. :mail_body_security_notification_add,
  751. field: I18n.t(:field_admin),
  752. value: User.find(2).login
  753. ),
  754. mail
  755. )
  756. # All admins should receive this
  757. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  758. assert_not_nil(
  759. ActionMailer::Base.deliveries.detect do |mail|
  760. [mail.to].flatten.include?(admin.mail)
  761. end
  762. )
  763. end
  764. end
  765. def test_update_unassign_admin_should_send_security_notification
  766. user = User.find(2)
  767. user.admin = true
  768. user.save!
  769. ActionMailer::Base.deliveries.clear
  770. put :update, :params => {
  771. :id => user.id,
  772. :user => {:admin => 0}
  773. }
  774. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  775. assert_mail_body_match(
  776. I18n.t(
  777. :mail_body_security_notification_remove,
  778. field: I18n.t(:field_admin),
  779. value: user.login
  780. ),
  781. mail
  782. )
  783. # All admins should receive this
  784. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  785. assert_not_nil(
  786. ActionMailer::Base.deliveries.detect do |mail|
  787. [mail.to].flatten.include?(admin.mail)
  788. end
  789. )
  790. end
  791. end
  792. def test_update_lock_admin_should_send_security_notification
  793. user = User.find(2)
  794. user.admin = true
  795. user.save!
  796. ActionMailer::Base.deliveries.clear
  797. put :update, :params => {
  798. :id => 2,
  799. :user => {:status => Principal::STATUS_LOCKED}
  800. }
  801. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  802. assert_mail_body_match(
  803. I18n.t(
  804. :mail_body_security_notification_remove,
  805. field: I18n.t(:field_admin),
  806. value: User.find(2).login
  807. ),
  808. mail
  809. )
  810. # All admins should receive this
  811. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  812. assert_not_nil(
  813. ActionMailer::Base.deliveries.detect do |mail|
  814. [mail.to].flatten.include?(admin.mail)
  815. end
  816. )
  817. end
  818. # if user is already locked, destroying should not send a second mail
  819. # (for active admins see furtherbelow)
  820. ActionMailer::Base.deliveries.clear
  821. delete :destroy, :params => {:id => 1, :confirm => User.find(1).login}
  822. assert_nil ActionMailer::Base.deliveries.last
  823. end
  824. def test_update_unlock_admin_should_send_security_notification
  825. user = User.find(5) # already locked
  826. user.admin = true
  827. user.save!
  828. ActionMailer::Base.deliveries.clear
  829. put :update, :params => {
  830. :id => user.id,
  831. :user => {:status => Principal::STATUS_ACTIVE}
  832. }
  833. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  834. assert_mail_body_match(
  835. I18n.t(
  836. :mail_body_security_notification_add,
  837. field: I18n.t(:field_admin),
  838. value: user.login
  839. ),
  840. mail
  841. )
  842. # All admins should receive this
  843. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  844. assert_not_nil(
  845. ActionMailer::Base.deliveries.detect do |mail|
  846. [mail.to].flatten.include?(admin.mail)
  847. end
  848. )
  849. end
  850. end
  851. def test_update_admin_unrelated_property_should_not_send_security_notification
  852. ActionMailer::Base.deliveries.clear
  853. put :update, :params => {
  854. :id => 1,
  855. :user => {:firstname => 'Jimmy'}
  856. }
  857. assert_nil ActionMailer::Base.deliveries.last
  858. end
  859. def test_update_should_be_denied_for_anonymous
  860. assert User.find(6).anonymous?
  861. put :update, :params => {:id => 6}
  862. assert_response 404
  863. end
  864. def test_update_with_blank_email_should_not_raise_exception
  865. assert_no_difference 'User.count' do
  866. with_settings :gravatar_enabled => '1' do
  867. put :update, :params => {
  868. :id => 2,
  869. :user => {:mail => ''}
  870. }
  871. end
  872. end
  873. assert_response :success
  874. assert_select_error /Email cannot be blank/
  875. end
  876. def test_destroy
  877. assert_difference 'User.count', -1 do
  878. delete :destroy, :params => {:id => 2, :confirm => User.find(2).login}
  879. end
  880. assert_redirected_to '/users'
  881. assert_nil User.find_by_id(2)
  882. end
  883. def test_destroy_with_lock_param_should_lock_instead
  884. assert_no_difference 'User.count' do
  885. delete :destroy, :params => {:id => 2, :lock => 'lock'}
  886. end
  887. assert_redirected_to '/users'
  888. assert User.find_by_id(2).locked?
  889. end
  890. def test_destroy_should_require_confirmation
  891. assert_no_difference 'User.count' do
  892. delete :destroy, :params => {:id => 2}
  893. end
  894. assert_response :success
  895. assert_select '.warning', :text => /Are you sure you want to delete this user/
  896. end
  897. def test_destroy_should_require_correct_confirmation
  898. assert_no_difference 'User.count' do
  899. delete :destroy, :params => {:id => 2, :confirm => 'wrong'}
  900. end
  901. assert_response :success
  902. assert_select '.warning', :text => /Are you sure you want to delete this user/
  903. end
  904. def test_destroy_should_be_denied_for_non_admin_users
  905. @request.session[:user_id] = 3
  906. assert_no_difference 'User.count' do
  907. delete :destroy, :params => {:id => 2, :confirm => User.find(2).login}
  908. end
  909. assert_response 403
  910. end
  911. def test_destroy_should_be_denied_for_anonymous
  912. assert User.find(6).anonymous?
  913. assert_no_difference 'User.count' do
  914. delete :destroy, :params => {:id => 6, :confirm => User.find(6).login}
  915. end
  916. assert_response 404
  917. end
  918. def test_destroy_should_redirect_to_back_url_param
  919. assert_difference 'User.count', -1 do
  920. delete :destroy, :params => {:id => 2,
  921. :confirm => User.find(2).login,
  922. :back_url => '/users?name=foo'}
  923. end
  924. assert_redirected_to '/users?name=foo'
  925. end
  926. def test_destroy_active_admin_should_send_security_notification
  927. user = User.find(2)
  928. user.admin = true
  929. user.save!
  930. ActionMailer::Base.deliveries.clear
  931. delete :destroy, :params => {:id => user.id, :confirm => user.login}
  932. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  933. assert_mail_body_match(
  934. I18n.t(
  935. :mail_body_security_notification_remove,
  936. field: I18n.t(:field_admin),
  937. value: user.login
  938. ),
  939. mail
  940. )
  941. # All admins should receive this
  942. User.where(admin: true, status: Principal::STATUS_ACTIVE).each do |admin|
  943. assert_not_nil(
  944. ActionMailer::Base.deliveries.detect do |mail|
  945. [mail.to].flatten.include?(admin.mail)
  946. end
  947. )
  948. end
  949. end
  950. def test_destroy_without_unsubscribe_is_denied
  951. user = User.find(2)
  952. user.update(admin: true) # Create other admin so self can be deleted
  953. @request.session[:user_id] = user.id
  954. with_settings unsubscribe: 0 do
  955. assert_no_difference 'User.count' do
  956. delete :destroy, params: {id: user.id}
  957. end
  958. assert_response 422
  959. end
  960. end
  961. def test_destroy_last_admin_is_denied
  962. user = User.find(1)
  963. @request.session[:user_id] = user.id
  964. with_settings unsubscribe: 1 do
  965. assert_no_difference 'User.count' do
  966. delete :destroy, params: {id: user.id}
  967. end
  968. assert_response 422
  969. end
  970. end
  971. def test_bulk_destroy
  972. assert_difference 'User.count', -1 do
  973. delete :bulk_destroy, :params => {:ids => [2], :confirm => 'Yes'}
  974. end
  975. assert_redirected_to '/users'
  976. assert_nil User.find_by_id(2)
  977. end
  978. def test_bulk_destroy_should_not_destroy_current_user
  979. assert_difference 'User.count', -1 do
  980. delete :bulk_destroy, :params => {:ids => [2, 1], :confirm => 'Yes'}
  981. end
  982. assert_redirected_to '/users'
  983. assert_nil User.find_by_id(2)
  984. end
  985. def test_bulk_destroy_with_lock_param_should_lock_instead
  986. assert_no_difference 'User.count' do
  987. delete :bulk_destroy, :params => {:ids => [2], :lock => 'lock'}
  988. end
  989. assert_redirected_to '/users'
  990. assert User.find_by_id(2).locked?
  991. end
  992. def test_bulk_destroy_should_require_confirmation
  993. assert_no_difference 'User.count' do
  994. delete :bulk_destroy, :params => {:ids => [2]}
  995. end
  996. assert_response :success
  997. assert_select '.warning', :text => /You are about to delete the following users/
  998. end
  999. def test_bulk_destroy_should_require_correct_confirmation
  1000. assert_no_difference 'User.count' do
  1001. delete :bulk_destroy, :params => {:ids => [2], :confirm => 'wrong'}
  1002. end
  1003. assert_response :success
  1004. assert_select '.warning', :text => /You are about to delete the following users/
  1005. end
  1006. def test_bulk_destroy_should_be_denied_for_non_admin_users
  1007. @request.session[:user_id] = 3
  1008. assert_no_difference 'User.count' do
  1009. delete :bulk_destroy, :params => {:ids => [2], :confirm => 'Yes'}
  1010. end
  1011. assert_response 403
  1012. end
  1013. def test_bulk_destroy_should_be_denied_for_anonymous
  1014. assert User.find(6).anonymous?
  1015. assert_no_difference 'User.count' do
  1016. delete :bulk_destroy, :params => {:ids => [6], :confirm => "Yes"}
  1017. end
  1018. assert_response 404
  1019. end
  1020. end