##// END OF EJS Templates
Additional tests for UsersController....
Jean-Philippe Lang -
r9111:81185a8d93c0
parent child
Show More
@@ -1,354 +1,391
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'users_controller'
19 require 'users_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
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, :custom_fields, :custom_values, :groups_users, :auth_sources
27 fixtures :users, :projects, :members, :member_roles, :roles, :custom_fields, :custom_values, :groups_users, :auth_sources
28
28
29 def setup
29 def setup
30 @controller = UsersController.new
30 @controller = UsersController.new
31 @request = ActionController::TestRequest.new
31 @request = ActionController::TestRequest.new
32 @response = ActionController::TestResponse.new
32 @response = ActionController::TestResponse.new
33 User.current = nil
33 User.current = nil
34 @request.session[:user_id] = 1 # admin
34 @request.session[:user_id] = 1 # admin
35 end
35 end
36
36
37 def test_index
37 def test_index
38 get :index
38 get :index
39 assert_response :success
39 assert_response :success
40 assert_template 'index'
40 assert_template 'index'
41 end
41 end
42
42
43 def test_index
43 def test_index
44 get :index
44 get :index
45 assert_response :success
45 assert_response :success
46 assert_template 'index'
46 assert_template 'index'
47 assert_not_nil assigns(:users)
47 assert_not_nil assigns(:users)
48 # active users only
48 # active users only
49 assert_nil assigns(:users).detect {|u| !u.active?}
49 assert_nil assigns(:users).detect {|u| !u.active?}
50 end
50 end
51
51
52 def test_index_with_status_filter
52 def test_index_with_status_filter
53 get :index, :status => 3
53 get :index, :status => 3
54 assert_response :success
54 assert_response :success
55 assert_template 'index'
55 assert_template 'index'
56 assert_not_nil assigns(:users)
56 assert_not_nil assigns(:users)
57 assert_equal [3], assigns(:users).map(&:status).uniq
57 assert_equal [3], assigns(:users).map(&:status).uniq
58 end
58 end
59
59
60 def test_index_with_name_filter
60 def test_index_with_name_filter
61 get :index, :name => 'john'
61 get :index, :name => 'john'
62 assert_response :success
62 assert_response :success
63 assert_template 'index'
63 assert_template 'index'
64 users = assigns(:users)
64 users = assigns(:users)
65 assert_not_nil users
65 assert_not_nil users
66 assert_equal 1, users.size
66 assert_equal 1, users.size
67 assert_equal 'John', users.first.firstname
67 assert_equal 'John', users.first.firstname
68 end
68 end
69
69
70 def test_index_with_group_filter
70 def test_index_with_group_filter
71 get :index, :group_id => '10'
71 get :index, :group_id => '10'
72 assert_response :success
72 assert_response :success
73 assert_template 'index'
73 assert_template 'index'
74 users = assigns(:users)
74 users = assigns(:users)
75 assert users.any?
75 assert users.any?
76 assert_equal([], (users - Group.find(10).users))
76 assert_equal([], (users - Group.find(10).users))
77 end
77 end
78
78
79 def test_show
79 def test_show
80 @request.session[:user_id] = nil
80 @request.session[:user_id] = nil
81 get :show, :id => 2
81 get :show, :id => 2
82 assert_response :success
82 assert_response :success
83 assert_template 'show'
83 assert_template 'show'
84 assert_not_nil assigns(:user)
84 assert_not_nil assigns(:user)
85
85
86 assert_tag 'li', :content => /Phone number/
86 assert_tag 'li', :content => /Phone number/
87 end
87 end
88
88
89 def test_show_should_not_display_hidden_custom_fields
89 def test_show_should_not_display_hidden_custom_fields
90 @request.session[:user_id] = nil
90 @request.session[:user_id] = nil
91 UserCustomField.find_by_name('Phone number').update_attribute :visible, false
91 UserCustomField.find_by_name('Phone number').update_attribute :visible, false
92 get :show, :id => 2
92 get :show, :id => 2
93 assert_response :success
93 assert_response :success
94 assert_template 'show'
94 assert_template 'show'
95 assert_not_nil assigns(:user)
95 assert_not_nil assigns(:user)
96
96
97 assert_no_tag 'li', :content => /Phone number/
97 assert_no_tag 'li', :content => /Phone number/
98 end
98 end
99
99
100 def test_show_should_not_fail_when_custom_values_are_nil
100 def test_show_should_not_fail_when_custom_values_are_nil
101 user = User.find(2)
101 user = User.find(2)
102
102
103 # Create a custom field to illustrate the issue
103 # Create a custom field to illustrate the issue
104 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
104 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
105 custom_value = user.custom_values.build(:custom_field => custom_field).save!
105 custom_value = user.custom_values.build(:custom_field => custom_field).save!
106
106
107 get :show, :id => 2
107 get :show, :id => 2
108 assert_response :success
108 assert_response :success
109 end
109 end
110
110
111 def test_show_inactive
111 def test_show_inactive
112 @request.session[:user_id] = nil
112 @request.session[:user_id] = nil
113 get :show, :id => 5
113 get :show, :id => 5
114 assert_response 404
114 assert_response 404
115 end
115 end
116
116
117 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
117 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
118 @request.session[:user_id] = nil
118 @request.session[:user_id] = nil
119 get :show, :id => 9
119 get :show, :id => 9
120 assert_response 404
120 assert_response 404
121 end
121 end
122
122
123 def test_show_inactive_by_admin
123 def test_show_inactive_by_admin
124 @request.session[:user_id] = 1
124 @request.session[:user_id] = 1
125 get :show, :id => 5
125 get :show, :id => 5
126 assert_response 200
126 assert_response 200
127 assert_not_nil assigns(:user)
127 assert_not_nil assigns(:user)
128 end
128 end
129
129
130 def test_show_displays_memberships_based_on_project_visibility
130 def test_show_displays_memberships_based_on_project_visibility
131 @request.session[:user_id] = 1
131 @request.session[:user_id] = 1
132 get :show, :id => 2
132 get :show, :id => 2
133 assert_response :success
133 assert_response :success
134 memberships = assigns(:memberships)
134 memberships = assigns(:memberships)
135 assert_not_nil memberships
135 assert_not_nil memberships
136 project_ids = memberships.map(&:project_id)
136 project_ids = memberships.map(&:project_id)
137 assert project_ids.include?(2) #private project admin can see
137 assert project_ids.include?(2) #private project admin can see
138 end
138 end
139
139
140 def test_show_current_should_require_authentication
140 def test_show_current_should_require_authentication
141 @request.session[:user_id] = nil
141 @request.session[:user_id] = nil
142 get :show, :id => 'current'
142 get :show, :id => 'current'
143 assert_response 302
143 assert_response 302
144 end
144 end
145
145
146 def test_show_current
146 def test_show_current
147 @request.session[:user_id] = 2
147 @request.session[:user_id] = 2
148 get :show, :id => 'current'
148 get :show, :id => 'current'
149 assert_response :success
149 assert_response :success
150 assert_template 'show'
150 assert_template 'show'
151 assert_equal User.find(2), assigns(:user)
151 assert_equal User.find(2), assigns(:user)
152 end
152 end
153
153
154 def test_new
154 def test_new
155 get :new
155 get :new
156
156
157 assert_response :success
157 assert_response :success
158 assert_template :new
158 assert_template :new
159 assert assigns(:user)
159 assert assigns(:user)
160 end
160 end
161
161
162 def test_create
162 def test_create
163 Setting.bcc_recipients = '1'
163 Setting.bcc_recipients = '1'
164
164
165 assert_difference 'User.count' do
165 assert_difference 'User.count' do
166 assert_difference 'ActionMailer::Base.deliveries.size' do
166 assert_difference 'ActionMailer::Base.deliveries.size' do
167 post :create,
167 post :create,
168 :user => {
168 :user => {
169 :firstname => 'John',
169 :firstname => 'John',
170 :lastname => 'Doe',
170 :lastname => 'Doe',
171 :login => 'jdoe',
171 :login => 'jdoe',
172 :password => 'secret',
172 :password => 'secret',
173 :password_confirmation => 'secret',
173 :password_confirmation => 'secret',
174 :mail => 'jdoe@gmail.com',
174 :mail => 'jdoe@gmail.com',
175 :mail_notification => 'none'
175 :mail_notification => 'none'
176 },
176 },
177 :send_information => '1'
177 :send_information => '1'
178 end
178 end
179 end
179 end
180
180
181 user = User.first(:order => 'id DESC')
181 user = User.first(:order => 'id DESC')
182 assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
182 assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
183
183
184 assert_equal 'John', user.firstname
184 assert_equal 'John', user.firstname
185 assert_equal 'Doe', user.lastname
185 assert_equal 'Doe', user.lastname
186 assert_equal 'jdoe', user.login
186 assert_equal 'jdoe', user.login
187 assert_equal 'jdoe@gmail.com', user.mail
187 assert_equal 'jdoe@gmail.com', user.mail
188 assert_equal 'none', user.mail_notification
188 assert_equal 'none', user.mail_notification
189 assert user.check_password?('secret')
189 assert user.check_password?('secret')
190
190
191 mail = ActionMailer::Base.deliveries.last
191 mail = ActionMailer::Base.deliveries.last
192 assert_not_nil mail
192 assert_not_nil mail
193 assert_equal [user.mail], mail.bcc
193 assert_equal [user.mail], mail.bcc
194 assert_mail_body_match 'secret', mail
194 assert_mail_body_match 'secret', mail
195 end
195 end
196
196
197 def test_create_with_preferences
197 def test_create_with_preferences
198 assert_difference 'User.count' do
198 assert_difference 'User.count' do
199 post :create,
199 post :create,
200 :user => {
200 :user => {
201 :firstname => 'John',
201 :firstname => 'John',
202 :lastname => 'Doe',
202 :lastname => 'Doe',
203 :login => 'jdoe',
203 :login => 'jdoe',
204 :password => 'secret',
204 :password => 'secret',
205 :password_confirmation => 'secret',
205 :password_confirmation => 'secret',
206 :mail => 'jdoe@gmail.com',
206 :mail => 'jdoe@gmail.com',
207 :mail_notification => 'none'
207 :mail_notification => 'none'
208 },
208 },
209 :pref => {
209 :pref => {
210 'hide_mail' => '1',
210 'hide_mail' => '1',
211 'time_zone' => 'Paris',
211 'time_zone' => 'Paris',
212 'comments_sorting' => 'desc',
212 'comments_sorting' => 'desc',
213 'warn_on_leaving_unsaved' => '0'
213 'warn_on_leaving_unsaved' => '0'
214 }
214 }
215 end
215 end
216 user = User.first(:order => 'id DESC')
216 user = User.first(:order => 'id DESC')
217 assert_equal 'jdoe', user.login
217 assert_equal 'jdoe', user.login
218 assert_equal true, user.pref.hide_mail
218 assert_equal true, user.pref.hide_mail
219 assert_equal 'Paris', user.pref.time_zone
219 assert_equal 'Paris', user.pref.time_zone
220 assert_equal 'desc', user.pref[:comments_sorting]
220 assert_equal 'desc', user.pref[:comments_sorting]
221 assert_equal '0', user.pref[:warn_on_leaving_unsaved]
221 assert_equal '0', user.pref[:warn_on_leaving_unsaved]
222 end
222 end
223
223
224 def test_create_with_failure
224 def test_create_with_failure
225 assert_no_difference 'User.count' do
225 assert_no_difference 'User.count' do
226 post :create, :user => {}
226 post :create, :user => {}
227 end
227 end
228
228
229 assert_response :success
229 assert_response :success
230 assert_template 'new'
230 assert_template 'new'
231 end
231 end
232
232
233 def test_edit
233 def test_edit
234 get :edit, :id => 2
234 get :edit, :id => 2
235
235
236 assert_response :success
236 assert_response :success
237 assert_template 'edit'
237 assert_template 'edit'
238 assert_equal User.find(2), assigns(:user)
238 assert_equal User.find(2), assigns(:user)
239 end
239 end
240
240
241 def test_update
241 def test_update
242 ActionMailer::Base.deliveries.clear
242 ActionMailer::Base.deliveries.clear
243 put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
243 put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
244
244
245 user = User.find(2)
245 user = User.find(2)
246 assert_equal 'Changed', user.firstname
246 assert_equal 'Changed', user.firstname
247 assert_equal 'only_assigned', user.mail_notification
247 assert_equal 'only_assigned', user.mail_notification
248 assert_equal true, user.pref[:hide_mail]
248 assert_equal true, user.pref[:hide_mail]
249 assert_equal 'desc', user.pref[:comments_sorting]
249 assert_equal 'desc', user.pref[:comments_sorting]
250 assert ActionMailer::Base.deliveries.empty?
250 assert ActionMailer::Base.deliveries.empty?
251 end
251 end
252
252
253 def test_update_with_failure
253 def test_update_with_failure
254 assert_no_difference 'User.count' do
254 assert_no_difference 'User.count' do
255 put :update, :id => 2, :user => {:firstname => ''}
255 put :update, :id => 2, :user => {:firstname => ''}
256 end
256 end
257
257
258 assert_response :success
258 assert_response :success
259 assert_template 'edit'
259 assert_template 'edit'
260 end
260 end
261
261
262 def test_update_with_group_ids_should_assign_groups
262 def test_update_with_group_ids_should_assign_groups
263 put :update, :id => 2, :user => {:group_ids => ['10']}
263 put :update, :id => 2, :user => {:group_ids => ['10']}
264
264
265 user = User.find(2)
265 user = User.find(2)
266 assert_equal [10], user.group_ids
266 assert_equal [10], user.group_ids
267 end
267 end
268
268
269 def test_update_with_activation_should_send_a_notification
269 def test_update_with_activation_should_send_a_notification
270 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
270 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
271 u.login = 'foo'
271 u.login = 'foo'
272 u.status = User::STATUS_REGISTERED
272 u.status = User::STATUS_REGISTERED
273 u.save!
273 u.save!
274 ActionMailer::Base.deliveries.clear
274 ActionMailer::Base.deliveries.clear
275 Setting.bcc_recipients = '1'
275 Setting.bcc_recipients = '1'
276
276
277 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
277 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
278 assert u.reload.active?
278 assert u.reload.active?
279 mail = ActionMailer::Base.deliveries.last
279 mail = ActionMailer::Base.deliveries.last
280 assert_not_nil mail
280 assert_not_nil mail
281 assert_equal ['foo.bar@somenet.foo'], mail.bcc
281 assert_equal ['foo.bar@somenet.foo'], mail.bcc
282 assert_mail_body_match ll('fr', :notice_account_activated), mail
282 assert_mail_body_match ll('fr', :notice_account_activated), mail
283 end
283 end
284
284
285 def test_update_with_password_change_should_send_a_notification
285 def test_update_with_password_change_should_send_a_notification
286 ActionMailer::Base.deliveries.clear
286 ActionMailer::Base.deliveries.clear
287 Setting.bcc_recipients = '1'
287 Setting.bcc_recipients = '1'
288
288
289 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
289 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
290 u = User.find(2)
290 u = User.find(2)
291 assert u.check_password?('newpass')
291 assert u.check_password?('newpass')
292
292
293 mail = ActionMailer::Base.deliveries.last
293 mail = ActionMailer::Base.deliveries.last
294 assert_not_nil mail
294 assert_not_nil mail
295 assert_equal [u.mail], mail.bcc
295 assert_equal [u.mail], mail.bcc
296 assert_mail_body_match 'newpass', mail
296 assert_mail_body_match 'newpass', mail
297 end
297 end
298
298
299 test "put :update with a password change to an AuthSource user switching to Internal authentication" do
299 def test_update_user_switchin_from_auth_source_to_password_authentication
300 # Configure as auth source
300 # Configure as auth source
301 u = User.find(2)
301 u = User.find(2)
302 u.auth_source = AuthSource.find(1)
302 u.auth_source = AuthSource.find(1)
303 u.save!
303 u.save!
304
304
305 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass'
305 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass', :password_confirmation => 'newpass'}
306
306
307 assert_equal nil, u.reload.auth_source
307 assert_equal nil, u.reload.auth_source
308 assert u.check_password?('newpass')
308 assert u.check_password?('newpass')
309 end
309 end
310
310
311 def test_destroy
311 def test_destroy
312 assert_difference 'User.count', -1 do
312 assert_difference 'User.count', -1 do
313 delete :destroy, :id => 2
313 delete :destroy, :id => 2
314 end
314 end
315 assert_redirected_to '/users'
315 assert_redirected_to '/users'
316 assert_nil User.find_by_id(2)
316 assert_nil User.find_by_id(2)
317 end
317 end
318
318
319 def test_destroy_should_be_denied_for_non_admin_users
319 def test_destroy_should_be_denied_for_non_admin_users
320 @request.session[:user_id] = 3
320 @request.session[:user_id] = 3
321
321
322 assert_no_difference 'User.count' do
322 assert_no_difference 'User.count' do
323 get :destroy, :id => 2
323 get :destroy, :id => 2
324 end
324 end
325 assert_response 403
325 assert_response 403
326 end
326 end
327
327
328 def test_create_membership
328 def test_create_membership
329 assert_difference 'Member.count' do
329 assert_difference 'Member.count' do
330 post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]}
330 post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]}
331 end
331 end
332 assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships'
332 assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships'
333 member = Member.first(:order => 'id DESC')
333 member = Member.first(:order => 'id DESC')
334 assert_equal User.find(7), member.principal
334 assert_equal User.find(7), member.principal
335 assert_equal [2], member.role_ids
335 assert_equal [2], member.role_ids
336 assert_equal 3, member.project_id
336 assert_equal 3, member.project_id
337 end
337 end
338
338
339 def test_create_membership_js_format
340 assert_difference 'Member.count' do
341 post :edit_membership, :id => 7, :membership => {:project_id => 3, :role_ids => [2]}, :format => 'js'
342 end
343 assert_response :success
344 assert_select_rjs :replace_html, 'tab-content-memberships'
345 member = Member.first(:order => 'id DESC')
346 assert_equal User.find(7), member.principal
347 assert_equal [2], member.role_ids
348 assert_equal 3, member.project_id
349 end
350
351 def test_create_membership_js_format_with_failure
352 assert_no_difference 'Member.count' do
353 post :edit_membership, :id => 7, :membership => {:project_id => 3}, :format => 'js'
354 end
355 assert_response :success
356 assert @response.body.match(/alert/i), "Alert message not sent"
357 assert @response.body.match(/role can't be empty/i), "Error message not sent"
358 end
359
339 def test_update_membership
360 def test_update_membership
340 assert_no_difference 'Member.count' do
361 assert_no_difference 'Member.count' do
341 put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]}
362 put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]}
342 end
363 end
343 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
364 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
344 assert_equal [2], Member.find(1).role_ids
365 assert_equal [2], Member.find(1).role_ids
345 end
366 end
346
367
368 def test_update_membership_js_format
369 assert_no_difference 'Member.count' do
370 put :edit_membership, :id => 2, :membership_id => 1, :membership => {:role_ids => [2]}, :format => 'js'
371 end
372 assert_response :success
373 assert_select_rjs :replace_html, 'tab-content-memberships'
374 end
375
347 def test_destroy_membership
376 def test_destroy_membership
348 assert_difference 'Member.count', -1 do
377 assert_difference 'Member.count', -1 do
349 delete :destroy_membership, :id => 2, :membership_id => 1
378 delete :destroy_membership, :id => 2, :membership_id => 1
350 end
379 end
351 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
380 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
352 assert_nil Member.find_by_id(1)
381 assert_nil Member.find_by_id(1)
353 end
382 end
383
384 def test_destroy_membership_js_format
385 assert_difference 'Member.count', -1 do
386 delete :destroy_membership, :id => 2, :membership_id => 1, :format => 'js'
387 end
388 assert_response :success
389 assert_select_rjs :replace_html, 'tab-content-memberships'
390 end
354 end
391 end
General Comments 0
You need to be logged in to leave comments. Login now