##// END OF EJS Templates
remove trailing white-spaces from test/functional/users_controller_test.rb....
Toshi MARUYAMA -
r6508:0a369b73831d
parent child
Show More
@@ -1,312 +1,312
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, :auth_sources, :custom_fields, :custom_values, :groups_users
27 fixtures :users, :projects, :members, :member_roles, :roles, :auth_sources, :custom_fields, :custom_values, :groups_users
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_name_filter
52 def test_index_with_name_filter
53 get :index, :name => 'john'
53 get :index, :name => 'john'
54 assert_response :success
54 assert_response :success
55 assert_template 'index'
55 assert_template 'index'
56 users = assigns(:users)
56 users = assigns(:users)
57 assert_not_nil users
57 assert_not_nil users
58 assert_equal 1, users.size
58 assert_equal 1, users.size
59 assert_equal 'John', users.first.firstname
59 assert_equal 'John', users.first.firstname
60 end
60 end
61
61
62 def test_index_with_group_filter
62 def test_index_with_group_filter
63 get :index, :group_id => '10'
63 get :index, :group_id => '10'
64 assert_response :success
64 assert_response :success
65 assert_template 'index'
65 assert_template 'index'
66 users = assigns(:users)
66 users = assigns(:users)
67 assert users.any?
67 assert users.any?
68 assert_equal([], (users - Group.find(10).users))
68 assert_equal([], (users - Group.find(10).users))
69 end
69 end
70
70
71 def test_show
71 def test_show
72 @request.session[:user_id] = nil
72 @request.session[:user_id] = nil
73 get :show, :id => 2
73 get :show, :id => 2
74 assert_response :success
74 assert_response :success
75 assert_template 'show'
75 assert_template 'show'
76 assert_not_nil assigns(:user)
76 assert_not_nil assigns(:user)
77
77
78 assert_tag 'li', :content => /Phone number/
78 assert_tag 'li', :content => /Phone number/
79 end
79 end
80
80
81 def test_show_should_not_display_hidden_custom_fields
81 def test_show_should_not_display_hidden_custom_fields
82 @request.session[:user_id] = nil
82 @request.session[:user_id] = nil
83 UserCustomField.find_by_name('Phone number').update_attribute :visible, false
83 UserCustomField.find_by_name('Phone number').update_attribute :visible, false
84 get :show, :id => 2
84 get :show, :id => 2
85 assert_response :success
85 assert_response :success
86 assert_template 'show'
86 assert_template 'show'
87 assert_not_nil assigns(:user)
87 assert_not_nil assigns(:user)
88
88
89 assert_no_tag 'li', :content => /Phone number/
89 assert_no_tag 'li', :content => /Phone number/
90 end
90 end
91
91
92 def test_show_should_not_fail_when_custom_values_are_nil
92 def test_show_should_not_fail_when_custom_values_are_nil
93 user = User.find(2)
93 user = User.find(2)
94
94
95 # Create a custom field to illustrate the issue
95 # Create a custom field to illustrate the issue
96 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
96 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
97 custom_value = user.custom_values.build(:custom_field => custom_field).save!
97 custom_value = user.custom_values.build(:custom_field => custom_field).save!
98
98
99 get :show, :id => 2
99 get :show, :id => 2
100 assert_response :success
100 assert_response :success
101 end
101 end
102
102
103 def test_show_inactive
103 def test_show_inactive
104 @request.session[:user_id] = nil
104 @request.session[:user_id] = nil
105 get :show, :id => 5
105 get :show, :id => 5
106 assert_response 404
106 assert_response 404
107 end
107 end
108
108
109 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
109 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
110 @request.session[:user_id] = nil
110 @request.session[:user_id] = nil
111 get :show, :id => 9
111 get :show, :id => 9
112 assert_response 404
112 assert_response 404
113 end
113 end
114
114
115 def test_show_inactive_by_admin
115 def test_show_inactive_by_admin
116 @request.session[:user_id] = 1
116 @request.session[:user_id] = 1
117 get :show, :id => 5
117 get :show, :id => 5
118 assert_response 200
118 assert_response 200
119 assert_not_nil assigns(:user)
119 assert_not_nil assigns(:user)
120 end
120 end
121
121
122 def test_show_displays_memberships_based_on_project_visibility
122 def test_show_displays_memberships_based_on_project_visibility
123 @request.session[:user_id] = 1
123 @request.session[:user_id] = 1
124 get :show, :id => 2
124 get :show, :id => 2
125 assert_response :success
125 assert_response :success
126 memberships = assigns(:memberships)
126 memberships = assigns(:memberships)
127 assert_not_nil memberships
127 assert_not_nil memberships
128 project_ids = memberships.map(&:project_id)
128 project_ids = memberships.map(&:project_id)
129 assert project_ids.include?(2) #private project admin can see
129 assert project_ids.include?(2) #private project admin can see
130 end
130 end
131
131
132 def test_show_current_should_require_authentication
132 def test_show_current_should_require_authentication
133 @request.session[:user_id] = nil
133 @request.session[:user_id] = nil
134 get :show, :id => 'current'
134 get :show, :id => 'current'
135 assert_response 302
135 assert_response 302
136 end
136 end
137
137
138 def test_show_current
138 def test_show_current
139 @request.session[:user_id] = 2
139 @request.session[:user_id] = 2
140 get :show, :id => 'current'
140 get :show, :id => 'current'
141 assert_response :success
141 assert_response :success
142 assert_template 'show'
142 assert_template 'show'
143 assert_equal User.find(2), assigns(:user)
143 assert_equal User.find(2), assigns(:user)
144 end
144 end
145
145
146 def test_new
146 def test_new
147 get :new
147 get :new
148
148
149 assert_response :success
149 assert_response :success
150 assert_template :new
150 assert_template :new
151 assert assigns(:user)
151 assert assigns(:user)
152 end
152 end
153
153
154 def test_create
154 def test_create
155 Setting.bcc_recipients = '1'
155 Setting.bcc_recipients = '1'
156
156
157 assert_difference 'User.count' do
157 assert_difference 'User.count' do
158 assert_difference 'ActionMailer::Base.deliveries.size' do
158 assert_difference 'ActionMailer::Base.deliveries.size' do
159 post :create,
159 post :create,
160 :user => {
160 :user => {
161 :firstname => 'John',
161 :firstname => 'John',
162 :lastname => 'Doe',
162 :lastname => 'Doe',
163 :login => 'jdoe',
163 :login => 'jdoe',
164 :password => 'secret',
164 :password => 'secret',
165 :password_confirmation => 'secret',
165 :password_confirmation => 'secret',
166 :mail => 'jdoe@gmail.com',
166 :mail => 'jdoe@gmail.com',
167 :mail_notification => 'none'
167 :mail_notification => 'none'
168 },
168 },
169 :send_information => '1'
169 :send_information => '1'
170 end
170 end
171 end
171 end
172
172
173 user = User.first(:order => 'id DESC')
173 user = User.first(:order => 'id DESC')
174 assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
174 assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
175
175
176 assert_equal 'John', user.firstname
176 assert_equal 'John', user.firstname
177 assert_equal 'Doe', user.lastname
177 assert_equal 'Doe', user.lastname
178 assert_equal 'jdoe', user.login
178 assert_equal 'jdoe', user.login
179 assert_equal 'jdoe@gmail.com', user.mail
179 assert_equal 'jdoe@gmail.com', user.mail
180 assert_equal 'none', user.mail_notification
180 assert_equal 'none', user.mail_notification
181 assert user.check_password?('secret')
181 assert user.check_password?('secret')
182
182
183 mail = ActionMailer::Base.deliveries.last
183 mail = ActionMailer::Base.deliveries.last
184 assert_not_nil mail
184 assert_not_nil mail
185 assert_equal [user.mail], mail.bcc
185 assert_equal [user.mail], mail.bcc
186 assert mail.body.include?('secret')
186 assert mail.body.include?('secret')
187 end
187 end
188
188
189 def test_create_with_failure
189 def test_create_with_failure
190 assert_no_difference 'User.count' do
190 assert_no_difference 'User.count' do
191 post :create, :user => {}
191 post :create, :user => {}
192 end
192 end
193
193
194 assert_response :success
194 assert_response :success
195 assert_template 'new'
195 assert_template 'new'
196 end
196 end
197
197
198 def test_edit
198 def test_edit
199 get :edit, :id => 2
199 get :edit, :id => 2
200
200
201 assert_response :success
201 assert_response :success
202 assert_template 'edit'
202 assert_template 'edit'
203 assert_equal User.find(2), assigns(:user)
203 assert_equal User.find(2), assigns(:user)
204 end
204 end
205
205
206 def test_update
206 def test_update
207 ActionMailer::Base.deliveries.clear
207 ActionMailer::Base.deliveries.clear
208 put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
208 put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
209
209
210 user = User.find(2)
210 user = User.find(2)
211 assert_equal 'Changed', user.firstname
211 assert_equal 'Changed', user.firstname
212 assert_equal 'only_assigned', user.mail_notification
212 assert_equal 'only_assigned', user.mail_notification
213 assert_equal true, user.pref[:hide_mail]
213 assert_equal true, user.pref[:hide_mail]
214 assert_equal 'desc', user.pref[:comments_sorting]
214 assert_equal 'desc', user.pref[:comments_sorting]
215 assert ActionMailer::Base.deliveries.empty?
215 assert ActionMailer::Base.deliveries.empty?
216 end
216 end
217
217
218 def test_update_with_failure
218 def test_update_with_failure
219 assert_no_difference 'User.count' do
219 assert_no_difference 'User.count' do
220 put :update, :id => 2, :user => {:firstname => ''}
220 put :update, :id => 2, :user => {:firstname => ''}
221 end
221 end
222
222
223 assert_response :success
223 assert_response :success
224 assert_template 'edit'
224 assert_template 'edit'
225 end
225 end
226
226
227 def test_update_with_group_ids_should_assign_groups
227 def test_update_with_group_ids_should_assign_groups
228 put :update, :id => 2, :user => {:group_ids => ['10']}
228 put :update, :id => 2, :user => {:group_ids => ['10']}
229
229
230 user = User.find(2)
230 user = User.find(2)
231 assert_equal [10], user.group_ids
231 assert_equal [10], user.group_ids
232 end
232 end
233
233
234 def test_update_with_activation_should_send_a_notification
234 def test_update_with_activation_should_send_a_notification
235 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
235 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
236 u.login = 'foo'
236 u.login = 'foo'
237 u.status = User::STATUS_REGISTERED
237 u.status = User::STATUS_REGISTERED
238 u.save!
238 u.save!
239 ActionMailer::Base.deliveries.clear
239 ActionMailer::Base.deliveries.clear
240 Setting.bcc_recipients = '1'
240 Setting.bcc_recipients = '1'
241
241
242 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
242 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
243 assert u.reload.active?
243 assert u.reload.active?
244 mail = ActionMailer::Base.deliveries.last
244 mail = ActionMailer::Base.deliveries.last
245 assert_not_nil mail
245 assert_not_nil mail
246 assert_equal ['foo.bar@somenet.foo'], mail.bcc
246 assert_equal ['foo.bar@somenet.foo'], mail.bcc
247 assert mail.body.include?(ll('fr', :notice_account_activated))
247 assert mail.body.include?(ll('fr', :notice_account_activated))
248 end
248 end
249
249
250 def test_update_with_password_change_should_send_a_notification
250 def test_update_with_password_change_should_send_a_notification
251 ActionMailer::Base.deliveries.clear
251 ActionMailer::Base.deliveries.clear
252 Setting.bcc_recipients = '1'
252 Setting.bcc_recipients = '1'
253
253
254 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
254 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
255 u = User.find(2)
255 u = User.find(2)
256 assert u.check_password?('newpass')
256 assert u.check_password?('newpass')
257
257
258 mail = ActionMailer::Base.deliveries.last
258 mail = ActionMailer::Base.deliveries.last
259 assert_not_nil mail
259 assert_not_nil mail
260 assert_equal [u.mail], mail.bcc
260 assert_equal [u.mail], mail.bcc
261 assert mail.body.include?('newpass')
261 assert mail.body.include?('newpass')
262 end
262 end
263
263
264 test "put :update with a password change to an AuthSource user switching to Internal authentication" do
264 test "put :update with a password change to an AuthSource user switching to Internal authentication" do
265 # Configure as auth source
265 # Configure as auth source
266 u = User.find(2)
266 u = User.find(2)
267 u.auth_source = AuthSource.find(1)
267 u.auth_source = AuthSource.find(1)
268 u.save!
268 u.save!
269
269
270 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass'
270 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass'
271
271
272 assert_equal nil, u.reload.auth_source
272 assert_equal nil, u.reload.auth_source
273 assert u.check_password?('newpass')
273 assert u.check_password?('newpass')
274 end
274 end
275
275
276 def test_destroy
276 def test_destroy
277 assert_difference 'User.count', -1 do
277 assert_difference 'User.count', -1 do
278 delete :destroy, :id => 2
278 delete :destroy, :id => 2
279 end
279 end
280 assert_redirected_to '/users'
280 assert_redirected_to '/users'
281 assert_nil User.find_by_id(2)
281 assert_nil User.find_by_id(2)
282 end
282 end
283
283
284 def test_destroy_should_not_accept_get_requests
284 def test_destroy_should_not_accept_get_requests
285 assert_no_difference 'User.count' do
285 assert_no_difference 'User.count' do
286 get :destroy, :id => 2
286 get :destroy, :id => 2
287 end
287 end
288 assert_response 405
288 assert_response 405
289 end
289 end
290
290
291 def test_destroy_should_be_denied_for_non_admin_users
291 def test_destroy_should_be_denied_for_non_admin_users
292 @request.session[:user_id] = 3
292 @request.session[:user_id] = 3
293
293
294 assert_no_difference 'User.count' do
294 assert_no_difference 'User.count' do
295 get :destroy, :id => 2
295 get :destroy, :id => 2
296 end
296 end
297 assert_response 403
297 assert_response 403
298 end
298 end
299
299
300 def test_edit_membership
300 def test_edit_membership
301 post :edit_membership, :id => 2, :membership_id => 1,
301 post :edit_membership, :id => 2, :membership_id => 1,
302 :membership => { :role_ids => [2]}
302 :membership => { :role_ids => [2]}
303 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
303 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
304 assert_equal [2], Member.find(1).role_ids
304 assert_equal [2], Member.find(1).role_ids
305 end
305 end
306
306
307 def test_destroy_membership
307 def test_destroy_membership
308 post :destroy_membership, :id => 2, :membership_id => 1
308 post :destroy_membership, :id => 2, :membership_id => 1
309 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
309 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
310 assert_nil Member.find_by_id(1)
310 assert_nil Member.find_by_id(1)
311 end
311 end
312 end
312 end
General Comments 0
You need to be logged in to leave comments. Login now