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.

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