@@ -1,346 +1,349 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 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 AccountTest < Redmine::IntegrationTest |
|
21 | 21 | fixtures :users, :email_addresses, :roles |
|
22 | 22 | |
|
23 | 23 | def test_login |
|
24 | 24 | get "/my/page" |
|
25 | 25 | assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage" |
|
26 | 26 | log_user('jsmith', 'jsmith') |
|
27 | 27 | |
|
28 | 28 | get "/my/account" |
|
29 | 29 | assert_response :success |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | def test_login_should_set_session_token |
|
33 | 33 | assert_difference 'Token.count' do |
|
34 | 34 | log_user('jsmith', 'jsmith') |
|
35 | 35 | |
|
36 | 36 | assert_equal 2, session[:user_id] |
|
37 | 37 | assert_not_nil session[:tk] |
|
38 | 38 | end |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def test_autologin |
|
42 | 42 | user = User.find(1) |
|
43 | 43 | Token.delete_all |
|
44 | 44 | |
|
45 | 45 | with_settings :autologin => '7' do |
|
46 | 46 | assert_difference 'Token.count', 2 do |
|
47 | 47 | # User logs in with 'autologin' checked |
|
48 | 48 | post '/login', :username => user.login, :password => 'admin', :autologin => 1 |
|
49 | 49 | assert_redirected_to '/my/page' |
|
50 | 50 | end |
|
51 | 51 | token = Token.where(:action => 'autologin').order(:id => :desc).first |
|
52 | 52 | assert_not_nil token |
|
53 | 53 | assert_equal user, token.user |
|
54 | 54 | assert_equal 'autologin', token.action |
|
55 | 55 | assert_equal user.id, session[:user_id] |
|
56 | 56 | assert_equal token.value, cookies['autologin'] |
|
57 | 57 | |
|
58 | 58 | # Session is cleared |
|
59 | 59 | reset! |
|
60 | 60 | User.current = nil |
|
61 | 61 | # Clears user's last login timestamp |
|
62 | 62 | user.update_attribute :last_login_on, nil |
|
63 | 63 | assert_nil user.reload.last_login_on |
|
64 | 64 | |
|
65 | 65 | # User comes back with user's autologin cookie |
|
66 | 66 | cookies[:autologin] = token.value |
|
67 | 67 | get '/my/page' |
|
68 | 68 | assert_response :success |
|
69 | 69 | assert_equal user.id, session[:user_id] |
|
70 | 70 | assert_not_nil user.reload.last_login_on |
|
71 | 71 | end |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def test_autologin_should_use_autologin_cookie_name |
|
75 | 75 | Token.delete_all |
|
76 | 76 | Redmine::Configuration.stubs(:[]).with('autologin_cookie_name').returns('custom_autologin') |
|
77 | 77 | Redmine::Configuration.stubs(:[]).with('autologin_cookie_path').returns('/') |
|
78 | 78 | Redmine::Configuration.stubs(:[]).with('autologin_cookie_secure').returns(false) |
|
79 | 79 | Redmine::Configuration.stubs(:[]).with('sudo_mode_timeout').returns(15) |
|
80 | 80 | |
|
81 | 81 | with_settings :autologin => '7' do |
|
82 | 82 | assert_difference 'Token.count', 2 do |
|
83 | 83 | post '/login', :username => 'admin', :password => 'admin', :autologin => 1 |
|
84 | 84 | assert_response 302 |
|
85 | 85 | end |
|
86 | 86 | assert cookies['custom_autologin'].present? |
|
87 | 87 | token = cookies['custom_autologin'] |
|
88 | 88 | |
|
89 | 89 | # Session is cleared |
|
90 | 90 | reset! |
|
91 | 91 | cookies['custom_autologin'] = token |
|
92 | 92 | get '/my/page' |
|
93 | 93 | assert_response :success |
|
94 | 94 | |
|
95 | 95 | assert_difference 'Token.count', -2 do |
|
96 | 96 | post '/logout' |
|
97 | 97 | end |
|
98 | 98 | assert cookies['custom_autologin'].blank? |
|
99 | 99 | end |
|
100 | 100 | end |
|
101 | 101 | |
|
102 | 102 | def test_lost_password |
|
103 | 103 | Token.delete_all |
|
104 | 104 | |
|
105 | 105 | get "/account/lost_password" |
|
106 | 106 | assert_response :success |
|
107 | 107 | assert_select 'input[name=mail]' |
|
108 | 108 | |
|
109 | 109 | post "/account/lost_password", :mail => 'jSmith@somenet.foo' |
|
110 | 110 | assert_redirected_to "/login" |
|
111 | 111 | |
|
112 | 112 | token = Token.first |
|
113 | 113 | assert_equal 'recovery', token.action |
|
114 | 114 | assert_equal 'jsmith@somenet.foo', token.user.mail |
|
115 | 115 | assert !token.expired? |
|
116 | 116 | |
|
117 | 117 | get "/account/lost_password", :token => token.value |
|
118 | assert_redirected_to '/account/lost_password' | |
|
119 | ||
|
120 | follow_redirect! | |
|
118 | 121 | assert_response :success |
|
119 | 122 | assert_select 'input[type=hidden][name=token][value=?]', token.value |
|
120 | 123 | assert_select 'input[name=new_password]' |
|
121 | 124 | assert_select 'input[name=new_password_confirmation]' |
|
122 | 125 | |
|
123 | 126 | post "/account/lost_password", |
|
124 | 127 | :token => token.value, :new_password => 'newpass123', |
|
125 | 128 | :new_password_confirmation => 'newpass123' |
|
126 | 129 | assert_redirected_to "/login" |
|
127 | 130 | assert_equal 'Password was successfully updated.', flash[:notice] |
|
128 | 131 | |
|
129 | 132 | log_user('jsmith', 'newpass123') |
|
130 | 133 | assert_equal false, Token.exists?(token.id), "Password recovery token was not deleted" |
|
131 | 134 | end |
|
132 | 135 | |
|
133 | 136 | def test_user_with_must_change_passwd_should_be_forced_to_change_its_password |
|
134 | 137 | User.find_by_login('jsmith').update_attribute :must_change_passwd, true |
|
135 | 138 | |
|
136 | 139 | post '/login', :username => 'jsmith', :password => 'jsmith' |
|
137 | 140 | assert_redirected_to '/my/page' |
|
138 | 141 | follow_redirect! |
|
139 | 142 | assert_redirected_to '/my/password' |
|
140 | 143 | |
|
141 | 144 | get '/issues' |
|
142 | 145 | assert_redirected_to '/my/password' |
|
143 | 146 | end |
|
144 | 147 | |
|
145 | 148 | def test_flash_message_should_use_user_language_when_redirecting_user_for_password_change |
|
146 | 149 | user = User.find_by_login('jsmith') |
|
147 | 150 | user.must_change_passwd = true |
|
148 | 151 | user.language = 'it' |
|
149 | 152 | user.save! |
|
150 | 153 | |
|
151 | 154 | post '/login', :username => 'jsmith', :password => 'jsmith' |
|
152 | 155 | assert_redirected_to '/my/page' |
|
153 | 156 | follow_redirect! |
|
154 | 157 | assert_redirected_to '/my/password' |
|
155 | 158 | follow_redirect! |
|
156 | 159 | |
|
157 | 160 | assert_select 'div.error', :text => /richiesto che sia cambiata/ |
|
158 | 161 | end |
|
159 | 162 | |
|
160 | 163 | def test_user_with_must_change_passwd_should_be_able_to_change_its_password |
|
161 | 164 | User.find_by_login('jsmith').update_attribute :must_change_passwd, true |
|
162 | 165 | |
|
163 | 166 | post '/login', :username => 'jsmith', :password => 'jsmith' |
|
164 | 167 | assert_redirected_to '/my/page' |
|
165 | 168 | follow_redirect! |
|
166 | 169 | assert_redirected_to '/my/password' |
|
167 | 170 | follow_redirect! |
|
168 | 171 | assert_response :success |
|
169 | 172 | post '/my/password', :password => 'jsmith', :new_password => 'newpassword', :new_password_confirmation => 'newpassword' |
|
170 | 173 | assert_redirected_to '/my/account' |
|
171 | 174 | follow_redirect! |
|
172 | 175 | assert_response :success |
|
173 | 176 | |
|
174 | 177 | assert_equal false, User.find_by_login('jsmith').must_change_passwd? |
|
175 | 178 | end |
|
176 | 179 | |
|
177 | 180 | def test_user_with_expired_password_should_be_forced_to_change_its_password |
|
178 | 181 | User.find_by_login('jsmith').update_attribute :passwd_changed_on, 14.days.ago |
|
179 | 182 | |
|
180 | 183 | with_settings :password_max_age => 7 do |
|
181 | 184 | post '/login', :username => 'jsmith', :password => 'jsmith' |
|
182 | 185 | assert_redirected_to '/my/page' |
|
183 | 186 | follow_redirect! |
|
184 | 187 | assert_redirected_to '/my/password' |
|
185 | 188 | |
|
186 | 189 | get '/issues' |
|
187 | 190 | assert_redirected_to '/my/password' |
|
188 | 191 | end |
|
189 | 192 | end |
|
190 | 193 | |
|
191 | 194 | def test_user_with_expired_password_should_be_able_to_change_its_password |
|
192 | 195 | User.find_by_login('jsmith').update_attribute :passwd_changed_on, 14.days.ago |
|
193 | 196 | |
|
194 | 197 | with_settings :password_max_age => 7 do |
|
195 | 198 | post '/login', :username => 'jsmith', :password => 'jsmith' |
|
196 | 199 | assert_redirected_to '/my/page' |
|
197 | 200 | follow_redirect! |
|
198 | 201 | assert_redirected_to '/my/password' |
|
199 | 202 | follow_redirect! |
|
200 | 203 | assert_response :success |
|
201 | 204 | post '/my/password', :password => 'jsmith', :new_password => 'newpassword', :new_password_confirmation => 'newpassword' |
|
202 | 205 | assert_redirected_to '/my/account' |
|
203 | 206 | follow_redirect! |
|
204 | 207 | assert_response :success |
|
205 | 208 | |
|
206 | 209 | assert_equal false, User.find_by_login('jsmith').must_change_passwd? |
|
207 | 210 | end |
|
208 | 211 | |
|
209 | 212 | end |
|
210 | 213 | |
|
211 | 214 | def test_register_with_automatic_activation |
|
212 | 215 | Setting.self_registration = '3' |
|
213 | 216 | |
|
214 | 217 | get '/account/register' |
|
215 | 218 | assert_response :success |
|
216 | 219 | |
|
217 | 220 | post '/account/register', |
|
218 | 221 | :user => {:login => "newuser", :language => "en", |
|
219 | 222 | :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar", |
|
220 | 223 | :password => "newpass123", :password_confirmation => "newpass123"} |
|
221 | 224 | assert_redirected_to '/my/account' |
|
222 | 225 | follow_redirect! |
|
223 | 226 | assert_response :success |
|
224 | 227 | |
|
225 | 228 | user = User.find_by_login('newuser') |
|
226 | 229 | assert_not_nil user |
|
227 | 230 | assert user.active? |
|
228 | 231 | assert_not_nil user.last_login_on |
|
229 | 232 | end |
|
230 | 233 | |
|
231 | 234 | def test_register_with_manual_activation |
|
232 | 235 | Setting.self_registration = '2' |
|
233 | 236 | |
|
234 | 237 | post '/account/register', |
|
235 | 238 | :user => {:login => "newuser", :language => "en", |
|
236 | 239 | :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar", |
|
237 | 240 | :password => "newpass123", :password_confirmation => "newpass123"} |
|
238 | 241 | assert_redirected_to '/login' |
|
239 | 242 | assert !User.find_by_login('newuser').active? |
|
240 | 243 | end |
|
241 | 244 | |
|
242 | 245 | def test_register_with_email_activation |
|
243 | 246 | Setting.self_registration = '1' |
|
244 | 247 | Token.delete_all |
|
245 | 248 | |
|
246 | 249 | post '/account/register', |
|
247 | 250 | :user => {:login => "newuser", :language => "en", |
|
248 | 251 | :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar", |
|
249 | 252 | :password => "newpass123", :password_confirmation => "newpass123"} |
|
250 | 253 | assert_redirected_to '/login' |
|
251 | 254 | assert !User.find_by_login('newuser').active? |
|
252 | 255 | |
|
253 | 256 | token = Token.first |
|
254 | 257 | assert_equal 'register', token.action |
|
255 | 258 | assert_equal 'newuser@foo.bar', token.user.mail |
|
256 | 259 | assert !token.expired? |
|
257 | 260 | |
|
258 | 261 | get '/account/activate', :token => token.value |
|
259 | 262 | assert_redirected_to '/login' |
|
260 | 263 | log_user('newuser', 'newpass123') |
|
261 | 264 | end |
|
262 | 265 | |
|
263 | 266 | def test_onthefly_registration |
|
264 | 267 | # disable registration |
|
265 | 268 | Setting.self_registration = '0' |
|
266 | 269 | AuthSource.expects(:authenticate).returns( |
|
267 | 270 | {:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', |
|
268 | 271 | :mail => 'foo@bar.com', :auth_source_id => 66}) |
|
269 | 272 | |
|
270 | 273 | post '/login', :username => 'foo', :password => 'bar' |
|
271 | 274 | assert_redirected_to '/my/page' |
|
272 | 275 | |
|
273 | 276 | user = User.find_by_login('foo') |
|
274 | 277 | assert user.is_a?(User) |
|
275 | 278 | assert_equal 66, user.auth_source_id |
|
276 | 279 | assert user.hashed_password.blank? |
|
277 | 280 | end |
|
278 | 281 | |
|
279 | 282 | def test_onthefly_registration_with_invalid_attributes |
|
280 | 283 | # disable registration |
|
281 | 284 | Setting.self_registration = '0' |
|
282 | 285 | AuthSource.expects(:authenticate).returns( |
|
283 | 286 | {:login => 'foo', :lastname => 'Smith', :auth_source_id => 66}) |
|
284 | 287 | |
|
285 | 288 | post '/login', :username => 'foo', :password => 'bar' |
|
286 | 289 | assert_response :success |
|
287 | 290 | assert_select 'input[name=?][value=""]', 'user[firstname]' |
|
288 | 291 | assert_select 'input[name=?][value=Smith]', 'user[lastname]' |
|
289 | 292 | assert_select 'input[name=?]', 'user[login]', 0 |
|
290 | 293 | assert_select 'input[name=?]', 'user[password]', 0 |
|
291 | 294 | |
|
292 | 295 | post '/account/register', |
|
293 | 296 | :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'} |
|
294 | 297 | assert_redirected_to '/my/account' |
|
295 | 298 | |
|
296 | 299 | user = User.find_by_login('foo') |
|
297 | 300 | assert user.is_a?(User) |
|
298 | 301 | assert_equal 66, user.auth_source_id |
|
299 | 302 | assert user.hashed_password.blank? |
|
300 | 303 | end |
|
301 | 304 | |
|
302 | 305 | def test_registered_user_should_be_able_to_get_a_new_activation_email |
|
303 | 306 | Token.delete_all |
|
304 | 307 | |
|
305 | 308 | with_settings :self_registration => '1', :default_language => 'en' do |
|
306 | 309 | # register a new account |
|
307 | 310 | assert_difference 'User.count' do |
|
308 | 311 | assert_difference 'Token.count' do |
|
309 | 312 | post '/account/register', |
|
310 | 313 | :user => {:login => "newuser", :language => "en", |
|
311 | 314 | :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar", |
|
312 | 315 | :password => "newpass123", :password_confirmation => "newpass123"} |
|
313 | 316 | end |
|
314 | 317 | end |
|
315 | 318 | user = User.order('id desc').first |
|
316 | 319 | assert_equal User::STATUS_REGISTERED, user.status |
|
317 | 320 | reset! |
|
318 | 321 | |
|
319 | 322 | # try to use "lost password" |
|
320 | 323 | assert_no_difference 'ActionMailer::Base.deliveries.size' do |
|
321 | 324 | post '/account/lost_password', :mail => 'newuser@foo.bar' |
|
322 | 325 | end |
|
323 | 326 | assert_redirected_to '/account/lost_password' |
|
324 | 327 | follow_redirect! |
|
325 | 328 | assert_response :success |
|
326 | 329 | assert_select 'div.flash', :text => /new activation email/ |
|
327 | 330 | assert_select 'div.flash a[href="/account/activation_email"]' |
|
328 | 331 | |
|
329 | 332 | # request a new action activation email |
|
330 | 333 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
331 | 334 | get '/account/activation_email' |
|
332 | 335 | end |
|
333 | 336 | assert_redirected_to '/login' |
|
334 | 337 | token = Token.order('id desc').first |
|
335 | 338 | activation_path = "/account/activate?token=#{token.value}" |
|
336 | 339 | assert_include activation_path, mail_body(ActionMailer::Base.deliveries.last) |
|
337 | 340 | |
|
338 | 341 | # activate the account |
|
339 | 342 | get activation_path |
|
340 | 343 | assert_redirected_to '/login' |
|
341 | 344 | |
|
342 | 345 | post '/login', :username => 'newuser', :password => 'newpass123' |
|
343 | 346 | assert_redirected_to '/my/page' |
|
344 | 347 | end |
|
345 | 348 | end |
|
346 | 349 | end |
General Comments 0
You need to be logged in to leave comments.
Login now