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.

71 lines
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2018 Therp BV <https://therp.nl>.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp.tests.common import TransactionCase
  5. from contextlib import contextmanager
  6. class PatchLDAPConnection(object):
  7. def __init__(self, results):
  8. self.results = results
  9. def simple_bind_s(self, user, password):
  10. return True
  11. def search_st(self, base, scope, ldap_filter, attributes, timeout=None):
  12. if ldap_filter == '(uid=*)':
  13. return self.results
  14. else:
  15. return []
  16. def unbind(self):
  17. return True
  18. @contextmanager
  19. def patch_ldap(self, results):
  20. """ defuse ldap functions to return fake entries instead of talking to a
  21. server. Use this in your own ldap related tests """
  22. import ldap
  23. original_initialize = ldap.initialize
  24. def initialize(uri):
  25. return PatchLDAPConnection(results)
  26. ldap.initialize = initialize
  27. yield
  28. ldap.initialize = original_initialize
  29. def get_fake_ldap(self):
  30. company = self.env.ref('base.main_company')
  31. company.write({
  32. 'ldaps': [(0, 0, {
  33. 'ldap_server': 'fake',
  34. 'ldap_server_port': 389,
  35. 'ldap_filter': '(uid=%s)',
  36. 'ldap_base': 'fake',
  37. 'deactivate_unknown_users': True,
  38. 'no_deactivate_user_ids': [(6, 0, [
  39. self.env.ref('base.user_root').id,
  40. ])],
  41. })],
  42. })
  43. return company.ldaps.filtered(
  44. lambda x: x.ldap_server == 'fake'
  45. )
  46. class TestUsersLdapPopulate(TransactionCase):
  47. def test_users_ldap_populate(self):
  48. with patch_ldap(self, [('DN=fake', {
  49. 'cn': ['fake'],
  50. 'uid': ['fake'],
  51. 'mail': ['fake@fakery.com'],
  52. })]):
  53. get_fake_ldap(self).populate_wizard()
  54. self.assertFalse(self.env.ref('base.user_demo').active)
  55. self.assertTrue(self.env.ref('base.user_root').active)
  56. self.assertTrue(self.env['res.users'].search([
  57. ('login', '=', 'fake')
  58. ]))