partner-contact/partner_tier_validation/tests/test_tier_validation.py

96 lines
3.7 KiB
Python
Raw Normal View History

2021-05-30 02:14:14 +00:00
# Copyright 2021 Patrick Wilson <pwilson@opensourceintegrators.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
from flectra.exceptions import ValidationError
from flectra.tests import common, tagged
@tagged("-at_install", "post_install")
class TestPartnerTierValidation(common.SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Create users
2022-06-29 18:38:32 +00:00
group_user = cls.env.ref("base.group_user")
group_contacts = cls.env.ref("base.group_partner_manager")
group_approver = cls.env.ref("base.group_no_one")
User = cls.env["res.users"]
cls.user_employee = User.create(
{
"name": "Employee",
"login": "empl1",
"email": "empl1@example.com",
"groups_id": (group_user | group_contacts).ids,
}
)
cls.user_approver = User.create(
2021-05-30 02:14:14 +00:00
{
2022-06-29 18:38:32 +00:00
"name": "Approver",
"login": "aprov1",
"email": "approv1@example.com",
"groups_id": (group_user | group_contacts | group_approver).ids,
2021-05-30 02:14:14 +00:00
}
)
2022-06-29 18:38:32 +00:00
# Create tier definition: example where only Company needs validation
cls.TierDefinition = cls.env["tier.definition"]
cls.TierDefinition.create(
2021-05-30 02:14:14 +00:00
{
2022-06-29 18:38:32 +00:00
"model_id": cls.env.ref("base.model_res_partner").id,
2021-05-30 02:14:14 +00:00
"review_type": "individual",
2022-06-29 18:38:32 +00:00
"reviewer_id": cls.user_approver.id,
"definition_domain": "[('is_company','=',True)]",
2021-05-30 02:14:14 +00:00
}
)
2022-06-29 18:38:32 +00:00
# Setup Contact Stages: draft is the default
Stage = cls.env["res.partner.stage"]
Stage.search([("is_default", "=", True)]).write({"is_default": False})
cls.stage_draft = Stage.search([("state", "=", "draft")], limit=1)
cls.stage_draft.is_default = True
cls.stage_confirmed = Stage.search([("state", "=", "confirmed")], limit=1)
2021-05-30 02:14:14 +00:00
def test_tier_validation_model_name(self):
self.assertIn(
2022-06-29 18:38:32 +00:00
"res.partner", self.TierDefinition._get_tier_validation_model_names()
2021-05-30 02:14:14 +00:00
)
def test_validation_res_partner(self):
2022-06-29 18:38:32 +00:00
"""
Case where new Contact requires validation
"""
Partner = self.env["res.partner"]
contact_vals = {"name": "Company for test", "company_type": "company"}
contact = Partner.with_user(self.user_employee).create(contact_vals)
self.assertEqual(contact.state, "draft")
2021-05-30 02:14:14 +00:00
# Assert an error shows if trying to make it active
with self.assertRaises(ValidationError):
2022-06-29 18:38:32 +00:00
contact.write({"stage_id": self.stage_confirmed.id})
2021-05-30 02:14:14 +00:00
# Request and validate partner
2022-06-29 18:38:32 +00:00
contact.request_validation()
contact.with_user(self.user_approver).validate_tier()
contact.with_user(self.user_approver).write(
{"stage_id": self.stage_confirmed.id}
)
self.assertEqual(contact.state, "confirmed")
2021-05-30 02:14:14 +00:00
# Change company type to retrigger validation
2022-06-29 18:38:32 +00:00
contact.write({"company_type": "person"})
self.assertEqual(
contact.state, "draft", "Change company type sets back to draft"
)
2021-05-30 02:14:14 +00:00
2022-06-29 18:38:32 +00:00
def test_no_validation_res_partner(self):
"""
Case where new Contact does not require validation
"""
Partner = self.env["res.partner"]
contact_vals = {"name": "Company for test", "company_type": "person"}
contact = Partner.with_user(self.user_employee).create(contact_vals)
self.assertEqual(contact.state, "draft")
# Can move to confirmed state without approval
contact.write({"stage_id": self.stage_confirmed.id})
self.assertEqual(contact.state, "confirmed")