예제 #1
0
def test_query_ldap_joins_missing(mocker):
    from ldap2pg.manager import SyncManager, UserError

    search_result = [('cn=A,ou=people,dc=global', {
        'cn': ['A'],
    })]

    manager = SyncManager(ldapconn=mocker.Mock())
    manager.ldapconn.search_s.side_effect = [search_result]

    with pytest.raises(UserError) as ei:
        manager.query_ldap(
            base='ou=people,dc=global',
            filter='(objectClass=group)',
            scope=2,
            joins={
                'member':
                dict(
                    filter='(objectClass=people)',
                    attributes=['sAMAccountName'],
                )
            },
            attributes=['cn', 'member'],
        )
    assert "Missing attribute member" in str(ei.value)
예제 #2
0
def test_query_ldap(mocker):
    from ldap2pg.manager import SyncManager, UserError

    manager = SyncManager(ldapconn=mocker.Mock())
    manager.ldapconn.search_s.return_value = [
        ('dn=a', {}),
        ('dn=b', {}),
        (None, {
            'ref': True
        }),
    ]

    entries = manager.query_ldap(
        base='ou=people,dc=global',
        filter='(objectClass=*)',
        scope=2,
        attributes=['cn'],
    )

    assert 2 == len(entries)

    manager.ldapconn.search_s.return_value = [('dn=a', {'a': b'\xbb'})]
    with pytest.raises(UserError):
        manager.query_ldap(
            base='ou=people,dc=global',
            filter='(objectClass=*)',
            scope=2,
            attributes=['cn'],
        )
예제 #3
0
def test_query_ldap_bad_filter(mocker):
    from ldap2pg.manager import SyncManager, LDAPError, UserError

    manager = SyncManager(ldapconn=mocker.Mock())
    manager.ldapconn.search_s.side_effect = LDAPError()

    with pytest.raises(UserError):
        manager.query_ldap(
            base='dc=unit', filter='(broken', scope=2, attributes=[],
        )

    assert manager.ldapconn.search_s.called is True
예제 #4
0
def test_query_ldap(mocker):
    from ldap2pg.manager import SyncManager

    manager = SyncManager(ldapconn=mocker.Mock())
    manager.ldapconn.search_s.return_value = [('dn=a', {}), ('dn=b', {})]

    entries = manager.query_ldap(
        base='ou=people,dc=global', filter='(objectClass=*)',
        scope=2, attributes=['cn'],
    )

    assert 2 == len(entries)
예제 #5
0
def test_query_ldap_joins_filtered_allowed(mocker):
    from ldap2pg.manager import SyncManager, LDAPEntry

    search_result = [
        ('cn=A,ou=people,dc=global', {
            'cn': ['A'],
            'member': ['cn=P,ou=people,dc=global']
        }),
    ]

    sub_search_result = []

    manager = SyncManager(ldapconn=mocker.Mock())
    manager.ldapconn.search_s.side_effect = [search_result, sub_search_result]

    entries = manager.query_ldap(
        base='ou=people,dc=global',
        filter='(objectClass=group)',
        scope=2,
        attributes=['cn', 'member'],
        joins={
            'member':
            dict(
                base='ou=people,dc=global',
                scope=2,
                filter='(objectClass=group)',
                attributes=['cn'],
                allow_missing_attributes=[],
            )
        },
        allow_missing_attributes=['member'],
    )

    assert 2 == manager.ldapconn.search_s.call_count

    expected_entries = [
        LDAPEntry(
            'cn=A,ou=people,dc=global',
            {
                'cn': ['A'],
                'member': ['cn=P,ou=people,dc=global'],
            },
            {
                'member': [],
            },
        ),
    ]

    assert expected_entries == entries
예제 #6
0
def test_query_ldap(mocker):
    from ldap2pg.manager import SyncManager, UserError

    manager = SyncManager(ldapconn=mocker.Mock())
    manager.ldapconn.search_s.return_value = [
        ('dn=a', {}),
        ('dn=b', {
            'member': ['m']
        }),
        (None, {
            'ref': True
        }),
        (None, ['ldap://list_ref']),
    ]

    entries = manager.query_ldap(
        base='ou=people,dc=global',
        filter='(objectClass=*)',
        scope=2,
        joins={},
        attributes=['cn'],
        allow_missing_attributes=['member'],
    )

    assert 2 == len(entries)
    assert [] == entries[0][1]['member']

    manager.ldapconn.search_s.return_value = [('dn=a', {'a': b'\xbb'})]
    with pytest.raises(UserError):
        manager.query_ldap(
            base='ou=people,dc=global',
            filter='(objectClass=*)',
            scope=2,
            joins={},
            attributes=['cn'],
        )
예제 #7
0
def test_query_ldap_joins_ignore_error(mocker):
    from ldap2pg.manager import SyncManager, LDAPError

    search_result = [
        ('cn=A,ou=people,dc=global', {
            'cn': ['A'],
            'member': ['cn=P,ou=people,dc=global']
        }),
    ]

    sub_search_result = LDAPError()

    manager = SyncManager(ldapconn=mocker.Mock())
    manager.ldapconn.search_s.side_effect = [search_result, sub_search_result]

    entries = manager.query_ldap(
        base='ou=people,dc=global',
        filter='(objectClass=group)',
        scope=2,
        joins={
            'member':
            dict(
                base='ou=people,dc=global',
                scope=2,
                filter='(objectClass=people)',
                attributes=['sAMAccountName'],
            )
        },
        attributes=['cn', 'member'],
    )

    expected_entries = [
        ('cn=A,ou=people,dc=global', {
            'cn': ['A'],
            'dn': ['cn=A,ou=people,dc=global'],
            'member': ['cn=P,ou=people,dc=global'],
        }, {}),
    ]

    assert expected_entries == entries
예제 #8
0
def test_query_ldap_joins_ok(mocker):
    from ldap2pg.manager import SyncManager

    search_result = [
        ('cn=A,ou=people,dc=global', {
            'cn': ['A'],
            'member': ['cn=P,ou=people,dc=global']
        }),
        ('cn=B,ou=people,dc=global', {
            'cn': ['B'],
            'member': ['cn=P,ou=people,dc=global']
        }),
    ]

    sub_search_result = [
        ('cn=P,ou=people,dc=global', {
            'sAMAccountName': ['P']
        }),
    ]

    manager = SyncManager(ldapconn=mocker.Mock())
    manager.ldapconn.search_s.side_effect = [search_result, sub_search_result]

    entries = manager.query_ldap(
        base='ou=people,dc=global',
        filter='(objectClass=group)',
        scope=2,
        attributes=['cn', 'member'],
        joins={
            'member':
            dict(
                base='ou=people,dc=global',
                scope=2,
                filter='(objectClass=people)',
                attributes=['sAMAccountName'],
            )
        },
    )

    assert 2 == manager.ldapconn.search_s.call_count

    expected_entries = [
        ('cn=A,ou=people,dc=global', {
            'cn': ['A'],
            'dn': ['cn=A,ou=people,dc=global'],
            'member': ['cn=P,ou=people,dc=global'],
        }, {
            'member': [('cn=P,ou=people,dc=global', {
                'dn': ['cn=P,ou=people,dc=global'],
                'samaccountname': ['P'],
            }, {})],
        }),
        ('cn=B,ou=people,dc=global', {
            'cn': ['B'],
            'dn': ['cn=B,ou=people,dc=global'],
            'member': ['cn=P,ou=people,dc=global'],
        }, {
            'member': [('cn=P,ou=people,dc=global', {
                'dn': ['cn=P,ou=people,dc=global'],
                'samaccountname': ['P'],
            }, {})],
        }),
    ]

    assert expected_entries == entries