Mercurial > hg > soundsoftware-site
comparison test/functional/users_controller_test.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | 622f24f53b42 261b3d9a4903 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | 2 # Copyright (C) 2006-2012 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
22 class UsersController; def rescue_action(e) raise e end; end | 22 class UsersController; def rescue_action(e) raise e end; end |
23 | 23 |
24 class UsersControllerTest < ActionController::TestCase | 24 class UsersControllerTest < ActionController::TestCase |
25 include Redmine::I18n | 25 include Redmine::I18n |
26 | 26 |
27 fixtures :users, :projects, :members, :member_roles, :roles, :auth_sources, :custom_fields, :custom_values, :groups_users | 27 fixtures :users, :projects, :members, :member_roles, :roles, |
28 :custom_fields, :custom_values, :groups_users, | |
29 :auth_sources | |
28 | 30 |
29 def setup | 31 def setup |
30 @controller = UsersController.new | 32 @controller = UsersController.new |
31 @request = ActionController::TestRequest.new | 33 @request = ActionController::TestRequest.new |
32 @response = ActionController::TestResponse.new | 34 @response = ActionController::TestResponse.new |
47 assert_not_nil assigns(:users) | 49 assert_not_nil assigns(:users) |
48 # active users only | 50 # active users only |
49 assert_nil assigns(:users).detect {|u| !u.active?} | 51 assert_nil assigns(:users).detect {|u| !u.active?} |
50 end | 52 end |
51 | 53 |
54 def test_index_with_status_filter | |
55 get :index, :status => 3 | |
56 assert_response :success | |
57 assert_template 'index' | |
58 assert_not_nil assigns(:users) | |
59 assert_equal [3], assigns(:users).map(&:status).uniq | |
60 end | |
61 | |
52 def test_index_with_name_filter | 62 def test_index_with_name_filter |
53 get :index, :name => 'john' | 63 get :index, :name => 'john' |
54 assert_response :success | 64 assert_response :success |
55 assert_template 'index' | 65 assert_template 'index' |
56 users = assigns(:users) | 66 users = assigns(:users) |
64 assert_response :success | 74 assert_response :success |
65 assert_template 'index' | 75 assert_template 'index' |
66 users = assigns(:users) | 76 users = assigns(:users) |
67 assert users.any? | 77 assert users.any? |
68 assert_equal([], (users - Group.find(10).users)) | 78 assert_equal([], (users - Group.find(10).users)) |
79 assert_select 'select[name=group_id]' do | |
80 assert_select 'option[value=10][selected=selected]' | |
81 end | |
69 end | 82 end |
70 | 83 |
71 def test_show | 84 def test_show |
72 @request.session[:user_id] = nil | 85 @request.session[:user_id] = nil |
73 get :show, :id => 2 | 86 get :show, :id => 2 |
143 assert_equal User.find(2), assigns(:user) | 156 assert_equal User.find(2), assigns(:user) |
144 end | 157 end |
145 | 158 |
146 def test_new | 159 def test_new |
147 get :new | 160 get :new |
148 | |
149 assert_response :success | 161 assert_response :success |
150 assert_template :new | 162 assert_template :new |
151 assert assigns(:user) | 163 assert assigns(:user) |
152 end | 164 end |
153 | 165 |
159 post :create, | 171 post :create, |
160 :user => { | 172 :user => { |
161 :firstname => 'John', | 173 :firstname => 'John', |
162 :lastname => 'Doe', | 174 :lastname => 'Doe', |
163 :login => 'jdoe', | 175 :login => 'jdoe', |
164 :password => 'secret', | 176 :password => 'secret123', |
165 :password_confirmation => 'secret', | 177 :password_confirmation => 'secret123', |
166 :mail => 'jdoe@gmail.com', | 178 :mail => 'jdoe@gmail.com', |
167 :mail_notification => 'none' | 179 :mail_notification => 'none' |
168 }, | 180 }, |
169 :send_information => '1' | 181 :send_information => '1' |
170 end | 182 end |
176 assert_equal 'John', user.firstname | 188 assert_equal 'John', user.firstname |
177 assert_equal 'Doe', user.lastname | 189 assert_equal 'Doe', user.lastname |
178 assert_equal 'jdoe', user.login | 190 assert_equal 'jdoe', user.login |
179 assert_equal 'jdoe@gmail.com', user.mail | 191 assert_equal 'jdoe@gmail.com', user.mail |
180 assert_equal 'none', user.mail_notification | 192 assert_equal 'none', user.mail_notification |
181 assert user.check_password?('secret') | 193 assert user.check_password?('secret123') |
182 | 194 |
183 mail = ActionMailer::Base.deliveries.last | 195 mail = ActionMailer::Base.deliveries.last |
184 assert_not_nil mail | 196 assert_not_nil mail |
185 assert_equal [user.mail], mail.bcc | 197 assert_equal [user.mail], mail.bcc |
186 assert mail.body.include?('secret') | 198 assert_mail_body_match 'secret', mail |
199 end | |
200 | |
201 def test_create_with_preferences | |
202 assert_difference 'User.count' do | |
203 post :create, | |
204 :user => { | |
205 :firstname => 'John', | |
206 :lastname => 'Doe', | |
207 :login => 'jdoe', | |
208 :password => 'secret123', | |
209 :password_confirmation => 'secret123', | |
210 :mail => 'jdoe@gmail.com', | |
211 :mail_notification => 'none' | |
212 }, | |
213 :pref => { | |
214 'hide_mail' => '1', | |
215 'time_zone' => 'Paris', | |
216 'comments_sorting' => 'desc', | |
217 'warn_on_leaving_unsaved' => '0' | |
218 } | |
219 end | |
220 user = User.first(:order => 'id DESC') | |
221 assert_equal 'jdoe', user.login | |
222 assert_equal true, user.pref.hide_mail | |
223 assert_equal 'Paris', user.pref.time_zone | |
224 assert_equal 'desc', user.pref[:comments_sorting] | |
225 assert_equal '0', user.pref[:warn_on_leaving_unsaved] | |
187 end | 226 end |
188 | 227 |
189 def test_create_with_failure | 228 def test_create_with_failure |
190 assert_no_difference 'User.count' do | 229 assert_no_difference 'User.count' do |
191 post :create, :user => {} | 230 post :create, :user => {} |
192 end | 231 end |
193 | |
194 assert_response :success | 232 assert_response :success |
195 assert_template 'new' | 233 assert_template 'new' |
196 end | 234 end |
197 | 235 |
198 def test_edit | 236 def test_edit |
199 get :edit, :id => 2 | 237 get :edit, :id => 2 |
200 | |
201 assert_response :success | 238 assert_response :success |
202 assert_template 'edit' | 239 assert_template 'edit' |
203 assert_equal User.find(2), assigns(:user) | 240 assert_equal User.find(2), assigns(:user) |
204 end | 241 end |
205 | 242 |
206 def test_update | 243 def test_update |
207 ActionMailer::Base.deliveries.clear | 244 ActionMailer::Base.deliveries.clear |
208 put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'} | 245 put :update, :id => 2, |
209 | 246 :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, |
247 :pref => {:hide_mail => '1', :comments_sorting => 'desc'} | |
210 user = User.find(2) | 248 user = User.find(2) |
211 assert_equal 'Changed', user.firstname | 249 assert_equal 'Changed', user.firstname |
212 assert_equal 'only_assigned', user.mail_notification | 250 assert_equal 'only_assigned', user.mail_notification |
213 assert_equal true, user.pref[:hide_mail] | 251 assert_equal true, user.pref[:hide_mail] |
214 assert_equal 'desc', user.pref[:comments_sorting] | 252 assert_equal 'desc', user.pref[:comments_sorting] |
217 | 255 |
218 def test_update_with_failure | 256 def test_update_with_failure |
219 assert_no_difference 'User.count' do | 257 assert_no_difference 'User.count' do |
220 put :update, :id => 2, :user => {:firstname => ''} | 258 put :update, :id => 2, :user => {:firstname => ''} |
221 end | 259 end |
222 | |
223 assert_response :success | 260 assert_response :success |
224 assert_template 'edit' | 261 assert_template 'edit' |
225 end | 262 end |
226 | 263 |
227 def test_update_with_group_ids_should_assign_groups | 264 def test_update_with_group_ids_should_assign_groups |
228 put :update, :id => 2, :user => {:group_ids => ['10']} | 265 put :update, :id => 2, :user => {:group_ids => ['10']} |
229 | |
230 user = User.find(2) | 266 user = User.find(2) |
231 assert_equal [10], user.group_ids | 267 assert_equal [10], user.group_ids |
232 end | 268 end |
233 | 269 |
234 def test_update_with_activation_should_send_a_notification | 270 def test_update_with_activation_should_send_a_notification |
242 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE} | 278 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE} |
243 assert u.reload.active? | 279 assert u.reload.active? |
244 mail = ActionMailer::Base.deliveries.last | 280 mail = ActionMailer::Base.deliveries.last |
245 assert_not_nil mail | 281 assert_not_nil mail |
246 assert_equal ['foo.bar@somenet.foo'], mail.bcc | 282 assert_equal ['foo.bar@somenet.foo'], mail.bcc |
247 assert mail.body.include?(ll('fr', :notice_account_activated)) | 283 assert_mail_body_match ll('fr', :notice_account_activated), mail |
248 end | 284 end |
249 | 285 |
250 def test_update_with_password_change_should_send_a_notification | 286 def test_update_with_password_change_should_send_a_notification |
251 ActionMailer::Base.deliveries.clear | 287 ActionMailer::Base.deliveries.clear |
252 Setting.bcc_recipients = '1' | 288 Setting.bcc_recipients = '1' |
253 | 289 |
254 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1' | 290 put :update, :id => 2, :user => {:password => 'newpass123', :password_confirmation => 'newpass123'}, :send_information => '1' |
255 u = User.find(2) | 291 u = User.find(2) |
256 assert u.check_password?('newpass') | 292 assert u.check_password?('newpass123') |
257 | 293 |
258 mail = ActionMailer::Base.deliveries.last | 294 mail = ActionMailer::Base.deliveries.last |
259 assert_not_nil mail | 295 assert_not_nil mail |
260 assert_equal [u.mail], mail.bcc | 296 assert_equal [u.mail], mail.bcc |
261 assert mail.body.include?('newpass') | 297 assert_mail_body_match 'newpass123', mail |
262 end | 298 end |
263 | 299 |
264 test "put :update with a password change to an AuthSource user switching to Internal authentication" do | 300 def test_update_user_switchin_from_auth_source_to_password_authentication |
265 # Configure as auth source | 301 # Configure as auth source |
266 u = User.find(2) | 302 u = User.find(2) |
267 u.auth_source = AuthSource.find(1) | 303 u.auth_source = AuthSource.find(1) |
268 u.save! | 304 u.save! |
269 | 305 |
270 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass' | 306 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass123', :password_confirmation => 'newpass123'} |
271 | 307 |
272 assert_equal nil, u.reload.auth_source | 308 assert_equal nil, u.reload.auth_source |
273 assert u.check_password?('newpass') | 309 assert u.check_password?('newpass123') |
310 end | |
311 | |
312 def test_update_notified_project | |
313 get :edit, :id => 2 | |
314 assert_response :success | |
315 assert_template 'edit' | |
316 u = User.find(2) | |
317 assert_equal [1, 2, 5], u.projects.collect{|p| p.id}.sort | |
318 assert_equal [1, 2, 5], u.notified_projects_ids.sort | |
319 assert_tag :tag => 'input', | |
320 :attributes => { | |
321 :id => 'notified_project_ids_', | |
322 :value => 1, | |
323 } | |
324 assert_equal 'all', u.mail_notification | |
325 put :update, :id => 2, | |
326 :user => { | |
327 :mail_notification => 'selected', | |
328 }, | |
329 :notified_project_ids => [1, 2] | |
330 u = User.find(2) | |
331 assert_equal 'selected', u.mail_notification | |
332 assert_equal [1, 2], u.notified_projects_ids.sort | |
274 end | 333 end |
275 | 334 |
276 def test_destroy | 335 def test_destroy |
277 assert_difference 'User.count', -1 do | 336 assert_difference 'User.count', -1 do |
278 delete :destroy, :id => 2 | 337 delete :destroy, :id => 2 |
279 end | 338 end |
280 assert_redirected_to '/users' | 339 assert_redirected_to '/users' |
281 assert_nil User.find_by_id(2) | 340 assert_nil User.find_by_id(2) |
282 end | 341 end |
283 | 342 |
284 def test_destroy_should_not_accept_get_requests | 343 def test_destroy_should_be_denied_for_non_admin_users |
344 @request.session[:user_id] = 3 | |
345 | |
285 assert_no_difference 'User.count' do | 346 assert_no_difference 'User.count' do |
286 get :destroy, :id => 2 | 347 get :destroy, :id => 2 |
287 end | 348 end |
288 assert_response 405 | |
289 end | |
290 | |
291 def test_destroy_should_be_denied_for_non_admin_users | |
292 @request.session[:user_id] = 3 | |
293 | |
294 assert_no_difference 'User.count' do | |
295 get :destroy, :id => 2 | |
296 end | |
297 assert_response 403 | 349 assert_response 403 |
298 end | 350 end |
299 | 351 |
300 def test_edit_membership | 352 def test_destroy_should_redirect_to_back_url_param |
301 post :edit_membership, :id => 2, :membership_id => 1, | 353 assert_difference 'User.count', -1 do |
302 :membership => { :role_ids => [2]} | 354 delete :destroy, :id => 2, :back_url => '/users?name=foo' |
303 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' | 355 end |
356 assert_redirected_to '/users?name=foo' | |
357 end | |
358 | |
359 def test_create_membership | |
360 assert_difference 'Member.count' do | |
361 post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]} | |
362 end | |
363 assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships' | |
364 member = Member.first(:order => 'id DESC') | |
365 assert_equal User.find(7), member.principal | |
366 assert_equal [2], member.role_ids | |
367 assert_equal 3, member.project_id | |
368 end | |
369 | |
370 def test_create_membership_js_format | |
371 assert_difference 'Member.count' do | |
372 post :edit_membership, :id => 7, :membership => {:project_id => 3, :role_ids => [2]}, :format => 'js' | |
373 assert_response :success | |
374 assert_template 'edit_membership' | |
375 assert_equal 'text/javascript', response.content_type | |
376 end | |
377 member = Member.first(:order => 'id DESC') | |
378 assert_equal User.find(7), member.principal | |
379 assert_equal [2], member.role_ids | |
380 assert_equal 3, member.project_id | |
381 assert_include 'tab-content-memberships', response.body | |
382 end | |
383 | |
384 def test_create_membership_js_format_with_failure | |
385 assert_no_difference 'Member.count' do | |
386 post :edit_membership, :id => 7, :membership => {:project_id => 3}, :format => 'js' | |
387 assert_response :success | |
388 assert_template 'edit_membership' | |
389 assert_equal 'text/javascript', response.content_type | |
390 end | |
391 assert_include 'alert', response.body, "Alert message not sent" | |
392 assert_include 'Role can\\\'t be empty', response.body, "Error message not sent" | |
393 end | |
394 | |
395 def test_update_membership | |
396 assert_no_difference 'Member.count' do | |
397 put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]} | |
398 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' | |
399 end | |
304 assert_equal [2], Member.find(1).role_ids | 400 assert_equal [2], Member.find(1).role_ids |
305 end | 401 end |
306 | 402 |
403 def test_update_membership_js_format | |
404 assert_no_difference 'Member.count' do | |
405 put :edit_membership, :id => 2, :membership_id => 1, :membership => {:role_ids => [2]}, :format => 'js' | |
406 assert_response :success | |
407 assert_template 'edit_membership' | |
408 assert_equal 'text/javascript', response.content_type | |
409 end | |
410 assert_equal [2], Member.find(1).role_ids | |
411 assert_include 'tab-content-memberships', response.body | |
412 end | |
413 | |
307 def test_destroy_membership | 414 def test_destroy_membership |
308 post :destroy_membership, :id => 2, :membership_id => 1 | 415 assert_difference 'Member.count', -1 do |
416 delete :destroy_membership, :id => 2, :membership_id => 1 | |
417 end | |
309 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' | 418 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships' |
310 assert_nil Member.find_by_id(1) | 419 assert_nil Member.find_by_id(1) |
311 end | 420 end |
421 | |
422 def test_destroy_membership_js_format | |
423 assert_difference 'Member.count', -1 do | |
424 delete :destroy_membership, :id => 2, :membership_id => 1, :format => 'js' | |
425 assert_response :success | |
426 assert_template 'destroy_membership' | |
427 assert_equal 'text/javascript', response.content_type | |
428 end | |
429 assert_nil Member.find_by_id(1) | |
430 assert_include 'tab-content-memberships', response.body | |
431 end | |
312 end | 432 end |