You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015-2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from odoo import api, registry
  5. from odoo.tests.common import TransactionCase
  6. class TestResUsers(TransactionCase):
  7. def setUp(self):
  8. super(TestResUsers, self).setUp()
  9. self.login = 'LasLabs@ExAmPlE.CoM'
  10. self.partner_vals = {
  11. 'name': 'Partner',
  12. 'is_company': False,
  13. 'email': self.login,
  14. }
  15. self.vals = {
  16. 'name': 'User',
  17. 'login': self.login,
  18. 'password': 'password',
  19. }
  20. self.model_obj = self.env['res.users']
  21. def _new_record(self):
  22. """ It should enerate a new record to test with """
  23. partner_id = self.env['res.partner'].create(self.partner_vals)
  24. self.vals['partner_id'] = partner_id.id
  25. return self.model_obj.create(self.vals)
  26. def test_login_is_lowercased_on_create(self):
  27. """ It should verify the login is set to lowercase on create """
  28. rec_id = self._new_record()
  29. self.assertEqual(
  30. self.login.lower(), rec_id.login,
  31. 'Login was not lowercased when saved to db.',
  32. )
  33. def test_login_is_lowercased_on_write(self):
  34. """ It should verify the login is set to lowercase on write """
  35. rec_id = self._new_record()
  36. rec_id.write({'login': self.login})
  37. self.assertEqual(
  38. self.login.lower(), rec_id.login,
  39. 'Login was not lowercased when saved to db.',
  40. )
  41. def test_login_login_is_lowercased(self):
  42. """ It should verify the login is set to lowercase on login """
  43. rec_id = self._new_record()
  44. # We have to commit this cursor, because `_login` uses a fresh cursor
  45. self.env.cr.commit()
  46. res_id = self.model_obj._login(
  47. self.env.registry.db_name, self.login.upper(), 'password'
  48. )
  49. # Now clean up our mess to preserve idempotence
  50. with api.Environment.manage():
  51. with registry(self.env.registry.db_name).cursor() as new_cr:
  52. new_cr.execute(
  53. "DELETE FROM res_users WHERE login='%s'" %
  54. self.login.lower()
  55. )
  56. new_cr.commit()
  57. self.assertEqual(
  58. rec_id.id, res_id,
  59. 'Login with with uppercase chars was not successful',
  60. )