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.

164 lines
6.4 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. # ROLE_1
  15. self.group_user_id = self.env.ref('base.group_user')
  16. self.group_no_one_id = self.env.ref('base.group_no_one')
  17. vals = {
  18. 'name': "ROLE_1",
  19. 'implied_ids': [
  20. (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(
  27. 'base.group_multi_currency')
  28. self.group_settings_id = self.env.ref('base.group_system')
  29. vals = {
  30. 'name': "ROLE_2",
  31. 'implied_ids': [
  32. (6, 0, [self.group_user_id.id,
  33. self.group_multi_currency_id.id,
  34. self.group_settings_id.id])],
  35. }
  36. self.role2_id = self.role_model.create(vals)
  37. def test_role_1(self):
  38. self.user_id.write(
  39. {'role_line_ids': [(0, 0, {'role_id': self.role1_id.id})]})
  40. user_group_ids = sorted(set(
  41. [group.id for group in self.user_id.groups_id]))
  42. role_group_ids = self.role1_id.trans_implied_ids.ids
  43. role_group_ids.append(self.role1_id.group_id.id)
  44. role_group_ids = sorted(set(role_group_ids))
  45. self.assertEqual(user_group_ids, role_group_ids)
  46. def test_role_2(self):
  47. self.user_id.write(
  48. {'role_line_ids': [(0, 0, {'role_id': self.role2_id.id})]})
  49. user_group_ids = sorted(set(
  50. [group.id for group in self.user_id.groups_id]))
  51. role_group_ids = self.role2_id.trans_implied_ids.ids
  52. role_group_ids.append(self.role2_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_1_2(self):
  56. self.user_id.write(
  57. {'role_line_ids': [
  58. (0, 0, {'role_id': self.role1_id.id}),
  59. (0, 0, {'role_id': self.role2_id.id}),
  60. ]})
  61. user_group_ids = sorted(set(
  62. [group.id for group in self.user_id.groups_id]))
  63. role1_group_ids = self.role1_id.trans_implied_ids.ids
  64. role1_group_ids.append(self.role1_id.group_id.id)
  65. role2_group_ids = self.role2_id.trans_implied_ids.ids
  66. role2_group_ids.append(self.role2_id.group_id.id)
  67. role_group_ids = sorted(set(role1_group_ids + role2_group_ids))
  68. self.assertEqual(user_group_ids, role_group_ids)
  69. def test_role_1_2_with_dates(self):
  70. today_str = fields.Date.today()
  71. today = fields.Date.from_string(today_str)
  72. yesterday = today - datetime.timedelta(days=1)
  73. yesterday_str = fields.Date.to_string(yesterday)
  74. self.user_id.write(
  75. {'role_line_ids': [
  76. # Role 1 should be enabled
  77. (0, 0, {'role_id': self.role1_id.id, 'date_from': today_str}),
  78. # Role 2 should be disabled
  79. (0, 0,
  80. {'role_id': self.role2_id.id, 'date_to': yesterday_str}),
  81. ]})
  82. user_group_ids = sorted(set(
  83. [group.id for group in self.user_id.groups_id]))
  84. role1_group_ids = self.role1_id.trans_implied_ids.ids
  85. role1_group_ids.append(self.role1_id.group_id.id)
  86. role_group_ids = sorted(set(role1_group_ids))
  87. self.assertEqual(user_group_ids, role_group_ids)
  88. def test_role_unlink(self):
  89. # Get role1 groups
  90. role1_group_ids = self.role1_id.implied_ids.ids
  91. role1_group_ids.append(self.role1_id.group_id.id)
  92. role1_group_ids = sorted(set(role1_group_ids))
  93. # Configure the user with role1 and role2
  94. self.user_id.write(
  95. {'role_line_ids': [
  96. (0, 0, {'role_id': self.role1_id.id}),
  97. (0, 0, {'role_id': self.role2_id.id}),
  98. ]})
  99. # Remove role2
  100. self.role2_id.unlink()
  101. user_group_ids = sorted(set([
  102. group.id for group in self.user_id.groups_id]))
  103. self.assertEqual(user_group_ids, role1_group_ids)
  104. # Remove role1
  105. self.role1_id.unlink()
  106. user_group_ids = sorted(set([
  107. group.id for group in self.user_id.groups_id]))
  108. self.assertEqual(user_group_ids, [])
  109. def test_role_line_unlink(self):
  110. # Get role1 groups
  111. role1_group_ids = self.role1_id.implied_ids.ids
  112. role1_group_ids.append(self.role1_id.group_id.id)
  113. role1_group_ids = sorted(set(role1_group_ids))
  114. # Configure the user with role1 and role2
  115. self.user_id.write(
  116. {'role_line_ids': [
  117. (0, 0, {'role_id': self.role1_id.id}),
  118. (0, 0, {'role_id': self.role2_id.id}),
  119. ]})
  120. # Remove role2 from the user
  121. self.user_id.role_line_ids.filtered(
  122. lambda l: l.role_id.id == self.role2_id.id).unlink()
  123. user_group_ids = sorted(set([
  124. group.id for group in self.user_id.groups_id]))
  125. self.assertEqual(user_group_ids, role1_group_ids)
  126. # Remove role1 from the user
  127. self.user_id.role_line_ids.filtered(
  128. lambda l: l.role_id.id == self.role1_id.id).unlink()
  129. user_group_ids = sorted(set([
  130. group.id for group in self.user_id.groups_id]))
  131. self.assertEqual(user_group_ids, [])
  132. def test_default_user_roles(self):
  133. self.default_user.write({
  134. 'role_line_ids': [
  135. (0, 0, {
  136. 'role_id': self.role1_id.id,
  137. }),
  138. (0, 0, {
  139. 'role_id': self.role2_id.id,
  140. })
  141. ]
  142. })
  143. user = self.user_model.create({
  144. 'name': "USER TEST (DEFAULT ROLES)",
  145. 'login': 'user_test_default_roles'
  146. })
  147. roles = self.role_model.browse([self.role1_id.id, self.role2_id.id])
  148. self.assertEqual(user.role_ids, roles)