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.

112 lines
4.5 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  2. from odoo.tests.common import TransactionCase
  3. class TestUserProfile(TransactionCase):
  4. def _helper_unpack_groups_role(self, role):
  5. role_group_ids = role.trans_implied_ids.ids
  6. role_group_ids.append(role.group_id.id)
  7. return sorted(set(role_group_ids))
  8. def _helper_unpack_groups_group(self, group):
  9. group_ids = group.trans_implied_ids.ids
  10. group_ids.append(group.id)
  11. return sorted(set(group_ids))
  12. def setUp(self):
  13. super().setUp()
  14. self.user_model = self.env["res.users"]
  15. self.role_model = self.env["res.users.role"]
  16. self.default_user = self.env.ref("base.default_user")
  17. user_vals = {
  18. "name": "USER TEST (ROLES)",
  19. "login": "user_test_roles",
  20. }
  21. self.user_id = self.user_model.create(user_vals)
  22. self.profile1_id = self.env["res.users.profile"].create({"name": "profile1"})
  23. self.profile2_id = self.env["res.users.profile"].create({"name": "profile2"})
  24. # role 1
  25. self.group_user_id = self.env.ref("base.group_user")
  26. self.group_no_one_id = self.env.ref("base.group_no_one")
  27. # role 2
  28. self.group_system_id = self.env.ref("base.group_system")
  29. self.group_multi_company_id = self.env.ref("base.group_multi_company")
  30. # role 3
  31. self.group_erp_manager_id = self.env.ref("base.group_erp_manager")
  32. self.group_partner_manager_id = self.env.ref("base.group_partner_manager")
  33. # roles 1 and 2 have a profile, role 3 no profile
  34. vals = {
  35. "name": "ROLE_1",
  36. "implied_ids": [(6, 0, [self.group_user_id.id, self.group_no_one_id.id])],
  37. "profile_id": self.profile1_id.id,
  38. }
  39. self.role1_id = self.role_model.create(vals)
  40. self.role1_group_ids = self._helper_unpack_groups_role(self.role1_id)
  41. vals = {
  42. "name": "ROLE_2",
  43. "implied_ids": [
  44. (
  45. 6,
  46. 0,
  47. [self.group_system_id.id, self.group_multi_company_id.id],
  48. )
  49. ],
  50. "profile_id": self.profile2_id.id,
  51. }
  52. self.role2_id = self.role_model.create(vals)
  53. self.role2_group_ids = self._helper_unpack_groups_role(self.role2_id)
  54. vals = {
  55. "name": "ROLE_3",
  56. "implied_ids": [
  57. (
  58. 6,
  59. 0,
  60. [
  61. self.group_erp_manager_id.id,
  62. self.group_partner_manager_id.id,
  63. ],
  64. )
  65. ],
  66. }
  67. self.role3_id = self.role_model.create(vals)
  68. self.role3_group_ids = self._helper_unpack_groups_role(self.role3_id)
  69. def test_filter_by_profile(self):
  70. line1_vals = {"role_id": self.role1_id.id, "user_id": self.user_id.id}
  71. self.user_id.write({"role_line_ids": [(0, 0, line1_vals)]})
  72. line2_vals = {"role_id": self.role2_id.id, "user_id": self.user_id.id}
  73. self.user_id.write({"role_line_ids": [(0, 0, line2_vals)]})
  74. self.assertEqual(self.user_id.profile_ids, self.profile1_id + self.profile2_id)
  75. self.assertEqual(self.user_id.profile_id, self.profile1_id)
  76. self.user_id.action_profile_change({"profile_id": self.profile1_id.id})
  77. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  78. expected_group_ids = sorted(set(self.role1_group_ids))
  79. self.assertEqual(user_group_ids, expected_group_ids)
  80. self.user_id.action_profile_change({"profile_id": self.profile2_id.id})
  81. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  82. expected_group_ids = sorted(set(self.role2_group_ids))
  83. self.assertEqual(user_group_ids, expected_group_ids)
  84. def test_allow_by_noprofile(self):
  85. line1_vals = {"role_id": self.role1_id.id, "user_id": self.user_id.id}
  86. self.user_id.write({"role_line_ids": [(0, 0, line1_vals)]})
  87. line2_vals = {"role_id": self.role3_id.id, "user_id": self.user_id.id}
  88. self.user_id.write({"role_line_ids": [(0, 0, line2_vals)]})
  89. self.assertEqual(self.user_id.profile_ids, self.profile1_id)
  90. user_group_ids = []
  91. for group in self.user_id.groups_id:
  92. user_group_ids += self._helper_unpack_groups_group(group)
  93. user_group_ids = set(user_group_ids)
  94. expected_groups = set(self.role1_group_ids + self.role3_group_ids)
  95. self.assertEqual(user_group_ids, expected_groups)