| import json |
|
|
| import pytest |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_convert_missing_inputs_returns_400(client) -> None: |
| resp = await client.post("/v1/convert") |
| assert resp.status_code == 400 |
| body = resp.json() |
| assert body["error"] == "InvalidInputError" |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_convert_disallowed_extension_returns_415(client) -> None: |
| resp = await client.post( |
| "/v1/convert", |
| files={"file": ("malware.exe", b"bin", "application/octet-stream")}, |
| ) |
| assert resp.status_code == 415 |
| assert resp.json()["error"] == "UnsupportedFormatError" |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_convert_payload_too_large(client) -> None: |
| big = b"a" * (3 * 1024 * 1024) |
| resp = await client.post( |
| "/v1/convert", |
| files={"file": ("big.txt", big, "text/plain")}, |
| ) |
| assert resp.status_code == 413 |
| assert resp.json()["error"] == "PayloadTooLargeError" |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_convert_invalid_json_in_form(client) -> None: |
| resp = await client.post( |
| "/v1/convert", |
| files={"file": ("a.txt", b"hi", "text/plain")}, |
| data={"openai": "{not json"}, |
| ) |
| assert resp.status_code == 422 |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_convert_basic_text(client) -> None: |
| resp = await client.post( |
| "/v1/convert", |
| files={"file": ("note.txt", b"# Hello\n\nworld", "text/plain")}, |
| data={"settings": json.dumps({"cleanup": False})}, |
| ) |
| |
| |
| |
| assert resp.headers["content-type"].startswith("application/json") |
| body = resp.json() |
| if resp.status_code == 200: |
| assert "filename" in body and "markdown" in body |
| else: |
| assert "error" in body and "message" in body |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_convert_path_traversal_filename_is_sanitized(client) -> None: |
| resp = await client.post( |
| "/v1/convert", |
| files={"file": ("../../etc/passwd.txt", b"hello", "text/plain")}, |
| ) |
| |
| |
| |
| body = resp.json() |
| if resp.status_code == 200: |
| assert "/" not in body["filename"] |
| assert ".." not in body["filename"] |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_stats_endpoint(client) -> None: |
| resp = await client.get("/v1/stats") |
| assert resp.status_code == 200 |
| body = resp.json() |
| assert "access" in body and "tokens" in body |
|
|