Skip to content

Commit

Permalink
users: fix get REST API for a user without profile
Browse files Browse the repository at this point in the history
* Fixes internal serveur error when we search for a user without profile.
* Fixes internal serveur error for the user search API without the `q` parameter.

Co-Authored-by: Johnny Mariéthoz <[email protected]>
  • Loading branch information
jma committed Mar 13, 2024
1 parent aa30131 commit 29a3040
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion rero_ils/modules/users/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ def dumps_metadata(self, dump_patron: bool = False) -> dict:
metadata = {
'roles': [r.name for r in self.user.roles]
}
metadata.update(self.user.user_profile)
if user_profile := self.user.user_profile:
metadata.update(user_profile)
if self.user.email:
metadata['email'] = self.user.email
if self.user.username:
Expand Down
2 changes: 1 addition & 1 deletion rero_ils/modules/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def __init__(self, **kwargs):
@check_user_list_permission
def get(self):
"""Get user info for the professionnal view."""
email_or_username = request.args.get('q', None).strip()
email_or_username = request.args.get('q', '').strip()
hits = {
'hits': {
'hits': [],
Expand Down
17 changes: 16 additions & 1 deletion tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,29 @@
from rero_ils.modules.items.api import Item, ItemsSearch


@pytest.fixture(scope='function')
def user_without_profile(db, default_user_password):
"""Create a simple invenio user with a profile."""
with db.session.begin_nested():
user = User(
email='[email protected]',
password=hash_password(default_user_password),
user_profile=None,
active=True,
)
db.session.add(user)
db.session.commit()
return user


@pytest.fixture(scope='function')
def user_with_profile(db, default_user_password):
"""Create a simple invenio user with a profile."""
with db.session.begin_nested():
user = User(
email='[email protected]',
password=hash_password(default_user_password),
profile={},
user_profile={},
active=True,
)
db.session.add(user)
Expand Down
26 changes: 25 additions & 1 deletion tests/api/users/test_users_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ def test_users_post_put(client, user_data_tmp, librarian_martigny,
assert res.status_code == 200


def test_users_search_api(client, librarian_martigny, patron_martigny):
def test_users_search_api(
client, librarian_martigny, patron_martigny, user_without_profile):
"""Test users search REST API."""
l_martigny = librarian_martigny
librarian_martigny = librarian_martigny.dumps()
Expand All @@ -151,6 +152,16 @@ def test_users_search_api(client, librarian_martigny, patron_martigny):

login_user_via_session(client, l_martigny.user)
# empty query => no result
res = client.get(
url_for(
'api_users.users_list'
)
)
assert res.status_code == 200
hits = get_json(res)
assert hits['hits']['hits'] == []
assert hits['hits']['total']['value'] == 0

res = client.get(
url_for(
'api_users.users_list',
Expand Down Expand Up @@ -214,6 +225,19 @@ def test_users_search_api(client, librarian_martigny, patron_martigny):
patron_martigny['username']
assert hits['hits']['total']['value'] == 1

# non patron by email
res = client.get(
url_for(
'api_users.users_list',
q='email:' + user_without_profile.email
)
)
assert res.status_code == 200
hits = get_json(res)
assert hits['hits']['hits'][0]['metadata']['email'] == \
user_without_profile.email
assert hits['hits']['total']['value'] == 1

# by uppercase email
res = client.get(
url_for(
Expand Down

0 comments on commit 29a3040

Please sign in to comment.