| 1 | import configparser |
| 2 | import os.path |
| 3 | import sys |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | import bonsai |
| 8 | from bonsai import LDAPReference |
| 9 | from bonsai import LDAPClient |
| 10 | from bonsai import LDAPURL |
| 11 | |
| 12 | |
| 13 | @pytest.fixture(scope="module") |
| 14 | def host_url(): |
| 15 | """ Set host url for connection. """ |
| 16 | curdir = os.path.abspath(os.path.dirname(__file__)) |
| 17 | cfg = configparser.ConfigParser() |
| 18 | cfg.read(os.path.join(curdir, "test.ini")) |
| 19 | url = "ldap://%s:%s" % (cfg["SERVER"]["hostname"], cfg["SERVER"]["port"]) |
| 20 | return url |
| 21 | |
| 22 | |
| 23 | def test_init_errors(host_url): |
| 24 | """ Testing errors during initialization of LDAPReference. """ |
| 25 | client = LDAPClient(host_url) |
| 26 | with pytest.raises(TypeError): |
| 27 | _ = LDAPReference(None, ["ldap://a"]) |
| 28 | with pytest.raises(TypeError): |
| 29 | _ = LDAPReference(client, [0]) |
| 30 | with pytest.raises(ValueError): |
| 31 | _ = LDAPReference(client, ["asd", LDAPURL("ldap://b")]) |
| 32 | |
| 33 | |
| 34 | def test_client_prop(host_url): |
| 35 | """ Testing client property. """ |
| 36 | client = LDAPClient(host_url) |
| 37 | ref = LDAPReference(client, []) |
| 38 | assert ref.client == client |
| 39 | with pytest.raises(TypeError): |
| 40 | ref.client = "b" |
| 41 | |
| 42 | ref.client = LDAPClient() |
| 43 | assert ref.client != client |
| 44 | |
| 45 | |
| 46 | def test_references_prop(host_url): |
| 47 | """ Testing references property. """ |
| 48 | client = LDAPClient(host_url) |
| 49 | reflist = [LDAPURL("ldap://localhost"), host_url] |
| 50 | ref = LDAPReference(client, reflist) |
| 51 | assert ref.references == reflist |
| 52 | with pytest.raises(AttributeError): |
| 53 | ref.references = None |
| 54 | |
| 55 | |
| 56 | @pytest.mark.skipif( |
| 57 | sys.platform.startswith("win"), reason="Referrals are not set in AD." |
| 58 | ) |
| 59 | def test_referral_chasing(host_url): |
| 60 | """ Testing referral chasing option. """ |
| 61 | refdn = "o=admin,ou=nerdherd-refs,dc=bonsai,dc=test" |
| 62 | client = LDAPClient(host_url) |
| 63 | client.server_chase_referrals = True |
| 64 | with client.connect() as conn: |
| 65 | res = conn.search(refdn, 0) |
| 66 | assert isinstance(res[0], bonsai.LDAPEntry) |
| 67 | client.server_chase_referrals = False |
| 68 | client.ignore_referrals = False |
| 69 | with client.connect() as conn: |
| 70 | res = conn.search(refdn, 0) |
| 71 | assert len(res) == 0 |
| 72 | res = conn.search("ou=nerdherd-refs,dc=bonsai,dc=test", 1) |
| 73 | refs = [item for item in res if isinstance(item, LDAPReference)] |
| 74 | assert any(refs) |
| 75 | |
| 76 | |
| 77 | @pytest.mark.skipif( |
| 78 | sys.platform.startswith("win"), reason="Referrals are not set in AD." |
| 79 | ) |
| 80 | def test_ignore_referrals(host_url): |
| 81 | """ Testing ignore referrals option. """ |
| 82 | client = LDAPClient(host_url) |
| 83 | client.ignore_referrals = True |
| 84 | with client.connect() as conn: |
| 85 | res = conn.search("ou=nerdherd-refs,dc=bonsai,dc=test", 1) |
| 86 | assert res == [] |
| 87 | client.ignore_referrals = False |
| 88 | with client.connect() as conn: |
| 89 | res = conn.search("ou=nerdherd-refs,dc=bonsai,dc=test", 1) |
| 90 | assert len(res) != 0 |
| 91 | refs = [item for item in res if isinstance(item, LDAPReference)] |
| 92 | assert any(refs) |