-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue #3447] Create API endpoint for GET /users/:userID/save-searches (
#3521) ## Summary Fixes #3447 ### Time to review: 15 mins ## Changes proposed Create GET endpoint to return saved searched, accounting for enum values Add unit tests ## Context for reviewers This is the final API required for saving user searches in this iteration. ## Additional information See unit tests
- Loading branch information
1 parent
e7b7db6
commit 476f3c8
Showing
6 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from uuid import UUID | ||
|
||
from sqlalchemy import select | ||
|
||
from src.adapters import db | ||
from src.db.models.user_models import UserSavedSearch | ||
|
||
|
||
def get_saved_searches(db_session: db.Session, user_id: UUID) -> list[UserSavedSearch]: | ||
"""Get all saved searches for a user""" | ||
saved_searches = ( | ||
db_session.execute( | ||
select(UserSavedSearch) | ||
.where(UserSavedSearch.user_id == user_id) | ||
.order_by(UserSavedSearch.created_at.desc()) | ||
) | ||
.scalars() | ||
.all() | ||
) | ||
|
||
return list(saved_searches) |
108 changes: 108 additions & 0 deletions
108
api/tests/src/api/users/test_user_get_saved_searches.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import uuid | ||
from datetime import datetime, timezone | ||
|
||
import pytest | ||
from sqlalchemy import delete | ||
|
||
from src.constants.lookup_constants import FundingInstrument | ||
from src.db.models.user_models import UserSavedSearch, UserTokenSession | ||
from tests.src.db.models.factories import UserFactory, UserSavedSearchFactory | ||
|
||
|
||
@pytest.fixture | ||
def saved_searches(user, db_session): | ||
searches = [ | ||
UserSavedSearchFactory.create( | ||
user=user, | ||
name="Test Search 1", | ||
search_query={ | ||
"query": "python", | ||
"filters": {"funding_instrument": {"one_of": [FundingInstrument.GRANT]}}, | ||
}, | ||
created_at=datetime(2024, 1, 1, tzinfo=timezone.utc), | ||
), | ||
UserSavedSearchFactory.create( | ||
user=user, | ||
name="Test Search 2", | ||
search_query={ | ||
"query": "python", | ||
"filters": { | ||
"keywords": "java", | ||
"funding_instrument": {"one_of": [FundingInstrument.COOPERATIVE_AGREEMENT]}, | ||
}, | ||
}, | ||
created_at=datetime(2024, 1, 2, tzinfo=timezone.utc), | ||
), | ||
] | ||
db_session.commit() | ||
return searches | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def clear_data(db_session): | ||
db_session.execute(delete(UserSavedSearch)) | ||
db_session.execute(delete(UserTokenSession)) | ||
db_session.commit() | ||
yield | ||
|
||
|
||
def test_user_get_saved_searches_unauthorized_user( | ||
client, db_session, user, user_auth_token, saved_searches | ||
): | ||
# Try to get searches for a different user ID | ||
different_user = UserFactory.create() | ||
db_session.commit() | ||
|
||
response = client.get( | ||
f"/v1/users/{different_user.user_id}/saved-searches", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
) | ||
|
||
assert response.status_code == 401 | ||
assert response.json["message"] == "Unauthorized user" | ||
|
||
|
||
def test_user_get_saved_searches_no_auth(client, db_session, user, saved_searches): | ||
# Try to get searches without authentication | ||
response = client.get( | ||
f"/v1/users/{user.user_id}/saved-searches", | ||
) | ||
|
||
assert response.status_code == 401 | ||
assert response.json["message"] == "Unable to process token" | ||
|
||
|
||
def test_user_get_saved_searches_empty(client, user, user_auth_token): | ||
response = client.get( | ||
f"/v1/users/{user.user_id}/saved-searches", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
) | ||
|
||
assert response.status_code == 200 | ||
assert response.json["message"] == "Success" | ||
assert response.json["data"] == [] | ||
|
||
|
||
def test_user_get_saved_searches(client, user, user_auth_token, saved_searches): | ||
response = client.get( | ||
f"/v1/users/{user.user_id}/saved-searches", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
) | ||
|
||
assert response.status_code == 200 | ||
assert response.json["message"] == "Success" | ||
|
||
data = response.json["data"] | ||
assert len(data) == 2 | ||
|
||
# Verify the searches are returned in descending order by created_at | ||
assert data[0]["name"] == "Test Search 2" | ||
assert data[0]["search_query"]["filters"]["funding_instrument"]["one_of"] == [ | ||
"cooperative_agreement" | ||
] | ||
assert data[1]["name"] == "Test Search 1" | ||
assert data[1]["search_query"]["filters"]["funding_instrument"]["one_of"] == ["grant"] | ||
|
||
# Verify UUIDs are properly serialized | ||
assert uuid.UUID(data[0]["saved_search_id"]) | ||
assert uuid.UUID(data[1]["saved_search_id"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters