Spaces:
Paused

miracle / tests /test_feedback.py
dushyantsingh0
Claude Sonnet 4.6
Add comprehensive test suite targeting >90% coverage
aa13248
Raw History Blame Contribute Delete
5.58 kB
"""
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