| 1 | import pytest |
| 2 | |
| 3 | from bonsai import LDAPURL |
| 4 | from bonsai import LDAPDN |
| 5 | from bonsai.errors import InvalidDN |
| 6 | |
| 7 | |
| 8 | @pytest.fixture |
| 9 | def valid_ldapurl(): |
| 10 | """ Get a valid LDAPURL. """ |
| 11 | strurl = "ldaps://testurl:444/cn=test,dc=test?sn,gn?base?(objectclass=*)?1.2.3.4" |
| 12 | return LDAPURL(strurl) |
| 13 | |
| 14 | |
| 15 | def test_get_address(valid_ldapurl): |
| 16 | """ Test get_address method. """ |
| 17 | ldapi_url = LDAPURL("ldapi://%2Ftmp%2Fldapi") |
| 18 | assert valid_ldapurl.get_address() == "ldaps://testurl:444" |
| 19 | assert ldapi_url.get_address() == "ldapi://%2Ftmp%2Fldapi" |
| 20 | |
| 21 | |
| 22 | def test_get_host_properties(valid_ldapurl): |
| 23 | """ Test getting LDAPURL host properties. """ |
| 24 | ldapi_url = LDAPURL("ldapi://%2Ftmp%2Fldapi") |
| 25 | assert valid_ldapurl.scheme == "ldaps" |
| 26 | assert valid_ldapurl.host == "testurl" |
| 27 | assert valid_ldapurl.port == 444 |
| 28 | assert ldapi_url.scheme == "ldapi" |
| 29 | assert ldapi_url.port == 0 |
| 30 | |
| 31 | |
| 32 | def test_set_host_properties(): |
| 33 | """ Test setting LDAPURL host properties. """ |
| 34 | url = LDAPURL() |
| 35 | with pytest.raises(ValueError): |
| 36 | url.host = ":malformed,@äđĐ-" |
| 37 | with pytest.raises(ValueError): |
| 38 | url.port = "9922" |
| 39 | with pytest.raises(ValueError): |
| 40 | url.scheme = "http" |
| 41 | |
| 42 | url.host = "testurl2" |
| 43 | url.port = 589 |
| 44 | url.scheme = "ldap" |
| 45 | assert url.scheme == "ldap" |
| 46 | assert url.host == "testurl2" |
| 47 | assert url.port == 589 |
| 48 | |
| 49 | |
| 50 | def test_get_bind_properties(valid_ldapurl): |
| 51 | """ Test getting LDAPURL bind properties. """ |
| 52 | assert valid_ldapurl.basedn == LDAPDN("cn=test,dc=test") |
| 53 | assert valid_ldapurl.scope == "base" |
| 54 | assert valid_ldapurl.filter_exp == "(objectclass=*)" |
| 55 | assert valid_ldapurl.attributes == ["sn", "gn"] |
| 56 | |
| 57 | |
| 58 | def test_set_bind_properties(): |
| 59 | """ Test setting LDAPURL bind properties. """ |
| 60 | url = LDAPURL() |
| 61 | with pytest.raises(InvalidDN): |
| 62 | url.basedn = "test" |
| 63 | |
| 64 | url.basedn = LDAPDN("cn=test") |
| 65 | assert str(url.basedn) == "cn=test" |
| 66 | |
| 67 | |
| 68 | def test_str(valid_ldapurl): |
| 69 | """ Test __str__ method of LDAPURL. """ |
| 70 | assert ( |
| 71 | str(valid_ldapurl) |
| 72 | == "ldaps://testurl:444/cn=test,dc=test?sn,gn?base?(objectclass=*)?1.2.3.4" |
| 73 | ) |
| 74 | assert str(LDAPURL("ldap://127.0.0.1/cn=x?cn")) == "ldap://127.0.0.1:389/cn=x?cn" |
| 75 | assert str(LDAPURL("ldap:///")) == "ldap://localhost:389" |
| 76 | assert str(LDAPURL("ldapi:///")) == "ldapi://localhost" |
| 77 | assert not LDAPURL("ldap:///") == "http://localhost:389" |
| 78 | assert "<LDAPURL" in repr(valid_ldapurl) |
| 79 | |
| 80 | |
| 81 | def test_conversion(): |
| 82 | """ Test ValueError exception for invalid URL format. """ |
| 83 | with pytest.raises(ValueError): |
| 84 | _ = LDAPURL("ldap://failed.com/?falsedn?d") |
| 85 | |
| 86 | |
| 87 | def test_del_attr(valid_ldapurl): |
| 88 | """ Test trying to delete an attribute. """ |
| 89 | with pytest.raises(AttributeError): |
| 90 | del valid_ldapurl.host |
| 91 | try: |
| 92 | _ = valid_ldapurl.host |
| 93 | except AttributeError: |
| 94 | pytest.fail("Attribute not should be deleted.") |
| 95 | |
| 96 | |
| 97 | def test_invalid(): |
| 98 | """ Test invalid LDAP URLs. """ |
| 99 | with pytest.raises(ValueError): |
| 100 | _ = LDAPURL("http://localhost") |
| 101 | with pytest.raises(ValueError): |
| 102 | _ = LDAPURL("ldaps://localost.") |
| 103 | |
| 104 | |
| 105 | def test_scope(): |
| 106 | """ Test scope and scope_num property. """ |
| 107 | url = LDAPURL("ldap:///??one") |
| 108 | assert url.scope_num == 1 |
| 109 | url.scope = "base" |
| 110 | assert url.scope_num == 0 |
| 111 | |
| 112 | with pytest.raises(TypeError): |
| 113 | url.scope = 2.1 |
| 114 | with pytest.raises(ValueError): |
| 115 | url.scope = "all" |
| 116 | |
| 117 | |
| 118 | def test_ipv6(): |
| 119 | """ Test IPv6 address """ |
| 120 | url = LDAPURL( |
| 121 | "ldap://[2001:db8:85a3::8a2e:370:7334]:1498/" |
| 122 | "o=University%20of%20Michigan,c=US??one?" |
| 123 | "(cn=Babs%20Jensen)" |
| 124 | ) |
| 125 | assert url.host == "2001:db8:85a3::8a2e:370:7334" |
| 126 | assert url.port == 1498 |
| 127 | assert url.scope == "one" |
| 128 | assert url.filter_exp == "(cn=Babs Jensen)" |
| 129 | addr = url.get_address() |
| 130 | assert addr == "ldap://[2001:db8:85a3::8a2e:370:7334]:1498" |
| 131 | with pytest.raises(ValueError): |
| 132 | _ = LDAPURL("ldap://2001::85::37:7334") |