Skip to main content

Tool for testing code speaking with LDAP. Allows to easily configure and run an embedded, in-memory LDAP server. Uses UnboundID LDAP SDK through Py4J.

Project description

Tool for testing code speaking with LDAP. Allows to easily configure and run an embedded, in-memory LDAP server. Uses UnboundID LDAP SDK through Py4J. Requires Java runtime on the system path to run the server.

Installation

With pip:

pip install python-ldap-test

When installing from source:

git clone https://github.com/zoldar/python-ldap-test
cd python-ldap-test
python setup.py install # you may need root privileges if installing system-wide

Usage

Example library usage with Python ldap client.

from ldap_test import LdapServer

server = LdapServer()

try:
    server.start()

    dn = server.config['bind_dn']
    pw = server.config['password']

    con = ldap.initialize('ldap://localhost:%s' % (server.config['port'],))
    con.simple_bind_s(dn, pw)

    base_dn = server.config['base']['dn']
    filter = '(objectclass=domain)'
    attrs = ['dc']

    print con.search_s(base_dn, ldap.SCOPE_SUBTREE, filter, attrs)

finally:
    server.stop()

Another example with non-standard settings:

from ldap_test import LdapServer

server = LdapServer({
    'port': 3333,
    'bind_dn': 'cn=admin,dc=zoldar,dc=net',
    'password': 'pass1',
    'base': {'objectclass': ['domain'],
             'dn': 'dc=zoldar,dc=net',
             'attributes': {'dc': 'zoldar'}},
    'entries': [
        {'objectclass': 'domain',
         'dn': 'dc=users,dc=zoldar,dc=net',
         'attributes': {'dc': 'users'}},
        {'objectclass': 'organization',
         'dn': 'o=foocompany,dc=users,dc=zoldar,dc=net',
         'attributes': {'o': 'foocompany'}},
    ]
})

try:
    server.start()

    dn = "cn=admin,dc=zoldar,dc=net"
    pw = "pass1"

    con = ldap.initialize('ldap://localhost:3333')
    con.simple_bind_s(dn, pw)

    base_dn = 'dc=zoldar,dc=net'
    filter = '(objectclass=domain)'
    attrs = ['o']

    print con.search_s(base_dn, ldap.SCOPE_SUBTREE, filter, attrs)

finally:
    server.stop()

The server initial configuration is represented by a simple dict, which may contain one or more optional parameters:

  • port - a port on which the LDAP server will listen
  • bind_dn - bind DN entry for authentication
  • password - authentication password
  • base - base DN entry
  • entries - a list of dicts representing intially loaded entries in the database. attributes are optional here
  • ldifs - a list of strings representing file paths to the LDIF files to load on start, like ..., 'ldifs': ['path/to/file1.ldif', 'path/to/file2.ldif'], ...

The format of entry in entries as well as base is following:

{'dn': 'o=some,dc=example,dc=com', # DN identifying the entry
 'objectclass': ['top', 'organization'], # objectclass may be either a
                                         # string in case of a single
                                         # class or a list of classes
 'attributes': {        # attributes are optional
    'o': 'some'         # every attribute may have either a single value
                        # or multiple values in a list like
                        # 'ou': ['Value1', 'Value2', ...]
 }
}

License

Copyright © 2013 Adrian Gruntkowski

Distributed under the MIT License.

Project details


Release history Release notifications

History Node

0.3.1

History Node

0.3.0

History Node

0.2.2

History Node

0.2.1

History Node

0.2.0

History Node

0.1.1

History Node

0.1.0

History Node

0.0.9

History Node

0.0.8

This version
History Node

0.0.7

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
python-ldap-test-0.0.7.tar.gz (1.5 MB) Copy SHA256 hash SHA256 Source None Jul 5, 2013

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page