Skip to content

Commit 79f5016

Browse files
committed
feat: add unit test for LDAPSearch
Signed-off-by: Nicolas <32845761+nicoklaus@users.noreply.github.com>
1 parent ef9ff07 commit 79f5016

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

dje/tests/test_ldap_config.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# DejaCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: AGPL-3.0-only
5+
# See https://github.com/aboutcode-org/dejacode for support or download.
6+
# See https://aboutcode.org for more information about AboutCode FOSS projects.
7+
#
8+
9+
from django.core.exceptions import ImproperlyConfigured
10+
from django.test import SimpleTestCase
11+
12+
from django_auth_ldap.config import LDAPSearch
13+
from django_auth_ldap.config import LDAPSearchUnion
14+
15+
from dejacode.ldap_config import build_user_search
16+
17+
18+
class BuildUserSearchTestCase(SimpleTestCase):
19+
def test_build_user_search_fallback_to_single_search_when_empty(self):
20+
result = build_user_search("", "ou=users,dc=example,dc=com", "(uid=%(user)s)")
21+
self.assertIsInstance(result, LDAPSearch)
22+
23+
def test_build_user_search_valid_json_returns_union(self):
24+
raw = (
25+
'[{"base": "ou=a,dc=example,dc=com", "filter": "(uid=%(user)s)"},'
26+
' {"base": "ou=b,dc=example,dc=com", "filter": "(uid=%(user)s)"}]'
27+
)
28+
result = build_user_search(raw, "", "")
29+
self.assertIsInstance(result, LDAPSearchUnion)
30+
31+
def test_build_user_search_single_entry_still_returns_union(self):
32+
raw = '[{"base": "ou=a,dc=example,dc=com", "filter": "(uid=%(user)s)"}]'
33+
result = build_user_search(raw, "", "")
34+
self.assertIsInstance(result, LDAPSearchUnion)
35+
36+
def test_build_user_search_invalid_json_raises(self):
37+
with self.assertRaisesMessage(ImproperlyConfigured, "Invalid JSON"):
38+
build_user_search("{not json", "", "")
39+
40+
def test_build_user_search_not_a_list_raises(self):
41+
with self.assertRaisesMessage(ImproperlyConfigured, "must be a JSON list"):
42+
build_user_search('{"base": "x", "filter": "y"}', "", "")
43+
44+
def test_build_user_search_empty_list_raises(self):
45+
with self.assertRaisesMessage(ImproperlyConfigured, "cannot be empty"):
46+
build_user_search("[]", "", "")
47+
48+
def test_build_user_search_entry_not_object_raises(self):
49+
with self.assertRaisesMessage(ImproperlyConfigured, "[0] must be a JSON object"):
50+
build_user_search('["not an object"]', "", "")
51+
52+
def test_build_user_search_missing_base_raises(self):
53+
raw = '[{"filter": "(uid=%(user)s)"}]'
54+
with self.assertRaisesMessage(
55+
ImproperlyConfigured, "[0] must define 'base' and 'filter'"
56+
):
57+
build_user_search(raw, "", "")
58+
59+
def test_build_user_search_missing_filter_raises(self):
60+
raw = '[{"base": "ou=a,dc=example,dc=com"}]'
61+
with self.assertRaisesMessage(
62+
ImproperlyConfigured, "[0] must define 'base' and 'filter'"
63+
):
64+
build_user_search(raw, "", "")
65+
66+
def test_build_user_search_error_index_points_to_bad_entry(self):
67+
raw = (
68+
'[{"base": "ou=a,dc=example,dc=com", "filter": "(uid=%(user)s)"},'
69+
' {"base": "ou=b,dc=example,dc=com"}]'
70+
)
71+
with self.assertRaisesMessage(
72+
ImproperlyConfigured, "[1] must define 'base' and 'filter'"
73+
):
74+
build_user_search(raw, "", "")

0 commit comments

Comments
 (0)