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

66 lines
1.8 KiB
Python

import uuid
from collections.abc import AsyncIterator
import httpx2
import pytest
from fastapi import FastAPI
from app.api import router
@pytest.fixture
async def client() -> AsyncIterator[httpx2.AsyncClient]:
app = FastAPI()
app.include_router(router)
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_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