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.

LDAPObject

class mockldap.LDAPObject(directory)
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.

options

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

tls_enabled

bool: True if start_tls_s() was called.

bound_as

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

static add_s(dn, record)
static compare_s(dn, attr, value)
static delete_s(dn)
static get_option(option)
static initialize(*args, **kwargs)

This only exists for recording purposes.

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

See search_s().

static search_s(base, scope, filterstr=u'(objectClass=*)', attrlist=None, attrsonly=0)

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.

static set_option(option, invalue)
static simple_bind_s(who=u'', cred=u'')
static start_tls_s()
static unbind()
static unbind_s()
static whoami_s()

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.

Warning

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

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.

Table Of Contents

Previous topic

Mock Directories

Next topic

Change Log

This Page