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.

171 lines
6.7 KiB

  1. # Copyright 2014 ABF OSIELL <http://osiell.com>
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. import datetime
  4. from odoo import fields
  5. from odoo.tests.common import TransactionCase
  6. class TestUserRole(TransactionCase):
  7. def setUp(self):
  8. super(TestUserRole, self).setUp()
  9. self.user_model = self.env["res.users"]
  10. self.role_model = self.env["res.users.role"]
  11. self.default_user = self.env.ref("base.default_user")
  12. self.user_id = self.user_model.create(
  13. {"name": "USER TEST (ROLES)", "login": "user_test_roles"}
  14. )
  15. # ROLE_1
  16. self.group_user_id = self.env.ref("base.group_user")
  17. self.group_no_one_id = self.env.ref("base.group_no_one")
  18. vals = {
  19. "name": "ROLE_1",
  20. "implied_ids": [(6, 0, [self.group_user_id.id, self.group_no_one_id.id])],
  21. }
  22. self.role1_id = self.role_model.create(vals)
  23. # ROLE_2
  24. # Must have group_user in order to have sufficient groups. Check:
  25. # github.com/odoo/odoo/commit/c3717f3018ce0571aa41f70da4262cc946d883b4
  26. self.group_multi_currency_id = self.env.ref("base.group_multi_currency")
  27. self.group_settings_id = self.env.ref("base.group_system")
  28. vals = {
  29. "name": "ROLE_2",
  30. "implied_ids": [
  31. (
  32. 6,
  33. 0,
  34. [
  35. self.group_user_id.id,
  36. self.group_multi_currency_id.id,
  37. self.group_settings_id.id,
  38. ],
  39. )
  40. ],
  41. }
  42. self.role2_id = self.role_model.create(vals)
  43. self.company1 = self.env.ref("base.main_company")
  44. self.company2 = self.env["res.company"].create({"name": "company2"})
  45. def test_role_1(self):
  46. self.user_id.write({"role_line_ids": [(0, 0, {"role_id": self.role1_id.id})]})
  47. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  48. role_group_ids = self.role1_id.trans_implied_ids.ids
  49. role_group_ids.append(self.role1_id.group_id.id)
  50. role_group_ids = sorted(set(role_group_ids))
  51. self.assertEqual(user_group_ids, role_group_ids)
  52. def test_role_2(self):
  53. self.user_id.write({"role_line_ids": [(0, 0, {"role_id": self.role2_id.id})]})
  54. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  55. role_group_ids = self.role2_id.trans_implied_ids.ids
  56. role_group_ids.append(self.role2_id.group_id.id)
  57. role_group_ids = sorted(set(role_group_ids))
  58. self.assertEqual(user_group_ids, role_group_ids)
  59. def test_role_1_2(self):
  60. self.user_id.write(
  61. {
  62. "role_line_ids": [
  63. (0, 0, {"role_id": self.role1_id.id}),
  64. (0, 0, {"role_id": self.role2_id.id}),
  65. ]
  66. }
  67. )
  68. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  69. role1_group_ids = self.role1_id.trans_implied_ids.ids
  70. role1_group_ids.append(self.role1_id.group_id.id)
  71. role2_group_ids = self.role2_id.trans_implied_ids.ids
  72. role2_group_ids.append(self.role2_id.group_id.id)
  73. role_group_ids = sorted(set(role1_group_ids + role2_group_ids))
  74. self.assertEqual(user_group_ids, role_group_ids)
  75. def test_role_1_2_with_dates(self):
  76. today_str = fields.Date.today()
  77. today = fields.Date.from_string(today_str)
  78. yesterday = today - datetime.timedelta(days=1)
  79. yesterday_str = fields.Date.to_string(yesterday)
  80. self.user_id.write(
  81. {
  82. "role_line_ids": [
  83. # Role 1 should be enabled
  84. (0, 0, {"role_id": self.role1_id.id, "date_from": today_str}),
  85. # Role 2 should be disabled
  86. (0, 0, {"role_id": self.role2_id.id, "date_to": yesterday_str}),
  87. ]
  88. }
  89. )
  90. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  91. role1_group_ids = self.role1_id.trans_implied_ids.ids
  92. role1_group_ids.append(self.role1_id.group_id.id)
  93. role_group_ids = sorted(set(role1_group_ids))
  94. self.assertEqual(user_group_ids, role_group_ids)
  95. def test_role_unlink(self):
  96. # Get role1 groups
  97. role1_group_ids = self.role1_id.implied_ids.ids
  98. role1_group_ids.append(self.role1_id.group_id.id)
  99. role1_group_ids = sorted(set(role1_group_ids))
  100. # Configure the user with role1 and role2
  101. self.user_id.write(
  102. {
  103. "role_line_ids": [
  104. (0, 0, {"role_id": self.role1_id.id}),
  105. (0, 0, {"role_id": self.role2_id.id}),
  106. ]
  107. }
  108. )
  109. # Remove role2
  110. self.role2_id.unlink()
  111. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  112. self.assertEqual(user_group_ids, role1_group_ids)
  113. # Remove role1
  114. self.role1_id.unlink()
  115. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  116. self.assertEqual(user_group_ids, [])
  117. def test_role_line_unlink(self):
  118. # Get role1 groups
  119. role1_group_ids = self.role1_id.implied_ids.ids
  120. role1_group_ids.append(self.role1_id.group_id.id)
  121. role1_group_ids = sorted(set(role1_group_ids))
  122. # Configure the user with role1 and role2
  123. self.user_id.write(
  124. {
  125. "role_line_ids": [
  126. (0, 0, {"role_id": self.role1_id.id}),
  127. (0, 0, {"role_id": self.role2_id.id}),
  128. ]
  129. }
  130. )
  131. # Remove role2 from the user
  132. self.user_id.role_line_ids.filtered(
  133. lambda l: l.role_id.id == self.role2_id.id
  134. ).unlink()
  135. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  136. self.assertEqual(user_group_ids, role1_group_ids)
  137. # Remove role1 from the user
  138. self.user_id.role_line_ids.filtered(
  139. lambda l: l.role_id.id == self.role1_id.id
  140. ).unlink()
  141. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  142. self.assertEqual(user_group_ids, [])
  143. def test_default_user_roles(self):
  144. self.default_user.write(
  145. {
  146. "role_line_ids": [
  147. (0, 0, {"role_id": self.role1_id.id}),
  148. (0, 0, {"role_id": self.role2_id.id}),
  149. ]
  150. }
  151. )
  152. user = self.user_model.create(
  153. {"name": "USER TEST (DEFAULT ROLES)", "login": "user_test_default_roles"}
  154. )
  155. roles = self.role_model.browse([self.role1_id.id, self.role2_id.id])
  156. self.assertEqual(user.role_ids, roles)