Update dependencies to use httpx2 and refactor tests for compatibility

This commit is contained in:
k1nq
2026-09-16 13:49:46 +05:00
parent 7d0762ef17
commit 01028f440e
4 changed files with 79 additions and 61 deletions
+42 -35
View File
@@ -1,58 +1,65 @@
import uuid
from collections.abc import AsyncIterator
import httpx2
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.api import router
def make_client() -> TestClient:
@pytest.fixture
async def client() -> AsyncIterator[httpx2.AsyncClient]:
app = FastAPI()
app.include_router(router)
return TestClient(app)
transport = httpx2.ASGITransport(app=app)
async with httpx2.AsyncClient(
transport=transport,
base_url="http://testserver",
) as test_client:
yield test_client
def test_get_payment_requires_api_key() -> None:
with make_client() as client:
response = client.get(f"/api/v1/payments/{uuid.uuid4()}")
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"}
def test_create_payment_requires_idempotency_key() -> None:
with make_client() as client:
response = 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",
},
)
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"]
def test_create_payment_rejects_unsupported_currency() -> None:
with make_client() as client:
response = 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",
},
)
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