comparison test/functional/users_controller_test.rb @ 524:1248a47e81b3 feature_36

Merge from branch "luisf"
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Mon, 25 Jul 2011 14:39:38 +0100
parents cbce1fd3b1b7
children cbb26bc654de
comparison
equal deleted inserted replaced
519:3be6bc3c2a17 524:1248a47e81b3
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2007 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.
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.dirname(__FILE__) + '/../test_helper' 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 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
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
63 get :index, :group_id => '10'
64 assert_response :success
65 assert_template 'index'
66 users = assigns(:users)
67 assert users.any?
68 assert_equal([], (users - Group.find(10).users))
69 end
70
62 def test_show 71 def test_show
63 @request.session[:user_id] = nil 72 @request.session[:user_id] = nil
64 get :show, :id => 2 73 get :show, :id => 2
65 assert_response :success 74 assert_response :success
66 assert_template 'show' 75 assert_template 'show'
117 memberships = assigns(:memberships) 126 memberships = assigns(:memberships)
118 assert_not_nil memberships 127 assert_not_nil memberships
119 project_ids = memberships.map(&:project_id) 128 project_ids = memberships.map(&:project_id)
120 assert project_ids.include?(2) #private project admin can see 129 assert project_ids.include?(2) #private project admin can see
121 end 130 end
122 131
123 context "GET :new" do 132 def test_show_current_should_require_authentication
124 setup do 133 @request.session[:user_id] = nil
125 get :new 134 get :show, :id => 'current'
126 end 135 assert_response 302
127 136 end
128 should_assign_to :user 137
129 should_respond_with :success 138 def test_show_current
130 should_render_template :new 139 @request.session[:user_id] = 2
131 end 140 get :show, :id => 'current'
132 141 assert_response :success
133 context "POST :create" do 142 assert_template 'show'
134 context "when successful" do 143 assert_equal User.find(2), assigns(:user)
135 setup do 144 end
136 post :create, :user => { 145
137 :firstname => 'John', 146 def test_new
138 :lastname => 'Doe', 147 get :new
139 :login => 'jdoe', 148
140 :password => 'test', 149 assert_response :success
141 :password_confirmation => 'test', 150 assert_template :new
142 :mail => 'jdoe@gmail.com' 151 assert assigns(:user)
143 }, 152 end
144 :notification_option => 'none' 153
154 def test_create
155 Setting.bcc_recipients = '1'
156
157 assert_difference 'User.count' do
158 assert_difference 'ActionMailer::Base.deliveries.size' do
159 post :create,
160 :user => {
161 :firstname => 'John',
162 :lastname => 'Doe',
163 :login => 'jdoe',
164 :password => 'secret',
165 :password_confirmation => 'secret',
166 :mail => 'jdoe@gmail.com',
167 :mail_notification => 'none'
168 },
169 :send_information => '1'
145 end 170 end
146 171 end
147 should_assign_to :user 172
148 should_respond_with :redirect 173 user = User.first(:order => 'id DESC')
149 should_redirect_to('user edit') { {:controller => 'users', :action => 'edit', :id => User.find_by_login('jdoe')}} 174 assert_redirected_to :controller => 'users', :action => 'edit', :id => user.id
150 175
151 should 'set the users mail notification' do 176 assert_equal 'John', user.firstname
152 user = User.last 177 assert_equal 'Doe', user.lastname
153 assert_equal 'none', user.mail_notification 178 assert_equal 'jdoe', user.login
154 end 179 assert_equal 'jdoe@gmail.com', user.mail
155 end 180 assert_equal 'none', user.mail_notification
156 181 assert user.check_password?('secret')
157 context "when unsuccessful" do 182
158 setup do 183 mail = ActionMailer::Base.deliveries.last
159 post :create, :user => {} 184 assert_not_nil mail
160 end 185 assert_equal [user.mail], mail.bcc
161 186 assert mail.body.include?('secret')
162 should_assign_to :user 187 end
163 should_respond_with :success 188
164 should_render_template :new 189 def test_create_with_failure
165 end 190 assert_no_difference 'User.count' do
166 191 post :create, :user => {}
192 end
193
194 assert_response :success
195 assert_template 'new'
196 end
197
198 def test_edit
199 get :edit, :id => 2
200
201 assert_response :success
202 assert_template 'edit'
203 assert_equal User.find(2), assigns(:user)
167 end 204 end
168 205
169 def test_update 206 def test_update
170 ActionMailer::Base.deliveries.clear 207 ActionMailer::Base.deliveries.clear
171 put :update, :id => 2, :user => {:firstname => 'Changed'}, :notification_option => 'all', :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'}
172 209
173 user = User.find(2) 210 user = User.find(2)
174 assert_equal 'Changed', user.firstname 211 assert_equal 'Changed', user.firstname
175 assert_equal 'all', user.mail_notification 212 assert_equal 'only_assigned', user.mail_notification
176 assert_equal true, user.pref[:hide_mail] 213 assert_equal true, user.pref[:hide_mail]
177 assert_equal 'desc', user.pref[:comments_sorting] 214 assert_equal 'desc', user.pref[:comments_sorting]
178 assert ActionMailer::Base.deliveries.empty? 215 assert ActionMailer::Base.deliveries.empty?
216 end
217
218 def test_update_with_failure
219 assert_no_difference 'User.count' do
220 put :update, :id => 2, :user => {:firstname => ''}
221 end
222
223 assert_response :success
224 assert_template 'edit'
225 end
226
227 def test_update_with_group_ids_should_assign_groups
228 put :update, :id => 2, :user => {:group_ids => ['10']}
229
230 user = User.find(2)
231 assert_equal [10], user.group_ids
179 end 232 end
180 233
181 def test_update_with_activation_should_send_a_notification 234 def test_update_with_activation_should_send_a_notification
182 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')
183 u.login = 'foo' 236 u.login = 'foo'
192 assert_not_nil mail 245 assert_not_nil mail
193 assert_equal ['foo.bar@somenet.foo'], mail.bcc 246 assert_equal ['foo.bar@somenet.foo'], mail.bcc
194 assert mail.body.include?(ll('fr', :notice_account_activated)) 247 assert mail.body.include?(ll('fr', :notice_account_activated))
195 end 248 end
196 249
197 def test_updat_with_password_change_should_send_a_notification 250 def test_update_with_password_change_should_send_a_notification
198 ActionMailer::Base.deliveries.clear 251 ActionMailer::Base.deliveries.clear
199 Setting.bcc_recipients = '1' 252 Setting.bcc_recipients = '1'
200 253
254 put :update, :id => 2, :user => {:password => 'newpass', :password_confirmation => 'newpass'}, :send_information => '1'
201 u = User.find(2) 255 u = User.find(2)
202 put :update, :id => u.id, :user => {}, :password => 'newpass', :password_confirmation => 'newpass', :send_information => '1' 256 assert u.check_password?('newpass')
203 assert_equal User.hash_password('newpass'), u.reload.hashed_password
204 257
205 mail = ActionMailer::Base.deliveries.last 258 mail = ActionMailer::Base.deliveries.last
206 assert_not_nil mail 259 assert_not_nil mail
207 assert_equal [u.mail], mail.bcc 260 assert_equal [u.mail], mail.bcc
208 assert mail.body.include?('newpass') 261 assert mail.body.include?('newpass')
212 # Configure as auth source 265 # Configure as auth source
213 u = User.find(2) 266 u = User.find(2)
214 u.auth_source = AuthSource.find(1) 267 u.auth_source = AuthSource.find(1)
215 u.save! 268 u.save!
216 269
217 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'
218 271
219 assert_equal nil, u.reload.auth_source 272 assert_equal nil, u.reload.auth_source
220 assert_equal User.hash_password('newpass'), u.reload.hashed_password 273 assert u.check_password?('newpass')
274 end
275
276 def test_destroy
277 assert_difference 'User.count', -1 do
278 delete :destroy, :id => 2
279 end
280 assert_redirected_to '/users'
281 assert_nil User.find_by_id(2)
282 end
283
284 def test_destroy_should_not_accept_get_requests
285 assert_no_difference 'User.count' do
286 get :destroy, :id => 2
287 end
288 assert_response 405
289 end
290
291 def test_destroy_should_be_denied_for_non_admin_users
292 @request.session[:user_id] = 3
293
294 assert_no_difference 'User.count' do
295 get :destroy, :id => 2
296 end
297 assert_response 403
221 end 298 end
222 299
223 def test_edit_membership 300 def test_edit_membership
224 post :edit_membership, :id => 2, :membership_id => 1, 301 post :edit_membership, :id => 2, :membership_id => 1,
225 :membership => { :role_ids => [2]} 302 :membership => { :role_ids => [2]}