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

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