Files
payment-processing-test/tests/unit/test_api_contract.py
T

78 lines
2.2 KiB
Python

import uuid
from collections.abc import AsyncIterator
import httpx2
import pytest
from fastapi import FastAPI
from app.api import router
from app.auth import get_settings
from app.config import Settings
@pytest.fixture
async def client() -> AsyncIterator[httpx2.AsyncClient]:
app = FastAPI()
app.include_router(router)
app.dependency_overrides[get_settings] = lambda: Settings(api_key="change-me")
transport = httpx2.ASGITransport(app=app)
async with httpx2.AsyncClient(
transport=transport,
base_url="http://testserver",
) as test_client:
yield test_client
async def test_get_payment_requires_api_key(client: httpx2.AsyncClient) -> None:
response = await client.get(f"/api/v1/payments/{uuid.uuid4()}")
assert response.status_code == 401
assert response.json() == {"detail": "Invalid or missing API key"}
async def test_get_payment_rejects_wrong_api_key(client: httpx2.AsyncClient) -> None:
response = await client.get(
f"/api/v1/payments/{uuid.uuid4()}",
headers={"X-API-Key": "wrong-key"},
)
assert response.status_code == 401
async def test_create_payment_requires_idempotency_key(client: httpx2.AsyncClient) -> None:
response = await client.post(
"/api/v1/payments",
headers={"X-API-Key": "change-me"},
json={
"amount": "100.00",
"currency": "RUB",
"description": "Test payment",
"metadata": {},
"webhook_url": "https://example.com/webhook",
},
)
assert response.status_code == 422
assert response.json()["detail"][0]["loc"] == ["header", "Idempotency-Key"]
async def test_create_payment_rejects_unsupported_currency(
client: httpx2.AsyncClient,
) -> None:
response = await client.post(
"/api/v1/payments",
headers={
"X-API-Key": "change-me",
"Idempotency-Key": "test-key",
},
json={
"amount": "100.00",
"currency": "GBP",
"description": "Test payment",
"metadata": {},
"webhook_url": "https://example.com/webhook",
},
)
assert response.status_code == 422