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