Skip to main content

An implementation of a LDAPObject to fake a ldap server in unittests.

Project description

The goal of this module is to provide a simple way to mock ldap backend servers for your unittests. It makes it possible to define upfront a set of directory entries that can be queried or set fixed return values to ldap queries. It acts as a drop in replacement for the LDAPObject class of the python-ldap module. It implements a subset of the allowed methods of this class.

This module implements the MockLDAP class that functions both as the LDAPObject as well as the ldap module. Most of the code and design has been taken from Peter Sagerson’s excellent django-auth-ldap module.

Installation

Get and install the code:

$ git clone git://github.com/zulip/fakeldap.git
$ cd fakeldap
$ python setup.py install

If you want, you can run the tests:

$ python setup.py nosetests

Usage

The MockLDAP class replaces the LDAPObject of the python-ldap module. The easiest way to use it, is to overwrite ldap.initialize to return MockLDAP instead of LDAPObject. The example below uses Michael Foord’s Mock library to achieve that:

import unittest
from mock import patch
from fakeldap import MockLDAP


_mock_ldap = MockLDAP()

class YourTestCase(unittest.TestCase):
    def setUp(self):
        # Patch where the ldap library is used:
        self.ldap_patcher = patch('app.module.ldap.initialize')
        self.mock_ldap = self.ldap_patcher.start()
        self.mock_ldap.return_value = _mock_ldap

    def tearDown(self):
        _mock_ldap.reset()
        self.mock_ldap.stop()

The mock ldap object implements the following ldap operations:

  • simple_bind_s

  • search_s

  • compare_s

  • modify_s

  • delete_s

  • add_s

  • rename_s

This is an example how to use MockLDAP with fixed return values:

def test_some_ldap_group_stuff(self):
    # Define the expected return value for the ldap operation
    return_value = ("cn=testgroup,ou=group,dc=30loops,dc=net", {
        'objectClass': ['posixGroup'],
        'cn': 'testgroup',
        'gidNumber': '2030',
    })

    # Register a return value with the MockLDAP object
    _mock_ldap.set_return_value('add_s',
        ("cn=testgroup,ou=groups,dc=30loops,dc=net", (
            ('objectClass', ('posixGroup')),
            ('cn', 'testgroup'),
            ('gidNumber', '2030'))),
        (105,[], 10, []))

    # Run your actual code, this is just an example
    group_manager = GroupManager()
    result = group_manager.add("testgroup")

    # assert that the return value of your method and of the MockLDAP
    # are as expected, here using python-nose's eq() test tool:
    eq_(return_value, result)

    # Each actual ldap call your software makes gets recorded. You could
    # prepare a list of calls that you expect to be issued and compare it:
    called_records = []

    called_records.append(('simple_bind_s',
        {'who': 'cn=admin,dc=30loops,dc=net', 'cred': 'ldaptest'}))

    called_records.append(('add_s', {
        'dn': 'cn=testgroup,ou=groups,dc=30loops,dc=net",
        'record': [
            ('objectClass', ['posixGroup']),
            ('gidNumber', '2030'),
            ('cn', 'testgroup'),
            ]}))

    # And again test the expected behaviour
    eq_(called_records, _mock_ldap.ldap_methods_called_with_arguments())

Besides of fixing return values for specific calls, you can also imitate a full ldap server with a directory of entries:

# Create an instance of MockLDAP with a preset directory
tree = {
    "cn=admin,dc=30loops,dc=net": {
            "userPassword": "ldaptest"
    }
}
mock_ldap = MockLDAP(tree)

record = [
    ('uid', 'crito'),
    ('userPassword', 'secret'),
]
# The return value I expect when I add another record to the directory
eq_(
    (105,[],1,[]),
    mock_ldap.add_s("uid=crito,ou=people,dc=30loops,dc=net", record)
)

# The expected directory
directory = {
    "cn=admin,dc=30loops,dc=net": {"userPassword": "ldaptest"},
    "uid=crito,ou=people,dc=30loops,dc=net": {
        "uid": "crito", "userPassword": "secret"}
}
# Compare the expected directory with the MockLDAP directory
eq_(directory, mock_ldap.directory)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fakeldap-0.6.4.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

fakeldap-0.6.4-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fakeldap-0.6.4.tar.gz.

File metadata

  • Download URL: fakeldap-0.6.4.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.2.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.9

File hashes

Hashes for fakeldap-0.6.4.tar.gz
Algorithm Hash digest
SHA256 ac5ff6431998c080b7d94a5190dd6e1ba8e48bbcf19e9360669855950886e12b
MD5 7c899bac27fbc1fb316a7f00aafbf7c4
BLAKE2b-256 980c8423d69eb1f1a7e8dc7f56628252bfab4cf9f49bba1b6a82d528379460c4

See more details on using hashes here.

File details

Details for the file fakeldap-0.6.4-py2.py3-none-any.whl.

File metadata

  • Download URL: fakeldap-0.6.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.2.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.9

File hashes

Hashes for fakeldap-0.6.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4e5e19aee88af41b2c5fd0bc4089374aa9d66d64e4a27b305a21f0b0f089841d
MD5 fc59881d5f04db12f90f3c907be8b8fb
BLAKE2b-256 0a386684fc8e3158d84742ecc2ff616ff53d7eb1f435991789b2acbb106dd4f2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page