-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Summary Fixes #{[3691](#3691)} ### Time to review: __10 mins__ ## Changes proposed New route PUT /users/:userID/save-searches/:saved_search_id added Input and Output schema for route added Pydantic Model for input update fields Tests added --------- Co-authored-by: nava-platform-bot <[email protected]>
- Loading branch information
1 parent
c4ce4a5
commit c46afb3
Showing
5 changed files
with
244 additions
and
0 deletions.
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,33 @@ | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
from sqlalchemy import select | ||
|
||
from src.adapters import db | ||
from src.api.route_utils import raise_flask_error | ||
from src.db.models.user_models import UserSavedSearch | ||
|
||
|
||
class UpdateSavedSearchInput(BaseModel): | ||
name: str | ||
|
||
|
||
def update_saved_search( | ||
db_session: db.Session, user_id: UUID, saved_search_id: UUID, json_data: dict | ||
) -> UserSavedSearch: | ||
"""Update saved search for a user""" | ||
update_input = UpdateSavedSearchInput.model_validate(json_data) | ||
|
||
saved_search = db_session.execute( | ||
select(UserSavedSearch).where( | ||
UserSavedSearch.saved_search_id == saved_search_id, UserSavedSearch.user_id == user_id | ||
) | ||
).scalar_one_or_none() | ||
|
||
if not saved_search: | ||
raise_flask_error(404, "Saved search not found") | ||
|
||
# Update | ||
saved_search.name = update_input.name | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import uuid | ||
|
||
import pytest | ||
|
||
from src.db.models.user_models import UserSavedSearch, UserTokenSession | ||
from tests.src.db.models.factories import UserFactory, UserSavedSearchFactory | ||
|
||
|
||
@pytest.fixture | ||
def saved_search(enable_factory_create, user, db_session): | ||
search = UserSavedSearchFactory.create( | ||
user=user, name="Save Search", search_query={"keywords": "python"} | ||
) | ||
return search | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def clear_data(db_session): | ||
db_session.query(UserSavedSearch).delete() | ||
db_session.query(UserTokenSession).delete() | ||
yield | ||
|
||
|
||
def test_user_update_saved_search(client, db_session, user, user_auth_token, saved_search): | ||
updated_name = "Update Search" | ||
response = client.put( | ||
f"/v1/users/{user.user_id}/saved-searches/{saved_search.saved_search_id}", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
json={"name": updated_name}, | ||
) | ||
|
||
db_session.refresh(saved_search) | ||
|
||
assert response.status_code == 200 | ||
assert response.json["message"] == "Success" | ||
|
||
# Verify search was updated | ||
updated_saved_search = db_session.query(UserSavedSearch).first() | ||
|
||
assert updated_saved_search.name == updated_name | ||
|
||
|
||
def test_user_update_saved_search_not_found( | ||
client, | ||
enable_factory_create, | ||
db_session, | ||
user, | ||
user_auth_token, | ||
): | ||
# Try to update a non-existent search | ||
response = client.put( | ||
f"/v1/users/{user.user_id}/saved-searches/{uuid.uuid4()}", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
json={"name": "Update Search"}, | ||
) | ||
|
||
assert response.status_code == 404 | ||
assert response.json["message"] == "Saved search not found" | ||
|
||
|
||
def test_user_update_saved_search_unauthorized( | ||
client, enable_factory_create, db_session, user, user_auth_token, saved_search | ||
): | ||
# Try to update a search with another user | ||
unauthorized_user = UserFactory.create() | ||
response = client.put( | ||
f"/v1/users/{unauthorized_user.user_id}/saved-searches/{saved_search.saved_search_id}", | ||
headers={"X-SGG-Token": user_auth_token}, | ||
json={"name": "Update Search"}, | ||
) | ||
|
||
db_session.refresh(saved_search) | ||
|
||
assert response.status_code == 401 | ||
assert response.json["message"] == "Unauthorized user" | ||
|
||
# Verify search was not updated | ||
saved_searches = db_session.query(UserSavedSearch).first() | ||
assert saved_searches.name == saved_search.name | ||
|
||
|
||
def test_user_update_saved_search_no_auth( | ||
client, enable_factory_create, db_session, user, user_auth_token, saved_search | ||
): | ||
# Try to update a search without authentication | ||
response = client.put( | ||
f"/v1/users/{user.user_id}/saved-searches/{saved_search.saved_search_id}", | ||
json={"name": "Update Search"}, | ||
) | ||
db_session.refresh(saved_search) | ||
|
||
assert response.status_code == 401 | ||
assert response.json["message"] == "Unable to process token" | ||
|
||
# Verify search was not updated | ||
saved_searches = db_session.query(UserSavedSearch).first() | ||
assert saved_searches.name == saved_search.name |