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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2015 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class UsersControllerTest < ActionController::TestCase
  19. include Redmine::I18n
  20. fixtures :users, :email_addresses, :projects, :members, :member_roles, :roles,
  21. :custom_fields, :custom_values, :groups_users,
  22. :auth_sources,
  23. :enabled_modules,
  24. :issues, :issue_statuses,
  25. :trackers
  26. def setup
  27. User.current = nil
  28. @request.session[:user_id] = 1 # admin
  29. end
  30. def test_index
  31. get :index
  32. assert_response :success
  33. assert_template 'index'
  34. assert_not_nil assigns(:users)
  35. # active users only
  36. assert_nil assigns(:users).detect {|u| !u.active?}
  37. end
  38. def test_index_with_status_filter
  39. get :index, :status => 3
  40. assert_response :success
  41. assert_template 'index'
  42. assert_not_nil assigns(:users)
  43. assert_equal [3], assigns(:users).map(&:status).uniq
  44. end
  45. def test_index_with_name_filter
  46. get :index, :name => 'john'
  47. assert_response :success
  48. assert_template 'index'
  49. users = assigns(:users)
  50. assert_not_nil users
  51. assert_equal 1, users.size
  52. assert_equal 'John', users.first.firstname
  53. end
  54. def test_index_with_group_filter
  55. get :index, :group_id => '10'
  56. assert_response :success
  57. assert_template 'index'
  58. users = assigns(:users)
  59. assert users.any?
  60. assert_equal([], (users - Group.find(10).users))
  61. assert_select 'select[name=group_id]' do
  62. assert_select 'option[value="10"][selected=selected]'
  63. end
  64. end
  65. def test_show
  66. @request.session[:user_id] = nil
  67. get :show, :id => 2
  68. assert_response :success
  69. assert_template 'show'
  70. assert_not_nil assigns(:user)
  71. assert_select 'li', :text => /Phone number/
  72. end
  73. def test_show_should_not_display_hidden_custom_fields
  74. @request.session[:user_id] = nil
  75. UserCustomField.find_by_name('Phone number').update_attribute :visible, false
  76. get :show, :id => 2
  77. assert_response :success
  78. assert_template 'show'
  79. assert_not_nil assigns(:user)
  80. assert_select 'li', :text => /Phone number/, :count => 0
  81. end
  82. def test_show_should_not_fail_when_custom_values_are_nil
  83. user = User.find(2)
  84. # Create a custom field to illustrate the issue
  85. custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
  86. custom_value = user.custom_values.build(:custom_field => custom_field).save!
  87. get :show, :id => 2
  88. assert_response :success
  89. end
  90. def test_show_inactive
  91. @request.session[:user_id] = nil
  92. get :show, :id => 5
  93. assert_response 404
  94. end
  95. def test_show_inactive_by_admin
  96. @request.session[:user_id] = 1
  97. get :show, :id => 5
  98. assert_response 200
  99. assert_not_nil assigns(:user)
  100. end
  101. def test_show_user_who_is_not_visible_should_return_404
  102. Role.anonymous.update! :users_visibility => 'members_of_visible_projects'
  103. user = User.generate!
  104. @request.session[:user_id] = nil
  105. get :show, :id => user.id
  106. assert_response 404
  107. end
  108. def test_show_displays_memberships_based_on_project_visibility
  109. @request.session[:user_id] = 1
  110. get :show, :id => 2
  111. assert_response :success
  112. memberships = assigns(:memberships)
  113. assert_not_nil memberships
  114. project_ids = memberships.map(&:project_id)
  115. assert project_ids.include?(2) #private project admin can see
  116. end
  117. def test_show_current_should_require_authentication
  118. @request.session[:user_id] = nil
  119. get :show, :id => 'current'
  120. assert_response 302
  121. end
  122. def test_show_current
  123. @request.session[:user_id] = 2
  124. get :show, :id => 'current'
  125. assert_response :success
  126. assert_template 'show'
  127. assert_equal User.find(2), assigns(:user)
  128. end
  129. def test_new
  130. get :new
  131. assert_response :success
  132. assert_template :new
  133. assert assigns(:user)
  134. end
  135. def test_create
  136. Setting.bcc_recipients = '1'
  137. assert_difference 'User.count' do
  138. assert_difference 'ActionMailer::Base.deliveries.size' do
  139. post :create,
  140. :user => {
  141. :firstname => 'John',
  142. :lastname => 'Doe',
  143. :login => 'jdoe',
  144. :password => 'secret123',
  145. :password_confirmation => 'secret123',
  146. :mail => 'jdoe@gmail.com',
  147. :mail_notification => 'none'
  148. },
  149. :send_information => '1'
  150. end
  151. end
  152. user = User.order('id DESC').first
  153. assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
  154. assert_equal 'John', user.firstname
  155. assert_equal 'Doe', user.lastname
  156. assert_equal 'jdoe', user.login
  157. assert_equal 'jdoe@gmail.com', user.mail
  158. assert_equal 'none', user.mail_notification
  159. assert user.check_password?('secret123')
  160. mail = ActionMailer::Base.deliveries.last
  161. assert_not_nil mail
  162. assert_equal [user.mail], mail.bcc
  163. assert_mail_body_match 'secret', mail
  164. end
  165. def test_create_with_preferences
  166. assert_difference 'User.count' do
  167. post :create,
  168. :user => {
  169. :firstname => 'John',
  170. :lastname => 'Doe',
  171. :login => 'jdoe',
  172. :password => 'secret123',
  173. :password_confirmation => 'secret123',
  174. :mail => 'jdoe@gmail.com',
  175. :mail_notification => 'none'
  176. },
  177. :pref => {
  178. 'hide_mail' => '1',
  179. 'time_zone' => 'Paris',
  180. 'comments_sorting' => 'desc',
  181. 'warn_on_leaving_unsaved' => '0'
  182. }
  183. end
  184. user = User.order('id DESC').first
  185. assert_equal 'jdoe', user.login
  186. assert_equal true, user.pref.hide_mail
  187. assert_equal 'Paris', user.pref.time_zone
  188. assert_equal 'desc', user.pref[:comments_sorting]
  189. assert_equal '0', user.pref[:warn_on_leaving_unsaved]
  190. end
  191. def test_create_with_generate_password_should_email_the_password
  192. assert_difference 'User.count' do
  193. post :create, :user => {
  194. :login => 'randompass',
  195. :firstname => 'Random',
  196. :lastname => 'Pass',
  197. :mail => 'randompass@example.net',
  198. :language => 'en',
  199. :generate_password => '1',
  200. :password => '',
  201. :password_confirmation => ''
  202. }, :send_information => 1
  203. end
  204. user = User.order('id DESC').first
  205. assert_equal 'randompass', user.login
  206. mail = ActionMailer::Base.deliveries.last
  207. assert_not_nil mail
  208. m = mail_body(mail).match(/Password: ([a-zA-Z0-9]+)/)
  209. assert m
  210. password = m[1]
  211. assert user.check_password?(password)
  212. end
  213. def test_create_and_continue
  214. post :create, :user => {
  215. :login => 'randompass',
  216. :firstname => 'Random',
  217. :lastname => 'Pass',
  218. :mail => 'randompass@example.net',
  219. :generate_password => '1'
  220. }, :continue => '1'
  221. assert_redirected_to '/users/new?user%5Bgenerate_password%5D=1'
  222. end
  223. def test_create_with_failure
  224. assert_no_difference 'User.count' do
  225. post :create, :user => {}
  226. end
  227. assert_response :success
  228. assert_template 'new'
  229. end
  230. def test_create_with_failure_sould_preserve_preference
  231. assert_no_difference 'User.count' do
  232. post :create,
  233. :user => {},
  234. :pref => {
  235. 'no_self_notified' => '1',
  236. 'hide_mail' => '1',
  237. 'time_zone' => 'Paris',
  238. 'comments_sorting' => 'desc',
  239. 'warn_on_leaving_unsaved' => '0'
  240. }
  241. end
  242. assert_response :success
  243. assert_template 'new'
  244. assert_select 'select#pref_time_zone option[selected=selected]', :text => /Paris/
  245. assert_select 'input#pref_no_self_notified[value="1"][checked=checked]'
  246. end
  247. def test_edit
  248. get :edit, :id => 2
  249. assert_response :success
  250. assert_template 'edit'
  251. assert_equal User.find(2), assigns(:user)
  252. end
  253. def test_edit_registered_user
  254. assert User.find(2).register!
  255. get :edit, :id => 2
  256. assert_response :success
  257. assert_select 'a', :text => 'Activate'
  258. end
  259. def test_update
  260. ActionMailer::Base.deliveries.clear
  261. put :update, :id => 2,
  262. :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'},
  263. :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
  264. user = User.find(2)
  265. assert_equal 'Changed', user.firstname
  266. assert_equal 'only_assigned', user.mail_notification
  267. assert_equal true, user.pref[:hide_mail]
  268. assert_equal 'desc', user.pref[:comments_sorting]
  269. assert ActionMailer::Base.deliveries.empty?
  270. end
  271. def test_update_with_failure
  272. assert_no_difference 'User.count' do
  273. put :update, :id => 2, :user => {:firstname => ''}
  274. end
  275. assert_response :success
  276. assert_template 'edit'
  277. end
  278. def test_update_with_group_ids_should_assign_groups
  279. put :update, :id => 2, :user => {:group_ids => ['10']}
  280. user = User.find(2)
  281. assert_equal [10], user.group_ids
  282. end
  283. def test_update_with_activation_should_send_a_notification
  284. u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
  285. u.login = 'foo'
  286. u.status = User::STATUS_REGISTERED
  287. u.save!
  288. ActionMailer::Base.deliveries.clear
  289. Setting.bcc_recipients = '1'
  290. put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
  291. assert u.reload.active?
  292. mail = ActionMailer::Base.deliveries.last
  293. assert_not_nil mail
  294. assert_equal ['foo.bar@somenet.foo'], mail.bcc
  295. assert_mail_body_match ll('fr', :notice_account_activated), mail
  296. end
  297. def test_update_with_password_change_should_send_a_notification
  298. ActionMailer::Base.deliveries.clear
  299. Setting.bcc_recipients = '1'
  300. put :update, :id => 2, :user => {:password => 'newpass123', :password_confirmation => 'newpass123'}, :send_information => '1'
  301. u = User.find(2)
  302. assert u.check_password?('newpass123')
  303. mail = ActionMailer::Base.deliveries.last
  304. assert_not_nil mail
  305. assert_equal [u.mail], mail.bcc
  306. assert_mail_body_match 'newpass123', mail
  307. end
  308. def test_update_with_generate_password_should_email_the_password
  309. ActionMailer::Base.deliveries.clear
  310. Setting.bcc_recipients = '1'
  311. put :update, :id => 2, :user => {
  312. :generate_password => '1',
  313. :password => '',
  314. :password_confirmation => ''
  315. }, :send_information => '1'
  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.find(2).check_password?(password)
  322. end
  323. def test_update_without_generate_password_should_not_change_password
  324. put :update, :id => 2, :user => {
  325. :firstname => 'changed',
  326. :generate_password => '0',
  327. :password => '',
  328. :password_confirmation => ''
  329. }, :send_information => '1'
  330. user = User.find(2)
  331. assert_equal 'changed', user.firstname
  332. assert user.check_password?('jsmith')
  333. end
  334. def test_update_user_switchin_from_auth_source_to_password_authentication
  335. # Configure as auth source
  336. u = User.find(2)
  337. u.auth_source = AuthSource.find(1)
  338. u.save!
  339. put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'}
  340. assert_equal nil, u.reload.auth_source
  341. assert u.check_password?('newpass123')
  342. end
  343. def test_update_notified_project
  344. get :edit, :id => 2
  345. assert_response :success
  346. assert_template 'edit'
  347. u = User.find(2)
  348. assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort
  349. assert_equal [1, 2, 5], u.notified_projects_ids.sort
  350. assert_select 'input[name=?][value=?]', 'user[notified_project_ids][]', '1'
  351. assert_equal 'all', u.mail_notification
  352. put :update, :id => 2,
  353. :user => {
  354. :mail_notification => 'selected',
  355. :notified_project_ids => [1, 2]
  356. }
  357. u = User.find(2)
  358. assert_equal 'selected', u.mail_notification
  359. assert_equal [1, 2], u.notified_projects_ids.sort
  360. end
  361. def test_update_status_should_not_update_attributes
  362. user = User.find(2)
  363. user.pref[:no_self_notified] = '1'
  364. user.pref.save
  365. put :update, :id => 2, :user => {:status => 3}
  366. assert_response 302
  367. user = User.find(2)
  368. assert_equal 3, user.status
  369. assert_equal '1', user.pref[:no_self_notified]
  370. end
  371. def test_destroy
  372. assert_difference 'User.count', -1 do
  373. delete :destroy, :id => 2
  374. end
  375. assert_redirected_to '/users'
  376. assert_nil User.find_by_id(2)
  377. end
  378. def test_destroy_should_be_denied_for_non_admin_users
  379. @request.session[:user_id] = 3
  380. assert_no_difference 'User.count' do
  381. get :destroy, :id => 2
  382. end
  383. assert_response 403
  384. end
  385. def test_destroy_should_redirect_to_back_url_param
  386. assert_difference 'User.count', -1 do
  387. delete :destroy, :id => 2, :back_url => '/users?name=foo'
  388. end
  389. assert_redirected_to '/users?name=foo'
  390. end
  391. end