import pytest from httpx import AsyncClient from app.models.customer import Customer import uuid class TestCustomerAPI: async def test_list_customers_empty(self, client: AsyncClient, auth_headers): response = await client.get("/api/v1/customers", headers=auth_headers) assert response.status_code == 200 data = response.json() assert "items" in data assert data["items"] == [] assert data["total"] == 0 async def test_create_customer(self, client: AsyncClient, auth_headers): response = await client.post( "/api/v1/customers", headers=auth_headers, json={ "name": "John Smith", "company": "ABC Corp", "country": "USA", "phone": "+1234567890", "whatsapp_id": "john123", "email": "john@abc.com", "status": "lead", }, ) assert response.status_code == 200 data = response.json() assert data["name"] == "John Smith" assert data["company"] == "ABC Corp" assert data["country"] == "USA" async def test_create_customer_minimal(self, client: AsyncClient, auth_headers): response = await client.post( "/api/v1/customers", headers=auth_headers, json={"name": "Minimal Customer"}, ) assert response.status_code == 200 data = response.json() assert data["name"] == "Minimal Customer" async def test_list_customers_with_data(self, client: AsyncClient, auth_headers, db_session, test_user): customer = Customer( user_id=test_user.id, name="Test Customer", company="Test Co", country="China", status="lead", ) db_session.add(customer) await db_session.commit() response = await client.get("/api/v1/customers", headers=auth_headers) assert response.status_code == 200 data = response.json() assert len(data["items"]) == 1 assert data["items"][0]["name"] == "Test Customer" async def test_get_customer(self, client: AsyncClient, auth_headers, db_session, test_user): customer = Customer( user_id=test_user.id, name="Get Test", company="Get Co", status="negotiating", ) db_session.add(customer) await db_session.commit() response = await client.get( f"/api/v1/customers/{customer.id}", headers=auth_headers, ) assert response.status_code == 200 data = response.json() assert data["name"] == "Get Test" async def test_get_customer_not_found(self, client: AsyncClient, auth_headers): fake_id = str(uuid.uuid4()) response = await client.get( f"/api/v1/customers/{fake_id}", headers=auth_headers, ) assert response.status_code == 404 async def test_update_customer(self, client: AsyncClient, auth_headers, db_session, test_user): customer = Customer( user_id=test_user.id, name="Original Name", status="lead", ) db_session.add(customer) await db_session.commit() response = await client.patch( f"/api/v1/customers/{customer.id}", headers=auth_headers, json={"name": "Updated Name", "status": "negotiating"}, ) assert response.status_code == 200 data = response.json() assert data["name"] == "Updated Name" assert data["status"] == "negotiating" async def test_delete_customer(self, client: AsyncClient, auth_headers, db_session, test_user): customer = Customer( user_id=test_user.id, name="To Delete", ) db_session.add(customer) await db_session.commit() customer_id = customer.id response = await client.delete( f"/api/v1/customers/{customer_id}", headers=auth_headers, ) assert response.status_code == 200 get_response = await client.get( f"/api/v1/customers/{customer_id}", headers=auth_headers, ) assert get_response.status_code == 404 async def test_get_silent_customers(self, client: AsyncClient, auth_headers, db_session, test_user): from datetime import datetime, timedelta customer = Customer( user_id=test_user.id, name="Silent Customer", status="lead", last_contact_at=datetime.utcnow() - timedelta(days=5), ) db_session.add(customer) await db_session.commit() response = await client.get( "/api/v1/customers/silent?days=3", headers=auth_headers, ) assert response.status_code == 200 data = response.json() assert data["count"] >= 1