-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_files.py
More file actions
450 lines (385 loc) · 15.5 KB
/
test_files.py
File metadata and controls
450 lines (385 loc) · 15.5 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""Functional tests for file management endpoints."""
import pytest
class TestFileUpload:
"""Test POST /upload."""
@pytest.mark.asyncio
async def test_upload_single_file(
self, async_client, auth_headers, unique_entity_id
):
"""Upload a single file using 'files' field."""
files = {"files": ("test.txt", b"Hello World", "text/plain")}
data = {"entity_id": unique_entity_id}
response = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data=data,
)
assert response.status_code == 200
result = response.json()
assert result["message"] == "success"
assert "storage_session_id" in result
assert len(result["files"]) == 1
assert "fileId" in result["files"][0]
assert "filename" in result["files"][0]
@pytest.mark.asyncio
async def test_librechat_upload_format(
self, async_client, auth_headers, unique_entity_id
):
"""Test LibreChat 'file' (singular) field name."""
# LibreChat uses 'file' singular
files = {"file": ("document.pdf", b"PDF content here", "application/pdf")}
data = {"entity_id": unique_entity_id}
response = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data=data,
)
assert response.status_code == 200
assert response.json()["message"] == "success"
@pytest.mark.asyncio
async def test_upload_returns_storage_session_id(
self, async_client, auth_headers, unique_entity_id
):
"""Upload response includes storage_session_id."""
files = {"files": ("test.txt", b"content", "text/plain")}
data = {"entity_id": unique_entity_id}
response = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data=data,
)
result = response.json()
assert "storage_session_id" in result
assert len(result["storage_session_id"]) > 0
@pytest.mark.asyncio
async def test_upload_returns_file_info(
self, async_client, auth_headers, unique_entity_id
):
"""Upload response includes file info with fileId and filename."""
files = {"files": ("myfile.csv", b"a,b,c\n1,2,3", "text/csv")}
data = {"entity_id": unique_entity_id}
response = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data=data,
)
result = response.json()
assert len(result["files"]) == 1
file_info = result["files"][0]
assert "fileId" in file_info
assert "filename" in file_info
assert file_info["filename"] == "myfile.csv"
class TestFileList:
"""Test GET /files/{session_id}."""
@pytest.mark.asyncio
async def test_list_files_empty_session(
self, async_client, auth_headers, unique_session_id
):
"""List files for non-existent session returns empty array."""
response = await async_client.get(
f"/files/{unique_session_id}",
headers=auth_headers,
)
assert response.status_code == 200
assert response.json() == []
@pytest.mark.asyncio
async def test_list_files_after_upload(
self, async_client, auth_headers, unique_entity_id
):
"""List files returns uploaded file info."""
# First upload a file
files = {"files": ("list-test.txt", b"content for list test", "text/plain")}
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
session_id = upload.json()["storage_session_id"]
# List files
response = await async_client.get(
f"/files/{session_id}",
headers=auth_headers,
)
assert response.status_code == 200
files_list = response.json()
assert len(files_list) >= 1
@pytest.mark.asyncio
async def test_list_files_detail_simple(
self, async_client, auth_headers, unique_entity_id
):
"""List files with detail=simple returns minimal info."""
# First upload a file
files = {"files": ("simple-test.txt", b"content", "text/plain")}
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
session_id = upload.json()["storage_session_id"]
# List with simple detail
response = await async_client.get(
f"/files/{session_id}?detail=simple",
headers=auth_headers,
)
assert response.status_code == 200
files_list = response.json()
assert isinstance(files_list, list)
@pytest.mark.asyncio
async def test_list_files_detail_summary(
self, async_client, auth_headers, unique_entity_id
):
"""List files with detail=summary returns summary info."""
# First upload a file
files = {"files": ("summary-test.txt", b"content", "text/plain")}
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
session_id = upload.json()["storage_session_id"]
# List with summary detail
response = await async_client.get(
f"/files/{session_id}?detail=summary",
headers=auth_headers,
)
assert response.status_code == 200
files_list = response.json()
assert isinstance(files_list, list)
class TestFileMetadata:
"""Test file metadata fields required by LibreChat."""
@pytest.mark.asyncio
async def test_detail_full_has_original_filename_metadata(
self, async_client, auth_headers, unique_entity_id
):
"""GET /files/{sid}?detail=full must include metadata['original-filename'].
LibreChat reads this field at CodeExecutor.ts:170 to map sanitized
filenames back to original upload names.
"""
# Upload a file with a distinctive name
files = {"files": ("My Report (2024).csv", b"a,b\n1,2", "text/csv")}
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
assert upload.status_code == 200
session_id = upload.json()["storage_session_id"]
# Get full detail
response = await async_client.get(
f"/files/{session_id}?detail=full",
headers=auth_headers,
)
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert len(data) >= 1
for item in data:
assert "metadata" in item, "Full detail must include 'metadata'"
assert (
"original-filename" in item["metadata"]
), "metadata must include 'original-filename'"
assert isinstance(item["metadata"]["original-filename"], str)
assert len(item["metadata"]["original-filename"]) > 0
@pytest.mark.asyncio
async def test_detail_full_has_required_fields(
self, async_client, auth_headers, unique_entity_id
):
"""GET /files/{sid}?detail=full returns all fields LibreChat expects."""
files = {"files": ("test.txt", b"content", "text/plain")}
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
session_id = upload.json()["storage_session_id"]
response = await async_client.get(
f"/files/{session_id}?detail=full",
headers=auth_headers,
)
assert response.status_code == 200
data = response.json()
assert len(data) >= 1
item = data[0]
# Fields LibreChat expects in full detail
assert "id" in item
assert "name" in item
assert "size" in item
assert "lastModified" in item
assert "contentType" in item
assert "metadata" in item
assert "content-type" in item["metadata"]
assert "original-filename" in item["metadata"]
class TestFileDownload:
"""Test GET /download/{session_id}/{file_id}."""
@pytest.mark.asyncio
async def test_download_uploaded_file(
self, async_client, auth_headers, unique_entity_id
):
"""Download uploaded file returns correct content."""
content = b"Download test content - unique data 12345"
files = {"files": ("download-test.txt", content, "text/plain")}
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
session_id = upload.json()["storage_session_id"]
file_id = upload.json()["files"][0]["fileId"]
response = await async_client.get(
f"/download/{session_id}/{file_id}",
headers=auth_headers,
)
assert response.status_code == 200
assert response.content == content
@pytest.mark.asyncio
async def test_download_nonexistent_returns_404(
self, async_client, auth_headers, unique_session_id
):
"""Download non-existent file returns 404."""
response = await async_client.get(
f"/download/{unique_session_id}/fake-file-id",
headers=auth_headers,
)
assert response.status_code == 404
class TestFileExecutionIntegration:
"""Test the full upload → execute (read file) → generate output → download flow."""
@pytest.mark.asyncio
async def test_uploaded_file_readable_at_mnt_data(
self, async_client, auth_headers, unique_entity_id
):
"""Uploaded file is readable at /mnt/data/ inside execution sandbox."""
csv_content = b"name,age,city\nAlice,30,NYC\nBob,25,LA\n"
files = {"files": ("people.csv", csv_content, "text/csv")}
# Upload
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
assert upload.status_code == 200
upload_data = upload.json()
session_id = upload_data["storage_session_id"]
file_id = upload_data["files"][0]["fileId"]
filename = upload_data["files"][0]["filename"]
# Execute code that reads the file via /mnt/data/ path
exec_response = await async_client.post(
"/exec",
headers=auth_headers,
json={
"code": (
"import csv\n"
f"with open('/mnt/data/{filename}') as f:\n"
" reader = csv.DictReader(f)\n"
" rows = list(reader)\n"
"print(len(rows))\n"
"print(rows[0]['name'])\n"
),
"lang": "py",
"session_id": session_id,
"files": [{"id": file_id, "storage_session_id": session_id, "name": filename}],
},
)
assert exec_response.status_code == 200
result = exec_response.json()
assert "2" in result["stdout"]
assert "Alice" in result["stdout"]
assert result["stderr"] == ""
@pytest.mark.asyncio
async def test_uploaded_file_readable_via_relative_path(
self, async_client, auth_headers, unique_entity_id
):
"""Uploaded file is also readable via relative path (CWD = /mnt/data)."""
content = b"hello from uploaded file"
files = {"files": ("greeting.txt", content, "text/plain")}
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
upload_data = upload.json()
session_id = upload_data["storage_session_id"]
file_id = upload_data["files"][0]["fileId"]
filename = upload_data["files"][0]["filename"]
exec_response = await async_client.post(
"/exec",
headers=auth_headers,
json={
"code": f"print(open('{filename}').read())",
"lang": "py",
"session_id": session_id,
"files": [{"id": file_id, "storage_session_id": session_id, "name": filename}],
},
)
result = exec_response.json()
assert "hello from uploaded file" in result["stdout"]
@pytest.mark.asyncio
async def test_upload_execute_generate_download(
self, async_client, auth_headers, unique_entity_id
):
"""Full round-trip: upload CSV → process with pandas → download result."""
csv_data = b"product,price\nWidget,9.99\nGadget,19.99\n"
files = {"files": ("input.csv", csv_data, "text/csv")}
# Upload
upload = await async_client.post(
"/upload",
headers={"x-api-key": auth_headers["x-api-key"]},
files=files,
data={"entity_id": unique_entity_id},
)
upload_data = upload.json()
session_id = upload_data["storage_session_id"]
file_id = upload_data["files"][0]["fileId"]
filename = upload_data["files"][0]["filename"]
# Execute: read input, transform, write output
exec_response = await async_client.post(
"/exec",
headers=auth_headers,
json={
"code": (
"import csv\n"
f"with open('/mnt/data/{filename}') as f:\n"
" reader = csv.DictReader(f)\n"
" rows = list(reader)\n"
"with open('/mnt/data/output.csv', 'w', newline='') as f:\n"
" writer = csv.DictWriter(f, fieldnames=['product', 'price', 'tax'])\n"
" writer.writeheader()\n"
" for row in rows:\n"
" row['tax'] = f\"{float(row['price']) * 0.1:.2f}\"\n"
" writer.writerow(row)\n"
"print('done')\n"
),
"lang": "py",
"session_id": session_id,
"files": [{"id": file_id, "storage_session_id": session_id, "name": filename}],
},
)
result = exec_response.json()
assert "done" in result["stdout"]
assert len(result["files"]) >= 1
# Find the generated output file
output_file = next(
(f for f in result["files"] if f["name"] == "output.csv"), None
)
assert output_file is not None, f"output.csv not in files: {result['files']}"
# Download and verify content
download = await async_client.get(
f"/download/{session_id}/{output_file['id']}",
headers=auth_headers,
)
assert download.status_code == 200
downloaded_text = download.content.decode()
assert "product,price,tax" in downloaded_text
assert "Widget" in downloaded_text
assert "1.00" in downloaded_text # 9.99 * 0.1 = 1.00