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