2022-12-14 23:03:01 +00:00
|
|
|
import datetime
|
|
|
|
|
2022-03-02 17:31:48 +00:00
|
|
|
import ldap.dn
|
2022-12-15 11:41:31 +00:00
|
|
|
from canaille.ldap_backend.ldapobject import LDAPObject
|
2022-12-14 23:03:01 +00:00
|
|
|
from canaille.ldap_backend.utils import ldap_to_python
|
|
|
|
from canaille.ldap_backend.utils import python_to_ldap
|
|
|
|
from canaille.ldap_backend.utils import Syntax
|
2021-06-03 13:00:11 +00:00
|
|
|
from canaille.models import Group
|
2022-03-02 17:31:48 +00:00
|
|
|
from canaille.models import User
|
2021-06-03 13:00:11 +00:00
|
|
|
|
|
|
|
|
2022-12-14 20:06:59 +00:00
|
|
|
def test_repr(slapd_connection, foo_group, user):
|
|
|
|
assert repr(foo_group) == "<Group cn=['foo']>"
|
|
|
|
assert repr(user) == "<User cn=['John (johnny) Doe']>"
|
|
|
|
|
|
|
|
|
2021-06-03 13:00:11 +00:00
|
|
|
def test_equality(slapd_connection, foo_group, bar_group):
|
2022-05-08 14:31:17 +00:00
|
|
|
Group.ldap_object_attributes()
|
2021-06-03 13:00:11 +00:00
|
|
|
assert foo_group != bar_group
|
2022-05-08 14:31:17 +00:00
|
|
|
foo_group2 = Group.get(dn=foo_group.dn)
|
2021-06-03 13:00:11 +00:00
|
|
|
assert foo_group == foo_group2
|
2022-03-02 17:31:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_dn_when_leading_space_in_id_attribute(slapd_connection):
|
|
|
|
User.initialize(slapd_connection)
|
|
|
|
user = User(
|
|
|
|
cn=" Doe", # leading space
|
|
|
|
sn="Doe",
|
|
|
|
uid="user",
|
|
|
|
mail="john@doe.com",
|
|
|
|
)
|
2022-05-08 14:31:17 +00:00
|
|
|
user.save()
|
2022-03-02 17:31:48 +00:00
|
|
|
|
|
|
|
assert ldap.dn.is_dn(user.dn)
|
|
|
|
assert ldap.dn.dn2str(ldap.dn.str2dn(user.dn)) == user.dn
|
2022-11-05 18:52:57 +00:00
|
|
|
assert user.dn == "cn=Doe,ou=users,dc=mydomain,dc=tld"
|
2022-03-02 17:31:48 +00:00
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
user.delete()
|
2022-05-18 09:31:26 +00:00
|
|
|
|
2022-03-02 17:31:48 +00:00
|
|
|
|
|
|
|
def test_dn_when_ldap_special_char_in_id_attribute(slapd_connection):
|
|
|
|
User.initialize(slapd_connection)
|
|
|
|
user = User(
|
|
|
|
cn="#Doe", # special char
|
|
|
|
sn="Doe",
|
|
|
|
uid="user",
|
|
|
|
mail="john@doe.com",
|
|
|
|
)
|
2022-05-08 14:31:17 +00:00
|
|
|
user.save()
|
2022-03-02 17:31:48 +00:00
|
|
|
|
|
|
|
assert ldap.dn.is_dn(user.dn)
|
|
|
|
assert ldap.dn.dn2str(ldap.dn.str2dn(user.dn)) == user.dn
|
2022-11-05 18:52:57 +00:00
|
|
|
assert user.dn == "cn=\\#Doe,ou=users,dc=mydomain,dc=tld"
|
2022-05-18 09:31:26 +00:00
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
user.delete()
|
2022-12-14 23:03:01 +00:00
|
|
|
|
|
|
|
|
2022-12-14 23:15:10 +00:00
|
|
|
def test_filter(slapd_connection, foo_group, bar_group):
|
|
|
|
assert Group.filter(cn="foo") == [foo_group]
|
|
|
|
assert Group.filter(cn="bar") == [bar_group]
|
|
|
|
|
|
|
|
assert Group.filter(cn=["foo"]) == [foo_group]
|
|
|
|
assert Group.filter(cn=["bar"]) == [bar_group]
|
|
|
|
|
|
|
|
assert set(Group.filter(cn=["foo", "bar"])) == {foo_group, bar_group}
|
|
|
|
|
|
|
|
|
2022-12-14 23:03:01 +00:00
|
|
|
def test_ldap_to_python():
|
|
|
|
assert (
|
|
|
|
python_to_ldap(datetime.datetime.min, Syntax.GENERALIZED_TIME)
|
|
|
|
== b"000001010000Z"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
python_to_ldap(datetime.datetime(2000, 1, 2, 3, 4, 5), Syntax.GENERALIZED_TIME)
|
|
|
|
== b"20000102030405Z"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert python_to_ldap(1337, Syntax.INTEGER) == b"1337"
|
|
|
|
|
|
|
|
assert python_to_ldap(True, Syntax.BOOLEAN) == b"TRUE"
|
|
|
|
assert python_to_ldap(False, Syntax.BOOLEAN) == b"FALSE"
|
|
|
|
|
|
|
|
assert python_to_ldap("foobar", Syntax.DIRECTORY_STRING) == b"foobar"
|
|
|
|
|
|
|
|
assert python_to_ldap(b"foobar", Syntax.JPEG) == b"foobar"
|
|
|
|
|
|
|
|
|
|
|
|
def test_python_to_ldap():
|
|
|
|
assert ldap_to_python(
|
|
|
|
b"20000102030405Z", Syntax.GENERALIZED_TIME
|
|
|
|
) == datetime.datetime(2000, 1, 2, 3, 4, 5)
|
|
|
|
assert (
|
|
|
|
ldap_to_python(b"000001010000Z", Syntax.GENERALIZED_TIME)
|
|
|
|
== datetime.datetime.min
|
|
|
|
)
|
|
|
|
|
|
|
|
assert ldap_to_python(b"1337", Syntax.INTEGER) == 1337
|
|
|
|
|
|
|
|
assert ldap_to_python(b"TRUE", Syntax.BOOLEAN) is True
|
|
|
|
assert ldap_to_python(b"FALSE", Syntax.BOOLEAN) is False
|
|
|
|
|
|
|
|
assert ldap_to_python(b"foobar", Syntax.DIRECTORY_STRING) == "foobar"
|
|
|
|
|
|
|
|
assert ldap_to_python(b"foobar", Syntax.JPEG) == b"foobar"
|
2022-12-15 11:41:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_operational_attribute_conversion(slapd_connection):
|
|
|
|
assert "oauthClientName" in LDAPObject.ldap_object_attributes(slapd_connection)
|
|
|
|
assert "invalidAttribute" not in LDAPObject.ldap_object_attributes(slapd_connection)
|
|
|
|
|
|
|
|
assert LDAPObject.ldap_attrs_to_python(
|
|
|
|
{
|
|
|
|
"oauthClientName": [b"foobar_name"],
|
|
|
|
"invalidAttribute": [b"foobar"],
|
|
|
|
}
|
|
|
|
) == {
|
|
|
|
"oauthClientName": ["foobar_name"],
|
|
|
|
"invalidAttribute": ["foobar"],
|
|
|
|
}
|
|
|
|
|
|
|
|
assert LDAPObject.python_attrs_to_ldap(
|
|
|
|
{
|
|
|
|
"oauthClientName": ["foobar_name"],
|
|
|
|
"invalidAttribute": ["foobar"],
|
|
|
|
}
|
|
|
|
) == {
|
|
|
|
"oauthClientName": [b"foobar_name"],
|
|
|
|
"invalidAttribute": [b"foobar"],
|
|
|
|
}
|