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.

156 lines
6.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 ABF OSIELL <http://osiell.com>
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. import datetime
  5. from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
  6. from odoo.tests.common import TransactionCase
  7. class TestUserRole(TransactionCase):
  8. def setUp(self):
  9. super(TestUserRole, self).setUp()
  10. self.user_model = self.env['res.users']
  11. self.role_model = self.env['res.users.role']
  12. self.default_user = self.env.ref('base.default_user')
  13. self.user_id = self.user_model.create(
  14. {'name': u"USER TEST (ROLES)", 'login': 'user_test_roles'})
  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': u"ROLE_1",
  20. 'implied_ids': [
  21. (6, 0, [self.group_user_id.id, self.group_no_one_id.id])],
  22. }
  23. self.role1_id = self.role_model.create(vals)
  24. # ROLE_2
  25. self.group_multi_currency_id = self.env.ref(
  26. 'base.group_multi_currency')
  27. self.group_settings_id = self.env.ref('base.group_system')
  28. vals = {
  29. 'name': u"ROLE_2",
  30. 'implied_ids': [
  31. (6, 0, [self.group_multi_currency_id.id,
  32. self.group_settings_id.id])],
  33. }
  34. self.role2_id = self.role_model.create(vals)
  35. self.company1 = self.env.ref('base.main_company')
  36. self.company2 = self.env['res.company'].create({'name': 'company2'})
  37. self.user_id.write(
  38. {'company_ids': [
  39. (4, self.company1.id, 0), (4, self.company2.id, 0)]})
  40. def test_role_1(self):
  41. self.user_id.write(
  42. {'role_line_ids': [(0, 0, {'role_id': self.role1_id.id})]})
  43. user_group_ids = sorted(set(
  44. [group.id for group in self.user_id.groups_id]))
  45. role_group_ids = self.role1_id.trans_implied_ids.ids
  46. role_group_ids.append(self.role1_id.group_id.id)
  47. role_group_ids = sorted(set(role_group_ids))
  48. self.assertEqual(user_group_ids, role_group_ids)
  49. def test_role_2(self):
  50. self.user_id.write(
  51. {'role_line_ids': [(0, 0, {'role_id': self.role2_id.id})]})
  52. user_group_ids = sorted(set(
  53. [group.id for group in self.user_id.groups_id]))
  54. role_group_ids = self.role2_id.trans_implied_ids.ids
  55. role_group_ids.append(self.role2_id.group_id.id)
  56. role_group_ids = sorted(set(role_group_ids))
  57. self.assertEqual(user_group_ids, role_group_ids)
  58. def test_role_1_2(self):
  59. self.user_id.write(
  60. {'role_line_ids': [
  61. (0, 0, {'role_id': self.role1_id.id}),
  62. (0, 0, {'role_id': self.role2_id.id}),
  63. ]})
  64. user_group_ids = sorted(set(
  65. [group.id for group in self.user_id.groups_id]))
  66. role1_group_ids = self.role1_id.trans_implied_ids.ids
  67. role1_group_ids.append(self.role1_id.group_id.id)
  68. role2_group_ids = self.role2_id.trans_implied_ids.ids
  69. role2_group_ids.append(self.role2_id.group_id.id)
  70. role_group_ids = sorted(set(role1_group_ids + role2_group_ids))
  71. self.assertEqual(user_group_ids, role_group_ids)
  72. def test_role_1_2_with_dates(self):
  73. today = datetime.date.today()
  74. today_str = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
  75. yesterday = today - datetime.timedelta(days=1)
  76. yesterday_str = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
  77. self.user_id.write(
  78. {'role_line_ids': [
  79. # Role 1 should be enabled
  80. (0, 0, {'role_id': self.role1_id.id, 'date_from': today_str}),
  81. # Role 2 should be disabled
  82. (0, 0,
  83. {'role_id': self.role2_id.id, 'date_to': yesterday_str}),
  84. ]})
  85. user_group_ids = sorted(set(
  86. [group.id for group in self.user_id.groups_id]))
  87. role1_group_ids = self.role1_id.trans_implied_ids.ids
  88. role1_group_ids.append(self.role1_id.group_id.id)
  89. role_group_ids = sorted(set(role1_group_ids))
  90. self.assertEqual(user_group_ids, role_group_ids)
  91. def test_default_user_roles(self):
  92. self.default_user.write({
  93. 'role_line_ids': [
  94. (0, 0, {
  95. 'role_id': self.role1_id.id,
  96. }),
  97. (0, 0, {
  98. 'role_id': self.role2_id.id,
  99. })
  100. ]
  101. })
  102. user = self.user_model.create({
  103. 'name': "USER TEST (DEFAULT ROLES)",
  104. 'login': 'user_test_default_roles'
  105. })
  106. roles = self.role_model.browse([self.role1_id.id, self.role2_id.id])
  107. self.assertEqual(user.role_ids, roles)
  108. def test_user_role_different_company(self):
  109. self.user_id.write({'company_id': self.company1.id})
  110. self.user_id.write({'role_line_ids': [(0, 0, {
  111. 'role_id': self.role2_id.id,
  112. 'company_id': self.company2.id})]})
  113. # Check that user does not have any groups
  114. self.assertEquals(
  115. self.user_id.groups_id, self.env['res.groups'].browse())
  116. def test_user_role_same_company(self):
  117. self.user_id.write({'company_id': self.company1.id})
  118. self.user_id.write({'role_line_ids': [(0, 0, {
  119. 'role_id': self.role1_id.id,
  120. 'company_id': self.company1.id})]})
  121. user_group_ids = sorted(set(
  122. [group.id for group in self.user_id.groups_id]))
  123. role_group_ids = self.role1_id.trans_implied_ids.ids
  124. role_group_ids.append(self.role1_id.group_id.id)
  125. role_group_ids = sorted(set(role_group_ids))
  126. # Check that user have groups implied by role 1
  127. self.assertEqual(user_group_ids, role_group_ids)
  128. def test_user_role_no_company(self):
  129. self.user_id.write({'company_id': self.company1.id})
  130. self.user_id.write({'role_line_ids': [(0, 0, {
  131. 'role_id': self.role2_id.id,
  132. 'company_id': False})]})
  133. user_group_ids = sorted(set(
  134. [group.id for group in self.user_id.groups_id]))
  135. role_group_ids = self.role2_id.trans_implied_ids.ids
  136. role_group_ids.append(self.role2_id.group_id.id)
  137. role_group_ids = sorted(set(role_group_ids))
  138. # Check that user have groups implied by role 2
  139. self.assertEqual(user_group_ids, role_group_ids)