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