# 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.

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

    This only exists for recording purposes.

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

    See `search_s()`.

  search_s(base, scope, filterstr='(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.

  set_option(option, invalue)
  simple_bind_s(who='', cred='')
  start_tls_s()
  unbind()
  unbind_s()
  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)
  seed(*args, **kwargs)

    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)

    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.