Python ldap3.MOCK_SYNC Examples

The following are 7 code examples of ldap3.MOCK_SYNC(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ldap3 , or try the search function .
Example #1
Source File: helper.py    From resilient-community-apps with MIT License 6 votes vote down vote up
def mocked_connection(self):
    """Mock ldap3 connection.
    :return: Return mocked connection object
    """
    # Create a MockSyncStrategy connection to the fake server
    mocked_connection = Connection(self.FAKE_SERVER, user='cn=my_user,ou=test,o=lab', password='my_password', client_strategy=MOCK_SYNC)
    
    # Populate the DIT of the fake server with mock entries
    mocked_connection.strategy.entries_from_json(self.MOCK_DATA_PATH + 'mock_server_entries.json')

    connection = Mock(return_value=mocked_connection)
    return connection 
Example #2
Source File: admin_test.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def test_init(self):
        """Tests init logic."""
        admin_obj = admin.Admin(None, 'dc=test,dc=com')
        admin_obj.write_ldap = ldap3.Connection(
            ldap3.Server('fake'), client_strategy=ldap3.MOCK_SYNC
        )

        admin_obj.init()

        dn_list = [
            arg[0][0] for arg in admin_obj.write_ldap.add.call_args_list
        ]

        self.assertTrue('dc=test,dc=com' in dn_list)
        self.assertTrue('ou=treadmill,dc=test,dc=com' in dn_list)
        self.assertTrue('ou=apps,ou=treadmill,dc=test,dc=com' in dn_list) 
Example #3
Source File: admin_test.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def test_add(self):
        """Tests add logic."""
        admin_obj = admin.Admin(None, 'dc=test,dc=com')
        admin_obj.write_ldap = ldap3.Connection(
            ldap3.Server('fake'), client_strategy=ldap3.MOCK_SYNC
        )

        admin_obj.add(
            'ou=example,dc=test,dc=com',
            'testClass',
            {
                'foo': 1,
                'bar': ['z', 'a'],
                'lot': 2,
                'exp': [3, 4]
            }
        )

        call = admin_obj.write_ldap.add.call_args_list[0][0]
        self.assertEqual(call[0], 'ou=example,dc=test,dc=com')
        self.assertEqual(call[1], 'testClass')
        self.assertEqual(
            [attr for attr in six.iteritems(call[2])],
            [('bar', ['z', 'a']), ('exp', [3, 4]), ('foo', 1), ('lot', 2)]
        ) 
Example #4
Source File: test_ldap_search.py    From resilient-community-apps with MIT License 5 votes vote down vote up
def mocked_connection():
    """Mock ldap3 connection.
    :return: Return mocked connection object
    """
    # Create a MockSyncStrategy connection to the fake server
    mocked_connection = Connection(fake_server, user='cn=my_user,ou=test,o=lab', password='my_password',
                                     client_strategy=MOCK_SYNC)
    # Populate the DIT of the fake server with mock entries
    mocked_connection.strategy.entries_from_json('mock_server_entries.json')

    connection = Mock(return_value=mocked_connection)
    return connection 
Example #5
Source File: test_ldap_search.py    From resilient-community-apps with MIT License 5 votes vote down vote up
def mocked_connection():
    """Mock ldap3 connection.
    :return: Return mocked connection object
    """
    # Create a MockSyncStrategy connection to the fake server
    mocked_connection = Connection(fake_server, user='cn=my_user,ou=test,o=lab', password='my_password',
                                     client_strategy=MOCK_SYNC)
    # Populate the DIT of the fake server with mock entries
    mocked_connection.strategy.entries_from_json(MOCK_DATA_PATH + 'mock_server_entries.json')

    connection = Mock(return_value=mocked_connection)
    return connection 
Example #6
Source File: admin_test.py    From treadmill with Apache License 2.0 4 votes vote down vote up
def test_schema(self):
        """Test schema parsing."""
        # Disable W0212: Test access protected members of admin module.
        # pylint: disable=W0212
        attrs = [
            '{0}( %s NAME x1 DESC \'x x\''
            ' ORDERING integerOrderingMatch'
            ' SYNTAX 1.3.6.1.4.1.1466.115.121.1.27'
            ' )' % (admin._TREADMILL_ATTR_OID_PREFIX + '11'),
            '{1}( %s NAME x2 DESC \'x x\''
            ' SUBSTR caseIgnoreSubstringsMatch'
            ' EQUALITY caseIgnoreMatch'
            ' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15'
            ' )' % (admin._TREADMILL_ATTR_OID_PREFIX + '22'),
        ]

        obj_classes = [
            '{0}( %s NAME o1 DESC \'x x\''
            ' SUP top STRUCTURAL'
            ' MUST ( xxx ) MAY ( a $ b )'
            ' )' % (admin._TREADMILL_OBJCLS_OID_PREFIX + '33'),
        ]

        admin_obj = admin.Admin(None, None)
        admin_obj.ldap = ldap3.Connection(ldap3.Server('fake'),
                                          client_strategy=ldap3.MOCK_SYNC)

        admin_obj.ldap.strategy.add_entry(
            'cn={1}treadmill,cn=schema,cn=config',
            {'olcAttributeTypes': attrs, 'olcObjectClasses': obj_classes}
        )

        admin_obj.ldap.bind()

        self.assertEqual(
            {
                'dn': 'cn={1}treadmill,cn=schema,cn=config',
                'objectClasses': {
                    'o1': {
                        'idx': 33,
                        'desc': 'x x',
                        'must': ['xxx'],
                        'may': ['a', 'b'],
                    },
                },
                'attributeTypes': {
                    'x1': {
                        'idx': 11,
                        'desc': 'x x',
                        'type': 'int'
                    },
                    'x2': {
                        'idx': 22,
                        'desc': 'x x',
                        'type': 'str',
                        'ignore_case': True
                    },
                }
            },
            admin_obj.schema()
        ) 
Example #7
Source File: test_unit.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_check(check, aggregator, mocker):
    server_mock = ldap3.Server("fake_server")
    conn_mock = ldap3.Connection(server_mock, client_strategy=ldap3.MOCK_SYNC, collect_usage=True)
    # usage.last_received_time is not populated when using mock connection, let's set a value
    conn_mock.usage.last_received_time = datetime.datetime.utcnow()
    conn_mock.strategy.entries_from_json(os.path.join(HERE, "fixtures", "monitor.json"))
    instance = {
        "url": "fake_server",
        "custom_queries": [
            {"name": "stats", "search_base": "cn=statistics,cn=monitor", "search_filter": "(!(cn=Statistics))"}
        ],
    }

    mocker.patch("datadog_checks.openldap.openldap.ldap3.Server", return_value=server_mock)
    mocker.patch("datadog_checks.openldap.openldap.ldap3.Connection", return_value=conn_mock)
    check.check(instance)
    tags = ["url:fake_server"]
    aggregator.assert_service_check("openldap.can_connect", check.OK, tags=tags)
    aggregator.assert_metric("openldap.bind_time", tags=tags)
    aggregator.assert_metric("openldap.connections.current", 1, tags=tags)
    aggregator.assert_metric("openldap.connections.max_file_descriptors", 1024, tags=tags)
    aggregator.assert_metric("openldap.connections.total", 3453, tags=tags)
    aggregator.assert_metric("openldap.operations.completed.total", 41398, tags=tags)
    aggregator.assert_metric("openldap.operations.initiated.total", 41399, tags=tags)
    aggregator.assert_metric("openldap.operations.completed", 0, tags=tags + ["operation:abandon"])
    aggregator.assert_metric("openldap.operations.initiated", 0, tags=tags + ["operation:abandon"])
    aggregator.assert_metric("openldap.operations.completed", 0, tags=tags + ["operation:add"])
    aggregator.assert_metric("openldap.operations.initiated", 0, tags=tags + ["operation:add"])
    aggregator.assert_metric("openldap.operations.completed", 9734, tags=tags + ["operation:bind"])
    aggregator.assert_metric("openldap.operations.initiated", 9734, tags=tags + ["operation:bind"])
    aggregator.assert_metric("openldap.operations.completed", 0, tags=tags + ["operation:compare"])
    aggregator.assert_metric("openldap.operations.initiated", 0, tags=tags + ["operation:compare"])
    aggregator.assert_metric("openldap.operations.completed", 0, tags=tags + ["operation:delete"])
    aggregator.assert_metric("openldap.operations.initiated", 0, tags=tags + ["operation:delete"])
    aggregator.assert_metric("openldap.operations.completed", 0, tags=tags + ["operation:extended"])
    aggregator.assert_metric("openldap.operations.initiated", 0, tags=tags + ["operation:extended"])
    aggregator.assert_metric("openldap.operations.completed", 0, tags=tags + ["operation:modify"])
    aggregator.assert_metric("openldap.operations.initiated", 0, tags=tags + ["operation:modify"])
    aggregator.assert_metric("openldap.operations.completed", 0, tags=tags + ["operation:modrdn"])
    aggregator.assert_metric("openldap.operations.initiated", 0, tags=tags + ["operation:modrdn"])
    aggregator.assert_metric("openldap.operations.completed", 29212, tags=tags + ["operation:search"])
    aggregator.assert_metric("openldap.operations.initiated", 29213, tags=tags + ["operation:search"])
    aggregator.assert_metric("openldap.operations.completed", 2452, tags=tags + ["operation:unbind"])
    aggregator.assert_metric("openldap.operations.initiated", 2452, tags=tags + ["operation:unbind"])
    aggregator.assert_metric("openldap.statistics.bytes", 796449497, tags=tags)
    aggregator.assert_metric("openldap.statistics.entries", 178382, tags=tags)
    aggregator.assert_metric("openldap.statistics.pdu", 217327, tags=tags)
    aggregator.assert_metric("openldap.statistics.referrals", 0, tags=tags)
    aggregator.assert_metric("openldap.threads", 1, tags=tags + ["status:active"])
    aggregator.assert_metric("openldap.threads", 1, tags=tags + ["status:backload"])
    aggregator.assert_metric("openldap.threads", 3, tags=tags + ["status:open"])
    aggregator.assert_metric("openldap.threads", 0, tags=tags + ["status:pending"])
    aggregator.assert_metric("openldap.threads", 0, tags=tags + ["status:starting"])
    aggregator.assert_metric("openldap.threads.max", 16, tags=tags)
    aggregator.assert_metric("openldap.threads.max_pending", 0, tags=tags)
    aggregator.assert_metric("openldap.uptime", 159182, tags=tags)
    aggregator.assert_metric("openldap.waiter.read", 1, tags=tags)
    aggregator.assert_metric("openldap.waiter.write", 0, tags=tags)
    aggregator.assert_metric("openldap.query.duration", tags=tags + ["query:stats"])
    aggregator.assert_metric("openldap.query.entries", 4, tags=tags + ["query:stats"])
    aggregator.assert_all_metrics_covered()