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
|
2023-03-09 00:14:07 +00:00
|
|
|
from canaille.ldap_backend.ldapobject import python_attrs_to_ldap
|
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
|
|
|
|
|
|
|
|
2023-03-09 12:00:17 +00:00
|
|
|
def test_object_creation(slapd_connection):
|
|
|
|
User.initialize(slapd_connection)
|
|
|
|
user = User(
|
|
|
|
cn="Doe", # leading space
|
|
|
|
sn="Doe",
|
|
|
|
uid="user",
|
|
|
|
mail="john@doe.com",
|
|
|
|
)
|
|
|
|
assert not user.exists
|
|
|
|
user.save()
|
|
|
|
assert user.exists
|
|
|
|
|
|
|
|
user = User.get(dn=user.dn)
|
|
|
|
assert user.exists
|
|
|
|
|
|
|
|
user.delete()
|
|
|
|
|
|
|
|
|
2022-12-14 20:06:59 +00:00
|
|
|
def test_repr(slapd_connection, foo_group, user):
|
2023-03-08 15:25:50 +00:00
|
|
|
assert repr(foo_group) == "<Group cn=foo>"
|
|
|
|
assert repr(user) == "<User cn=John (johnny) Doe>"
|
2022-12-14 20:06:59 +00:00
|
|
|
|
|
|
|
|
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):
|
2023-03-07 13:49:44 +00:00
|
|
|
assert Group.query(cn="foo") == [foo_group]
|
|
|
|
assert Group.query(cn="bar") == [bar_group]
|
2022-12-14 23:15:10 +00:00
|
|
|
|
2023-03-02 17:35:26 +00:00
|
|
|
assert Group.query(cn="foo") != 3
|
|
|
|
|
2023-03-07 13:49:44 +00:00
|
|
|
assert Group.query(cn=["foo"]) == [foo_group]
|
|
|
|
assert Group.query(cn=["bar"]) == [bar_group]
|
2022-12-14 23:15:10 +00:00
|
|
|
|
2023-03-07 13:49:44 +00:00
|
|
|
assert set(Group.query(cn=["foo", "bar"])) == {foo_group, bar_group}
|
2022-12-14 23:15:10 +00:00
|
|
|
|
|
|
|
|
2023-03-07 16:46:34 +00:00
|
|
|
def test_fuzzy(slapd_connection, user, moderator, admin):
|
|
|
|
assert set(User.query()) == {user, moderator, admin}
|
|
|
|
assert set(User.fuzzy("Jack")) == {moderator}
|
|
|
|
assert set(User.fuzzy("moderator")) == {moderator}
|
|
|
|
assert set(User.fuzzy("oderat")) == {moderator}
|
|
|
|
assert set(User.fuzzy("oDeRat")) == {moderator}
|
|
|
|
assert set(User.fuzzy("ack")) == {moderator}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-03-09 00:14:07 +00:00
|
|
|
assert python_attrs_to_ldap(
|
2022-12-15 11:41:31 +00:00
|
|
|
{
|
|
|
|
"oauthClientName": ["foobar_name"],
|
|
|
|
"invalidAttribute": ["foobar"],
|
|
|
|
}
|
|
|
|
) == {
|
|
|
|
"oauthClientName": [b"foobar_name"],
|
|
|
|
"invalidAttribute": [b"foobar"],
|
|
|
|
}
|
2023-03-08 22:53:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_guess_object_from_dn(slapd_connection, testclient, foo_group):
|
|
|
|
foo_group.member = [foo_group]
|
|
|
|
foo_group.save()
|
|
|
|
g = LDAPObject.get(dn=foo_group.dn)
|
|
|
|
assert isinstance(g, Group)
|
|
|
|
assert g == foo_group
|
|
|
|
assert g.cn == foo_group.cn
|
|
|
|
|
|
|
|
ou = LDAPObject.get(dn=f"{Group.base},{Group.root_dn}")
|
|
|
|
assert isinstance(g, LDAPObject)
|