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.

96 lines
3.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 ABF OSIELL <http://osiell.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  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.user_id = self.user_model.create(
  13. {'name': u"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': u"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. self.group_multi_currency_id = self.env.ref(
  25. 'base.group_multi_currency')
  26. self.group_settings_id = self.env.ref('base.group_system')
  27. vals = {
  28. 'name': u"ROLE_2",
  29. 'implied_ids': [
  30. (6, 0, [self.group_multi_currency_id.id,
  31. self.group_settings_id.id])],
  32. }
  33. self.role2_id = self.role_model.create(vals)
  34. def test_role_1(self):
  35. self.user_id.write(
  36. {'role_line_ids': [(0, 0, {'role_id': self.role1_id.id})]})
  37. user_group_ids = sorted(set(
  38. [group.id for group in self.user_id.groups_id]))
  39. role_group_ids = self.role1_id.trans_implied_ids.ids
  40. role_group_ids.append(self.role1_id.group_id.id)
  41. role_group_ids = sorted(set(role_group_ids))
  42. self.assertEqual(user_group_ids, role_group_ids)
  43. def test_role_2(self):
  44. self.user_id.write(
  45. {'role_line_ids': [(0, 0, {'role_id': self.role2_id.id})]})
  46. user_group_ids = sorted(set(
  47. [group.id for group in self.user_id.groups_id]))
  48. role_group_ids = self.role2_id.trans_implied_ids.ids
  49. role_group_ids.append(self.role2_id.group_id.id)
  50. role_group_ids = sorted(set(role_group_ids))
  51. self.assertEqual(user_group_ids, role_group_ids)
  52. def test_role_1_2(self):
  53. self.user_id.write(
  54. {'role_line_ids': [
  55. (0, 0, {'role_id': self.role1_id.id}),
  56. (0, 0, {'role_id': self.role2_id.id}),
  57. ]})
  58. user_group_ids = sorted(set(
  59. [group.id for group in self.user_id.groups_id]))
  60. role1_group_ids = self.role1_id.trans_implied_ids.ids
  61. role1_group_ids.append(self.role1_id.group_id.id)
  62. role2_group_ids = self.role2_id.trans_implied_ids.ids
  63. role2_group_ids.append(self.role2_id.group_id.id)
  64. role_group_ids = sorted(set(role1_group_ids + role2_group_ids))
  65. self.assertEqual(user_group_ids, role_group_ids)
  66. def test_role_1_2_with_dates(self):
  67. today = datetime.date.today()
  68. today_str = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
  69. yesterday = today - datetime.timedelta(days=1)
  70. yesterday_str = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
  71. self.user_id.write(
  72. {'role_line_ids': [
  73. # Role 1 should be enabled
  74. (0, 0, {'role_id': self.role1_id.id, 'date_from': today_str}),
  75. # Role 2 should be disabled
  76. (0, 0,
  77. {'role_id': self.role2_id.id, 'date_to': yesterday_str}),
  78. ]})
  79. user_group_ids = sorted(set(
  80. [group.id for group in self.user_id.groups_id]))
  81. role1_group_ids = self.role1_id.trans_implied_ids.ids
  82. role1_group_ids.append(self.role1_id.group_id.id)
  83. role_group_ids = sorted(set(role1_group_ids))
  84. self.assertEqual(user_group_ids, role_group_ids)