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.

196 lines
7.5 KiB

  1. # Copyright 2018 Eficent Business and IT Consulting Services S.L.
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  3. from odoo.tests import common
  4. from odoo.exceptions import ValidationError
  5. from .common import setup_test_model, teardown_test_model
  6. from .tier_validation_tester import TierValidationTester, TierValidationTester2
  7. @common.at_install(False)
  8. @common.post_install(True)
  9. class TierTierValidation(common.SavepointCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. super(TierTierValidation, cls).setUpClass()
  13. setup_test_model(cls.env,
  14. [TierValidationTester, TierValidationTester2])
  15. cls.test_model = cls.env[TierValidationTester._name]
  16. cls.test_model_2 = cls.env[TierValidationTester2._name]
  17. cls.tester_model = cls.env['ir.model'].search([
  18. ('model', '=', 'tier.validation.tester')])
  19. cls.tester_model_2 = cls.env['ir.model'].search([
  20. ('model', '=', 'tier.validation.tester2')])
  21. # Access record:
  22. cls.env["ir.model.access"].create({
  23. 'name': "access.tester",
  24. 'model_id': cls.tester_model.id,
  25. 'perm_read': 1,
  26. 'perm_write': 1,
  27. 'perm_create': 1,
  28. 'perm_unlink': 1,
  29. })
  30. cls.env["ir.model.access"].create({
  31. 'name': "access.tester2",
  32. 'model_id': cls.tester_model_2.id,
  33. 'perm_read': 1,
  34. 'perm_write': 1,
  35. 'perm_create': 1,
  36. 'perm_unlink': 1,
  37. })
  38. # Create users:
  39. group_ids = cls.env.ref('base.group_system').ids
  40. cls.test_user_1 = cls.env['res.users'].create({
  41. 'name': 'John',
  42. 'login': 'test1',
  43. 'groups_id': [(6, 0, group_ids)],
  44. })
  45. cls.test_user_2 = cls.env['res.users'].create({
  46. 'name': 'Mike',
  47. 'login': 'test2',
  48. })
  49. # Create tier definitions:
  50. cls.tier_def_obj = cls.env['tier.definition']
  51. cls.tier_def_obj.create({
  52. 'model_id': cls.tester_model.id,
  53. 'review_type': 'individual',
  54. 'reviewer_id': cls.test_user_1.id,
  55. 'definition_domain': "[('test_field', '>', 1.0)]",
  56. })
  57. cls.test_record = cls.test_model.create({
  58. 'test_field': 2.5,
  59. })
  60. cls.test_record_2 = cls.test_model_2.create({
  61. 'test_field': 2.5,
  62. })
  63. @classmethod
  64. def tearDownClass(cls):
  65. teardown_test_model(cls.env,
  66. [TierValidationTester, TierValidationTester2])
  67. super(TierTierValidation, cls).tearDownClass()
  68. def test_01_auto_validation(self):
  69. """When the user can validate all future reviews, it is not needed
  70. to request a validation, the action can be done straight forward."""
  71. self.test_record.sudo(self.test_user_1.id).action_confirm()
  72. self.assertEqual(self.test_record.state, 'confirmed')
  73. def test_02_no_auto_validation(self):
  74. """User with no right to validate future reviews must request a
  75. validation."""
  76. with self.assertRaises(ValidationError):
  77. self.test_record.sudo(self.test_user_2.id).action_confirm()
  78. def test_03_request_validation_approved(self):
  79. """User 2 request a validation and user 1 approves it."""
  80. self.assertFalse(self.test_record.review_ids)
  81. reviews = self.test_record.sudo(
  82. self.test_user_2.id).request_validation()
  83. self.assertTrue(reviews)
  84. record = self.test_record.sudo(self.test_user_1.id)
  85. record.validate_tier()
  86. self.assertTrue(record.validated)
  87. def test_04_request_validation_rejected(self):
  88. """Request validation, rejection and reset."""
  89. self.assertFalse(self.test_record.review_ids)
  90. reviews = self.test_record.sudo(
  91. self.test_user_2.id).request_validation()
  92. self.assertTrue(reviews)
  93. record = self.test_record.sudo(self.test_user_1.id)
  94. record.reject_tier()
  95. self.assertTrue(record.review_ids)
  96. self.assertTrue(record.rejected)
  97. record.restart_validation()
  98. self.assertFalse(record.review_ids)
  99. def test_05_under_validation(self):
  100. """Write is forbidden in a record under validation."""
  101. self.assertFalse(self.test_record.review_ids)
  102. reviews = self.test_record.sudo(
  103. self.test_user_2.id).request_validation()
  104. self.assertTrue(reviews)
  105. record = self.test_record.sudo(self.test_user_1.id)
  106. with self.assertRaises(ValidationError):
  107. record.write({'test_field': 0.5})
  108. def test_06_validation_process_open(self):
  109. """Operation forbidden while a validation process is open."""
  110. self.assertFalse(self.test_record.review_ids)
  111. reviews = self.test_record.sudo(
  112. self.test_user_2.id).request_validation()
  113. self.assertTrue(reviews)
  114. record = self.test_record.sudo(self.test_user_1.id)
  115. with self.assertRaises(ValidationError):
  116. record.action_confirm()
  117. def test_07_search_reviewers(self):
  118. """Test search methods."""
  119. reviews = self.test_record.sudo(
  120. self.test_user_2.id).request_validation()
  121. self.assertTrue(reviews)
  122. record = self.test_record.sudo(self.test_user_1.id)
  123. self.assertIn(self.test_user_1, record.reviewer_ids)
  124. res = self.test_model.search(
  125. [('reviewer_ids', 'in', self.test_user_1.id)])
  126. self.assertTrue(res)
  127. def test_08_search_validated(self):
  128. """Test for the validated search method."""
  129. self.test_record.sudo(self.test_user_2.id).request_validation()
  130. res = self.test_model.sudo(self.test_user_1.id).search(
  131. [('validated', '=', False)])
  132. self.assertTrue(res)
  133. def test_09_dummy_tier_definition(self):
  134. """Test tier.definition methods."""
  135. res = self.tier_def_obj._get_tier_validation_model_names()
  136. self.assertEqual(res, [])
  137. res = self.tier_def_obj.onchange_model_id()
  138. self.assertTrue(res)
  139. def test_10_systray_counter(self):
  140. # Create new test record
  141. test_record = self.test_model.create({
  142. 'test_field': 2.5,
  143. })
  144. # Create tier definitions for both tester models
  145. self.tier_def_obj.create({
  146. 'model_id': self.tester_model.id,
  147. 'review_type': 'individual',
  148. 'reviewer_id': self.test_user_1.id,
  149. 'definition_domain': "[('test_field', '>', 1.0)]",
  150. })
  151. self.tier_def_obj.create({
  152. 'model_id': self.tester_model.id,
  153. 'review_type': 'individual',
  154. 'reviewer_id': self.test_user_1.id,
  155. 'definition_domain': "[('test_field', '>', 1.0)]",
  156. })
  157. self.tier_def_obj.create({
  158. 'model_id': self.tester_model_2.id,
  159. 'review_type': 'individual',
  160. 'reviewer_id': self.test_user_1.id,
  161. 'definition_domain': "[('test_field', '>', 1.0)]",
  162. })
  163. # Request validation
  164. self.test_record.sudo(self.test_user_2.id).request_validation()
  165. test_record.sudo(self.test_user_2.id).request_validation()
  166. self.test_record_2.sudo(self.test_user_2.id).request_validation()
  167. # Get review user count as systray icon would do and check count value
  168. docs = self.test_user_1.sudo(
  169. self.test_user_1).review_user_count()
  170. for doc in docs:
  171. if doc.get('name') == 'tier.validation.tester2':
  172. self.assertEqual(doc.get('pending_count'), 1)
  173. else:
  174. self.assertEqual(doc.get('pending_count'), 2)