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.

65 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Therp BV <https://therp.nl>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from mock import Mock, patch
  5. from odoo.tests.common import TransactionCase
  6. @patch('ldap.initialize', return_value=Mock(
  7. search_st=Mock(return_value=[
  8. ('cn=hello', {'name': ['hello', 'hello2']})
  9. ]),
  10. ))
  11. class TestUsersLdapGroups(TransactionCase):
  12. def test_users_ldap_groups(self, ldap_initialize):
  13. # _login does its work in a new cursor, so we need one too
  14. with self.env.registry.cursor() as cr:
  15. env = self.env(cr=cr)
  16. group_contains = env['res.groups'].create({'name': 'contains'})
  17. group_equals = env['res.groups'].create({'name': 'equals'})
  18. group_query = env['res.groups'].create({'name': 'query'})
  19. env.ref('base.main_company').write({'ldaps': [(0, 0, {
  20. 'ldap_server': 'localhost',
  21. 'ldap_filter': '(&(objectClass=*),(uid=%s))',
  22. 'ldap_base': 'base',
  23. 'only_ldap_groups': True,
  24. 'group_mapping_ids': [
  25. (0, 0, {
  26. 'ldap_attribute': 'name',
  27. 'operator': 'contains',
  28. 'value': 'hello3',
  29. 'group_id': env.ref('base.group_system').id,
  30. }),
  31. (0, 0, {
  32. 'ldap_attribute': 'name',
  33. 'operator': 'contains',
  34. 'value': 'hello2',
  35. 'group_id': group_contains.id,
  36. }),
  37. (0, 0, {
  38. 'ldap_attribute': 'name',
  39. 'operator': 'equals',
  40. 'value': 'hello',
  41. 'group_id': group_equals.id,
  42. }),
  43. (0, 0, {
  44. 'ldap_attribute': '',
  45. 'operator': 'query',
  46. 'value': 'is not run because of patching',
  47. 'group_id': group_query.id,
  48. }),
  49. ],
  50. })]})
  51. self.env['res.users']._login(self.env.cr.dbname, 'demo', 'wrong')
  52. with self.env.registry.cursor() as cr:
  53. env = self.env(cr=cr)
  54. demo_user = env.ref('base.user_demo')
  55. # this asserts group mappings from demo data
  56. groups = demo_user.groups_id
  57. self.assertIn(group_contains, groups)
  58. self.assertNotIn(group_equals, groups)
  59. self.assertIn(group_query, groups)
  60. self.assertNotIn(env.ref('base.group_system'), groups)
  61. # clean up
  62. env.ref('base.main_company').write({'ldaps': [(6, False, [])]})