Spaces:
Paused
Paused
Download tests/test_attachments.py from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 7.71 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/tests/test_attachments.py
- Command line
-
hf download hf://spaces/3SCError404/miracle/tests/test_attachments.py
-
curl -L -o test_attachments.py https://huggingface.co/spaces/3SCError404/miracle/resolve/main/tests/test_attachments.py
7.71 kB
| """ | |
| Integration tests for the attachments API. | |
| Storage falls back to local disk when S3 is not configured (default for tests). | |
| The test verifies: | |
| - Generic upload (no ticket ID) creates an Attachment record | |
| - Ticket-linked upload creates an Attachment record for that ticket | |
| - List attachments returns uploaded files | |
| - Delete attachment soft-deletes it (disappears from list) | |
| - BOLA: user B cannot delete user A's attachment (403) | |
| - Cross-workspace ticket attachment blocked (404) | |
| - Unauthenticated upload rejected (401) | |
| """ | |
| import io | |
| import pytest | |
| from httpx import AsyncClient | |
| # --------------------------------------------------------------------------- | |
| # Helpers | |
| # --------------------------------------------------------------------------- | |
| def _txt_file(name: str = "test.txt", content: str = "hello world") -> dict: | |
| """Return an httpx-compatible files dict for a plain-text upload.""" | |
| return {"file": (name, io.BytesIO(content.encode()), "text/plain")} | |
| async def _create_ticket(client: AsyncClient, token: str) -> str: | |
| res = await client.post( | |
| "/api/v1/tickets/", | |
| json={"title": "Attachment Test Ticket", "description": "desc", "priority": "P2"}, | |
| headers={"Authorization": f"Bearer {token}"}, | |
| ) | |
| assert res.status_code == 201 | |
| return res.json()["id"] | |
| # --------------------------------------------------------------------------- | |
| # Generic upload (no ticket) | |
| # --------------------------------------------------------------------------- | |
| async def test_upload_generic_attachment(client: AsyncClient, token_a: str): | |
| """POST /attachments/upload/ creates a record and returns AttachmentResponse.""" | |
| res = await client.post( | |
| "/api/v1/attachments/upload/", | |
| files=_txt_file("doc.txt"), | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| assert res.status_code == 201 | |
| data = res.json() | |
| assert data["filename"] == "doc.txt" | |
| assert "id" in data | |
| async def test_upload_generic_requires_auth(client: AsyncClient): | |
| """POST /attachments/upload/ without a token returns 401.""" | |
| res = await client.post( | |
| "/api/v1/attachments/upload/", | |
| files=_txt_file(), | |
| ) | |
| assert res.status_code == 401 | |
| # --------------------------------------------------------------------------- | |
| # Ticket-linked upload | |
| # --------------------------------------------------------------------------- | |
| async def test_upload_attachment_to_ticket(client: AsyncClient, token_a: str): | |
| """POST /tickets/{id}/attachments/ links the file to the ticket.""" | |
| ticket_id = await _create_ticket(client, token_a) | |
| res = await client.post( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| files=_txt_file("linked.txt"), | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| assert res.status_code == 201 | |
| data = res.json() | |
| assert data["filename"] == "linked.txt" | |
| assert str(data["ticket_id"]) == ticket_id | |
| async def test_upload_to_unknown_ticket_returns_404(client: AsyncClient, token_a: str): | |
| """Uploading to a non-existent ticket ID returns 404.""" | |
| res = await client.post( | |
| "/api/v1/tickets/00000000-0000-0000-0000-000000000000/attachments/", | |
| files=_txt_file(), | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| assert res.status_code == 404 | |
| async def test_upload_to_cross_workspace_ticket_returns_404( | |
| client: AsyncClient, token_a: str, token_b: str | |
| ): | |
| """Customer B cannot upload to workspace A's ticket (BOLA → 404).""" | |
| ticket_id = await _create_ticket(client, token_a) | |
| res = await client.post( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| files=_txt_file("evil.txt"), | |
| headers={"Authorization": f"Bearer {token_b}"}, | |
| ) | |
| assert res.status_code == 404 | |
| # --------------------------------------------------------------------------- | |
| # List attachments | |
| # --------------------------------------------------------------------------- | |
| async def test_list_attachments_empty_initially(client: AsyncClient, token_a: str): | |
| """A fresh ticket has no attachments.""" | |
| ticket_id = await _create_ticket(client, token_a) | |
| res = await client.get( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| assert res.status_code == 200 | |
| assert res.json() == [] | |
| async def test_list_attachments_after_upload(client: AsyncClient, token_a: str): | |
| """Uploaded files appear in the attachment list.""" | |
| ticket_id = await _create_ticket(client, token_a) | |
| await client.post( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| files=_txt_file("visible.txt"), | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| res = await client.get( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| assert res.status_code == 200 | |
| filenames = [a["filename"] for a in res.json()] | |
| assert "visible.txt" in filenames | |
| async def test_list_attachments_cross_workspace_blocked( | |
| client: AsyncClient, token_a: str, token_b: str | |
| ): | |
| """Customer B cannot list attachments on workspace A's ticket.""" | |
| ticket_id = await _create_ticket(client, token_a) | |
| res = await client.get( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| headers={"Authorization": f"Bearer {token_b}"}, | |
| ) | |
| assert res.status_code == 404 | |
| # --------------------------------------------------------------------------- | |
| # Delete attachment | |
| # --------------------------------------------------------------------------- | |
| async def test_uploader_can_delete_own_attachment(client: AsyncClient, token_a: str): | |
| """The uploader can soft-delete their own attachment.""" | |
| ticket_id = await _create_ticket(client, token_a) | |
| upload_res = await client.post( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| files=_txt_file("deleteme.txt"), | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| attach_id = upload_res.json()["id"] | |
| del_res = await client.delete( | |
| f"/api/v1/attachments/{attach_id}/", | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| assert del_res.status_code == 200 | |
| # Confirm it's gone from the list | |
| list_res = await client.get( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| assert not any(a["id"] == attach_id for a in list_res.json()) | |
| async def test_delete_nonexistent_attachment_returns_404( | |
| client: AsyncClient, token_a: str | |
| ): | |
| res = await client.delete( | |
| "/api/v1/attachments/00000000-0000-0000-0000-000000000000/", | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| assert res.status_code == 404 | |
| async def test_cross_workspace_attachment_delete_blocked( | |
| client: AsyncClient, token_a: str, token_b: str | |
| ): | |
| """ | |
| Customer B cannot delete an attachment that belongs to workspace A. | |
| The delete endpoint filters by workspace_id, so it returns 404. | |
| """ | |
| ticket_id = await _create_ticket(client, token_a) | |
| upload_res = await client.post( | |
| f"/api/v1/tickets/{ticket_id}/attachments/", | |
| files=_txt_file("a_only.txt"), | |
| headers={"Authorization": f"Bearer {token_a}"}, | |
| ) | |
| attach_id = upload_res.json()["id"] | |
| res = await client.delete( | |
| f"/api/v1/attachments/{attach_id}/", | |
| headers={"Authorization": f"Bearer {token_b}"}, | |
| ) | |
| assert res.status_code == 404 | |