Mercurial > hg > soundsoftware-site
comparison .svn/pristine/28/28a718992fc9b78318367f1ea258610932c7f8f4.svn-base @ 1516:b450a9d58aed redmine-2.4
Update to Redmine SVN revision 13356 on 2.4-stable branch
author | Chris Cannam |
---|---|
date | Tue, 09 Sep 2014 09:28:31 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1494:e248c7af89ec | 1516:b450a9d58aed |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2014 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 AccountControllerTest < ActionController::TestCase | |
21 fixtures :users, :roles | |
22 | |
23 def setup | |
24 User.current = nil | |
25 end | |
26 | |
27 def test_get_login | |
28 get :login | |
29 assert_response :success | |
30 assert_template 'login' | |
31 | |
32 assert_select 'input[name=username]' | |
33 assert_select 'input[name=password]' | |
34 end | |
35 | |
36 def test_get_login_while_logged_in_should_redirect_to_home | |
37 @request.session[:user_id] = 2 | |
38 | |
39 get :login | |
40 assert_redirected_to '/' | |
41 assert_equal 2, @request.session[:user_id] | |
42 end | |
43 | |
44 def test_login_should_redirect_to_back_url_param | |
45 # request.uri is "test.host" in test environment | |
46 back_urls = [ | |
47 'http://test.host/issues/show/1', | |
48 '/' | |
49 ] | |
50 back_urls.each do |back_url| | |
51 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url | |
52 assert_redirected_to back_url | |
53 end | |
54 end | |
55 | |
56 def test_login_with_suburi_should_redirect_to_back_url_param | |
57 @relative_url_root = ApplicationController.relative_url_root | |
58 ApplicationController.relative_url_root = '/redmine' | |
59 | |
60 back_urls = [ | |
61 'http://test.host/redmine/issues/show/1', | |
62 '/redmine' | |
63 ] | |
64 back_urls.each do |back_url| | |
65 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url | |
66 assert_redirected_to back_url | |
67 end | |
68 ensure | |
69 ApplicationController.relative_url_root = @relative_url_root | |
70 end | |
71 | |
72 def test_login_should_not_redirect_to_another_host | |
73 back_urls = [ | |
74 'http://test.foo/fake', | |
75 '//test.foo/fake' | |
76 ] | |
77 back_urls.each do |back_url| | |
78 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url | |
79 assert_redirected_to '/my/page' | |
80 end | |
81 end | |
82 | |
83 def test_login_with_suburi_should_not_redirect_to_another_suburi | |
84 @relative_url_root = ApplicationController.relative_url_root | |
85 ApplicationController.relative_url_root = '/redmine' | |
86 | |
87 back_urls = [ | |
88 'http://test.host/', | |
89 'http://test.host/fake', | |
90 'http://test.host/fake/issues', | |
91 'http://test.host/redmine/../fake', | |
92 'http://test.host/redmine/../fake/issues', | |
93 'http://test.host/redmine/%2e%2e/fake' | |
94 ] | |
95 back_urls.each do |back_url| | |
96 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url | |
97 assert_redirected_to '/my/page' | |
98 end | |
99 ensure | |
100 ApplicationController.relative_url_root = @relative_url_root | |
101 end | |
102 | |
103 def test_login_with_wrong_password | |
104 post :login, :username => 'admin', :password => 'bad' | |
105 assert_response :success | |
106 assert_template 'login' | |
107 | |
108 assert_select 'div.flash.error', :text => /Invalid user or password/ | |
109 assert_select 'input[name=username][value=admin]' | |
110 assert_select 'input[name=password]' | |
111 assert_select 'input[name=password][value]', 0 | |
112 end | |
113 | |
114 def test_login_with_locked_account_should_fail | |
115 User.find(2).update_attribute :status, User::STATUS_LOCKED | |
116 | |
117 post :login, :username => 'jsmith', :password => 'jsmith' | |
118 assert_redirected_to '/login' | |
119 assert_include 'locked', flash[:error] | |
120 assert_nil @request.session[:user_id] | |
121 end | |
122 | |
123 def test_login_as_registered_user_with_manual_activation_should_inform_user | |
124 User.find(2).update_attribute :status, User::STATUS_REGISTERED | |
125 | |
126 with_settings :self_registration => '2', :default_language => 'en' do | |
127 post :login, :username => 'jsmith', :password => 'jsmith' | |
128 assert_redirected_to '/login' | |
129 assert_include 'pending administrator approval', flash[:error] | |
130 end | |
131 end | |
132 | |
133 def test_login_as_registered_user_with_email_activation_should_propose_new_activation_email | |
134 User.find(2).update_attribute :status, User::STATUS_REGISTERED | |
135 | |
136 with_settings :self_registration => '1', :default_language => 'en' do | |
137 post :login, :username => 'jsmith', :password => 'jsmith' | |
138 assert_redirected_to '/login' | |
139 assert_equal 2, @request.session[:registered_user_id] | |
140 assert_include 'new activation email', flash[:error] | |
141 end | |
142 end | |
143 | |
144 def test_login_should_rescue_auth_source_exception | |
145 source = AuthSource.create!(:name => 'Test') | |
146 User.find(2).update_attribute :auth_source_id, source.id | |
147 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) | |
148 | |
149 post :login, :username => 'jsmith', :password => 'jsmith' | |
150 assert_response 500 | |
151 assert_error_tag :content => /Something wrong/ | |
152 end | |
153 | |
154 def test_login_should_reset_session | |
155 @controller.expects(:reset_session).once | |
156 | |
157 post :login, :username => 'jsmith', :password => 'jsmith' | |
158 assert_response 302 | |
159 end | |
160 | |
161 def test_get_logout_should_not_logout | |
162 @request.session[:user_id] = 2 | |
163 get :logout | |
164 assert_response :success | |
165 assert_template 'logout' | |
166 | |
167 assert_equal 2, @request.session[:user_id] | |
168 end | |
169 | |
170 def test_get_logout_with_anonymous_should_redirect | |
171 get :logout | |
172 assert_redirected_to '/' | |
173 end | |
174 | |
175 def test_logout | |
176 @request.session[:user_id] = 2 | |
177 post :logout | |
178 assert_redirected_to '/' | |
179 assert_nil @request.session[:user_id] | |
180 end | |
181 | |
182 def test_logout_should_reset_session | |
183 @controller.expects(:reset_session).once | |
184 | |
185 @request.session[:user_id] = 2 | |
186 post :logout | |
187 assert_response 302 | |
188 end | |
189 | |
190 def test_get_register_with_registration_on | |
191 with_settings :self_registration => '3' do | |
192 get :register | |
193 assert_response :success | |
194 assert_template 'register' | |
195 assert_not_nil assigns(:user) | |
196 | |
197 assert_select 'input[name=?]', 'user[password]' | |
198 assert_select 'input[name=?]', 'user[password_confirmation]' | |
199 end | |
200 end | |
201 | |
202 def test_get_register_should_detect_user_language | |
203 with_settings :self_registration => '3' do | |
204 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' | |
205 get :register | |
206 assert_response :success | |
207 assert_not_nil assigns(:user) | |
208 assert_equal 'fr', assigns(:user).language | |
209 assert_select 'select[name=?]', 'user[language]' do | |
210 assert_select 'option[value=fr][selected=selected]' | |
211 end | |
212 end | |
213 end | |
214 | |
215 def test_get_register_with_registration_off_should_redirect | |
216 with_settings :self_registration => '0' do | |
217 get :register | |
218 assert_redirected_to '/' | |
219 end | |
220 end | |
221 | |
222 # See integration/account_test.rb for the full test | |
223 def test_post_register_with_registration_on | |
224 with_settings :self_registration => '3' do | |
225 assert_difference 'User.count' do | |
226 post :register, :user => { | |
227 :login => 'register', | |
228 :password => 'secret123', | |
229 :password_confirmation => 'secret123', | |
230 :firstname => 'John', | |
231 :lastname => 'Doe', | |
232 :mail => 'register@example.com' | |
233 } | |
234 assert_redirected_to '/my/account' | |
235 end | |
236 user = User.first(:order => 'id DESC') | |
237 assert_equal 'register', user.login | |
238 assert_equal 'John', user.firstname | |
239 assert_equal 'Doe', user.lastname | |
240 assert_equal 'register@example.com', user.mail | |
241 assert user.check_password?('secret123') | |
242 assert user.active? | |
243 end | |
244 end | |
245 | |
246 def test_post_register_with_registration_off_should_redirect | |
247 with_settings :self_registration => '0' do | |
248 assert_no_difference 'User.count' do | |
249 post :register, :user => { | |
250 :login => 'register', | |
251 :password => 'test', | |
252 :password_confirmation => 'test', | |
253 :firstname => 'John', | |
254 :lastname => 'Doe', | |
255 :mail => 'register@example.com' | |
256 } | |
257 assert_redirected_to '/' | |
258 end | |
259 end | |
260 end | |
261 | |
262 def test_get_lost_password_should_display_lost_password_form | |
263 get :lost_password | |
264 assert_response :success | |
265 assert_select 'input[name=mail]' | |
266 end | |
267 | |
268 def test_lost_password_for_active_user_should_create_a_token | |
269 Token.delete_all | |
270 ActionMailer::Base.deliveries.clear | |
271 assert_difference 'ActionMailer::Base.deliveries.size' do | |
272 assert_difference 'Token.count' do | |
273 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do | |
274 post :lost_password, :mail => 'JSmith@somenet.foo' | |
275 assert_redirected_to '/login' | |
276 end | |
277 end | |
278 end | |
279 | |
280 token = Token.order('id DESC').first | |
281 assert_equal User.find(2), token.user | |
282 assert_equal 'recovery', token.action | |
283 | |
284 assert_select_email do | |
285 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}" | |
286 end | |
287 end | |
288 | |
289 def test_lost_password_for_unknown_user_should_fail | |
290 Token.delete_all | |
291 assert_no_difference 'Token.count' do | |
292 post :lost_password, :mail => 'invalid@somenet.foo' | |
293 assert_response :success | |
294 end | |
295 end | |
296 | |
297 def test_lost_password_for_non_active_user_should_fail | |
298 Token.delete_all | |
299 assert User.find(2).lock! | |
300 | |
301 assert_no_difference 'Token.count' do | |
302 post :lost_password, :mail => 'JSmith@somenet.foo' | |
303 assert_redirected_to '/account/lost_password' | |
304 end | |
305 end | |
306 | |
307 def test_lost_password_for_user_who_cannot_change_password_should_fail | |
308 User.any_instance.stubs(:change_password_allowed?).returns(false) | |
309 | |
310 assert_no_difference 'Token.count' do | |
311 post :lost_password, :mail => 'JSmith@somenet.foo' | |
312 assert_response :success | |
313 end | |
314 end | |
315 | |
316 def test_get_lost_password_with_token_should_display_the_password_recovery_form | |
317 user = User.find(2) | |
318 token = Token.create!(:action => 'recovery', :user => user) | |
319 | |
320 get :lost_password, :token => token.value | |
321 assert_response :success | |
322 assert_template 'password_recovery' | |
323 | |
324 assert_select 'input[type=hidden][name=token][value=?]', token.value | |
325 end | |
326 | |
327 def test_get_lost_password_with_invalid_token_should_redirect | |
328 get :lost_password, :token => "abcdef" | |
329 assert_redirected_to '/' | |
330 end | |
331 | |
332 def test_post_lost_password_with_token_should_change_the_user_password | |
333 user = User.find(2) | |
334 token = Token.create!(:action => 'recovery', :user => user) | |
335 | |
336 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' | |
337 assert_redirected_to '/login' | |
338 user.reload | |
339 assert user.check_password?('newpass123') | |
340 assert_nil Token.find_by_id(token.id), "Token was not deleted" | |
341 end | |
342 | |
343 def test_post_lost_password_with_token_for_non_active_user_should_fail | |
344 user = User.find(2) | |
345 token = Token.create!(:action => 'recovery', :user => user) | |
346 user.lock! | |
347 | |
348 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' | |
349 assert_redirected_to '/' | |
350 assert ! user.check_password?('newpass123') | |
351 end | |
352 | |
353 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form | |
354 user = User.find(2) | |
355 token = Token.create!(:action => 'recovery', :user => user) | |
356 | |
357 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass' | |
358 assert_response :success | |
359 assert_template 'password_recovery' | |
360 assert_not_nil Token.find_by_id(token.id), "Token was deleted" | |
361 | |
362 assert_select 'input[type=hidden][name=token][value=?]', token.value | |
363 end | |
364 | |
365 def test_post_lost_password_with_invalid_token_should_redirect | |
366 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass' | |
367 assert_redirected_to '/' | |
368 end | |
369 | |
370 def test_activation_email_should_send_an_activation_email | |
371 User.find(2).update_attribute :status, User::STATUS_REGISTERED | |
372 @request.session[:registered_user_id] = 2 | |
373 | |
374 with_settings :self_registration => '1' do | |
375 assert_difference 'ActionMailer::Base.deliveries.size' do | |
376 get :activation_email | |
377 assert_redirected_to '/login' | |
378 end | |
379 end | |
380 end | |
381 | |
382 def test_activation_email_without_session_data_should_fail | |
383 User.find(2).update_attribute :status, User::STATUS_REGISTERED | |
384 | |
385 with_settings :self_registration => '1' do | |
386 assert_no_difference 'ActionMailer::Base.deliveries.size' do | |
387 get :activation_email | |
388 assert_redirected_to '/' | |
389 end | |
390 end | |
391 end | |
392 end |