|
1 | 1 | # Copyright 2016 LasLabs Inc.
|
2 | 2 | # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
|
3 | 3 |
|
4 |
| -import mock |
| 4 | +from datetime import datetime, timedelta |
| 5 | +from unittest import mock |
5 | 6 |
|
6 | 7 | from contextlib import contextmanager
|
7 | 8 |
|
8 |
| -from odoo.tests.common import TransactionCase |
| 9 | +from odoo.tests.common import HttpCase, TransactionCase |
9 | 10 | from odoo.http import Response
|
10 | 11 |
|
11 | 12 | from ..controllers import main
|
@@ -102,82 +103,6 @@ def test_web_login_super(self):
|
102 | 103 | *expect_list, **expect_dict
|
103 | 104 | )
|
104 | 105 |
|
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 |
| - |
181 | 106 | def test_web_login_log_out_if_expired(self):
|
182 | 107 | """It should log out user if password expired"""
|
183 | 108 | with self.mock_assets() as assets:
|
@@ -278,3 +203,44 @@ def test_web_auth_reset_password_success(self):
|
278 | 203 | self.assertEqual(
|
279 | 204 | assets['web_auth_reset_password'](), res,
|
280 | 205 | )
|
| 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