Chris@441
|
1 # Redmine - project management software
|
Chris@441
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@119
|
18 require File.expand_path('../../test_helper', __FILE__)
|
Chris@0
|
19 require 'users_controller'
|
Chris@0
|
20
|
Chris@0
|
21 # Re-raise errors caught by the controller.
|
Chris@0
|
22 class UsersController; def rescue_action(e) raise e end; end
|
Chris@0
|
23
|
Chris@0
|
24 class UsersControllerTest < ActionController::TestCase
|
Chris@0
|
25 include Redmine::I18n
|
Chris@0
|
26
|
Chris@441
|
27 fixtures :users, :projects, :members, :member_roles, :roles, :auth_sources, :custom_fields, :custom_values, :groups_users
|
Chris@0
|
28
|
Chris@0
|
29 def setup
|
Chris@0
|
30 @controller = UsersController.new
|
Chris@0
|
31 @request = ActionController::TestRequest.new
|
Chris@0
|
32 @response = ActionController::TestResponse.new
|
Chris@0
|
33 User.current = nil
|
Chris@0
|
34 @request.session[:user_id] = 1 # admin
|
Chris@0
|
35 end
|
Chris@0
|
36
|
Chris@0
|
37 def test_index
|
Chris@0
|
38 get :index
|
Chris@0
|
39 assert_response :success
|
Chris@0
|
40 assert_template 'index'
|
Chris@0
|
41 end
|
Chris@0
|
42
|
Chris@0
|
43 def test_index
|
Chris@0
|
44 get :index
|
Chris@0
|
45 assert_response :success
|
Chris@0
|
46 assert_template 'index'
|
Chris@0
|
47 assert_not_nil assigns(:users)
|
Chris@0
|
48 # active users only
|
Chris@0
|
49 assert_nil assigns(:users).detect {|u| !u.active?}
|
Chris@0
|
50 end
|
Chris@0
|
51
|
Chris@0
|
52 def test_index_with_name_filter
|
Chris@0
|
53 get :index, :name => 'john'
|
Chris@0
|
54 assert_response :success
|
Chris@0
|
55 assert_template 'index'
|
Chris@0
|
56 users = assigns(:users)
|
Chris@0
|
57 assert_not_nil users
|
Chris@0
|
58 assert_equal 1, users.size
|
Chris@0
|
59 assert_equal 'John', users.first.firstname
|
Chris@0
|
60 end
|
Chris@0
|
61
|
Chris@441
|
62 def test_index_with_group_filter
|
Chris@441
|
63 get :index, :group_id => '10'
|
Chris@441
|
64 assert_response :success
|
Chris@441
|
65 assert_template 'index'
|
Chris@441
|
66 users = assigns(:users)
|
Chris@441
|
67 assert users.any?
|
Chris@441
|
68 assert_equal([], (users - Group.find(10).users))
|
Chris@441
|
69 end
|
Chris@441
|
70
|
Chris@0
|
71 def test_show
|
Chris@0
|
72 @request.session[:user_id] = nil
|
Chris@0
|
73 get :show, :id => 2
|
Chris@0
|
74 assert_response :success
|
Chris@0
|
75 assert_template 'show'
|
Chris@0
|
76 assert_not_nil assigns(:user)
|
chris@37
|
77
|
chris@37
|
78 assert_tag 'li', :content => /Phone number/
|
chris@37
|
79 end
|
chris@37
|
80
|
chris@37
|
81 def test_show_should_not_display_hidden_custom_fields
|
chris@37
|
82 @request.session[:user_id] = nil
|
chris@37
|
83 UserCustomField.find_by_name('Phone number').update_attribute :visible, false
|
chris@37
|
84 get :show, :id => 2
|
chris@37
|
85 assert_response :success
|
chris@37
|
86 assert_template 'show'
|
chris@37
|
87 assert_not_nil assigns(:user)
|
chris@37
|
88
|
chris@37
|
89 assert_no_tag 'li', :content => /Phone number/
|
Chris@0
|
90 end
|
Chris@0
|
91
|
Chris@0
|
92 def test_show_should_not_fail_when_custom_values_are_nil
|
Chris@0
|
93 user = User.find(2)
|
Chris@0
|
94
|
Chris@0
|
95 # Create a custom field to illustrate the issue
|
Chris@0
|
96 custom_field = CustomField.create!(:name => 'Testing', :field_format => 'text')
|
Chris@0
|
97 custom_value = user.custom_values.build(:custom_field => custom_field).save!
|
Chris@0
|
98
|
Chris@0
|
99 get :show, :id => 2
|
Chris@0
|
100 assert_response :success
|
Chris@0
|
101 end
|
Chris@0
|
102
|
Chris@0
|
103 def test_show_inactive
|
Chris@0
|
104 @request.session[:user_id] = nil
|
Chris@0
|
105 get :show, :id => 5
|
Chris@0
|
106 assert_response 404
|
Chris@0
|
107 end
|
Chris@0
|
108
|
Chris@0
|
109 def test_show_should_not_reveal_users_with_no_visible_activity_or_project
|
Chris@0
|
110 @request.session[:user_id] = nil
|
Chris@0
|
111 get :show, :id => 9
|
Chris@0
|
112 assert_response 404
|
Chris@0
|
113 end
|
Chris@0
|
114
|
Chris@0
|
115 def test_show_inactive_by_admin
|
Chris@0
|
116 @request.session[:user_id] = 1
|
Chris@0
|
117 get :show, :id => 5
|
Chris@0
|
118 assert_response 200
|
Chris@0
|
119 assert_not_nil assigns(:user)
|
Chris@0
|
120 end
|
Chris@14
|
121
|
Chris@14
|
122 def test_show_displays_memberships_based_on_project_visibility
|
Chris@14
|
123 @request.session[:user_id] = 1
|
Chris@14
|
124 get :show, :id => 2
|
Chris@14
|
125 assert_response :success
|
Chris@14
|
126 memberships = assigns(:memberships)
|
Chris@14
|
127 assert_not_nil memberships
|
Chris@14
|
128 project_ids = memberships.map(&:project_id)
|
Chris@14
|
129 assert project_ids.include?(2) #private project admin can see
|
Chris@14
|
130 end
|
Chris@119
|
131
|
Chris@119
|
132 def test_show_current_should_require_authentication
|
Chris@119
|
133 @request.session[:user_id] = nil
|
Chris@119
|
134 get :show, :id => 'current'
|
Chris@119
|
135 assert_response 302
|
Chris@119
|
136 end
|
Chris@119
|
137
|
Chris@119
|
138 def test_show_current
|
Chris@119
|
139 @request.session[:user_id] = 2
|
Chris@119
|
140 get :show, :id => 'current'
|
Chris@119
|
141 assert_response :success
|
Chris@119
|
142 assert_template 'show'
|
Chris@119
|
143 assert_equal User.find(2), assigns(:user)
|
Chris@119
|
144 end
|
Chris@119
|
145
|
Chris@119
|
146 def test_new
|
Chris@119
|
147 get :new
|
Chris@119
|
148
|
Chris@119
|
149 assert_response :success
|
Chris@119
|
150 assert_template :new
|
Chris@119
|
151 assert assigns(:user)
|
Chris@119
|
152 end
|
Chris@119
|
153
|
Chris@119
|
154 def test_create
|
Chris@119
|
155 Setting.bcc_recipients = '1'
|
Chris@119
|
156
|
Chris@119
|
157 assert_difference 'User.count' do
|
Chris@119
|
158 assert_difference 'ActionMailer::Base.deliveries.size' do
|
Chris@119
|
159 post :create,
|
Chris@119
|
160 :user => {
|
Chris@119
|
161 :firstname => 'John',
|
Chris@119
|
162 :lastname => 'Doe',
|
Chris@119
|
163 :login => 'jdoe',
|
Chris@119
|
164 :password => 'secret',
|
Chris@119
|
165 :password_confirmation => 'secret',
|
Chris@119
|
166 :mail => 'jdoe@gmail.com',
|
Chris@119
|
167 :mail_notification => 'none'
|
Chris@119
|
168 },
|
Chris@119
|
169 :send_information => '1'
|
Chris@119
|
170 end
|
chris@37
|
171 end
|
Chris@119
|
172
|
Chris@119
|
173 user = User.first(:order => 'id DESC')
|
Chris@119
|
174 assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
|
Chris@119
|
175
|
Chris@119
|
176 assert_equal 'John', user.firstname
|
Chris@119
|
177 assert_equal 'Doe', user.lastname
|
Chris@119
|
178 assert_equal 'jdoe', user.login
|
Chris@119
|
179 assert_equal 'jdoe@gmail.com', user.mail
|
Chris@119
|
180 assert_equal 'none', user.mail_notification
|
Chris@119
|
181 assert user.check_password?('secret')
|
Chris@119
|
182
|
Chris@119
|
183 mail = ActionMailer::Base.deliveries.last
|
Chris@119
|
184 assert_not_nil mail
|
Chris@119
|
185 assert_equal [user.mail], mail.bcc
|
Chris@119
|
186 assert mail.body.include?('secret')
|
Chris@119
|
187 end
|
Chris@119
|
188
|
Chris@119
|
189 def test_create_with_failure
|
Chris@119
|
190 assert_no_difference 'User.count' do
|
Chris@119
|
191 post :create, :user => {}
|
Chris@119
|
192 end
|
Chris@119
|
193
|
Chris@119
|
194 assert_response :success
|
Chris@119
|
195 assert_template 'new'
|
chris@37
|
196 end
|
chris@37
|
197
|
Chris@119
|
198 def test_edit
|
Chris@119
|
199 get :edit, :id => 2
|
Chris@119
|
200
|
Chris@119
|
201 assert_response :success
|
Chris@119
|
202 assert_template 'edit'
|
Chris@119
|
203 assert_equal User.find(2), assigns(:user)
|
chris@37
|
204 end
|
chris@37
|
205
|
chris@37
|
206 def test_update
|
Chris@0
|
207 ActionMailer::Base.deliveries.clear
|
Chris@119
|
208 put :update, :id => 2, :user => {:firstname => 'Changed', :mail_notification => 'only_assigned'}, :pref => {:hide_mail => '1', :comments_sorting => 'desc'}
|
chris@37
|
209
|
chris@37
|
210 user = User.find(2)
|
chris@37
|
211 assert_equal 'Changed', user.firstname
|
Chris@119
|
212 assert_equal 'only_assigned', user.mail_notification
|
chris@37
|
213 assert_equal true, user.pref[:hide_mail]
|
chris@37
|
214 assert_equal 'desc', user.pref[:comments_sorting]
|
Chris@0
|
215 assert ActionMailer::Base.deliveries.empty?
|
Chris@0
|
216 end
|
Chris@119
|
217
|
Chris@119
|
218 def test_update_with_failure
|
Chris@119
|
219 assert_no_difference 'User.count' do
|
Chris@119
|
220 put :update, :id => 2, :user => {:firstname => ''}
|
Chris@119
|
221 end
|
Chris@119
|
222
|
Chris@119
|
223 assert_response :success
|
Chris@119
|
224 assert_template 'edit'
|
Chris@119
|
225 end
|
Chris@119
|
226
|
Chris@119
|
227 def test_update_with_group_ids_should_assign_groups
|
Chris@119
|
228 put :update, :id => 2, :user => {:group_ids => ['10']}
|
Chris@119
|
229
|
Chris@119
|
230 user = User.find(2)
|
Chris@119
|
231 assert_equal [10], user.group_ids
|
Chris@119
|
232 end
|
Chris@0
|
233
|
chris@37
|
234 def test_update_with_activation_should_send_a_notification
|
Chris@0
|
235 u = User.new(:firstname => 'Foo', :lastname => 'Bar', :mail => 'foo.bar@somenet.foo', :language => 'fr')
|
Chris@0
|
236 u.login = 'foo'
|
Chris@0
|
237 u.status = User::STATUS_REGISTERED
|
Chris@0
|
238 u.save!
|
Chris@0
|
239 ActionMailer::Base.deliveries.clear
|
Chris@0
|
240 Setting.bcc_recipients = '1'
|
Chris@0
|
241
|
chris@37
|
242 put :update, :id => u.id, :user => {:status => User::STATUS_ACTIVE}
|
Chris@0
|
243 assert u.reload.active?
|
Chris@0
|
244 mail = ActionMailer::Base.deliveries.last
|
Chris@0
|
245 assert_not_nil mail
|
Chris@0
|
246 assert_equal ['foo.bar@somenet.foo'], mail.bcc
|
Chris@0
|
247 assert mail.body.include?(ll('fr', :notice_account_activated))
|
Chris@0
|
248 end
|
Chris@0
|
249
|
Chris@119
|
250 def test_update_with_password_change_should_send_a_notification
|
Chris@0
|
251 ActionMailer::Base.deliveries.clear
|
Chris@0
|
252 Setting.bcc_recipients = '1'
|
Chris@0
|
253
|
Chris@119
|
254 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
|
Chris@0
|
255 u = User.find(2)
|
Chris@119
|
256 assert u.check_password?('newpass')
|
Chris@0
|
257
|
Chris@0
|
258 mail = ActionMailer::Base.deliveries.last
|
Chris@0
|
259 assert_not_nil mail
|
Chris@0
|
260 assert_equal [u.mail], mail.bcc
|
Chris@0
|
261 assert mail.body.include?('newpass')
|
Chris@0
|
262 end
|
chris@22
|
263
|
chris@37
|
264 test "put :update with a password change to an AuthSource user switching to Internal authentication" do
|
chris@22
|
265 # Configure as auth source
|
chris@22
|
266 u = User.find(2)
|
chris@22
|
267 u.auth_source = AuthSource.find(1)
|
chris@22
|
268 u.save!
|
chris@22
|
269
|
Chris@119
|
270 put :update, :id => u.id, :user => {:auth_source_id => '', :password => 'newpass'}, :password_confirmation => 'newpass'
|
chris@22
|
271
|
chris@22
|
272 assert_equal nil, u.reload.auth_source
|
Chris@119
|
273 assert u.check_password?('newpass')
|
chris@22
|
274 end
|
Chris@0
|
275
|
Chris@128
|
276 def test_destroy
|
Chris@128
|
277 assert_difference 'User.count', -1 do
|
Chris@128
|
278 delete :destroy, :id => 2
|
Chris@128
|
279 end
|
Chris@128
|
280 assert_redirected_to '/users'
|
Chris@128
|
281 assert_nil User.find_by_id(2)
|
Chris@128
|
282 end
|
Chris@128
|
283
|
Chris@128
|
284 def test_destroy_should_not_accept_get_requests
|
Chris@128
|
285 assert_no_difference 'User.count' do
|
Chris@128
|
286 get :destroy, :id => 2
|
Chris@128
|
287 end
|
Chris@128
|
288 assert_response 405
|
Chris@128
|
289 end
|
Chris@128
|
290
|
Chris@128
|
291 def test_destroy_should_be_denied_for_non_admin_users
|
Chris@128
|
292 @request.session[:user_id] = 3
|
Chris@128
|
293
|
Chris@128
|
294 assert_no_difference 'User.count' do
|
Chris@128
|
295 get :destroy, :id => 2
|
Chris@128
|
296 end
|
Chris@128
|
297 assert_response 403
|
Chris@128
|
298 end
|
Chris@128
|
299
|
Chris@0
|
300 def test_edit_membership
|
Chris@0
|
301 post :edit_membership, :id => 2, :membership_id => 1,
|
Chris@0
|
302 :membership => { :role_ids => [2]}
|
Chris@0
|
303 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
|
Chris@0
|
304 assert_equal [2], Member.find(1).role_ids
|
Chris@0
|
305 end
|
Chris@0
|
306
|
Chris@0
|
307 def test_destroy_membership
|
Chris@0
|
308 post :destroy_membership, :id => 2, :membership_id => 1
|
Chris@0
|
309 assert_redirected_to :action => 'edit', :id => '2', :tab => 'memberships'
|
Chris@0
|
310 assert_nil Member.find_by_id(1)
|
Chris@0
|
311 end
|
Chris@0
|
312 end
|