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.

116 lines
4.4 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. def test_role_1(self):
  36. self.user_id.write(
  37. {'role_line_ids': [(0, 0, {'role_id': self.role1_id.id})]})
  38. user_group_ids = sorted(set(
  39. [group.id for group in self.user_id.groups_id]))
  40. role_group_ids = self.role1_id.trans_implied_ids.ids
  41. role_group_ids.append(self.role1_id.group_id.id)
  42. role_group_ids = sorted(set(role_group_ids))
  43. self.assertEqual(user_group_ids, role_group_ids)
  44. def test_role_2(self):
  45. self.user_id.write(
  46. {'role_line_ids': [(0, 0, {'role_id': self.role2_id.id})]})
  47. user_group_ids = sorted(set(
  48. [group.id for group in self.user_id.groups_id]))
  49. role_group_ids = self.role2_id.trans_implied_ids.ids
  50. role_group_ids.append(self.role2_id.group_id.id)
  51. role_group_ids = sorted(set(role_group_ids))
  52. self.assertEqual(user_group_ids, role_group_ids)
  53. def test_role_1_2(self):
  54. self.user_id.write(
  55. {'role_line_ids': [
  56. (0, 0, {'role_id': self.role1_id.id}),
  57. (0, 0, {'role_id': self.role2_id.id}),
  58. ]})
  59. user_group_ids = sorted(set(
  60. [group.id for group in self.user_id.groups_id]))
  61. role1_group_ids = self.role1_id.trans_implied_ids.ids
  62. role1_group_ids.append(self.role1_id.group_id.id)
  63. role2_group_ids = self.role2_id.trans_implied_ids.ids
  64. role2_group_ids.append(self.role2_id.group_id.id)
  65. role_group_ids = sorted(set(role1_group_ids + role2_group_ids))
  66. self.assertEqual(user_group_ids, role_group_ids)
  67. def test_role_1_2_with_dates(self):
  68. today = datetime.date.today()
  69. today_str = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
  70. yesterday = today - datetime.timedelta(days=1)
  71. yesterday_str = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
  72. self.user_id.write(
  73. {'role_line_ids': [
  74. # Role 1 should be enabled
  75. (0, 0, {'role_id': self.role1_id.id, 'date_from': today_str}),
  76. # Role 2 should be disabled
  77. (0, 0,
  78. {'role_id': self.role2_id.id, 'date_to': yesterday_str}),
  79. ]})
  80. user_group_ids = sorted(set(
  81. [group.id for group in self.user_id.groups_id]))
  82. role1_group_ids = self.role1_id.trans_implied_ids.ids
  83. role1_group_ids.append(self.role1_id.group_id.id)
  84. role_group_ids = sorted(set(role1_group_ids))
  85. self.assertEqual(user_group_ids, role_group_ids)
  86. def test_default_user_roles(self):
  87. self.default_user.write({
  88. 'role_line_ids': [
  89. (0, 0, {
  90. 'role_id': self.role1_id.id,
  91. }),
  92. (0, 0, {
  93. 'role_id': self.role2_id.id,
  94. })
  95. ]
  96. })
  97. user = self.user_model.create({
  98. 'name': "USER TEST (DEFAULT ROLES)",
  99. 'login': 'user_test_default_roles'
  100. })
  101. roles = self.role_model.browse([self.role1_id.id, self.role2_id.id])
  102. self.assertEqual(user.role_ids, roles)