-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Issue #3448] Create API endpoint for POST /users/:userID/save-searches #3472
Merged
mikehgrantsgov
merged 20 commits into
main
from
mikehgrantsgov/3448-create-save-search-endpoint
Jan 13, 2025
+256
−16
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e21a736
Add schema and route for create API
mikehgrantsgov 71cde06
Update examples
mikehgrantsgov 27613ef
Create ERD diagram and Update OpenAPI spec
nava-platform-bot 499dc5d
Update api/src/api/users/user_routes.py
mikehgrantsgov 54f007f
Update test for actual search body
mikehgrantsgov 4c82206
Merge branch 'mikehgrantsgov/3448-create-save-search-endpoint' of htt…
mikehgrantsgov 23e428e
Format
mikehgrantsgov c97fc30
Return saved search
mikehgrantsgov 50c900a
Lint
mikehgrantsgov 14c9e46
Update return type
mikehgrantsgov 2887afc
Update import
mikehgrantsgov 2620530
Format
mikehgrantsgov f56a1d5
Format / move user fixtures to conftest and save token session correctly
mikehgrantsgov a6a92fd
Merge branch 'main' into mikehgrantsgov/3448-create-save-search-endpoint
mikehgrantsgov 0fdf13c
Update api/src/api/users/user_routes.py
mikehgrantsgov e144b68
Update query request format
mikehgrantsgov 4ba5696
Merge branch 'mikehgrantsgov/3448-create-save-search-endpoint' of htt…
mikehgrantsgov c16acf7
Create ERD diagram and Update OpenAPI spec
nava-platform-bot ea6dca6
Remove begin from service
mikehgrantsgov 811bdb8
Lint
mikehgrantsgov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,18 @@ | ||
import logging | ||
from uuid import UUID | ||
|
||
from src.adapters import db | ||
from src.db.models.user_models import UserSavedSearch | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_saved_search(db_session: db.Session, user_id: UUID, json_data: dict) -> UserSavedSearch: | ||
saved_search = UserSavedSearch( | ||
user_id=user_id, name=json_data["name"], search_query=json_data["search_query"] | ||
) | ||
|
||
with db_session.begin(): | ||
db_session.add(saved_search) | ||
|
||
return saved_search |
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,105 @@ | ||
import pytest | ||
|
||
from src.constants.lookup_constants import FundingInstrument | ||
from src.db.models.user_models import UserSavedSearch | ||
from tests.src.api.opportunities_v1.conftest import get_search_request | ||
from tests.src.db.models.factories import UserFactory | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="function") | ||
def clear_saved_searches(db_session): | ||
db_session.query(UserSavedSearch).delete() | ||
db_session.commit() | ||
yield | ||
|
||
|
||
def test_user_save_search_post_unauthorized_user(client, db_session, user, user_auth_token): | ||
# Try to save a search for a different user ID | ||
different_user = UserFactory.create() | ||
|
||
search_query = get_search_request( | ||
funding_instrument_one_of=[FundingInstrument.GRANT], | ||
agency_one_of=["LOC"], | ||
) | ||
|
||
response = client.post( | ||
f"/v1/users/{different_user.user_id}/saved-searches", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
json={"name": "Test Search", "search_query": search_query}, | ||
) | ||
|
||
assert response.status_code == 401 | ||
assert response.json["message"] == "Unauthorized user" | ||
|
||
# Verify no search was saved | ||
saved_searches = db_session.query(UserSavedSearch).all() | ||
assert len(saved_searches) == 0 | ||
|
||
|
||
def test_user_save_search_post_no_auth(client, db_session, user): | ||
search_query = get_search_request( | ||
funding_instrument_one_of=[FundingInstrument.GRANT], | ||
agency_one_of=["LOC"], | ||
) | ||
|
||
# Try to save a search without authentication | ||
response = client.post( | ||
f"/v1/users/{user.user_id}/saved-searches", | ||
json={"name": "Test Search", "search_query": search_query}, | ||
) | ||
|
||
assert response.status_code == 401 | ||
assert response.json["message"] == "Unable to process token" | ||
|
||
# Verify no search was saved | ||
saved_searches = db_session.query(UserSavedSearch).all() | ||
assert len(saved_searches) == 0 | ||
|
||
|
||
def test_user_save_search_post_invalid_request(client, user, user_auth_token, db_session): | ||
# Make request with missing required fields | ||
response = client.post( | ||
f"/v1/users/{user.user_id}/saved-searches", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
json={}, | ||
) | ||
|
||
assert response.status_code == 422 # Validation error | ||
|
||
# Verify no search was saved | ||
saved_searches = db_session.query(UserSavedSearch).all() | ||
assert len(saved_searches) == 0 | ||
|
||
|
||
def test_user_save_search_post(client, user, user_auth_token, enable_factory_create, db_session): | ||
# Test data | ||
search_name = "Test Search" | ||
search_query = get_search_request( | ||
funding_instrument_one_of=[FundingInstrument.GRANT], | ||
agency_one_of=["LOC"], | ||
) | ||
|
||
# Make the request to save a search | ||
response = client.post( | ||
f"/v1/users/{user.user_id}/saved-searches", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
json={"name": search_name, "search_query": search_query}, | ||
) | ||
|
||
assert response.status_code == 200 | ||
assert response.json["message"] == "Success" | ||
|
||
# Verify the search was saved in the database | ||
saved_search = db_session.query(UserSavedSearch).one() | ||
assert saved_search.user_id == user.user_id | ||
assert saved_search.name == search_name | ||
assert saved_search.search_query == { | ||
"format": "json", | ||
"filters": {"agency": {"one_of": ["LOC"]}, "funding_instrument": {"one_of": ["grant"]}}, | ||
"pagination": { | ||
"order_by": "opportunity_id", | ||
"page_size": 25, | ||
"page_offset": 1, | ||
"sort_direction": "ascending", | ||
}, | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can exclude the begin here since we already began it in the route. Actually, in a lot of cases this would error with an "already started transaction error", guessing that didn't happen because you hadn't done any DB operations yet.