mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 06:57:47 +01:00
72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
from fastapi import status
|
|
import pytest
|
|
from httpx import AsyncClient, ASGITransport
|
|
|
|
|
|
def test_root_ok(client):
|
|
resp = client.get("/")
|
|
assert resp.status_code == status.HTTP_200_OK
|
|
assert resp.json() == {"status": "ok"}
|
|
|
|
|
|
def test_authenticated_route_requires_auth(client):
|
|
resp = client.get("/authenticated-route")
|
|
assert resp.status_code in (status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN)
|
|
|
|
|
|
def test_sentry_debug_raises_exception(client):
|
|
with pytest.raises(ZeroDivisionError):
|
|
client.get("/sentry-debug")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_and_get_category(fastapi_app, test_user):
|
|
# Use AsyncClient for async tests
|
|
transport = ASGITransport(app=fastapi_app)
|
|
async with AsyncClient(transport=transport, base_url="http://testserver") as ac:
|
|
# 1. Log in to get an auth token
|
|
login_resp = await ac.post("/auth/jwt/login", data=test_user)
|
|
token = login_resp.json()["access_token"]
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
# 2. Define and create the new category
|
|
category_name = "Async Integration Test"
|
|
category_payload = {"name": category_name}
|
|
create_resp = await ac.post("/categories/create", json=category_payload, headers=headers)
|
|
|
|
# 3. Assert creation was successful
|
|
assert create_resp.status_code == status.HTTP_201_CREATED
|
|
created_data = create_resp.json()
|
|
category_id = created_data["id"]
|
|
assert created_data["name"] == category_name
|
|
|
|
# 4. GET the list of categories to verify
|
|
list_resp = await ac.get("/categories/", headers=headers)
|
|
assert list_resp.status_code == status.HTTP_200_OK
|
|
|
|
# 5. Check that our new category is in the list
|
|
categories_list = list_resp.json()
|
|
assert any(cat["name"] == category_name for cat in categories_list)
|
|
|
|
delete_resp = await ac.delete(f"/categories/{category_id}", headers=headers)
|
|
assert delete_resp.status_code in (status.HTTP_200_OK, status.HTTP_204_NO_CONTENT)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_transaction_missing_amount_fails(fastapi_app, test_user):
|
|
transport = ASGITransport(app=fastapi_app)
|
|
async with AsyncClient(transport=transport, base_url="http://testserver") as ac:
|
|
# 1. Log in to get an auth token
|
|
login_resp = await ac.post("/auth/jwt/login", data=test_user)
|
|
token = login_resp.json()["access_token"]
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
# 2. Define an invalid payload
|
|
invalid_payload = {"description": "This should fail"}
|
|
|
|
# 3. Attempt to create the transaction
|
|
resp = await ac.post("/transactions/create", json=invalid_payload, headers=headers)
|
|
|
|
# 4. Assert the expected validation error
|
|
assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|