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