Skip to content

Commit e742e23

Browse files
yajodsolanki-initos
authored andcommitted
[FIX] password_security: Error 500 when login with bad password (OCA#27)
Since some implementation details are changed, I had to change some tests that were actually testing the implementation instead of the desired result of the method.
1 parent 4e60972 commit e742e23

File tree

3 files changed

+56
-93
lines changed

3 files changed

+56
-93
lines changed

password_security/__manifest__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
'name': 'Password Security',
66
"summary": "Allow admin to set password security requirements.",
7-
'version': '11.0.1.0.0',
8-
'author': "LasLabs, Odoo Community Association (OCA), Kaushal Prajapati",
7+
'version': '11.0.1.0.1',
8+
'author':
9+
"LasLabs, "
10+
"Kaushal Prajapati, "
11+
"Tecnativa, "
12+
"Odoo Community Association (OCA)",
913
'category': 'Base',
1014
'depends': [
1115
'auth_crypt',

password_security/controllers/main.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,15 @@ def do_signup(self, qcontext):
3535
def web_login(self, *args, **kw):
3636
ensure_db()
3737
response = super(PasswordSecurityHome, self).web_login(*args, **kw)
38-
if not request.httprequest.method == 'POST':
38+
if not request.params.get("login_success"):
3939
return response
40-
uid = request.session.authenticate(
41-
request.session.db,
42-
request.params['login'],
43-
request.params['password']
44-
)
45-
if not uid:
46-
return response
47-
users_obj = request.env['res.users'].sudo()
48-
user_id = users_obj.browse(request.uid)
49-
if not user_id._password_has_expired():
40+
# Now, I'm an authenticated user
41+
if not request.env.user._password_has_expired():
5042
return response
51-
user_id.action_expire_password()
43+
# My password is expired, kick me out
44+
request.env.user.action_expire_password()
5245
request.session.logout(keep_db=True)
53-
redirect = user_id.partner_id.signup_url
46+
redirect = request.env.user.partner_id.signup_url
5447
return http.redirect_with_hash(redirect)
5548

5649
@http.route()

password_security/tests/test_password_security_home.py

Lines changed: 44 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Copyright 2016 LasLabs Inc.
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
33

4-
import mock
4+
from datetime import datetime, timedelta
5+
from unittest import mock
56

67
from contextlib import contextmanager
78

8-
from odoo.tests.common import TransactionCase
9+
from odoo.tests.common import HttpCase, TransactionCase
910
from odoo.http import Response
1011

1112
from ..controllers import main
@@ -102,82 +103,6 @@ def test_web_login_super(self):
102103
*expect_list, **expect_dict
103104
)
104105

105-
def test_web_login_no_post(self):
106-
""" It should return immediate result of super when not POST """
107-
with self.mock_assets() as assets:
108-
assets['request'].httprequest.method = 'GET'
109-
assets['request'].session.authenticate.side_effect = \
110-
EndTestException
111-
res = self.password_security_home.web_login()
112-
self.assertEqual(
113-
assets['web_login'](), res,
114-
)
115-
116-
def test_web_login_authenticate(self):
117-
""" It should attempt authentication to obtain uid """
118-
with self.mock_assets() as assets:
119-
assets['request'].httprequest.method = 'POST'
120-
authenticate = assets['request'].session.authenticate
121-
request = assets['request']
122-
authenticate.side_effect = EndTestException
123-
with self.assertRaises(EndTestException):
124-
self.password_security_home.web_login()
125-
authenticate.assert_called_once_with(
126-
request.session.db,
127-
request.params['login'],
128-
request.params['password'],
129-
)
130-
131-
def test_web_login_authenticate_fail(self):
132-
""" It should return super result if failed auth """
133-
with self.mock_assets() as assets:
134-
authenticate = assets['request'].session.authenticate
135-
request = assets['request']
136-
request.httprequest.method = 'POST'
137-
request.env['res.users'].sudo.side_effect = EndTestException
138-
authenticate.return_value = False
139-
res = self.password_security_home.web_login()
140-
self.assertEqual(
141-
assets['web_login'](), res,
142-
)
143-
144-
def test_web_login_get_user(self):
145-
""" It should get the proper user as sudo """
146-
with self.mock_assets() as assets:
147-
request = assets['request']
148-
request.httprequest.method = 'POST'
149-
sudo = request.env['res.users'].sudo()
150-
sudo.browse.side_effect = EndTestException
151-
with self.assertRaises(EndTestException):
152-
self.password_security_home.web_login()
153-
sudo.browse.assert_called_once_with(
154-
request.uid
155-
)
156-
157-
def test_web_login_valid_pass(self):
158-
""" It should return parent result if pass isn't expired """
159-
with self.mock_assets() as assets:
160-
request = assets['request']
161-
request.httprequest.method = 'POST'
162-
user = request.env['res.users'].sudo().browse()
163-
user.action_expire_password.side_effect = EndTestException
164-
user._password_has_expired.return_value = False
165-
res = self.password_security_home.web_login()
166-
self.assertEqual(
167-
assets['web_login'](), res,
168-
)
169-
170-
def test_web_login_expire_pass(self):
171-
""" It should expire password if necessary """
172-
with self.mock_assets() as assets:
173-
request = assets['request']
174-
request.httprequest.method = 'POST'
175-
user = request.env['res.users'].sudo().browse()
176-
user.action_expire_password.side_effect = EndTestException
177-
user._password_has_expired.return_value = True
178-
with self.assertRaises(EndTestException):
179-
self.password_security_home.web_login()
180-
181106
def test_web_login_log_out_if_expired(self):
182107
"""It should log out user if password expired"""
183108
with self.mock_assets() as assets:
@@ -278,3 +203,44 @@ def test_web_auth_reset_password_success(self):
278203
self.assertEqual(
279204
assets['web_auth_reset_password'](), res,
280205
)
206+
207+
208+
@mock.patch("odoo.http.WebRequest.validate_csrf", return_value=True)
209+
class LoginCase(HttpCase):
210+
def test_web_login_authenticate(self, *args):
211+
"""It should allow authenticating by login"""
212+
response = self.url_open(
213+
"/web/login",
214+
{"login": "admin", "password": "admin"},
215+
)
216+
self.assertIn(
217+
"window.location = '/web'",
218+
response.text,
219+
)
220+
221+
def test_web_login_authenticate_fail(self, *args):
222+
"""It should fail auth"""
223+
response = self.url_open(
224+
"/web/login",
225+
{"login": "admin", "password": "noadmin"},
226+
)
227+
self.assertIn(
228+
"Wrong login/password",
229+
response.text,
230+
)
231+
232+
def test_web_login_expire_pass(self, *args):
233+
"""It should expire password if necessary"""
234+
two_days_ago = datetime.now() - timedelta(days=2)
235+
with self.cursor() as cr:
236+
env = self.env(cr)
237+
env.user.password_write_date = two_days_ago
238+
env.user.company_id.password_expiration = 1
239+
response = self.url_open(
240+
"/web/login",
241+
{"login": "admin", "password": "admin"},
242+
)
243+
self.assertIn(
244+
"/web/reset_password",
245+
response.text,
246+
)

0 commit comments

Comments
 (0)