e62ece1c06
- FastAPI app with CORS, lifespan handlers - Pydantic Settings config (DB, Redis, CORS, SMTP, JWT, Rentman) - SQLAlchemy async engine + session (DeclarativeBase) - 6 DB models: EquipmentCache, RentalRequest, RentalRequestItem, Contact, AdminUser, SyncLog - Pydantic schemas: EquipmentItem, EquipmentDetail, PaginatedEquipment, ContactCreate, ContactResponse - Redis cache helper: set/get/delete_pattern, rate limiting, equipment key builders - Equipment router: list (search/category/sort/pagination), detail, categories – all cached - Contact router: POST with Pydantic validation + rate limiting (5/min) - Health router: GET /api/health with DB + Redis status - 28 pytest tests (all pass, 90% coverage) - Dockerfile, requirements.txt, pytest.ini, test_report.md
71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
"""Tests for the contact form endpoint."""
|
|
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_contact_valid(client: AsyncClient):
|
|
"""POST /api/contact with valid data returns 201."""
|
|
response = await client.post("/api/contact", json={
|
|
"name": "Max Mustermann",
|
|
"email": "max@example.com",
|
|
"phone": "+49 170 1234567",
|
|
"message": "Hallo, ich habe eine Frage.",
|
|
"privacy_consent": True,
|
|
})
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["success"] is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_contact_invalid_email(client: AsyncClient):
|
|
"""POST /api/contact with invalid email returns 422."""
|
|
response = await client.post("/api/contact", json={
|
|
"name": "Max Mustermann",
|
|
"email": "invalid-email",
|
|
"message": "Hallo",
|
|
"privacy_consent": True,
|
|
})
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_contact_no_consent(client: AsyncClient):
|
|
"""POST /api/contact with privacy_consent=False returns 422."""
|
|
response = await client.post("/api/contact", json={
|
|
"name": "Max Mustermann",
|
|
"email": "max@example.com",
|
|
"message": "Hallo",
|
|
"privacy_consent": False,
|
|
})
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_contact_missing_name(client: AsyncClient):
|
|
"""POST /api/contact with missing name returns 422."""
|
|
response = await client.post("/api/contact", json={
|
|
"email": "max@example.com",
|
|
"message": "Hallo",
|
|
"privacy_consent": True,
|
|
})
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_contact_rate_limit(client: AsyncClient):
|
|
"""6th contact request within 60s is rate-limited."""
|
|
payload = {
|
|
"name": "Max Mustermann",
|
|
"email": "max@example.com",
|
|
"message": "Hallo",
|
|
"privacy_consent": True,
|
|
}
|
|
for i in range(5):
|
|
resp = await client.post("/api/contact", json=payload)
|
|
assert resp.status_code == 201
|
|
resp6 = await client.post("/api/contact", json=payload)
|
|
assert resp6.status_code == 429
|