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.

95 lines
3.7 KiB

  1. # Copyright 2021 Patrick Wilson <pwilson@opensourceintegrators.com>
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  3. from odoo.exceptions import ValidationError
  4. from odoo.tests import common, tagged
  5. @tagged("-at_install", "post_install")
  6. class TestPartnerTierValidation(common.SavepointCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. super().setUpClass()
  10. # Create users
  11. group_user = cls.env.ref("base.group_user")
  12. group_contacts = cls.env.ref("base.group_partner_manager")
  13. group_approver = cls.env.ref("base.group_no_one")
  14. User = cls.env["res.users"]
  15. cls.user_employee = User.create(
  16. {
  17. "name": "Employee",
  18. "login": "empl1",
  19. "email": "empl1@example.com",
  20. "groups_id": (group_user | group_contacts).ids,
  21. }
  22. )
  23. cls.user_approver = User.create(
  24. {
  25. "name": "Approver",
  26. "login": "aprov1",
  27. "email": "approv1@example.com",
  28. "groups_id": (group_user | group_contacts | group_approver).ids,
  29. }
  30. )
  31. # Create tier definition: example where only Company needs validation
  32. cls.TierDefinition = cls.env["tier.definition"]
  33. cls.TierDefinition.create(
  34. {
  35. "model_id": cls.env.ref("base.model_res_partner").id,
  36. "review_type": "individual",
  37. "reviewer_id": cls.user_approver.id,
  38. "definition_domain": "[('is_company','=',True)]",
  39. }
  40. )
  41. # Setup Contact Stages: draft is the default
  42. Stage = cls.env["res.partner.stage"]
  43. Stage.search([("is_default", "=", True)]).write({"is_default": False})
  44. cls.stage_draft = Stage.search([("state", "=", "draft")], limit=1)
  45. cls.stage_draft.is_default = True
  46. cls.stage_confirmed = Stage.search([("state", "=", "confirmed")], limit=1)
  47. def test_tier_validation_model_name(self):
  48. self.assertIn(
  49. "res.partner", self.TierDefinition._get_tier_validation_model_names()
  50. )
  51. def test_validation_res_partner(self):
  52. """
  53. Case where new Contact requires validation
  54. """
  55. Partner = self.env["res.partner"]
  56. contact_vals = {"name": "Company for test", "company_type": "company"}
  57. contact = Partner.with_user(self.user_employee).create(contact_vals)
  58. self.assertEqual(contact.state, "draft")
  59. # Assert an error shows if trying to make it active
  60. with self.assertRaises(ValidationError):
  61. contact.write({"stage_id": self.stage_confirmed.id})
  62. # Request and validate partner
  63. contact.request_validation()
  64. contact.with_user(self.user_approver).validate_tier()
  65. contact.with_user(self.user_approver).write(
  66. {"stage_id": self.stage_confirmed.id}
  67. )
  68. self.assertEqual(contact.state, "confirmed")
  69. # Change company type to retrigger validation
  70. contact.write({"company_type": "person"})
  71. self.assertEqual(
  72. contact.state, "draft", "Change company type sets back to draft"
  73. )
  74. def test_no_validation_res_partner(self):
  75. """
  76. Case where new Contact does not require validation
  77. """
  78. Partner = self.env["res.partner"]
  79. contact_vals = {"name": "Company for test", "company_type": "person"}
  80. contact = Partner.with_user(self.user_employee).create(contact_vals)
  81. self.assertEqual(contact.state, "draft")
  82. # Can move to confirmed state without approval
  83. contact.write({"stage_id": self.stage_confirmed.id})
  84. self.assertEqual(contact.state, "confirmed")