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.

127 lines
4.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from lxml import etree
  5. from odoo.exceptions import AccessError, ValidationError
  6. from .common import Common, MAX_DB_USER_PARAM
  7. class TestResUsers(Common):
  8. def setUp(self):
  9. super(TestResUsers, self).setUp()
  10. self.env['ir.config_parameter'].set_param(MAX_DB_USER_PARAM, '0')
  11. def test_copy_global(self):
  12. """
  13. It should restrict the user count in copy() as prescribed by the
  14. global threshold parameter
  15. """
  16. self.env['ir.config_parameter'].set_param(MAX_DB_USER_PARAM, 3)
  17. self._create_test_user()
  18. with self.assertRaises(ValidationError):
  19. self._create_test_user()
  20. def test_create_global(self):
  21. """
  22. It should restrict the user count as prescribed by the global
  23. threshold parameter
  24. """
  25. self.env['ir.config_parameter'].set_param(MAX_DB_USER_PARAM, 3)
  26. self._create_test_user()
  27. with self.assertRaises(ValidationError):
  28. self.env['res.users'].create({
  29. 'login': 'Derp Derpington',
  30. 'email': 'dderpington@example.com',
  31. 'notify_email': 'always',
  32. })
  33. def test_copy_company(self):
  34. """
  35. It should restrict the user count in copy() as prescribed by the
  36. companies threshold parameter
  37. """
  38. c = self.env['res.company'].browse(1)
  39. c.max_users = 3
  40. self._create_test_user()
  41. with self.assertRaises(ValidationError):
  42. self._create_test_user()
  43. def test_create_company(self):
  44. """
  45. It should restrict the user count as prescribed by the companies
  46. threshold parameter
  47. """
  48. c = self.env['res.company'].browse(1)
  49. c.max_users = 3
  50. self._create_test_user()
  51. with self.assertRaises(ValidationError):
  52. self.env['res.users'].create({
  53. 'login': 'Derp Derpington',
  54. 'email': 'dderpington@example.com',
  55. 'notify_email': 'always',
  56. })
  57. def test_fields_view_get(self):
  58. """
  59. It should verify that setting THRESHOLD_HIDE removes the parameter
  60. from the view
  61. """
  62. import odoo.addons.user_threshold.models.res_users as mdl
  63. mdl.THRESHOLD_HIDE = True
  64. view = self.env.ref('user_threshold.view_users_form')
  65. u = self._create_test_user()
  66. ret = u.fields_view_get(view.id)
  67. doc = etree.XML(ret['arch'])
  68. self.assertEquals(doc.xpath("//group[@name='user_threshold']"), [])
  69. def test_cannot_write_exempt(self):
  70. """
  71. It should restrict the threshold exempt parameter to Threshold
  72. Managers
  73. """
  74. u = self._create_test_user()
  75. tu = self._create_test_user()
  76. with self.assertRaises(AccessError):
  77. tu.sudo(u.id).write({'threshold_exempt': True})
  78. def test_can_write_exempt(self):
  79. """
  80. It should restrict the threshold exempt parameter to Threshold
  81. Managers
  82. """
  83. u = self._create_test_user()
  84. self._add_user_to_group(u)
  85. tu = self._create_test_user()
  86. tu.sudo(u.id).write({'threshold_exempt': True})
  87. self.assertEquals(tu.threshold_exempt, True)
  88. def test_cannot_write_group(self):
  89. """
  90. It should restrict additions to the Threshold Managers to users in
  91. that group
  92. """
  93. u = self._create_test_user()
  94. u.write({
  95. 'in_group_%s' % self.env.ref('base.group_erp_manager').id: True
  96. })
  97. tu = self._create_test_user()
  98. th_group = self.env.ref('user_threshold.group_threshold_manager')
  99. with self.assertRaises(AccessError):
  100. tu.sudo(u.id).write({'in_group_%s' % th_group.id: True})
  101. def test_can_write_group(self):
  102. """
  103. It should restrict additions to the Threshold Managers to users in
  104. that group
  105. """
  106. u = self._create_test_user()
  107. self._add_user_to_group(u)
  108. tu = self._create_test_user()
  109. th_group = self.env.ref('user_threshold.group_threshold_manager')
  110. tu.sudo(u.id).write({'in_group_%s' % th_group.id: True})
  111. self.assertEquals(
  112. tu.has_group('user_threshold.group_threshold_manager'), True
  113. )