{"id":19466760,"url":"https://github.com/zulip/fakeldap","last_synced_at":"2025-10-07T07:30:49.238Z","repository":{"id":1766918,"uuid":"2684172","full_name":"zulip/fakeldap","owner":"zulip","description":"A fake ldap implementation to be used in unittests.","archived":false,"fork":false,"pushed_at":"2023-05-31T00:42:36.000Z","size":26,"stargazers_count":20,"open_issues_count":11,"forks_count":25,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-01-19T22:48:10.464Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"icco/Resume","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zulip.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2011-10-31T22:40:08.000Z","updated_at":"2024-12-08T15:32:21.000Z","dependencies_parsed_at":"2023-07-06T15:46:29.036Z","dependency_job_id":null,"html_url":"https://github.com/zulip/fakeldap","commit_stats":{"total_commits":37,"total_committers":9,"mean_commits":4.111111111111111,"dds":0.7837837837837838,"last_synced_commit":"282d47b09d953713c124742b82316421444db24b"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zulip%2Ffakeldap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zulip%2Ffakeldap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zulip%2Ffakeldap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zulip%2Ffakeldap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zulip","download_url":"https://codeload.github.com/zulip/fakeldap/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235600073,"owners_count":19016204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T18:30:03.870Z","updated_at":"2025-10-07T07:30:48.903Z","avatar_url":"https://github.com/zulip.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"========\nfakeldap\n========\n\nThe goal of this module is to provide a simple way to mock ldap backend servers\nfor your unittests. It makes it possible to define upfront a set of directory\nentries that can be queried or set fixed return values to ldap queries. It acts\nas a drop in replacement for the ``LDAPObject`` class of the python-ldap\nmodule. It implements a subset of the allowed methods of this class.\n\nThis module implements the ``MockLDAP`` class that functions both as the\n``LDAPObject`` as well as the ldap module. Most of the code and design has been\ntaken from Peter Sagerson's excellent django-auth-ldap_ module.\n\n.. _django-auth-ldap: https://bitbucket.org/psagers/django-auth-ldap/wiki/Home\n\nInstallation\n============\n\nInstall the latest release from PyPI::\n\n    $ pip install fakeldap\n\nRunning tests in development\n============================\nIf you've cloned the repository locally::\n\n    $ git clone git://github.com/zulip/fakeldap.git\n\nand made changes, ensure you have ``pytest`` installed::\n\n    $ pip install pytest\n\nand you can run the tests with::\n\n    $ pytest tests.py\n\nUsage\n=====\n\n.. note::\n\n    This code is still experimental and not very tested as of yet. So is the\n    documentation\n\nThe ``MockLDAP`` class replaces the ``LDAPObject`` of the python-ldap module.\nThe easiest way to use it, is to overwrite ``ldap.initialize`` to return\n``MockLDAP`` instead of ``LDAPObject``. The example below uses Michael Foord's\nMock_ library to achieve that::\n\n    import unittest\n    from mock import patch\n    from fakeldap import MockLDAP\n\n\n    _mock_ldap = MockLDAP()\n\n    class YourTestCase(unittest.TestCase):\n        def setUp(self):\n            # Patch where the ldap library is used:\n            self.ldap_patcher = patch('app.module.ldap.initialize')\n            self.mock_ldap = self.ldap_patcher.start()\n            self.mock_ldap.return_value = _mock_ldap\n\n        def tearDown(self):\n            _mock_ldap.reset()\n            self.mock_ldap.stop()\n\nThe mock ldap object implements the following ldap operations:\n\n- simple_bind_s\n- search_s\n- compare_s\n- modify_s\n- delete_s\n- add_s\n- rename_s\n\nThis is an example how to use ``MockLDAP`` with fixed return values::\n\n    def test_some_ldap_group_stuff(self):\n        # Define the expected return value for the ldap operation\n        return_value = (\"cn=testgroup,ou=group,dc=30loops,dc=net\", {\n            'objectClass': ['posixGroup'],\n            'cn': 'testgroup',\n            'gidNumber': '2030',\n        })\n\n        # Register a return value with the MockLDAP object\n        _mock_ldap.set_return_value('add_s',\n            (\"cn=testgroup,ou=groups,dc=30loops,dc=net\", (\n                ('objectClass', ('posixGroup')),\n                ('cn', 'testgroup'),\n                ('gidNumber', '2030'))),\n            (105,[], 10, []))\n\n        # Run your actual code, this is just an example\n        group_manager = GroupManager()\n        result = group_manager.add(\"testgroup\")\n\n        # assert that the return value of your method and of the MockLDAP\n        # are as expected, here using python-nose's eq() test tool:\n        self.assertEqual(return_value, result)\n\n        # Each actual ldap call your software makes gets recorded. You could\n        # prepare a list of calls that you expect to be issued and compare it:\n        called_records = []\n\n        called_records.append(('simple_bind_s',\n            {'who': 'cn=admin,dc=30loops,dc=net', 'cred': 'ldaptest'}))\n\n        called_records.append(('add_s', {\n            'dn': 'cn=testgroup,ou=groups,dc=30loops,dc=net\",\n            'record': [\n                ('objectClass', ['posixGroup']),\n                ('gidNumber', '2030'),\n                ('cn', 'testgroup'),\n                ]}))\n\n        # And again test the expected behaviour\n        self.assertEqual(called_records, _mock_ldap.ldap_methods_called_with_arguments())\n\nBesides of fixing return values for specific calls, you can also imitate a full\nldap server with a directory of entries::\n\n    # Create an instance of MockLDAP with a preset directory\n    tree = {\n        \"cn=admin,dc=30loops,dc=net\": {\n                \"userPassword\": \"ldaptest\"\n        }\n    }\n    mock_ldap = MockLDAP(tree)\n\n    record = [\n        ('uid', 'crito'),\n        ('userPassword', 'secret'),\n    ]\n    # The return value I expect when I add another record to the directory\n    self.assertEqual(\n        (105,[],1,[]),\n        mock_ldap.add_s(\"uid=crito,ou=people,dc=30loops,dc=net\", record)\n    )\n\n    # The expected directory\n    directory = {\n        \"cn=admin,dc=30loops,dc=net\": {\"userPassword\": \"ldaptest\"},\n        \"uid=crito,ou=people,dc=30loops,dc=net\": {\n            \"uid\": \"crito\", \"userPassword\": \"secret\"}\n    }\n    # Compare the expected directory with the MockLDAP directory\n    self.assertEqual(directory, mock_ldap.directory)\n\n.. _Mock: http://www.voidspace.org.uk/python/mock/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzulip%2Ffakeldap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzulip%2Ffakeldap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzulip%2Ffakeldap/lists"}