Files
crm-system/tests/test_contacts.py
T

57 lines
1.8 KiB
Python
Raw Normal View History

2026-06-03 23:52:07 +00:00
"""Tests for FR-3 (Contact entity)."""
from __future__ import annotations
import pytest
from httpx import AsyncClient
@pytest.mark.asyncio
async def test_create_contact_with_account(
client: AsyncClient, auth_headers: dict[str, str], seed_data: dict
) -> None:
acc_id = seed_data["account_ids"][0]
resp = await client.post(
"/api/v1/contacts/",
json={
"first_name": "Diana",
"last_name": "Prince",
"email": "diana@x.example",
"account_id": acc_id,
},
2026-06-03 23:52:07 +00:00
headers=auth_headers,
)
assert resp.status_code == 201, resp.text
body = resp.json()
assert body["account_id"] == acc_id
@pytest.mark.asyncio
async def test_create_contact_invalid_account(
client: AsyncClient, auth_headers: dict[str, str]
) -> None:
resp = await client.post(
"/api/v1/contacts/",
json={"first_name": "Eve", "last_name": "Adams", "account_id": 99999},
headers=auth_headers,
)
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_list_contacts_filter_account(client: AsyncClient, seed_data: dict) -> None:
2026-06-03 23:52:07 +00:00
acc_id = seed_data["account_ids"][0]
resp = await client.get(f"/api/v1/contacts/?account_id={acc_id}", headers=seed_data["headers"])
assert resp.status_code == 200
body = resp.json()
assert all(c["account_id"] == acc_id for c in body)
@pytest.mark.asyncio
async def test_search_contacts_by_email(client: AsyncClient, seed_data: dict) -> None:
2026-06-03 23:52:07 +00:00
# seed_data creates contact with email 'anna@acme.example' on account 0
resp = await client.get("/api/v1/contacts/?q=anna@acme.example", headers=seed_data["headers"])
assert resp.status_code == 200
body = resp.json()
assert any("anna@acme.example" in (c.get("email") or "") for c in body)