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.

150 lines
5.6 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, UserError
  5. from .common import setup_test_model
  6. from .tier_validation_tester import TierValidationTester
  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, [TierValidationTester])
  14. cls.test_model = cls.env[TierValidationTester._name]
  15. cls.tester_model = cls.env['ir.model'].search([
  16. ('model', '=', 'tier.validation.tester')])
  17. # Access record:
  18. cls.env["ir.model.access"].create({
  19. 'name': "access.tester",
  20. 'model_id': cls.tester_model.id,
  21. 'perm_read': 1,
  22. 'perm_write': 1,
  23. 'perm_create': 1,
  24. 'perm_unlink': 1,
  25. })
  26. # Create users:
  27. group_ids = cls.env.ref('base.group_system').ids
  28. cls.test_user_1 = cls.env['res.users'].create({
  29. 'name': 'John',
  30. 'login': 'test1',
  31. 'groups_id': [(6, 0, group_ids)],
  32. })
  33. cls.test_user_2 = cls.env['res.users'].create({
  34. 'name': 'Mike',
  35. 'login': 'test2',
  36. })
  37. # Create tier definition:
  38. cls.tier_def_obj = cls.env['tier.definition']
  39. cls.tier_def_obj.create({
  40. 'model_id': cls.tester_model.id,
  41. 'review_type': 'individual',
  42. 'reviewer_id': cls.test_user_1.id,
  43. 'python_code': 'rec.test_field > 1.0',
  44. })
  45. cls.test_record = cls.test_model.create({
  46. 'test_field': 2.5,
  47. })
  48. def test_01_auto_validation(self):
  49. """When the user can validate all future reviews, it is not needed
  50. to request a validation, the action can be done straight forward."""
  51. self.test_record.sudo(self.test_user_1.id).action_confirm()
  52. self.assertEqual(self.test_record.state, 'confirmed')
  53. def test_02_no_auto_validation(self):
  54. """User with no right to validate future reviews must request a
  55. validation."""
  56. with self.assertRaises(ValidationError):
  57. self.test_record.sudo(self.test_user_2.id).action_confirm()
  58. def test_03_request_validation_approved(self):
  59. """User 2 request a validation and user 1 approves it."""
  60. self.assertFalse(self.test_record.review_ids)
  61. reviews = self.test_record.sudo(
  62. self.test_user_2.id).request_validation()
  63. self.assertTrue(reviews)
  64. record = self.test_record.sudo(self.test_user_1.id)
  65. record.validate_tier()
  66. self.assertTrue(record.validated)
  67. def test_04_request_validation_rejected(self):
  68. """Request validation, rejection and reset."""
  69. self.assertFalse(self.test_record.review_ids)
  70. reviews = self.test_record.sudo(
  71. self.test_user_2.id).request_validation()
  72. self.assertTrue(reviews)
  73. record = self.test_record.sudo(self.test_user_1.id)
  74. record.reject_tier()
  75. self.assertTrue(record.review_ids)
  76. self.assertTrue(record.rejected)
  77. record.restart_validation()
  78. self.assertFalse(record.review_ids)
  79. def test_05_under_validation(self):
  80. """Write is forbidden in a record under validation."""
  81. self.assertFalse(self.test_record.review_ids)
  82. reviews = self.test_record.sudo(
  83. self.test_user_2.id).request_validation()
  84. self.assertTrue(reviews)
  85. record = self.test_record.sudo(self.test_user_1.id)
  86. with self.assertRaises(ValidationError):
  87. record.write({'test_field': 0.5})
  88. def test_06_validation_process_open(self):
  89. """Operation forbidden while a validation process is open."""
  90. self.assertFalse(self.test_record.review_ids)
  91. reviews = self.test_record.sudo(
  92. self.test_user_2.id).request_validation()
  93. self.assertTrue(reviews)
  94. record = self.test_record.sudo(self.test_user_1.id)
  95. with self.assertRaises(ValidationError):
  96. record.action_confirm()
  97. def test_07_search_reviewers(self):
  98. """Test search methods."""
  99. reviews = self.test_record.sudo(
  100. self.test_user_2.id).request_validation()
  101. self.assertTrue(reviews)
  102. record = self.test_record.sudo(self.test_user_1.id)
  103. self.assertIn(self.test_user_1, record.reviewer_ids)
  104. res = self.test_model.search(
  105. [('reviewer_ids', 'in', self.test_user_1.id)])
  106. self.assertTrue(res)
  107. def test_08_search_validated(self):
  108. """Test for the validated search method."""
  109. self.test_record.sudo(self.test_user_2.id).request_validation()
  110. res = self.test_model.sudo(self.test_user_1.id).search(
  111. [('validated', '=', False)])
  112. self.assertTrue(res)
  113. def test_09_wrong_tier_definition(self):
  114. """Error should raise with incorrect python expresions on
  115. tier definitions."""
  116. self.tier_def_obj.create({
  117. 'model_id': self.tester_model.id,
  118. 'review_type': 'individual',
  119. 'reviewer_id': self.test_user_1.id,
  120. 'python_code': 'rec.not_existing_field > 1.0',
  121. })
  122. with self.assertRaises(UserError):
  123. self.test_record.sudo(self.test_user_1.id).action_confirm()
  124. def test_10_dummy_tier_definition(self):
  125. """Test tier.definition methods."""
  126. res = self.tier_def_obj._get_tier_validation_model_names()
  127. self.assertEqual(res, [])
  128. res = self.tier_def_obj.onchange_model_id()
  129. self.assertTrue(res)