File size: 1,508 Bytes
8ca4657
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pytest
from httpx import AsyncClient

async def get_auth_headers(client: AsyncClient, email: str) -> dict:
    pwd = "password123"
    await client.post("/api/v1/auth/signup", json={"email": email, "password": pwd, "full_name": "Analytics Test"})
    login_res = await client.post("/api/v1/auth/login", data={"username": email, "password": pwd})
    token = login_res.json()["data"]["access_token"]
    
    ws_res = await client.get("/api/v1/workspaces", headers={"Authorization": f"Bearer {token}"})
    ws_id = ws_res.json()["data"][0]["id"]
    return {"Authorization": f"Bearer {token}", "X-Workspace-ID": ws_id}

@pytest.mark.asyncio
async def test_analytics_envelope(async_client: AsyncClient):
    headers = await get_auth_headers(async_client, "analytics_test@example.com")
    
    res = await async_client.get("/api/v1/analytics/summary", headers=headers)
    if res.status_code != 200:
        print(f"FAILED: {res.text}")
    assert res.status_code == 200
    
    json_data = res.json()
    
    # Verify ResponseEnvelope structure
    assert "success" in json_data
    assert "data" in json_data
    assert "error" in json_data
    
    assert json_data["success"] is True
    assert json_data["error"] is None
    
    # Verify Data keys
    data = json_data["data"]
    expected_keys = [
        "conversations_count", 
        "new_contacts_count", 
        "inbound_messages_count",
        "execution_completed_count"
    ]
    for key in expected_keys:
        assert key in data