askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_multipart.py · 4.4 KB · 131 lines
| 1 | """Tests for multipart form parsing and upload-form object execution.""" |
| 2 | |
| 3 | import base64 |
| 4 | import json |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | import object_multipart |
| 9 | import object_server |
| 10 | |
| 11 | from test_object_server import ANONYMOUS_IDENTITY, request, write_source |
| 12 | |
| 13 | |
| 14 | def multipart_body(parts, boundary="testboundary42"): |
| 15 | chunks = [] |
| 16 | for name, filename, content_type, content in parts: |
| 17 | chunks.append(f"--{boundary}\r\n".encode()) |
| 18 | if filename is not None: |
| 19 | chunks.append( |
| 20 | f'Content-Disposition: form-data; name="{name}"; filename="{filename}"\r\n'.encode() |
| 21 | ) |
| 22 | chunks.append(f"Content-Type: {content_type}\r\n\r\n".encode()) |
| 23 | else: |
| 24 | chunks.append(f'Content-Disposition: form-data; name="{name}"\r\n\r\n'.encode()) |
| 25 | chunks.append(content if isinstance(content, bytes) else content.encode()) |
| 26 | chunks.append(b"\r\n") |
| 27 | chunks.append(f"--{boundary}--\r\n".encode()) |
| 28 | return b"".join(chunks), f"multipart/form-data; boundary={boundary}" |
| 29 | |
| 30 | |
| 31 | def test_parse_multipart_fields_and_files(): |
| 32 | body, content_type = multipart_body( |
| 33 | [ |
| 34 | ("caption", None, None, "vacation photo"), |
| 35 | ("photo", "beach.png", "image/png", b"\x89PNG-fake-bytes"), |
| 36 | ] |
| 37 | ) |
| 38 | |
| 39 | payload = object_multipart.parse_multipart(body, content_type) |
| 40 | |
| 41 | assert payload["caption"] == "vacation photo" |
| 42 | file_entry = payload["_files"]["photo"] |
| 43 | assert file_entry["filename"] == "beach.png" |
| 44 | assert file_entry["content_type"] == "image/png" |
| 45 | assert file_entry["size"] == len(b"\x89PNG-fake-bytes") |
| 46 | assert base64.b64decode(file_entry["content_base64"]) == b"\x89PNG-fake-bytes" |
| 47 | |
| 48 | |
| 49 | def test_parse_multipart_rejects_garbage(): |
| 50 | with pytest.raises(object_multipart.InvalidMultipartError): |
| 51 | object_multipart.parse_multipart(b"stuff", "multipart/form-data") |
| 52 | with pytest.raises(object_multipart.InvalidMultipartError): |
| 53 | object_multipart.parse_multipart(b"not multipart", "text/plain") |
| 54 | |
| 55 | |
| 56 | def test_upload_form_post_to_object(tmp_path, monkeypatch): |
| 57 | root = tmp_path / "objects" |
| 58 | write_source( |
| 59 | root / "site" / "upload.py", |
| 60 | "import base64\n" |
| 61 | "def POST(request):\n" |
| 62 | " upload = request['_files']['document']\n" |
| 63 | " content = base64.b64decode(upload['content_base64'])\n" |
| 64 | " return {\n" |
| 65 | " 'caption': request.get('caption'),\n" |
| 66 | " 'filename': upload['filename'],\n" |
| 67 | " 'bytes': len(content),\n" |
| 68 | " 'first_word': content.split()[0].decode(),\n" |
| 69 | " }\n", |
| 70 | ) |
| 71 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 72 | |
| 73 | body, content_type = multipart_body( |
| 74 | [ |
| 75 | ("caption", None, None, "quarterly report"), |
| 76 | ("document", "report.txt", "text/plain", b"hello multipart world"), |
| 77 | ] |
| 78 | ) |
| 79 | status, _, payload = request( |
| 80 | "/objects/site_upload", |
| 81 | method="POST", |
| 82 | body=body, |
| 83 | headers=[("content-type", content_type)], |
| 84 | ) |
| 85 | |
| 86 | assert status == 200 |
| 87 | assert payload == { |
| 88 | "caption": "quarterly report", |
| 89 | "filename": "report.txt", |
| 90 | "bytes": 21, |
| 91 | "first_word": "hello", |
| 92 | } |
| 93 | |
| 94 | |
| 95 | def test_upload_form_via_site_route(tmp_path, monkeypatch): |
| 96 | root = tmp_path / "objects" |
| 97 | write_source( |
| 98 | root / "site" / "upload.py", |
| 99 | "def POST(request):\n" |
| 100 | " return {'got': sorted(request['_files']), 'user': request['_identity']['user_id']}\n", |
| 101 | ) |
| 102 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 103 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path / "data")) |
| 104 | monkeypatch.setenv(object_server.SITE_ROUTES_ENV, "true") |
| 105 | |
| 106 | body, content_type = multipart_body([("attachment", "a.bin", "application/octet-stream", b"\x00\x01")]) |
| 107 | status, _, payload = request( |
| 108 | "/upload", |
| 109 | method="POST", |
| 110 | body=body, |
| 111 | headers=[("content-type", content_type)], |
| 112 | ) |
| 113 | |
| 114 | assert status == 200 |
| 115 | assert payload == {"got": ["attachment"], "user": None} |
| 116 | |
| 117 | |
| 118 | def test_malformed_multipart_returns_400(tmp_path, monkeypatch): |
| 119 | root = tmp_path / "objects" |
| 120 | write_source(root / "site" / "upload.py", "def POST(request):\n return {}\n") |
| 121 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 122 | |
| 123 | status, _, payload = request( |
| 124 | "/objects/site_upload", |
| 125 | method="POST", |
| 126 | body=b"no boundary here", |
| 127 | headers=[("content-type", "multipart/form-data")], |
| 128 | ) |
| 129 | |
| 130 | assert status == 400 |
| 131 | assert "boundary" in payload["error"] |