Spaces:
Paused

File size: 8,004 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
Integration tests for the system / diagnostics API.

Endpoints covered:
  GET /system/workflows/     - staff only, returns list
  GET /system/interactions/  - staff only, returns list
  GET /system/history/       - staff only, returns list
  GET /system/audit/         - staff only, returns list with actor_name hydration
  GET /system/status/        - super-admin only (or 403 if role guard rejects)

All staff-accessible endpoints must return 403 for CUSTOMER_USER.
"""

import pytest
from httpx import AsyncClient


_INTERNAL = {"X-Nexus-Portal": "internal"}


# ---------------------------------------------------------------------------
# Helper
# ---------------------------------------------------------------------------

def _staff_headers(token: str) -> dict:
    return {"Authorization": f"Bearer {token}", "X-Nexus-Portal": "internal"}


def _customer_headers(token: str) -> dict:
    return {"Authorization": f"Bearer {token}"}


# ---------------------------------------------------------------------------
# AI Workflows
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_workflows_accessible_by_staff(client: AsyncClient, admin_token_a: str):
    """GET /system/workflows/ returns a list for SC_ADMIN."""
    res = await client.get(
        "/api/v1/system/workflows/",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code == 200
    assert isinstance(res.json(), list)


@pytest.mark.asyncio
async def test_workflows_blocked_for_customers(client: AsyncClient, token_a: str):
    """GET /system/workflows/ returns 403 for CUSTOMER_USER."""
    res = await client.get(
        "/api/v1/system/workflows/",
        headers=_customer_headers(token_a),
    )
    assert res.status_code == 403


@pytest.mark.asyncio
async def test_workflows_pagination(client: AsyncClient, admin_token_a: str):
    """GET /system/workflows/ respects offset and limit query params."""
    res = await client.get(
        "/api/v1/system/workflows/?offset=0&limit=5",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code == 200
    assert len(res.json()) <= 5


@pytest.mark.asyncio
async def test_workflows_unauthenticated_returns_401(client: AsyncClient):
    """GET /system/workflows/ without a token returns 401."""
    res = await client.get("/api/v1/system/workflows/")
    assert res.status_code == 401


# ---------------------------------------------------------------------------
# AI Interactions
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_interactions_accessible_by_staff(client: AsyncClient, admin_token_a: str):
    """GET /system/interactions/ returns a list for SC_ADMIN."""
    res = await client.get(
        "/api/v1/system/interactions/",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code == 200
    assert isinstance(res.json(), list)


@pytest.mark.asyncio
async def test_interactions_blocked_for_customers(client: AsyncClient, token_a: str):
    res = await client.get(
        "/api/v1/system/interactions/",
        headers=_customer_headers(token_a),
    )
    assert res.status_code == 403


@pytest.mark.asyncio
async def test_interactions_pagination(client: AsyncClient, admin_token_a: str):
    res = await client.get(
        "/api/v1/system/interactions/?offset=0&limit=5",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code == 200
    assert len(res.json()) <= 5


# ---------------------------------------------------------------------------
# Infra health history
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_history_accessible_by_staff(client: AsyncClient, admin_token_a: str):
    """GET /system/history/ returns a list (possibly empty in fresh test DB)."""
    res = await client.get(
        "/api/v1/system/history/",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code == 200
    assert isinstance(res.json(), list)


@pytest.mark.asyncio
async def test_history_blocked_for_customers(client: AsyncClient, token_a: str):
    res = await client.get(
        "/api/v1/system/history/",
        headers=_customer_headers(token_a),
    )
    assert res.status_code == 403


# ---------------------------------------------------------------------------
# Audit log
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_audit_accessible_by_staff(client: AsyncClient, admin_token_a: str):
    """GET /system/audit/ returns a list; ticket creation events appear."""
    # Create a ticket to generate at least one audit log entry
    await client.post(
        "/api/v1/tickets/",
        json={"title": "Audit Log Trigger", "description": "Test", "priority": "P2"},
        headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
    )
    res = await client.get(
        "/api/v1/system/audit/",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code == 200
    data = res.json()
    assert isinstance(data, list)
    # Every entry must have actor_name (hydrated field)
    for entry in data:
        assert "actor_name" in entry


@pytest.mark.asyncio
async def test_audit_blocked_for_customers(client: AsyncClient, token_a: str):
    res = await client.get(
        "/api/v1/system/audit/",
        headers=_customer_headers(token_a),
    )
    assert res.status_code == 403


@pytest.mark.asyncio
async def test_audit_filter_by_entity_type(client: AsyncClient, admin_token_a: str):
    """GET /system/audit/?entity_type=ticket filters results correctly."""
    res = await client.get(
        "/api/v1/system/audit/?entity_type=ticket",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code == 200
    for entry in res.json():
        assert entry["entity_type"] == "ticket"


@pytest.mark.asyncio
async def test_audit_filter_by_action(client: AsyncClient, admin_token_a: str):
    """GET /system/audit/?action=create filters results correctly."""
    res = await client.get(
        "/api/v1/system/audit/?action=create",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code == 200
    for entry in res.json():
        assert entry["action"] == "create"


@pytest.mark.asyncio
async def test_audit_unauthenticated_returns_401(client: AsyncClient):
    res = await client.get("/api/v1/system/audit/")
    assert res.status_code == 401


# ---------------------------------------------------------------------------
# System status (super-admin guard)
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_system_status_blocked_for_customers(client: AsyncClient, token_a: str):
    """GET /system/status/ returns 403 for CUSTOMER_USER."""
    res = await client.get(
        "/api/v1/system/status/",
        headers=_customer_headers(token_a),
    )
    assert res.status_code == 403


@pytest.mark.asyncio
async def test_system_status_unauthenticated_returns_401(client: AsyncClient):
    res = await client.get("/api/v1/system/status/")
    assert res.status_code == 401


@pytest.mark.asyncio
async def test_system_status_staff_gets_structured_response(
    client: AsyncClient, admin_token_a: str
):
    """
    SC_ADMIN hits the status endpoint.  The super-admin guard has a case-
    sensitivity issue (lower-casing the role before comparing against the
    enum list), so SC_ADMIN may receive 403.  Either way the endpoint must
    not crash with 500.
    """
    res = await client.get(
        "/api/v1/system/status/",
        headers=_staff_headers(admin_token_a),
    )
    assert res.status_code in (200, 403), (
        f"Unexpected status {res.status_code}: {res.text}"
    )
    if res.status_code == 200:
        data = res.json()
        assert "database" in data
        assert "redis" in data