-
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.
Merge branch 'main' into mikehgrantsgov/3296-add-delete-opp-endpoint
- Loading branch information
Showing
41 changed files
with
1,693 additions
and
903 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,27 @@ | ||
import logging | ||
from uuid import UUID | ||
|
||
from sqlalchemy import select | ||
from sqlalchemy.orm import selectinload | ||
|
||
from src.adapters import db | ||
from src.db.models.opportunity_models import Opportunity | ||
from src.db.models.user_models import UserSavedOpportunity | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_saved_opportunities(db_session: db.Session, user_id: UUID) -> list[Opportunity]: | ||
logger.info(f"Getting saved opportunities for user {user_id}") | ||
|
||
saved_opportunities = ( | ||
db_session.execute( | ||
select(Opportunity) | ||
.join(UserSavedOpportunity) | ||
.where(UserSavedOpportunity.user_id == user_id) | ||
.options(selectinload("*")) | ||
) | ||
.scalars() | ||
.all() | ||
) | ||
return list(saved_opportunities) |
74 changes: 74 additions & 0 deletions
74
api/tests/src/api/users/test_user_saved_opportunities_get.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,74 @@ | ||
import pytest | ||
|
||
from src.auth.api_jwt_auth import create_jwt_for_user | ||
from src.db.models.user_models import UserSavedOpportunity | ||
from tests.src.db.models.factories import ( | ||
OpportunityFactory, | ||
UserFactory, | ||
UserSavedOpportunityFactory, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def user(enable_factory_create, db_session): | ||
return UserFactory.create() | ||
|
||
|
||
@pytest.fixture | ||
def user_auth_token(user, db_session): | ||
token, _ = create_jwt_for_user(user, db_session) | ||
return token | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="function") | ||
def clear_opportunities(db_session): | ||
db_session.query(UserSavedOpportunity).delete() | ||
db_session.commit() | ||
yield | ||
|
||
|
||
def test_user_get_saved_opportunities( | ||
client, user, user_auth_token, enable_factory_create, db_session | ||
): | ||
# Create an opportunity and save it for the user | ||
opportunity = OpportunityFactory.create(opportunity_title="Test Opportunity") | ||
UserSavedOpportunityFactory.create(user=user, opportunity=opportunity) | ||
|
||
# Make the request | ||
response = client.get( | ||
f"/v1/users/{user.user_id}/saved-opportunities", headers={"X-SGG-Token": user_auth_token} | ||
) | ||
|
||
assert response.status_code == 200 | ||
assert len(response.json["data"]) == 1 | ||
assert response.json["data"][0]["opportunity_id"] == opportunity.opportunity_id | ||
assert response.json["data"][0]["opportunity_title"] == opportunity.opportunity_title | ||
|
||
|
||
def test_get_saved_opportunities_unauthorized_user(client, enable_factory_create, db_session, user): | ||
"""Test that a user cannot view another user's saved opportunities""" | ||
# Create a user and get their token | ||
user = UserFactory.create() | ||
token, _ = create_jwt_for_user(user, db_session) | ||
|
||
# Create another user and save an opportunity for them | ||
other_user = UserFactory.create() | ||
opportunity = OpportunityFactory.create() | ||
UserSavedOpportunityFactory.create(user=other_user, opportunity=opportunity) | ||
|
||
# Try to get the other user's saved opportunities | ||
response = client.get( | ||
f"/v1/users/{other_user.user_id}/saved-opportunities", headers={"X-SGG-Token": token} | ||
) | ||
|
||
assert response.status_code == 401 | ||
assert response.json["message"] == "Unauthorized user" | ||
|
||
# Try with a non-existent user ID | ||
different_user_id = "123e4567-e89b-12d3-a456-426614174000" | ||
response = client.get( | ||
f"/v1/users/{different_user_id}/saved-opportunities", headers={"X-SGG-Token": token} | ||
) | ||
|
||
assert response.status_code == 401 | ||
assert response.json["message"] == "Unauthorized user" |
Oops, something went wrong.