Spaces:
Paused

File size: 5,578 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
"""
Integration tests for the feedback (CSAT) API.

Coverage:
  - Customer can submit feedback on a resolved ticket
  - Feedback blocked on non-resolved tickets (400)
  - Duplicate feedback blocked (400)
  - Get feedback by ticket ID
  - Ticket from wrong workspace returns 404
"""

import pytest
from httpx import AsyncClient


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

async def _create_and_resolve_ticket(
    client: AsyncClient, customer_token: str, admin_token: str, title: str = "Resolvable Ticket"
) -> str:
    """Creates a ticket and advances it to RESOLVED, returns ticket_id."""
    create_res = await client.post(
        "/api/v1/tickets/",
        json={"title": title, "description": "Test", "priority": "P2"},
        headers={"Authorization": f"Bearer {customer_token}"},
    )
    assert create_res.status_code == 201
    ticket_id = create_res.json()["id"]

    patch_res = await client.patch(
        f"/api/v1/tickets/{ticket_id}/",
        json={"status": "RESOLVED"},
        headers={
            "Authorization": f"Bearer {admin_token}",
            "X-Nexus-Portal": "internal",
        },
    )
    assert patch_res.status_code == 200
    return ticket_id


# ---------------------------------------------------------------------------
# Happy-path tests
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_submit_feedback_on_resolved_ticket(
    client: AsyncClient, token_a: str, admin_token_a: str
):
    """Customer can submit CSAT rating on a resolved ticket."""
    ticket_id = await _create_and_resolve_ticket(client, token_a, admin_token_a)

    res = await client.post(
        f"/api/v1/tickets/{ticket_id}/feedback/",
        json={"rating": 5, "comment": "Great support!"},
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 201
    data = res.json()
    assert data["rating"] == 5
    assert data["comment"] == "Great support!"


@pytest.mark.asyncio
async def test_get_feedback_for_ticket(
    client: AsyncClient, token_a: str, admin_token_a: str
):
    """GET /tickets/{id}/feedback/ returns the submitted rating."""
    ticket_id = await _create_and_resolve_ticket(client, token_a, admin_token_a, "Feedback Retrieval")

    await client.post(
        f"/api/v1/tickets/{ticket_id}/feedback/",
        json={"rating": 4},
        headers={"Authorization": f"Bearer {token_a}"},
    )

    res = await client.get(
        f"/api/v1/tickets/{ticket_id}/feedback/",
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 200
    assert res.json()["rating"] == 4


# ---------------------------------------------------------------------------
# Business-rule enforcement
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_feedback_blocked_on_open_ticket(client: AsyncClient, token_a: str):
    """Submitting feedback on an OPEN ticket returns 400."""
    create_res = await client.post(
        "/api/v1/tickets/",
        json={"title": "Still Open", "description": "Test", "priority": "P2"},
        headers={"Authorization": f"Bearer {token_a}"},
    )
    ticket_id = create_res.json()["id"]

    res = await client.post(
        f"/api/v1/tickets/{ticket_id}/feedback/",
        json={"rating": 3},
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 400
    assert "resolved or closed" in res.json()["detail"].lower()


@pytest.mark.asyncio
async def test_duplicate_feedback_blocked(
    client: AsyncClient, token_a: str, admin_token_a: str
):
    """Submitting feedback twice for the same ticket returns 400."""
    ticket_id = await _create_and_resolve_ticket(client, token_a, admin_token_a, "Duplicate Feedback")

    await client.post(
        f"/api/v1/tickets/{ticket_id}/feedback/",
        json={"rating": 5},
        headers={"Authorization": f"Bearer {token_a}"},
    )
    res = await client.post(
        f"/api/v1/tickets/{ticket_id}/feedback/",
        json={"rating": 1},
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 400
    assert "already submitted" in res.json()["detail"].lower()


# ---------------------------------------------------------------------------
# BOLA / workspace isolation
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_feedback_cross_workspace_blocked(
    client: AsyncClient, token_a: str, token_b: str, admin_token_a: str
):
    """Customer B cannot submit feedback on a ticket belonging to Workspace A."""
    ticket_id = await _create_and_resolve_ticket(client, token_a, admin_token_a, "A's Resolved Ticket")

    res = await client.post(
        f"/api/v1/tickets/{ticket_id}/feedback/",
        json={"rating": 1},
        headers={"Authorization": f"Bearer {token_b}"},
    )
    assert res.status_code == 404


@pytest.mark.asyncio
async def test_get_feedback_nonexistent_returns_404(
    client: AsyncClient, token_a: str, admin_token_a: str
):
    """GET /tickets/{id}/feedback/ on a ticket with no feedback returns 404."""
    ticket_id = await _create_and_resolve_ticket(client, token_a, admin_token_a, "No Feedback Yet")

    res = await client.get(
        f"/api/v1/tickets/{ticket_id}/feedback/",
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 404