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.

63 lines
2.7 KiB

  1. # Copyright 2021 Open Source Integrators
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. from odoo.tests.common import TransactionCase
  4. class TestUserRoleCompany(TransactionCase):
  5. def setUp(self):
  6. super().setUp()
  7. # COMPANIES
  8. self.Company = self.env["res.company"]
  9. self.company1 = self.env.ref("base.main_company")
  10. self.company2 = self.Company.create({"name": "company2"})
  11. # GROUPS for roles
  12. self.groupA = self.env.ref("base.group_user")
  13. self.groupB = self.env.ref("base.group_system")
  14. self.groupC = self.env.ref("base.group_partner_manager")
  15. # ROLES
  16. self.Role = self.env["res.users.role"]
  17. self.roleA = self.Role.create({"name": "ROLE All Companies"})
  18. self.roleA.implied_ids |= self.groupA
  19. self.roleB = self.Role.create({"name": "ROLE Company 1"})
  20. self.roleB.implied_ids |= self.groupB
  21. self.roleC = self.Role.create({"name": "ROLE Company 1 and 2"})
  22. self.roleC.implied_ids |= self.groupC
  23. # USER
  24. # ==Role=== ==Company== C1 C2 C1+C2
  25. # Role A Yes Yes Yes
  26. # Role B Company1 Yes
  27. # Role C Company1 Yes Yes
  28. # Role C Company2 Yes Yes
  29. self.User = self.env["res.users"]
  30. user_vals = {
  31. "name": "ROLES TEST USER",
  32. "login": "test_user",
  33. "company_ids": [(6, 0, [self.company1.id, self.company2.id])],
  34. "role_line_ids": [
  35. (0, 0, {"role_id": self.roleA.id}),
  36. (0, 0, {"role_id": self.roleB.id, "company_id": self.company1.id}),
  37. (0, 0, {"role_id": self.roleC.id, "company_id": self.company1.id}),
  38. (0, 0, {"role_id": self.roleC.id, "company_id": self.company2.id}),
  39. ],
  40. }
  41. self.test_user = self.User.create(user_vals)
  42. def test_110_company_1(self):
  43. "Company 1 selected: Roles A, B and C are enabled"
  44. self.test_user.set_groups_from_roles(company_id=self.company1.id)
  45. expected = self.groupA | self.groupB | self.groupC
  46. found = self.test_user.groups_id.filtered(lambda x: x in expected)
  47. self.assertEqual(expected, found)
  48. def test_120_company_2(self):
  49. "Company 2 selected: Roles A and C are enabled"
  50. self.test_user.set_groups_from_roles(company_id=self.company2.id)
  51. enabled = self.test_user.groups_id
  52. expected = self.groupA | self.groupC
  53. found = enabled.filtered(lambda x: x in expected)
  54. self.assertEqual(expected, found)
  55. not_expected = self.groupB
  56. found = enabled.filtered(lambda x: x in not_expected)
  57. self.assertFalse(found)