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.

107 lines
4.7 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 openerp.tools import DEFAULT_SERVER_DATE_FORMAT
  6. from openerp.tests.common import TransactionCase
  7. class TestUserRole(TransactionCase):
  8. def setUp(self):
  9. super(TestUserRole, self).setUp()
  10. self.imd_model = self.registry('ir.model.data')
  11. self.user_model = self.registry('res.users')
  12. self.role_model = self.registry('res.users.role')
  13. self.user_id = self.user_model.create(
  14. self.cr, self.uid,
  15. {'name': u"USER TEST (ROLES)", 'login': 'user_test_roles'})
  16. # ROLE_1
  17. self.group_user_id = self.imd_model.get_object_reference(
  18. self.cr, self.uid, 'base', 'group_user')[1]
  19. self.group_no_one_id = self.imd_model.get_object_reference(
  20. self.cr, self.uid, 'base', 'group_no_one')[1]
  21. vals = {
  22. 'name': u"ROLE_1",
  23. 'implied_ids': [6, 0, [self.group_user_id, self.group_no_one_id]],
  24. }
  25. self.role1_id = self.role_model.create(self.cr, self.uid, vals)
  26. # ROLE_2
  27. self.group_multi_currency_id = self.imd_model.get_object_reference(
  28. self.cr, self.uid, 'base', 'group_multi_currency')[1]
  29. self.group_sale_manager_id = self.imd_model.get_object_reference(
  30. self.cr, self.uid, 'base', 'group_sale_manager')[1]
  31. vals = {
  32. 'name': u"ROLE_2",
  33. 'implied_ids': [6, 0, [self.group_multi_currency_id,
  34. self.group_sale_manager_id]],
  35. }
  36. self.role2_id = self.role_model.create(self.cr, self.uid, vals)
  37. def test_role_1(self):
  38. role1 = self.role_model.browse(self.cr, self.uid, self.role1_id)
  39. self.user_model.write(
  40. self.cr, self.uid, [self.user_id],
  41. {'role_line_ids': [(0, 0, {'role_id': self.role1_id})]})
  42. user = self.user_model.browse(self.cr, self.uid, self.user_id)
  43. user_group_ids = sorted(set([group.id for group in user.groups_id]))
  44. role_group_ids = role1.implied_ids.ids
  45. role_group_ids.append(role1.group_id.id)
  46. role_group_ids = sorted(set(role_group_ids))
  47. self.assertEqual(user_group_ids, role_group_ids)
  48. def test_role_2(self):
  49. role2 = self.role_model.browse(self.cr, self.uid, self.role2_id)
  50. self.user_model.write(
  51. self.cr, self.uid, [self.user_id],
  52. {'role_line_ids': [(0, 0, {'role_id': self.role2_id})]})
  53. user = self.user_model.browse(self.cr, self.uid, self.user_id)
  54. user_group_ids = sorted(set([group.id for group in user.groups_id]))
  55. role_group_ids = role2.implied_ids.ids
  56. role_group_ids.append(role2.group_id.id)
  57. role_group_ids = sorted(set(role_group_ids))
  58. self.assertEqual(user_group_ids, role_group_ids)
  59. def test_role_1_2(self):
  60. role1 = self.role_model.browse(self.cr, self.uid, self.role1_id)
  61. role2 = self.role_model.browse(self.cr, self.uid, self.role2_id)
  62. self.user_model.write(
  63. self.cr, self.uid, [self.user_id],
  64. {'role_line_ids': [
  65. (0, 0, {'role_id': self.role1_id}),
  66. (0, 0, {'role_id': self.role2_id}),
  67. ]})
  68. user = self.user_model.browse(self.cr, self.uid, self.user_id)
  69. user_group_ids = sorted(set([group.id for group in user.groups_id]))
  70. role1_group_ids = role1.implied_ids.ids
  71. role1_group_ids.append(role1.group_id.id)
  72. role2_group_ids = role2.implied_ids.ids
  73. role2_group_ids.append(role2.group_id.id)
  74. role_group_ids = sorted(set(role1_group_ids + role2_group_ids))
  75. self.assertEqual(user_group_ids, role_group_ids)
  76. def test_role_1_2_with_dates(self):
  77. role1 = self.role_model.browse(self.cr, self.uid, self.role1_id)
  78. today = datetime.date.today()
  79. today_str = today.strftime(DEFAULT_SERVER_DATE_FORMAT)
  80. yesterday = today - datetime.timedelta(days=1)
  81. yesterday_str = yesterday.strftime(DEFAULT_SERVER_DATE_FORMAT)
  82. self.user_model.write(
  83. self.cr, self.uid, [self.user_id],
  84. {'role_line_ids': [
  85. # Role 1 should be enabled
  86. (0, 0, {'role_id': self.role1_id, 'date_from': today_str}),
  87. # Role 2 should be disabled
  88. (0, 0, {'role_id': self.role2_id, 'date_to': yesterday_str}),
  89. ]})
  90. user = self.user_model.browse(self.cr, self.uid, self.user_id)
  91. user_group_ids = sorted(set([group.id for group in user.groups_id]))
  92. role1_group_ids = role1.implied_ids.ids
  93. role1_group_ids.append(role1.group_id.id)
  94. role_group_ids = sorted(set(role1_group_ids))
  95. self.assertEqual(user_group_ids, role_group_ids)