"""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