Skip to content

Commit

Permalink
Search functional w/ new filtering code
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysyngsun committed Feb 14, 2025
1 parent 1272350 commit aa9fe80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
27 changes: 16 additions & 11 deletions scim/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import operator
from collections.abc import Callable
from typing import Optional

from django.contrib.auth import get_user_model
from django.db.models import Model, Q
Expand All @@ -13,7 +14,7 @@ class FilterQuery:

model_cls: type[Model]

attr_map: dict[tuple[str, ...], tuple[str, ...]]
attr_map: dict[tuple[str, Optional[str]], tuple[str, ...]]

related_selects: list[str] = []

Expand Down Expand Up @@ -48,13 +49,17 @@ def _filter_expr(cls, parsed: ParseResults) -> Q:
def _attr_expr(cls, parsed: ParseResults) -> Q:
dj_op = cls.dj_op_mapping[parsed.comparison_operator.lower()]

scim_keys = (
(parsed.attr_name,)
if parsed.sub_path is None
else (parsed.attr_name, parsed.sub_path)
)
scim_keys = (parsed.attr_name, parsed.sub_path or None)

path_parts = [*cls.attr_map.get(scim_keys, scim_keys), dj_op]
path_parts = list(
filter(
lambda part: part is not None,
(
*cls.attr_map.get(scim_keys, scim_keys),
dj_op,
),
)
)
path = "__".join(path_parts)

q = Q(**{path: parsed.value})
Expand Down Expand Up @@ -107,11 +112,11 @@ def search(cls, filter_query, request=None): # noqa: ARG003
class UserFilterQuery(FilterQuery):
"""FilterQuery for User"""

attr_map: dict[tuple[str, ...], tuple[str, ...]] = {
("userName",): ("username",),
attr_map: dict[tuple[str, Optional[str]], tuple[str, ...]] = {
("userName", None): ("username",),
("emails", "value"): ("email",),
("active",): ("is_active",),
("fullName",): ("profile", "name"),
("active", None): ("is_active",),
("fullName", None): ("profile", "name"),
("name", "givenName"): ("first_name",),
("name", "familyName"): ("last_name",),
}
Expand Down
3 changes: 1 addition & 2 deletions scim/parser/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
Char,
Combine,
DelimitedList,
Empty,
FollowedBy,
Forward,
Group,
Expand Down Expand Up @@ -113,7 +112,7 @@ def _tag_value_type(value_type: ValueType) -> Tag:


SubAttr = ungroup(Combine(Suppress(".") + AttrName)).set_results_name("sub_attr") ^ (
Empty() + Tag("sub_attr", None)
Tag("sub_attr", None)
)

AttrPath = (
Expand Down
14 changes: 11 additions & 3 deletions scim/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,11 @@ def test_bulk_post(scim_client, bulk_test_data):

def test_user_search(scim_client):
"""Test the user search endpoint"""
users = UserFactory.create_batch(500)
emails = [user.email for user in users[:300]]
users = UserFactory.create_batch(1500)
emails = [user.email for user in users[:1000]]

resp = scim_client.post(
reverse("scim:users-search"),
f"{reverse('scim:users-search')}?count={len(emails)}",
content_type="application/scim+json",
data=json.dumps(
{
Expand All @@ -432,3 +432,11 @@ def test_user_search(scim_client):
)

assert resp.status_code == 200

data = resp.json()

assert data["totalResults"] == len(emails)
assert len(data["Resources"]) == len(emails)

for resource in data["Resources"]:
assert resource["emails"][0]["value"] in emails

0 comments on commit aa9fe80

Please sign in to comment.