Spaces:
Paused

File size: 7,709 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
"""
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)
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
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


@pytest.mark.asyncio
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
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
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


@pytest.mark.asyncio
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


@pytest.mark.asyncio
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
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
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() == []


@pytest.mark.asyncio
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


@pytest.mark.asyncio
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
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
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())


@pytest.mark.asyncio
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


@pytest.mark.asyncio
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