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.

161 lines
5.4 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.main_comp = self.env.ref('base.main_company')
  11. # Modify users as privileged, but non-root user
  12. privileged_user = self.env['res.users'].create({
  13. 'name': 'Privileged User',
  14. 'login': 'privileged_user@example.com',
  15. 'company_id': self.main_comp.id,
  16. 'groups_id': [
  17. (4, self.env.ref('base.group_erp_manager').id),
  18. (4, self.env.ref('base.group_partner_manager').id),
  19. (4, self.env.ref('base.group_user').id),
  20. ],
  21. })
  22. privileged_user.email = privileged_user.login
  23. self.env = self.env(user=privileged_user)
  24. self.login = 'foslabs@example.com'
  25. self.partner_vals = {
  26. 'name': 'Partner',
  27. 'is_company': False,
  28. 'email': self.login,
  29. }
  30. self.password = 'asdQWE123$%^'
  31. self.vals = {
  32. 'name': 'User',
  33. 'login': self.login,
  34. 'password': self.password,
  35. 'company_id': self.main_comp.id
  36. }
  37. self.model_obj = self.env['res.users']
  38. def _new_record(self):
  39. partner_id = self.env['res.partner'].create(self.partner_vals)
  40. self.vals['partner_id'] = partner_id.id
  41. return self.model_obj.create(self.vals)
  42. def test_password_write_date_is_saved_on_create(self):
  43. rec_id = self._new_record()
  44. self.assertTrue(
  45. rec_id.password_write_date,
  46. 'Password write date was not saved to db.',
  47. )
  48. def test_password_write_date_is_updated_on_write(self):
  49. rec_id = self._new_record()
  50. old_write_date = rec_id.password_write_date
  51. time.sleep(2)
  52. rec_id.write({'password': 'asdQWE123$%^2'})
  53. rec_id.refresh()
  54. new_write_date = rec_id.password_write_date
  55. self.assertNotEqual(
  56. old_write_date, new_write_date,
  57. 'Password write date was not updated on write.',
  58. )
  59. def test_does_not_update_write_date_if_password_unchanged(self):
  60. rec_id = self._new_record()
  61. old_write_date = rec_id.password_write_date
  62. time.sleep(2)
  63. rec_id.write({'name': 'Luser'})
  64. rec_id.refresh()
  65. new_write_date = rec_id.password_write_date
  66. self.assertEqual(
  67. old_write_date, new_write_date,
  68. 'Password not changed but write date updated anyway.',
  69. )
  70. def test_check_password_returns_true_for_valid_password(self):
  71. rec_id = self._new_record()
  72. self.assertTrue(
  73. rec_id._check_password('asdQWE123$%^3'),
  74. 'Password is valid but check failed.',
  75. )
  76. def test_check_password_raises_error_for_invalid_password(self):
  77. rec_id = self._new_record()
  78. with self.assertRaises(PassError):
  79. rec_id._check_password('password')
  80. def test_save_password_crypt(self):
  81. rec_id = self._new_record()
  82. self.assertEqual(
  83. 1, len(rec_id.password_history_ids),
  84. )
  85. def test_check_password_crypt(self):
  86. """ It should raise PassError if previously used """
  87. rec_id = self._new_record()
  88. with self.assertRaises(PassError):
  89. rec_id.write({'password': self.password})
  90. def test_password_is_expired_if_record_has_no_write_date(self):
  91. rec_id = self._new_record()
  92. rec_id.write({'password_write_date': None})
  93. rec_id.refresh()
  94. self.assertTrue(
  95. rec_id._password_has_expired(),
  96. 'Record has no password write date but check failed.',
  97. )
  98. def test_an_old_password_is_expired(self):
  99. rec_id = self._new_record()
  100. old_write_date = '1970-01-01 00:00:00'
  101. rec_id.write({'password_write_date': old_write_date})
  102. rec_id.refresh()
  103. self.assertTrue(
  104. rec_id._password_has_expired(),
  105. 'Password is out of date but check failed.',
  106. )
  107. def test_a_new_password_is_not_expired(self):
  108. rec_id = self._new_record()
  109. self.assertFalse(
  110. rec_id._password_has_expired(),
  111. 'Password was just created but has already expired.',
  112. )
  113. def test_expire_password_generates_token(self):
  114. rec_id = self._new_record()
  115. rec_id.sudo().action_expire_password()
  116. rec_id.refresh()
  117. token = rec_id.partner_id.signup_token
  118. self.assertTrue(
  119. token,
  120. 'A token was not generated.',
  121. )
  122. def test_validate_pass_reset_error(self):
  123. """ It should throw PassError on reset inside min threshold """
  124. rec_id = self._new_record()
  125. with self.assertRaises(PassError):
  126. rec_id._validate_pass_reset()
  127. def test_validate_pass_reset_allow(self):
  128. """ It should allow reset pass when outside threshold """
  129. rec_id = self._new_record()
  130. rec_id.password_write_date = '2016-01-01'
  131. self.assertEqual(
  132. True, rec_id._validate_pass_reset(),
  133. )
  134. def test_validate_pass_reset_zero(self):
  135. """ It should allow reset pass when <= 0 """
  136. rec_id = self._new_record()
  137. rec_id.company_id.password_minimum = 0
  138. self.assertEqual(
  139. True, rec_id._validate_pass_reset(),
  140. )