Skip to content

Commit

Permalink
Clean up fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehgrantsgov committed Dec 20, 2024
1 parent 8a01adf commit 78298b3
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions api/tests/src/api/users/test_user_save_opportunity_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,37 @@
from tests.src.db.models.factories import OpportunityFactory, UserFactory


@pytest.fixture
def user(enable_factory_create, db_session):
user = UserFactory.create()
db_session.commit()
return user


@pytest.fixture
def user_auth_token(user, db_session):
token, _ = create_jwt_for_user(user, db_session)
return token


@pytest.fixture(autouse=True)
def clear_opportunities(db_session):
db_session.query(UserSavedOpportunity).delete()
db_session.commit()
yield


def test_user_save_opportunity_post_unauthorized_user(client, enable_factory_create, db_session):
# Create a user and get their token
user = UserFactory.create()
token, _ = create_jwt_for_user(user, db_session)
db_session.commit()

def test_user_save_opportunity_post_unauthorized_user(
client, db_session, user, user_auth_token, enable_factory_create
):
# Create an opportunity
opportunity = OpportunityFactory.create()

# Try to save an opportunity for a different user ID
different_user_id = uuid.uuid4()
response = client.post(
f"/v1/users/{different_user_id}/saved-opportunities",
headers={"X-SGG-Token": token},
headers={"X-SGG-Token": user_auth_token},
json={"opportunity_id": opportunity.opportunity_id},
)

Expand All @@ -38,11 +48,7 @@ def test_user_save_opportunity_post_unauthorized_user(client, enable_factory_cre
assert len(saved_opportunities) == 0


def test_user_save_opportunity_post_no_auth(client, enable_factory_create, db_session):
# Create a user but don't get a token
user = UserFactory.create()
db_session.commit()

def test_user_save_opportunity_post_no_auth(client, db_session, user, enable_factory_create):
# Create an opportunity
opportunity = OpportunityFactory.create()

Expand All @@ -60,16 +66,13 @@ def test_user_save_opportunity_post_no_auth(client, enable_factory_create, db_se
assert len(saved_opportunities) == 0


def test_user_save_opportunity_post_invalid_request(client, enable_factory_create, db_session):
# Create a user and get their token
user = UserFactory.create()
token, _ = create_jwt_for_user(user, db_session)
db_session.commit()

def test_user_save_opportunity_post_invalid_request(
client, user, user_auth_token, enable_factory_create, db_session
):
# Make request with missing opportunity_id
response = client.post(
f"/v1/users/{user.user_id}/saved-opportunities",
headers={"X-SGG-Token": token},
headers={"X-SGG-Token": user_auth_token},
json={},
)

Expand All @@ -80,19 +83,16 @@ def test_user_save_opportunity_post_invalid_request(client, enable_factory_creat
assert len(saved_opportunities) == 0


def test_user_save_opportunity_post(client, enable_factory_create, db_session):
# Create a user and get their token
user = UserFactory.create()
token, _ = create_jwt_for_user(user, db_session)
db_session.commit()

def test_user_save_opportunity_post(
client, user, user_auth_token, enable_factory_create, db_session
):
# Create an opportunity
opportunity = OpportunityFactory.create()

# Make the request to save an opportunity
response = client.post(
f"/v1/users/{user.user_id}/saved-opportunities",
headers={"X-SGG-Token": token},
headers={"X-SGG-Token": user_auth_token},
json={"opportunity_id": opportunity.opportunity_id},
)

Expand Down

0 comments on commit 78298b3

Please sign in to comment.