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