Browse Source

[IMP] auth_user_case_insensitive: Drop `search()` override in favor of `_login`

* Update code and tests to override `_login` method
pull/826/head
Ted Salmon 7 years ago
parent
commit
9ed29479a3
No known key found for this signature in database GPG Key ID: 1E92222DD9AF3435
  1. 16
      auth_user_case_insensitive/models/res_users.py
  2. 24
      auth_user_case_insensitive/tests/test_res_users.py

16
auth_user_case_insensitive/models/res_users.py

@ -13,16 +13,12 @@ class ResUsers(models.Model):
help='Used to log into the system. Case insensitive.',
)
@api.model
def search(self, domain, *args, **kwargs):
""" Overload search to lowercase name domains. This can't be done in
a regular search method as the field is not computed
"""
for idx, _domain in enumerate(domain):
if _domain[0] == 'login':
lower = _domain[2].lower() if _domain[2] else False
domain[idx] = (_domain[0], _domain[1], lower)
return super(ResUsers, self).search(domain, *args, **kwargs)
@classmethod
def _login(cls, db, login, password):
""" Overload _login to lowercase the `login` before passing to the
super """
login = login.lower()
return super(ResUsers, cls)._login(db, login, password)
@api.model
def create(self, vals):

24
auth_user_case_insensitive/tests/test_res_users.py

@ -2,6 +2,7 @@
# Copyright 2015-2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
from odoo import api, registry
from odoo.tests.common import TransactionCase
@ -45,12 +46,23 @@ class TestResUsers(TransactionCase):
'Login was not lowercased when saved to db.',
)
def test_login_search_is_lowercased(self):
""" It should verify the login is set to lowercase on search """
def test_login_login_is_lowercased(self):
""" It should verify the login is set to lowercase on login """
rec_id = self._new_record()
res_id = self.model_obj.search([('login', '=', self.login.upper())])
res = res_id.id if res_id else False
# We have to commit this cursor, because `_login` uses a fresh cursor
self.env.cr.commit()
res_id = self.model_obj._login(
self.env.registry.db_name, self.login.upper(), 'password'
)
# Now clean up our mess to preserve idempotence
with api.Environment.manage():
with registry(self.env.registry.db_name).cursor() as new_cr:
new_cr.execute(
"DELETE FROM res_users WHERE login='%s'" %
self.login.lower()
)
new_cr.commit()
self.assertEqual(
rec_id.id, res,
'Search for login with uppercase chars did not yield results.',
rec_id.id, res_id,
'Login with with uppercase chars was not successful',
)
Loading…
Cancel
Save