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.

148 lines
4.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. import time
  5. from odoo.tests.common import TransactionCase
  6. from ..exceptions import PassError
  7. class TestResUsers(TransactionCase):
  8. def setUp(self):
  9. super(TestResUsers, self).setUp()
  10. self.login = 'foslabs@example.com'
  11. self.partner_vals = {
  12. 'name': 'Partner',
  13. 'is_company': False,
  14. 'email': self.login,
  15. }
  16. self.password = 'asdQWE123$%^'
  17. self.main_comp = self.env.ref('base.main_company')
  18. self.vals = {
  19. 'name': 'User',
  20. 'login': self.login,
  21. 'password': self.password,
  22. 'company_id': self.main_comp.id
  23. }
  24. self.model_obj = self.env['res.users']
  25. def _new_record(self):
  26. partner_id = self.env['res.partner'].create(self.partner_vals)
  27. self.vals['partner_id'] = partner_id.id
  28. return self.model_obj.create(self.vals)
  29. def test_password_write_date_is_saved_on_create(self):
  30. rec_id = self._new_record()
  31. self.assertTrue(
  32. rec_id.password_write_date,
  33. 'Password write date was not saved to db.',
  34. )
  35. def test_password_write_date_is_updated_on_write(self):
  36. rec_id = self._new_record()
  37. old_write_date = rec_id.password_write_date
  38. time.sleep(2)
  39. rec_id.write({'password': 'asdQWE123$%^2'})
  40. rec_id.refresh()
  41. new_write_date = rec_id.password_write_date
  42. self.assertNotEqual(
  43. old_write_date, new_write_date,
  44. 'Password write date was not updated on write.',
  45. )
  46. def test_does_not_update_write_date_if_password_unchanged(self):
  47. rec_id = self._new_record()
  48. old_write_date = rec_id.password_write_date
  49. time.sleep(2)
  50. rec_id.write({'name': 'Luser'})
  51. rec_id.refresh()
  52. new_write_date = rec_id.password_write_date
  53. self.assertEqual(
  54. old_write_date, new_write_date,
  55. 'Password not changed but write date updated anyway.',
  56. )
  57. def test_check_password_returns_true_for_valid_password(self):
  58. rec_id = self._new_record()
  59. self.assertTrue(
  60. rec_id._check_password('asdQWE123$%^3'),
  61. 'Password is valid but check failed.',
  62. )
  63. def test_check_password_raises_error_for_invalid_password(self):
  64. rec_id = self._new_record()
  65. with self.assertRaises(PassError):
  66. rec_id._check_password('password')
  67. def test_save_password_crypt(self):
  68. rec_id = self._new_record()
  69. self.assertEqual(
  70. 1, len(rec_id.password_history_ids),
  71. )
  72. def test_check_password_crypt(self):
  73. """ It should raise PassError if previously used """
  74. rec_id = self._new_record()
  75. with self.assertRaises(PassError):
  76. rec_id.write({'password': self.password})
  77. def test_password_is_expired_if_record_has_no_write_date(self):
  78. rec_id = self._new_record()
  79. rec_id.write({'password_write_date': None})
  80. rec_id.refresh()
  81. self.assertTrue(
  82. rec_id._password_has_expired(),
  83. 'Record has no password write date but check failed.',
  84. )
  85. def test_an_old_password_is_expired(self):
  86. rec_id = self._new_record()
  87. old_write_date = '1970-01-01 00:00:00'
  88. rec_id.write({'password_write_date': old_write_date})
  89. rec_id.refresh()
  90. self.assertTrue(
  91. rec_id._password_has_expired(),
  92. 'Password is out of date but check failed.',
  93. )
  94. def test_a_new_password_is_not_expired(self):
  95. rec_id = self._new_record()
  96. self.assertFalse(
  97. rec_id._password_has_expired(),
  98. 'Password was just created but has already expired.',
  99. )
  100. def test_expire_password_generates_token(self):
  101. rec_id = self._new_record()
  102. rec_id.sudo().action_expire_password()
  103. rec_id.refresh()
  104. token = rec_id.partner_id.signup_token
  105. self.assertTrue(
  106. token,
  107. 'A token was not generated.',
  108. )
  109. def test_validate_pass_reset_error(self):
  110. """ It should throw PassError on reset inside min threshold """
  111. rec_id = self._new_record()
  112. with self.assertRaises(PassError):
  113. rec_id._validate_pass_reset()
  114. def test_validate_pass_reset_allow(self):
  115. """ It should allow reset pass when outside threshold """
  116. rec_id = self._new_record()
  117. rec_id.password_write_date = '2016-01-01'
  118. self.assertEqual(
  119. True, rec_id._validate_pass_reset(),
  120. )
  121. def test_validate_pass_reset_zero(self):
  122. """ It should allow reset pass when <= 0 """
  123. rec_id = self._new_record()
  124. rec_id.company_id.password_minimum = 0
  125. self.assertEqual(
  126. True, rec_id._validate_pass_reset(),
  127. )