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