@@ -1,319 +1,344 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class AccountControllerTest < ActionController::TestCase |
|
21 | 21 | fixtures :users, :roles |
|
22 | 22 | |
|
23 | 23 | def setup |
|
24 | 24 | User.current = nil |
|
25 | 25 | end |
|
26 | 26 | |
|
27 | 27 | def test_get_login |
|
28 | 28 | get :login |
|
29 | 29 | assert_response :success |
|
30 | 30 | assert_template 'login' |
|
31 | 31 | |
|
32 | 32 | assert_select 'input[name=username]' |
|
33 | 33 | assert_select 'input[name=password]' |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | def test_get_login_while_logged_in_should_redirect_to_home |
|
37 | 37 | @request.session[:user_id] = 2 |
|
38 | 38 | |
|
39 | 39 | get :login |
|
40 | 40 | assert_redirected_to '/' |
|
41 | 41 | assert_equal 2, @request.session[:user_id] |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def test_login_should_redirect_to_back_url_param |
|
45 | 45 | # request.uri is "test.host" in test environment |
|
46 | 46 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1' |
|
47 | 47 | assert_redirected_to '/issues/show/1' |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def test_login_should_not_redirect_to_another_host |
|
51 | 51 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake' |
|
52 | 52 | assert_redirected_to '/my/page' |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def test_login_with_wrong_password |
|
56 | 56 | post :login, :username => 'admin', :password => 'bad' |
|
57 | 57 | assert_response :success |
|
58 | 58 | assert_template 'login' |
|
59 | 59 | |
|
60 | 60 | assert_select 'div.flash.error', :text => /Invalid user or password/ |
|
61 | 61 | assert_select 'input[name=username][value=admin]' |
|
62 | 62 | assert_select 'input[name=password]' |
|
63 | 63 | assert_select 'input[name=password][value]', 0 |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def test_login_with_locked_account_should_fail |
|
67 | 67 | User.find(2).update_attribute :status, User::STATUS_LOCKED |
|
68 | 68 | |
|
69 | 69 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
70 | 70 | assert_redirected_to '/login' |
|
71 | 71 | assert_include 'locked', flash[:error] |
|
72 | 72 | assert_nil @request.session[:user_id] |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | def test_login_as_registered_user_with_manual_activation_should_inform_user |
|
76 | 76 | User.find(2).update_attribute :status, User::STATUS_REGISTERED |
|
77 | 77 | |
|
78 | 78 | with_settings :self_registration => '2', :default_language => 'en' do |
|
79 | 79 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
80 | 80 | assert_redirected_to '/login' |
|
81 | 81 | assert_include 'pending administrator approval', flash[:error] |
|
82 | 82 | end |
|
83 | 83 | end |
|
84 | 84 | |
|
85 | 85 | def test_login_as_registered_user_with_email_activation_should_propose_new_activation_email |
|
86 | 86 | User.find(2).update_attribute :status, User::STATUS_REGISTERED |
|
87 | 87 | |
|
88 | 88 | with_settings :self_registration => '1', :default_language => 'en' do |
|
89 | 89 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
90 | 90 | assert_redirected_to '/login' |
|
91 | 91 | assert_equal 2, @request.session[:registered_user_id] |
|
92 | 92 | assert_include 'new activation email', flash[:error] |
|
93 | 93 | end |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def test_login_should_rescue_auth_source_exception |
|
97 | 97 | source = AuthSource.create!(:name => 'Test') |
|
98 | 98 | User.find(2).update_attribute :auth_source_id, source.id |
|
99 | 99 | AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) |
|
100 | 100 | |
|
101 | 101 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
102 | 102 | assert_response 500 |
|
103 | 103 | assert_error_tag :content => /Something wrong/ |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | def test_login_should_reset_session |
|
107 | 107 | @controller.expects(:reset_session).once |
|
108 | 108 | |
|
109 | 109 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
110 | 110 | assert_response 302 |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | def test_get_logout_should_not_logout |
|
114 | 114 | @request.session[:user_id] = 2 |
|
115 | 115 | get :logout |
|
116 | 116 | assert_response :success |
|
117 | 117 | assert_template 'logout' |
|
118 | 118 | |
|
119 | 119 | assert_equal 2, @request.session[:user_id] |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | def test_get_logout_with_anonymous_should_redirect | |
|
123 | get :logout | |
|
124 | assert_redirected_to '/' | |
|
125 | end | |
|
126 | ||
|
122 | 127 | def test_logout |
|
123 | 128 | @request.session[:user_id] = 2 |
|
124 | 129 | post :logout |
|
125 | 130 | assert_redirected_to '/' |
|
126 | 131 | assert_nil @request.session[:user_id] |
|
127 | 132 | end |
|
128 | 133 | |
|
129 | 134 | def test_logout_should_reset_session |
|
130 | 135 | @controller.expects(:reset_session).once |
|
131 | 136 | |
|
132 | 137 | @request.session[:user_id] = 2 |
|
133 | 138 | post :logout |
|
134 | 139 | assert_response 302 |
|
135 | 140 | end |
|
136 | 141 | |
|
137 | 142 | def test_get_register_with_registration_on |
|
138 | 143 | with_settings :self_registration => '3' do |
|
139 | 144 | get :register |
|
140 | 145 | assert_response :success |
|
141 | 146 | assert_template 'register' |
|
142 | 147 | assert_not_nil assigns(:user) |
|
143 | 148 | |
|
144 | 149 | assert_select 'input[name=?]', 'user[password]' |
|
145 | 150 | assert_select 'input[name=?]', 'user[password_confirmation]' |
|
146 | 151 | end |
|
147 | 152 | end |
|
148 | 153 | |
|
149 | 154 | def test_get_register_should_detect_user_language |
|
150 | 155 | with_settings :self_registration => '3' do |
|
151 | 156 | @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' |
|
152 | 157 | get :register |
|
153 | 158 | assert_response :success |
|
154 | 159 | assert_not_nil assigns(:user) |
|
155 | 160 | assert_equal 'fr', assigns(:user).language |
|
156 | 161 | assert_select 'select[name=?]', 'user[language]' do |
|
157 | 162 | assert_select 'option[value=fr][selected=selected]' |
|
158 | 163 | end |
|
159 | 164 | end |
|
160 | 165 | end |
|
161 | 166 | |
|
162 | 167 | def test_get_register_with_registration_off_should_redirect |
|
163 | 168 | with_settings :self_registration => '0' do |
|
164 | 169 | get :register |
|
165 | 170 | assert_redirected_to '/' |
|
166 | 171 | end |
|
167 | 172 | end |
|
168 | 173 | |
|
169 | 174 | # See integration/account_test.rb for the full test |
|
170 | 175 | def test_post_register_with_registration_on |
|
171 | 176 | with_settings :self_registration => '3' do |
|
172 | 177 | assert_difference 'User.count' do |
|
173 | 178 | post :register, :user => { |
|
174 | 179 | :login => 'register', |
|
175 | 180 | :password => 'secret123', |
|
176 | 181 | :password_confirmation => 'secret123', |
|
177 | 182 | :firstname => 'John', |
|
178 | 183 | :lastname => 'Doe', |
|
179 | 184 | :mail => 'register@example.com' |
|
180 | 185 | } |
|
181 | 186 | assert_redirected_to '/my/account' |
|
182 | 187 | end |
|
183 | 188 | user = User.first(:order => 'id DESC') |
|
184 | 189 | assert_equal 'register', user.login |
|
185 | 190 | assert_equal 'John', user.firstname |
|
186 | 191 | assert_equal 'Doe', user.lastname |
|
187 | 192 | assert_equal 'register@example.com', user.mail |
|
188 | 193 | assert user.check_password?('secret123') |
|
189 | 194 | assert user.active? |
|
190 | 195 | end |
|
191 | 196 | end |
|
192 | 197 | |
|
193 | 198 | def test_post_register_with_registration_off_should_redirect |
|
194 | 199 | with_settings :self_registration => '0' do |
|
195 | 200 | assert_no_difference 'User.count' do |
|
196 | 201 | post :register, :user => { |
|
197 | 202 | :login => 'register', |
|
198 | 203 | :password => 'test', |
|
199 | 204 | :password_confirmation => 'test', |
|
200 | 205 | :firstname => 'John', |
|
201 | 206 | :lastname => 'Doe', |
|
202 | 207 | :mail => 'register@example.com' |
|
203 | 208 | } |
|
204 | 209 | assert_redirected_to '/' |
|
205 | 210 | end |
|
206 | 211 | end |
|
207 | 212 | end |
|
208 | 213 | |
|
209 | 214 | def test_get_lost_password_should_display_lost_password_form |
|
210 | 215 | get :lost_password |
|
211 | 216 | assert_response :success |
|
212 | 217 | assert_select 'input[name=mail]' |
|
213 | 218 | end |
|
214 | 219 | |
|
215 | 220 | def test_lost_password_for_active_user_should_create_a_token |
|
216 | 221 | Token.delete_all |
|
217 | 222 | ActionMailer::Base.deliveries.clear |
|
218 | 223 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
219 | 224 | assert_difference 'Token.count' do |
|
220 | 225 | with_settings :host_name => 'mydomain.foo', :protocol => 'http' do |
|
221 | 226 | post :lost_password, :mail => 'JSmith@somenet.foo' |
|
222 | 227 | assert_redirected_to '/login' |
|
223 | 228 | end |
|
224 | 229 | end |
|
225 | 230 | end |
|
226 | 231 | |
|
227 | 232 | token = Token.order('id DESC').first |
|
228 | 233 | assert_equal User.find(2), token.user |
|
229 | 234 | assert_equal 'recovery', token.action |
|
230 | 235 | |
|
231 | 236 | assert_select_email do |
|
232 | 237 | assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}" |
|
233 | 238 | end |
|
234 | 239 | end |
|
235 | 240 | |
|
236 | 241 | def test_lost_password_for_unknown_user_should_fail |
|
237 | 242 | Token.delete_all |
|
238 | 243 | assert_no_difference 'Token.count' do |
|
239 | 244 | post :lost_password, :mail => 'invalid@somenet.foo' |
|
240 | 245 | assert_response :success |
|
241 | 246 | end |
|
242 | 247 | end |
|
243 | 248 | |
|
244 | 249 | def test_lost_password_for_non_active_user_should_fail |
|
245 | 250 | Token.delete_all |
|
246 | 251 | assert User.find(2).lock! |
|
247 | 252 | |
|
248 | 253 | assert_no_difference 'Token.count' do |
|
249 | 254 | post :lost_password, :mail => 'JSmith@somenet.foo' |
|
250 | 255 | assert_redirected_to '/account/lost_password' |
|
251 | 256 | end |
|
252 | 257 | end |
|
253 | 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 | ||
|
254 | 268 | def test_get_lost_password_with_token_should_display_the_password_recovery_form |
|
255 | 269 | user = User.find(2) |
|
256 | 270 | token = Token.create!(:action => 'recovery', :user => user) |
|
257 | 271 | |
|
258 | 272 | get :lost_password, :token => token.value |
|
259 | 273 | assert_response :success |
|
260 | 274 | assert_template 'password_recovery' |
|
261 | 275 | |
|
262 | 276 | assert_select 'input[type=hidden][name=token][value=?]', token.value |
|
263 | 277 | end |
|
264 | 278 | |
|
265 | 279 | def test_get_lost_password_with_invalid_token_should_redirect |
|
266 | 280 | get :lost_password, :token => "abcdef" |
|
267 | 281 | assert_redirected_to '/' |
|
268 | 282 | end |
|
269 | 283 | |
|
270 | 284 | def test_post_lost_password_with_token_should_change_the_user_password |
|
271 | 285 | user = User.find(2) |
|
272 | 286 | token = Token.create!(:action => 'recovery', :user => user) |
|
273 | 287 | |
|
274 | 288 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' |
|
275 | 289 | assert_redirected_to '/login' |
|
276 | 290 | user.reload |
|
277 | 291 | assert user.check_password?('newpass123') |
|
278 | 292 | assert_nil Token.find_by_id(token.id), "Token was not deleted" |
|
279 | 293 | end |
|
280 | 294 | |
|
281 | 295 | def test_post_lost_password_with_token_for_non_active_user_should_fail |
|
282 | 296 | user = User.find(2) |
|
283 | 297 | token = Token.create!(:action => 'recovery', :user => user) |
|
284 | 298 | user.lock! |
|
285 | 299 | |
|
286 | 300 | post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' |
|
287 | 301 | assert_redirected_to '/' |
|
288 | 302 | assert ! user.check_password?('newpass123') |
|
289 | 303 | end |
|
290 | 304 | |
|
291 | 305 | def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form |
|
292 | 306 | user = User.find(2) |
|
293 | 307 | token = Token.create!(:action => 'recovery', :user => user) |
|
294 | 308 | |
|
295 | 309 | post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass' |
|
296 | 310 | assert_response :success |
|
297 | 311 | assert_template 'password_recovery' |
|
298 | 312 | assert_not_nil Token.find_by_id(token.id), "Token was deleted" |
|
299 | 313 | |
|
300 | 314 | assert_select 'input[type=hidden][name=token][value=?]', token.value |
|
301 | 315 | end |
|
302 | 316 | |
|
303 | 317 | def test_post_lost_password_with_invalid_token_should_redirect |
|
304 | 318 | post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass' |
|
305 | 319 | assert_redirected_to '/' |
|
306 | 320 | end |
|
307 | 321 | |
|
308 | 322 | def test_activation_email_should_send_an_activation_email |
|
309 | 323 | User.find(2).update_attribute :status, User::STATUS_REGISTERED |
|
310 | 324 | @request.session[:registered_user_id] = 2 |
|
311 | 325 | |
|
312 | 326 | with_settings :self_registration => '1' do |
|
313 | 327 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
314 | 328 | get :activation_email |
|
315 | 329 | assert_redirected_to '/login' |
|
316 | 330 | end |
|
317 | 331 | end |
|
318 | 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 | |
|
319 | 344 | end |
General Comments 0
You need to be logged in to leave comments.
Login now