Skip to content

Commit

Permalink
feat(search): search for user's roles and scopes
Browse files Browse the repository at this point in the history
Scope and Role of a user should be searchable. Also, modify the search
test so we also test substring matches
  • Loading branch information
winged committed Mar 21, 2022
1 parent 53c08fa commit 5418e18
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 17 additions & 5 deletions emeis/core/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,31 @@
from emeis.core.models import User


def test_search_users(
admin_client,
acl_factory,
):
@pytest.mark.parametrize(
"user_attribute",
[
lambda u: u.first_name,
lambda u: u.last_name,
lambda u: u.acls.first().scope.name,
lambda u: u.acls.first().role.name,
],
)
@pytest.mark.parametrize(
"partial_search", [lambda val: val[:-2], lambda val: val[2:], lambda val: val]
)
def test_search_users(admin_client, acl_factory, user_attribute, partial_search):

users_list = [acl.user for acl in acl_factory.create_batch(5)]

resp = admin_client.get(
reverse("user-list"), {"filter[search]": users_list[0].first_name}
reverse("user-list"),
{"filter[search]": partial_search(str(user_attribute(users_list[0])))},
)
returned_user_ids = [us["id"] for us in resp.json()["data"]]

assert str(users_list[0].pk) in returned_user_ids
# ensure we don't just return the full user list
assert len(returned_user_ids) < len(users_list)


@pytest.mark.parametrize(
Expand Down
4 changes: 4 additions & 0 deletions emeis/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ class UserViewSet(BaseViewset):
"username",
"first_name",
"last_name",
"acls__role__name",
"acls__scope__name",
"email",
"=zip",
"city",
)
multilingual_search_fields = [
"city",
"acls__role__name",
"acls__scope__name",
]

case_insensitive_ordering_fields = [
Expand Down

0 comments on commit 5418e18

Please sign in to comment.