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.

62 lines
2.6 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. # ROLES
  12. self.Role = self.env["res.users.role"]
  13. self.roleA = self.Role.create({"name": "ROLE All Companies"})
  14. self.roleB = self.Role.create({"name": "ROLE Company 1"})
  15. self.roleC = self.Role.create({"name": "ROLE Company 1 and 2"})
  16. # USER
  17. # ==Role=== ==Company== C1 C2 C1+C2
  18. # Role A Yes Yes Yes
  19. # Role B Company1 Yes
  20. # Role C Company1 Yes Yes
  21. # Role C Company2 Yes Yes
  22. self.User = self.env["res.users"]
  23. user_vals = {
  24. "name": "ROLES TEST USER",
  25. "login": "test_user",
  26. "company_ids": [(6, 0, [self.company1.id, self.company2.id])],
  27. "role_line_ids": [
  28. (0, 0, {"role_id": self.roleA.id}),
  29. (0, 0, {"role_id": self.roleB.id, "company_id": self.company1.id}),
  30. (0, 0, {"role_id": self.roleC.id, "company_id": self.company1.id}),
  31. (0, 0, {"role_id": self.roleC.id, "company_id": self.company2.id}),
  32. ],
  33. }
  34. self.test_user = self.User.create(user_vals)
  35. self.User = self.User.with_user(self.test_user)
  36. def test_110_company_1(self):
  37. "Company 1 selected: Tech and Settings roles are activated"
  38. self.User._set_session_active_roles([self.company1.id])
  39. active_roles = self.test_user.role_line_ids.filtered("active_role").mapped(
  40. "role_id"
  41. )
  42. self.assertEqual(active_roles, self.roleA | self.roleB | self.roleC)
  43. def test_120_company_2(self):
  44. "Company 2 selected: only Tech role enabled"
  45. self.User._set_session_active_roles([self.company2.id])
  46. active_roles = self.test_user.role_line_ids.filtered("active_role").mapped(
  47. "role_id"
  48. )
  49. self.assertEqual(active_roles, self.roleA | self.roleC)
  50. def test_130_company_1_2(self):
  51. "Settings Role enabled for Company 1 and 2"
  52. self.User._set_session_active_roles([self.company1.id, self.company2.id])
  53. active_roles = self.test_user.role_line_ids.filtered("active_role").mapped(
  54. "role_id"
  55. )
  56. self.assertEqual(active_roles, self.roleA | self.roleC)