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.

226 lines
8.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. self.user_id.write(
  46. {"company_ids": [(4, self.company1.id, 0), (4, self.company2.id, 0)]}
  47. )
  48. def test_role_1(self):
  49. self.user_id.write({"role_line_ids": [(0, 0, {"role_id": self.role1_id.id})]})
  50. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  51. role_group_ids = self.role1_id.trans_implied_ids.ids
  52. role_group_ids.append(self.role1_id.group_id.id)
  53. role_group_ids = sorted(set(role_group_ids))
  54. self.assertEqual(user_group_ids, role_group_ids)
  55. def test_role_2(self):
  56. self.user_id.write({"role_line_ids": [(0, 0, {"role_id": self.role2_id.id})]})
  57. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  58. role_group_ids = self.role2_id.trans_implied_ids.ids
  59. role_group_ids.append(self.role2_id.group_id.id)
  60. role_group_ids = sorted(set(role_group_ids))
  61. self.assertEqual(user_group_ids, role_group_ids)
  62. def test_role_1_2(self):
  63. self.user_id.write(
  64. {
  65. "role_line_ids": [
  66. (0, 0, {"role_id": self.role1_id.id}),
  67. (0, 0, {"role_id": self.role2_id.id}),
  68. ]
  69. }
  70. )
  71. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  72. role1_group_ids = self.role1_id.trans_implied_ids.ids
  73. role1_group_ids.append(self.role1_id.group_id.id)
  74. role2_group_ids = self.role2_id.trans_implied_ids.ids
  75. role2_group_ids.append(self.role2_id.group_id.id)
  76. role_group_ids = sorted(set(role1_group_ids + role2_group_ids))
  77. self.assertEqual(user_group_ids, role_group_ids)
  78. def test_role_1_2_with_dates(self):
  79. today_str = fields.Date.today()
  80. today = fields.Date.from_string(today_str)
  81. yesterday = today - datetime.timedelta(days=1)
  82. yesterday_str = fields.Date.to_string(yesterday)
  83. self.user_id.write(
  84. {
  85. "role_line_ids": [
  86. # Role 1 should be enabled
  87. (0, 0, {"role_id": self.role1_id.id, "date_from": today_str}),
  88. # Role 2 should be disabled
  89. (0, 0, {"role_id": self.role2_id.id, "date_to": yesterday_str}),
  90. ]
  91. }
  92. )
  93. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  94. role1_group_ids = self.role1_id.trans_implied_ids.ids
  95. role1_group_ids.append(self.role1_id.group_id.id)
  96. role_group_ids = sorted(set(role1_group_ids))
  97. self.assertEqual(user_group_ids, role_group_ids)
  98. def test_role_unlink(self):
  99. # Get role1 groups
  100. role1_group_ids = self.role1_id.implied_ids.ids
  101. role1_group_ids.append(self.role1_id.group_id.id)
  102. role1_group_ids = sorted(set(role1_group_ids))
  103. # Configure the user with role1 and role2
  104. self.user_id.write(
  105. {
  106. "role_line_ids": [
  107. (0, 0, {"role_id": self.role1_id.id}),
  108. (0, 0, {"role_id": self.role2_id.id}),
  109. ]
  110. }
  111. )
  112. # Remove role2
  113. self.role2_id.unlink()
  114. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  115. self.assertEqual(user_group_ids, role1_group_ids)
  116. # Remove role1
  117. self.role1_id.unlink()
  118. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  119. self.assertEqual(user_group_ids, [])
  120. def test_role_line_unlink(self):
  121. # Get role1 groups
  122. role1_group_ids = self.role1_id.implied_ids.ids
  123. role1_group_ids.append(self.role1_id.group_id.id)
  124. role1_group_ids = sorted(set(role1_group_ids))
  125. # Configure the user with role1 and role2
  126. self.user_id.write(
  127. {
  128. "role_line_ids": [
  129. (0, 0, {"role_id": self.role1_id.id}),
  130. (0, 0, {"role_id": self.role2_id.id}),
  131. ]
  132. }
  133. )
  134. # Remove role2 from the user
  135. self.user_id.role_line_ids.filtered(
  136. lambda l: l.role_id.id == self.role2_id.id
  137. ).unlink()
  138. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  139. self.assertEqual(user_group_ids, role1_group_ids)
  140. # Remove role1 from the user
  141. self.user_id.role_line_ids.filtered(
  142. lambda l: l.role_id.id == self.role1_id.id
  143. ).unlink()
  144. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  145. self.assertEqual(user_group_ids, [])
  146. def test_default_user_roles(self):
  147. self.default_user.write(
  148. {
  149. "role_line_ids": [
  150. (0, 0, {"role_id": self.role1_id.id}),
  151. (0, 0, {"role_id": self.role2_id.id}),
  152. ]
  153. }
  154. )
  155. user = self.user_model.create(
  156. {"name": "USER TEST (DEFAULT ROLES)", "login": "user_test_default_roles"}
  157. )
  158. roles = self.role_model.browse([self.role1_id.id, self.role2_id.id])
  159. self.assertEqual(user.role_ids, roles)
  160. def test_user_role_different_company(self):
  161. self.user_id.write({"company_id": self.company1.id})
  162. self.user_id.write(
  163. {
  164. "role_line_ids": [
  165. (
  166. 0,
  167. 0,
  168. {"role_id": self.role2_id.id, "company_id": self.company2.id},
  169. )
  170. ]
  171. }
  172. )
  173. # Check that user does not have any groups
  174. self.assertEqual(self.user_id.groups_id, self.env["res.groups"].browse())
  175. def test_user_role_same_company(self):
  176. self.user_id.write({"company_id": self.company1.id})
  177. self.user_id.write(
  178. {
  179. "role_line_ids": [
  180. (
  181. 0,
  182. 0,
  183. {"role_id": self.role1_id.id, "company_id": self.company1.id},
  184. )
  185. ]
  186. }
  187. )
  188. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  189. role_group_ids = self.role1_id.trans_implied_ids.ids
  190. role_group_ids.append(self.role1_id.group_id.id)
  191. role_group_ids = sorted(set(role_group_ids))
  192. # Check that user have groups implied by role 1
  193. self.assertEqual(user_group_ids, role_group_ids)
  194. def test_user_role_no_company(self):
  195. self.user_id.write({"company_id": self.company1.id})
  196. self.user_id.write(
  197. {
  198. "role_line_ids": [
  199. (0, 0, {"role_id": self.role2_id.id, "company_id": False})
  200. ]
  201. }
  202. )
  203. user_group_ids = sorted({group.id for group in self.user_id.groups_id})
  204. role_group_ids = self.role2_id.trans_implied_ids.ids
  205. role_group_ids.append(self.role2_id.group_id.id)
  206. role_group_ids = sorted(set(role_group_ids))
  207. # Check that user have groups implied by role 2
  208. self.assertEqual(user_group_ids, role_group_ids)