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