##// END OF EJS Templates
Adds required fixtures back....
Jean-Philippe Lang -
r8832:0bb957908837
parent child
Show More
@@ -1,334 +1,334
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
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.include?('secret')
194 assert mail.body.include?('secret')
195 end
195 end
196
196
197 def test_create_with_failure
197 def test_create_with_failure
198 assert_no_difference 'User.count' do
198 assert_no_difference 'User.count' do
199 post :create, :user => {}
199 post :create, :user => {}
200 end
200 end
201
201
202 assert_response :success
202 assert_response :success
203 assert_template 'new'
203 assert_template 'new'
204 end
204 end
205
205
206 def test_edit
206 def test_edit
207 get :edit, :id => 2
207 get :edit, :id => 2
208
208
209 assert_response :success
209 assert_response :success
210 assert_template 'edit'
210 assert_template 'edit'
211 assert_equal User.find(2), assigns(:user)
211 assert_equal User.find(2), assigns(:user)
212 end
212 end
213
213
214 def test_update
214 def test_update
215 ActionMailer::Base.deliveries.clear
215 ActionMailer::Base.deliveries.clear
216 put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
216 put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
217
217
218 user = User.find(2)
218 user = User.find(2)
219 assert_equal 'Changed', user.firstname
219 assert_equal 'Changed', user.firstname
220 assert_equal 'only_assigned', user.mail_notification
220 assert_equal 'only_assigned', user.mail_notification
221 assert_equal true, user.pref[:hide_mail]
221 assert_equal true, user.pref[:hide_mail]
222 assert_equal 'desc', user.pref[:comments_sorting]
222 assert_equal 'desc', user.pref[:comments_sorting]
223 assert ActionMailer::Base.deliveries.empty?
223 assert ActionMailer::Base.deliveries.empty?
224 end
224 end
225
225
226 def test_update_with_failure
226 def test_update_with_failure
227 assert_no_difference 'User.count' do
227 assert_no_difference 'User.count' do
228 put :update, :id => 2, :user => {:firstname => ''}
228 put :update, :id => 2, :user => {:firstname => ''}
229 end
229 end
230
230
231 assert_response :success
231 assert_response :success
232 assert_template 'edit'
232 assert_template 'edit'
233 end
233 end
234
234
235 def test_update_with_group_ids_should_assign_groups
235 def test_update_with_group_ids_should_assign_groups
236 put :update, :id => 2, :user => {:group_ids => ['10']}
236 put :update, :id => 2, :user => {:group_ids => ['10']}
237
237
238 user = User.find(2)
238 user = User.find(2)
239 assert_equal [10], user.group_ids
239 assert_equal [10], user.group_ids
240 end
240 end
241
241
242 def test_update_with_activation_should_send_a_notification
242 def test_update_with_activation_should_send_a_notification
243 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
243 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
244 u.login = 'foo'
244 u.login = 'foo'
245 u.status = User::STATUS_REGISTERED
245 u.status = User::STATUS_REGISTERED
246 u.save!
246 u.save!
247 ActionMailer::Base.deliveries.clear
247 ActionMailer::Base.deliveries.clear
248 Setting.bcc_recipients = '1'
248 Setting.bcc_recipients = '1'
249
249
250 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
250 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
251 assert u.reload.active?
251 assert u.reload.active?
252 mail = ActionMailer::Base.deliveries.last
252 mail = ActionMailer::Base.deliveries.last
253 assert_not_nil mail
253 assert_not_nil mail
254 assert_equal ['foo.bar@somenet.foo'], mail.bcc
254 assert_equal ['foo.bar@somenet.foo'], mail.bcc
255 assert mail.body.include?(ll('fr', :notice_account_activated))
255 assert mail.body.include?(ll('fr', :notice_account_activated))
256 end
256 end
257
257
258 def test_update_with_password_change_should_send_a_notification
258 def test_update_with_password_change_should_send_a_notification
259 ActionMailer::Base.deliveries.clear
259 ActionMailer::Base.deliveries.clear
260 Setting.bcc_recipients = '1'
260 Setting.bcc_recipients = '1'
261
261
262 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
262 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
263 u = User.find(2)
263 u = User.find(2)
264 assert u.check_password?('newpass')
264 assert u.check_password?('newpass')
265
265
266 mail = ActionMailer::Base.deliveries.last
266 mail = ActionMailer::Base.deliveries.last
267 assert_not_nil mail
267 assert_not_nil mail
268 assert_equal [u.mail], mail.bcc
268 assert_equal [u.mail], mail.bcc
269 assert mail.body.include?('newpass')
269 assert mail.body.include?('newpass')
270 end
270 end
271
271
272 test "put :update with a password change to an AuthSource user switching to Internal authentication" do
272 test "put :update with a password change to an AuthSource user switching to Internal authentication" do
273 # Configure as auth source
273 # Configure as auth source
274 u = User.find(2)
274 u = User.find(2)
275 u.auth_source = AuthSource.find(1)
275 u.auth_source = AuthSource.find(1)
276 u.save!
276 u.save!
277
277
278 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass'
278 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass'
279
279
280 assert_equal nil, u.reload.auth_source
280 assert_equal nil, u.reload.auth_source
281 assert u.check_password?('newpass')
281 assert u.check_password?('newpass')
282 end
282 end
283
283
284 def test_destroy
284 def test_destroy
285 assert_difference 'User.count', -1 do
285 assert_difference 'User.count', -1 do
286 delete :destroy, :id => 2
286 delete :destroy, :id => 2
287 end
287 end
288 assert_redirected_to '/users'
288 assert_redirected_to '/users'
289 assert_nil User.find_by_id(2)
289 assert_nil User.find_by_id(2)
290 end
290 end
291
291
292 def test_destroy_should_not_accept_get_requests
292 def test_destroy_should_not_accept_get_requests
293 assert_no_difference 'User.count' do
293 assert_no_difference 'User.count' do
294 get :destroy, :id => 2
294 get :destroy, :id => 2
295 end
295 end
296 assert_response 405
296 assert_response 405
297 end
297 end
298
298
299 def test_destroy_should_be_denied_for_non_admin_users
299 def test_destroy_should_be_denied_for_non_admin_users
300 @request.session[:user_id] = 3
300 @request.session[:user_id] = 3
301
301
302 assert_no_difference 'User.count' do
302 assert_no_difference 'User.count' do
303 get :destroy, :id => 2
303 get :destroy, :id => 2
304 end
304 end
305 assert_response 403
305 assert_response 403
306 end
306 end
307
307
308 def test_create_membership
308 def test_create_membership
309 assert_difference 'Member.count' do
309 assert_difference 'Member.count' do
310 post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]}
310 post :edit_membership, :id => 7, :membership => { :project_id => 3, :role_ids => [2]}
311 end
311 end
312 assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships'
312 assert_redirected_to :action => 'edit', :id => '7', :tab => 'memberships'
313 member = Member.first(:order => 'id DESC')
313 member = Member.first(:order => 'id DESC')
314 assert_equal User.find(7), member.principal
314 assert_equal User.find(7), member.principal
315 assert_equal [2], member.role_ids
315 assert_equal [2], member.role_ids
316 assert_equal 3, member.project_id
316 assert_equal 3, member.project_id
317 end
317 end
318
318
319 def test_update_membership
319 def test_update_membership
320 assert_no_difference 'Member.count' do
320 assert_no_difference 'Member.count' do
321 put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]}
321 put :edit_membership, :id => 2, :membership_id => 1, :membership => { :role_ids => [2]}
322 end
322 end
323 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
323 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
324 assert_equal [2], Member.find(1).role_ids
324 assert_equal [2], Member.find(1).role_ids
325 end
325 end
326
326
327 def test_destroy_membership
327 def test_destroy_membership
328 assert_difference 'Member.count', -1 do
328 assert_difference 'Member.count', -1 do
329 delete :destroy_membership, :id => 2, :membership_id => 1
329 delete :destroy_membership, :id => 2, :membership_id => 1
330 end
330 end
331 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
331 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
332 assert_nil Member.find_by_id(1)
332 assert_nil Member.find_by_id(1)
333 end
333 end
334 end
334 end
General Comments 0
You need to be logged in to leave comments. Login now