Package: python-ldap-faker
python-ldap-fakerDocumentation:https://python-ldap-faker.readthedocs.orgFakes are objects that have working implementations. while mocks are objects that have predefined behavior.python-ldap-fakerprovides a fakepython-ldapinterface and "server" that can be used for automated testing of code that usespython-ldap.Managing an actual LDAP server during our tests is usually out of the question, so typically we revert to patching thepython-ldapcode to use mock objects instead, but this is very verbose and can lead to test code errors in practice.Installationpython-ldap-fakersupports Python 3.7+.To install from PyPI:pipinstallpython-ldap-fakerIf you want, you can run the tests:python-munittestdiscoverFeaturesThesepython-ldapglobal functions are faked:ldap.initializeldap.set_optionldap.get_optionTheseldap.ldapobject.LDAPObjectmethods are faked:set_optionget_optionstart_tls_ssimple_bind_sunbind_ssearch_ssearch_extresult3compare_sadd_smodify_srename_sdelete_sForsearch_extandsearch_s, your filter string will be validated as a valid LDAP filter, and your filter will be applied directly to your objects in our fake "server" to generate the result list.  No canned searches!Inspect your call history for all calls (name, arguments), and test the order in which they were madeSimulate multiple fake LDAP "servers" with different sets of objects that correspond to different LDAP URIs.Ease your test setup with :py:class:LDAPFakerMixin, a mixin for :py:class:unittest.TestCaseAutomatically manages patchingpython-ldapfor the code under testAllows you to populate objects into one or more LDAP "servers" with fixture filesProvides the following test instrumentation for inspecting state after the test:Access to the full object store for each LDAP uri accessedAll connections madeAllpython-ldapAPI calls madeAllpython-ldapLDAP options setProvides test isolation: object store changes, connections, call history, option changes are all reset between testsUse handy LDAP specific asserts to ease your testingDefine your own hooks to change the behavior of your fake "servers"Support behavior for specific LDAP implementations:Redhat Directory Server/389 implementation support: have your test believe it's talking to an RHDS/389 server.QuickstartThe easiest way to usepython-ldap-fakerin yourunittestbased tests is to use theldap_faker.LDAPFakerMixinmixin forunittest.TestCase.This will patchldap.initialize,ldap.set_optionandldap.get_optionto use ourFakeLDAPinterface, and load fixtures in from JSON files to use as test data.Let's say we have a classAppin ourmyappmodule that does LDAP work that we want to test.First, prepare a file nameddata.jsonwith the objects you want loaded into your fake LDAP server.   Let's say you want your data to consist of someposixAccountobjects.  If we makedata.jsonlook like this:[["uid=foo,ou=bar,o=baz,c=country",{"uid":["foo"],"cn":["Foo Bar"],"uidNumber":["123"],"gidNumber":["123"],"homeDirectory":["/home/foo"],"userPassword":["the password"],"objectclass":["posixAccount","top"]}],["uid=fred,ou=bar,o=baz,c=country",{"uid":["fred"],"cn":["Fred Flintstone"],"uidNumber":["124"],"gidNumber":["124"],"homeDirectory":["/home/fred"],"userPassword":["the fredpassword"],"objectclass":["posixAccount","top"]}],["uid=barney,ou=bar,o=baz,c=country",{"uid":["barney"],"cn":["Barney Rubble"],"uidNumber":["125"],"gidNumber":["125"],"homeDirectory":["/home/barney"],"userPassword":["the barneypassword"],"objectclass":["posixAccount","top"]}]]Then we can write aTestCasethat looks like this:importunittestimportldapfromldap_fakerimportLDAPFakerMixinfrommyappimportAppclassYourTestCase(LDAPFakerMixin,unittest.TestCase):ldap_modules=['myapp']ldap_fixtures='data.json'deftest_auth_works(self):app=App()# A method that does a `simple_bind_s`app.auth('fred','the fredpassword')conn=self.get_connections()[0]self.assertLDAPConnectionMethodCalled(conn,'simple_bind_s',{'who':'uid=fred,ou=bar,o=baz,c=country','cred':'the fredpassword'})deftest_correct_connection_options_were_set(self):app=App()app.auth('fred','the fredpassword')conn=self.get_connections()[0]self.assertLDAPConnectionOptionSet(conn,ldap.OPT_X_TLX_NEWCTX,0)deftest_tls_was_used_before_auth(self):app=App()app.auth('fred','the fredpassword')conn=self.get_connections()[0]self.assertLDAPConnectiontMethodCalled(conn,'start_tls_s')self.assertLDAPConnectionMethodCalledAfter(conn,'simple_bind_s','start_tls_s')