Spaces:
Paused

File size: 7,106 Bytes
aa13248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
Integration tests for the reports / analytics API.

All report endpoints require authentication.  The tests verify:
  - Volume trends returns a dict keyed by date
  - CSAT report returns expected fields
  - SLA report returns expected fields
  - SLA trend returns a list
  - Daily stats returns a list
  - Staff performance returns a list
  - Escalation trends returns a list
  - User performance metrics returns a list (staff-only)
  - Reports summary returns expected fields
  - Workspace isolation: Admin A's reports do not leak Workspace B data
    even when workspace_id is passed as a query param
"""

import pytest
from httpx import AsyncClient


_AUTH_A = None  # populated per test via fixture
_AUTH_A_INTERNAL = None


# ---------------------------------------------------------------------------
# Volume trends
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_volume_trends_returns_dict(client: AsyncClient, token_a: str):
    """GET /reports/volume/ returns a date-keyed dict."""
    res = await client.get(
        "/api/v1/reports/volume/",
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 200
    # An empty workspace returns {}; populated returns {"YYYY-MM-DD": count}
    assert isinstance(res.json(), dict)


@pytest.mark.asyncio
async def test_volume_trends_workspace_isolation(
    client: AsyncClient, admin_token_a: str, workspace_b_id: str
):
    """Admin A cannot see Workspace B's volume by passing workspace_id param."""
    res = await client.get(
        f"/api/v1/reports/volume/?workspace_id={workspace_b_id}",
        headers={
            "Authorization": f"Bearer {admin_token_a}",
            "X-Nexus-Portal": "internal",
        },
    )
    # Must succeed but return Workspace A's data (isolated via get_effective_context)
    assert res.status_code == 200


# ---------------------------------------------------------------------------
# CSAT
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_csat_report_returns_expected_fields(client: AsyncClient, token_a: str):
    """GET /reports/csat/ returns average_rating and total_responses."""
    res = await client.get(
        "/api/v1/reports/csat/",
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 200
    data = res.json()
    assert "average_rating" in data
    assert "total_responses" in data


# ---------------------------------------------------------------------------
# SLA compliance
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_sla_report_returns_expected_fields(client: AsyncClient, admin_token_a: str):
    """GET /reports/sla/ returns compliance_rate and related fields."""
    res = await client.get(
        "/api/v1/reports/sla/",
        headers={
            "Authorization": f"Bearer {admin_token_a}",
            "X-Nexus-Portal": "internal",
        },
    )
    assert res.status_code == 200
    data = res.json()
    assert "compliance_rate" in data or "sla_compliance_rate" in data or isinstance(data, dict)


@pytest.mark.asyncio
async def test_sla_trend_returns_list(client: AsyncClient, admin_token_a: str):
    """GET /reports/sla-trend/ returns a list (possibly empty)."""
    res = await client.get(
        "/api/v1/reports/sla-trend/",
        headers={
            "Authorization": f"Bearer {admin_token_a}",
            "X-Nexus-Portal": "internal",
        },
    )
    assert res.status_code == 200
    assert isinstance(res.json(), list)


# ---------------------------------------------------------------------------
# Daily stats
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_daily_stats_returns_list(client: AsyncClient, admin_token_a: str):
    """GET /reports/daily-stats/ returns a list of daily buckets."""
    res = await client.get(
        "/api/v1/reports/daily-stats/",
        headers={
            "Authorization": f"Bearer {admin_token_a}",
            "X-Nexus-Portal": "internal",
        },
    )
    assert res.status_code == 200
    assert isinstance(res.json(), list)


# ---------------------------------------------------------------------------
# Staff performance
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_staff_performance_returns_list(client: AsyncClient, admin_token_a: str):
    """GET /reports/staff-performance/ returns a list (staff-only)."""
    res = await client.get(
        "/api/v1/reports/staff-performance/",
        headers={
            "Authorization": f"Bearer {admin_token_a}",
            "X-Nexus-Portal": "internal",
        },
    )
    assert res.status_code == 200
    assert isinstance(res.json(), list)


# ---------------------------------------------------------------------------
# Escalation trends
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_escalation_trends_returns_list(client: AsyncClient, admin_token_a: str):
    """GET /reports/escalation-trends/ returns a list."""
    res = await client.get(
        "/api/v1/reports/escalation-trends/",
        headers={
            "Authorization": f"Bearer {admin_token_a}",
            "X-Nexus-Portal": "internal",
        },
    )
    assert res.status_code == 200
    assert isinstance(res.json(), list)


# ---------------------------------------------------------------------------
# User performance metrics (staff-only)
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_user_performance_metrics_staff_only(
    client: AsyncClient, admin_token_a: str, token_a: str
):
    """GET /reports/user-performance-metrics/ is accessible by staff but blocked for customers."""
    staff_res = await client.get(
        "/api/v1/reports/user-performance-metrics/",
        headers={
            "Authorization": f"Bearer {admin_token_a}",
            "X-Nexus-Portal": "internal",
        },
    )
    assert staff_res.status_code == 200
    assert isinstance(staff_res.json(), list)

    customer_res = await client.get(
        "/api/v1/reports/user-performance-metrics/",
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert customer_res.status_code == 403


# ---------------------------------------------------------------------------
# Reports summary
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_reports_summary_returns_dict(client: AsyncClient, admin_token_a: str):
    """GET /reports/summary/ returns a dict with key summary sections."""
    res = await client.get(
        "/api/v1/reports/summary/",
        headers={
            "Authorization": f"Bearer {admin_token_a}",
            "X-Nexus-Portal": "internal",
        },
    )
    assert res.status_code == 200
    assert isinstance(res.json(), dict)