Skip to content

Commit

Permalink
tests: Update tests for users and admin
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshamarora1 authored and ntarocco committed Aug 22, 2024
1 parent a481568 commit 5080d8e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
6 changes: 5 additions & 1 deletion invenio_users_resources/services/users/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
FieldValueMapper,
QueryParser,
SearchFieldTransformer,
SuggestQueryParser,
)
from luqum.tree import Word

Expand Down Expand Up @@ -67,7 +68,7 @@ class UserSearchOptions(SearchOptions, SearchOptionsMixin):
# The user search needs to be highly restricted to avoid leaking
# account information, hence do not edit here unless you are
# absolutely sure what you're doing.
query_parser_cls = QueryParser.factory(
suggest_parser_cls = SuggestQueryParser.factory(
tree_transformer_cls=SearchFieldTransformer,
fields=["username^2", "email^2", "profile.full_name^3", "profile.affiliations"],
# Only public emails because hidden emails are stored in email_hidden field.
Expand All @@ -79,6 +80,8 @@ class UserSearchOptions(SearchOptions, SearchOptionsMixin):
"fullname": "profile.full_name",
"name": "profile.full_name",
},
type="most_fields", # https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#multi-match-types
fuzziness="AUTO", # https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness
)

params_interpreters_cls = [
Expand All @@ -104,6 +107,7 @@ class AdminUserSearchOptions(UserSearchOptions):
"email_hidden^3",
"domain^2",
"profile.full_name^3",
"profile.affiliations",
],
allow_list=[
"id",
Expand Down
63 changes: 41 additions & 22 deletions tests/services/users/test_service_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,50 +54,67 @@ def test_search_public_users(user_service, user_pub):
assert res["hits"]["total"] == 2 # 2 public users in conftest


# Admin search
@pytest.mark.parametrize(
"query",
[
"affiliations:CERN",
"affiliation:CERN",
"name:Jose affiliation:CERN",
"+name:Jose +affiliation:CERN",
"CERN",
"Tim",
"Tim CERN",
"Jose",
"Jos",
"Jose CERN",
"email:[email protected]",
"username:pub",
],
)
def test_admin_search_field(user_service, user_moderator, query):
"""Make sure certain fields ARE searchable."""
res = user_service.search_all(user_moderator.identity, q=query).to_dict()
assert res["hits"]["total"] > 0


# User search
@pytest.mark.parametrize(
"query",
[
"email:[email protected]",
"[email protected]",
"email:[email protected]",
"[email protected]",
"Plazi",
"+name:Jose -affiliation:CERN",
"name:Jose AND NOT affiliation:CERN",
"username:inactive",
"username:unconfirmed",
"preferences.visibility:public",
"preferences.email_visibility:restricted",
"profile.affiliations:Plazi",
"invalid:test",
"inactive",
"unconfirmed",
"restricted",
"Plazi",
"test",
],
)
def test_search_field_not_searchable(user_service, user_pub, query):
def test_user_search_field_not_searchable(user_service, user_pub, query):
"""Make sure certain fields are NOT searchable."""
res = user_service.search(user_pub.identity, q=query).to_dict()
res = user_service.search(user_pub.identity, suggest=query).to_dict()
assert res["hits"]["total"] == 0


@pytest.mark.parametrize(
"query",
[
"affiliations:CERN",
"affiliation:CERN",
"name:Jose affiliation:CERN",
"+name:Jose +affiliation:CERN",
"CERN",
"Jose CERN",
"Jose AND CERN",
"Tim",
"Tim CERN",
"Jose",
"Jos",
"Jose CERN",
"email:[email protected]",
"username:pub",
"[email protected]",
"pub",
],
)
def test_search_field(user_service, user_pub, query):
def test_user_search_field(user_service, user_pub, query):
"""Make sure certain fields ARE searchable."""
res = user_service.search(user_pub.identity, q=query).to_dict()
res = user_service.search(user_pub.identity, suggest=query).to_dict()
assert res["hits"]["total"] > 0


Expand Down Expand Up @@ -162,7 +179,9 @@ def test_search_permissions(app, db, user_service, user_moderator, user_res):
"""Test service search for permissions."""
# User can search for himself
search = user_service.search(
user_res.identity, q=f"username:{user_res._user.username}"
user_res.identity,
q=user_res._user.username,
fields=["username"],
)
assert search.total > 0

Expand Down

0 comments on commit 5080d8e

Please sign in to comment.