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.

104 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # This module copyright (C) 2015 Therp BV <http://therp.nl>.
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. import ldap
  21. from openerp.tests.common import TransactionCase
  22. class FakeLdapConnection(object):
  23. def __init__(self):
  24. self.entries = {}
  25. def simple_bind_s(self, dn, passwd):
  26. pass
  27. def add_s(self, dn, modlist):
  28. self.entries[dn] = modlist
  29. def search_s(self, dn, scope, ldap_filter, attributes):
  30. if dn in self.entries:
  31. return [(dn, dict(self.entries[dn]))]
  32. return None
  33. def modify_s(self, dn, modlist):
  34. if dn not in self.entries:
  35. raise ldap.NO_SUCH_OBJECT()
  36. for operation, attribute, value in modlist:
  37. if operation == ldap.MOD_ADD:
  38. self.entries[dn].append((attribute, value))
  39. continue
  40. def unbind_s(self):
  41. pass
  42. class TestUsersLdapPush(TransactionCase):
  43. def test_users_ldap_push(self):
  44. company = self.env['res.company'].create({
  45. 'name': 'testcompany',
  46. 'ldaps': [(0, 0, {
  47. 'ldap_base': 'dc=test',
  48. 'ldap_filter': '(uid=%s)',
  49. 'create_ldap_entry_field_mappings': [
  50. (
  51. 0, 0, {
  52. 'field_id':
  53. self.env.ref('base.field_res_users_login').id,
  54. 'attribute': 'userid',
  55. 'use_for_dn': True,
  56. },
  57. ),
  58. (
  59. 0, 0, {
  60. 'field_id':
  61. self.env.ref('base.field_res_users_name').id,
  62. 'attribute': 'sn',
  63. },
  64. ),
  65. ],
  66. })],
  67. })
  68. fake_ldap = FakeLdapConnection()
  69. self.env['res.company.ldap']._patch_method(
  70. 'connect', lambda x, y: fake_ldap)
  71. user = self.env['res.users'].create({
  72. 'name': 'testuser',
  73. 'login': 'testuser',
  74. 'company_ids': [(6, 0, company.ids)],
  75. 'company_id': company.id,
  76. 'is_ldap_user': False,
  77. })
  78. self.assertFalse(user.ldap_entry_dn)
  79. user.unlink()
  80. user = self.env['res.users'].create({
  81. 'name': 'testuser',
  82. 'login': 'testuser',
  83. 'company_ids': [(6, 0, company.ids)],
  84. 'company_id': company.id,
  85. 'is_ldap_user': True,
  86. })
  87. self.assertTrue(fake_ldap.entries[user.ldap_entry_dn])
  88. self.assertEqual(
  89. dict(fake_ldap.entries[user.ldap_entry_dn])['userid'],
  90. [user.login])
  91. user.partner_id.write({'name': 'testuser2'})
  92. self.assertTrue([
  93. v for a, v in fake_ldap.entries[user.ldap_entry_dn]
  94. if v == ['testuser2']
  95. ])