LDAP Operations

Inside of an individual test, you will be performing some task that involves LDAP operations and then verifying the outcome. In some cases, you will need to prepare your mock LDAPObject to return specific results for a given API call.


class mockldap.LDAPObject(directory)[source]
Parameters:directory (ldap.cidict.cidict: {dn: {attr: [values]}}) – The initial content of this LDAP connection.

Our mock replacement for ldap.LDAPObject. This exports selected LDAP operations and allows you to set return values in advance as well as discover which methods were called after the fact.

All of these methods take the same arguments as their python-ldap counterparts. Some are self-explanatory; those that are only partially implemented are documented as such.

Ignore the static annotations; that’s just a Sphinx artifact.


dict: Options that have been set by set_option().


bool: True if start_tls_s() was called.


string: DN of the last successful bind. None if unbound.

add_s(dn, record)[source]
compare_s(dn, attr, value)[source]
initialize(*args, **kwargs)[source]

This only exists for recording purposes.

modify_s(dn, mod_attrs)[source]
passwd_s(user, oldpw, newpw, serverctrls=None, clientctrls=None)[source]
rename_s(dn, newrdn, newsuperior=None)[source]
result(msgid, all=1, timeout=None)[source]
search(base, scope, filterstr='(objectClass=*)', attrlist=None, attrsonly=0)[source]

See search_s().

search_s(base, scope, filterstr='(objectClass=*)', attrlist=None, attrsonly=0)[source]

Supports many, but not all, filter strings.

Tests of the form '(foo=bar)' and '(foo=\*)' are supported, as are the &, |, and ! operators. attrlist and attrsonly are also supported. Beyond that, this method must be seeded.

set_option(option, invalue)[source]
simple_bind_s(who='', cred='')[source]

Every LDAP method on LDAPObject is actually an instance of RecordedMethod, which allows you to set return values in advance for different sets of arguments.

class mockldap.recording.RecordedMethod(func, instance)[source]
seed(*args, **kwargs)[source]

A convenience wrapper for set_return_value().

method.seed(arg1, arg2=True)(value) is equivalent to method.set_return_value([arg1], {'arg2': True}, value).

set_return_value(args, kwargs, value)[source]

Set a method’s return value for a set of arguments.

Subsequent calls to this method will check for a matching set of arguments and return the assoiated value. If the value is an exception class or instance, it will be raised instead.


When the method is called, the arguments must be passed in exactly the same form. We don’t automatically match equivalent positional and keyword arguments.

If no preset return value is found, the underlying method will be called normally. If that method can not handle the request, it may raise mockldap.SeedRequired, indicating that the method must be seeded with a return value for these arguments.

exception mockldap.SeedRequired[source]

An API call must be seeded with a return value.

This is raised by LDAPObject methods when they can’t satisfy a request internally. The messsage will contain a representation of the method call that triggered it, including all arguments.