def test_search_ou_by_dn(): # Pass the new employees data to the controller controller = LdapController() add_entry_request = schema.load({ 'dn': 'cn=employees,ou=test,o=lab', 'object_class': 'organizationalUnit' }) result = controller.add(mocked_name, add_entry_request) dn = add_entry_request.dn assert result, f'There was a problem adding {dn}' search_data = { 'search_base': dn, 'search_filter': '(objectClass=organizationalUnit)', 'search_scope': 'BASE' } search_schema = SearchSchema() search_results: SearchResults = controller.search( mocked_name, search_schema.load(search_data)) assert search_results is not None assert len( search_results.data ) == 1, f'Expect one search result but found {len(search_results.data)}' for entry in search_results.data: assert entry['dn'] == dn, f'Expected {dn} but found {entry["dn"]}'
def test_scrub_dict_do_not_remove_empty(): data = { 'key1': 'A Key', 'controls': '' } result = LdapController.scrub_dict(data) assert 'controls' in result, 'Controls were deleted'
def test_scrub_dict_present_value(): data = { 'key1': 'A Key', 'controls': 'control=value' } result = LdapController.scrub_dict(data) assert 'controls' in result, 'Controls were not deleted'
def test_scrub_dict_present_empty(): data = { 'key1': 'A Key', 'controls': '' } result = LdapController.scrub_dict(data, True) assert 'controls' not in result, 'Controls were not deleted'
def test_search_person_by_dn(): # Pass the new employees data to the controller controller = LdapController() add_entry_request = schema.load({ 'dn': 'cn=employees,ou=test,o=lab', 'object_class': 'organizationalUnit' }) controller.add(mocked_name, add_entry_request) add_entry_request = schema.load({ 'dn': 'cn=users,cn=employees,ou=test,o=lab', 'object_class': 'organizationalUnit' }) controller.add(mocked_name, add_entry_request) add_entry_request = schema.load({ 'dn': 'cn=cevans,cn=users,cn=employees,ou=test,o=lab', 'object_class': ['person', 'organizationalPerson', 'inetOrgPerson'], 'attributes': attributes }) controller.add(mocked_name, add_entry_request) search_data = { 'search_base': add_entry_request.dn, 'search_filter': '(objectClass=organizationalPerson)', 'search_scope': 'BASE' } search_schema = SearchSchema() search_results: SearchResults = controller.search( mocked_name, search_schema.load(search_data)) assert search_results is not None assert len( search_results.data ) == 1, f'Expect one search result but found {len(search_results.data)}' for entry in search_results.data: assert entry[ 'dn'] == add_entry_request.dn, f'Expected {add_entry_request.dn} but found {entry["dn"]}'
def test_add_user_with_controller(): # Build out the fake organization controller = LdapController() add_entry_request = schema.load({ 'dn': 'cn=employees,ou=test,o=lab', 'object_class': 'organizationalUnit' }) controller.add(mocked_name, add_entry_request) add_entry_request = schema.load({ 'dn': 'cn=users,cn=employees,ou=test,o=lab', 'object_class': 'organizationalUnit' }) controller.add(mocked_name, add_entry_request) # Pass the new user data to the controller add_entry_request = schema.load({ 'dn': 'cn=mwatkins,cn=users,cn=employees,ou=test,o=lab', 'object_class': ['top,person', 'organizationalPerson', 'inetOrgPerson'], 'attributes': attributes }) result = controller.add(mocked_name, add_entry_request) assert result, 'There was a problem adding the user' dn = add_entry_request.dn data = { 'search_base': dn, 'search_filter': '(objectClass=organizationalPerson)', 'attributes': 'ALL_ATTRIBUTES' } search_schema = SearchSchema() search_results: SearchResults = controller.search(mocked_name, search_schema.load(data)) total_results = len(search_results.data) assert total_results == 1, f'Expected 1 search result for {dn} but found {total_results}'
def test_add_user_with_controller(): # Pass the new employees data to the controller controller = LdapController() add_entry_request = schema.load({ 'dn': 'cn=employees,ou=test,o=lab', 'object_class': 'organizationalUnit', 'attributes': None, 'controls': None }) result = controller.add(mocked_name, add_entry_request) assert result, 'There was a problem adding {add_entry_request.dn}' dn = add_entry_request.dn data = { 'search_base': dn, 'search_filter': '(objectClass=organizationalUnit)', 'search_scope': 'SUBTREE' } search_schema = SearchSchema() search_results: SearchResults = controller.search(mocked_name, search_schema.load(data)) total_results = len(search_results.data) assert total_results == 1, f'Expected 1 search result for {dn} but found {total_results}' add_entry_request = schema.load({ 'dn': 'cn=users,cn=employees,ou=test,o=lab', 'object_class': 'organizationalUnit' }) result = controller.add(mocked_name, add_entry_request) dn = add_entry_request.dn assert result, 'There was a problem adding {dn}' data = { 'search_base': dn, 'search_filter': '(objectClass=organizationalUnit)', 'search_scope': 'SUBTREE' } search_schema = SearchSchema() search_results: SearchResults = controller.search(mocked_name, search_schema.load(data)) total_results = len(search_results.data) assert total_results == 1, f'Expected 1 search result for {dn} but found {total_results}'
from flask import request from ldap3.core.usage import ConnectionUsage from ldap.controllers.ldap_controller import LdapController from ldap.dtos.add_entry_request import AddEntryRequest from ldap.dtos.health_check import HealthCheck from ldap.dtos.modify_entry_request import ModifyEntryRequest from ldap.dtos.search_results import SearchResults from ldap.schemas.add_entry_request_schema import AddEntryRequestSchema from ldap.schemas.health_check_schema import HealthCheckSchema from ldap.schemas.modify_entry_request_schema import ModifyEntryRequestSchema from ldap.schemas.search_results_schema import SearchResultsSchema from ldap.schemas.search_schema import SearchSchema ldap_api_blueprint = Blueprint('ldap_api', __name__) ldap_controller = LdapController() search_schema = SearchSchema() modify_entry_request_schema = ModifyEntryRequestSchema() success = 'success' def get_blueprint(): """Return the blueprint for the main app module""" return ldap_api_blueprint @ldap_api_blueprint.route('/api/health_check', methods=['GET']) def get_gateway_health_check() -> Response: health_check = HealthCheck(__name__) health_check.hostname = socket.gethostname() try: