Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_object_user_files.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_user_files.py · 6.3 KB · 177 lines
1"""Tests for user file storage: bytes module and the /api/files surface."""
2
3import json
4
5import pytest
6
7import object_server
8import object_user_files
9
10from test_object_server import (
11 create_identity_session,
12 enable_admin_token,
13 raw_request,
14 request,
15 save_permission_policy,
16 write_records,
17)
18
19FILES_POLICY = {
20 "access_mode": "role_based",
21 "rules": [
22 {
23 "effect": "allow",
24 "principal": "registered",
25 "actions": ["create", "read", "update", "delete"],
26 "collection": "files",
27 "row_filter": {"owner_id": "$user_id"},
28 "reason": "own files",
29 },
30 {
31 "effect": "allow",
32 "principal": "public",
33 "actions": ["read"],
34 "collection": "files",
35 "row_filter": {"is_public": "true"},
36 "reason": "public files",
37 },
38 ],
39}
40
41
42def multipart_body(field, filename, content, extra=()):
43 boundary = "testboundary42"
44 parts = []
45 for name, value in extra:
46 parts.append(
47 f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"\r\n\r\n{value}\r\n'.encode()
48 )
49 parts.append(
50 (
51 f'--{boundary}\r\nContent-Disposition: form-data; name="{field}"; '
52 f'filename="{filename}"\r\nContent-Type: text/plain\r\n\r\n'
53 ).encode()
54 + content
55 + b"\r\n"
56 )
57 parts.append(f"--{boundary}--\r\n".encode())
58 return b"".join(parts), f"multipart/form-data; boundary={boundary}"
59
60
61def files_env(tmp_path, monkeypatch):
62 data_dir = tmp_path / "data"
63 write_records(
64 data_dir,
65 "files",
66 "id\tfilename\tcontent_type\tsize\tdescription\tproject_id\tis_public\towner_id\n",
67 )
68 schema_file = data_dir / "schemas" / "files.json"
69 schema_file.parent.mkdir(parents=True, exist_ok=True)
70 schema_file.write_text(
71 json.dumps(
72 {
73 "fields": [
74 {"name": "id"},
75 {"name": "filename", "required": True},
76 {"name": "content_type"},
77 {"name": "size", "type": "integer"},
78 {"name": "description"},
79 {"name": "is_public", "type": "boolean", "default": "false"},
80 {"name": "owner_id"},
81 ]
82 }
83 )
84 )
85 save_permission_policy(data_dir, FILES_POLICY)
86 monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir))
87 monkeypatch.setenv(object_server.USER_FILES_ENABLED_ENV, "true")
88 monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true")
89 enable_admin_token(monkeypatch)
90 return data_dir
91
92
93def test_module_save_read_delete_and_usage(tmp_path):
94 size = object_user_files.save_file("dan", "f1", b"hello", base_dir=tmp_path)
95 assert size == 5
96 assert object_user_files.read_file("dan", "f1", base_dir=tmp_path) == b"hello"
97 object_user_files.save_file("dan", "f2", b"world!", base_dir=tmp_path)
98 assert object_user_files.usage_bytes("dan", base_dir=tmp_path) == 11
99 assert object_user_files.delete_file("dan", "f1", base_dir=tmp_path) is True
100 assert object_user_files.delete_file("dan", "f1", base_dir=tmp_path) is False
101 with pytest.raises(object_user_files.UserFileNotFoundError):
102 object_user_files.read_file("dan", "f1", base_dir=tmp_path)
103 with pytest.raises(object_user_files.InvalidUserFileError):
104 object_user_files.file_path("../etc", "f1", base_dir=tmp_path)
105 with pytest.raises(object_user_files.InvalidUserFileError):
106 object_user_files.file_path("dan", "../../secrets", base_dir=tmp_path)
107
108
109def test_upload_download_share_delete_flow(tmp_path, monkeypatch):
110 data_dir = files_env(tmp_path, monkeypatch)
111 token, _ = create_identity_session({"user_id": "dan"})
112 bearer = [("authorization", f"Bearer {token}")]
113
114 body, content_type = multipart_body("file", "notes.txt", b"file bytes here")
115 status, _, uploaded = request(
116 "/api/files",
117 method="POST",
118 body=body,
119 headers=bearer + [("content-type", content_type)],
120 )
121 assert status == 201, uploaded
122 file_id = uploaded["file"]["id"]
123 assert uploaded["file"]["size"] == "15"
124 assert uploaded["file"]["owner_id"] == "dan"
125
126 status, response_headers, payload = raw_request(f"/api/files/{file_id}", headers=bearer)
127 assert status == 200
128 assert payload == b"file bytes here"
129 assert response_headers[b"content-type"] == b"text/plain"
130
131 # Anonymous cannot download a private file.
132 status, _, _ = request(f"/api/files/{file_id}")
133 assert status == 403
134
135 # Owner shares it; anonymous download works.
136 status, _, _ = request(
137 f"/collections/files/records/{file_id}",
138 method="PUT",
139 body=json.dumps({"is_public": "true"}).encode(),
140 headers=bearer + [("content-type", "application/json")],
141 )
142 assert status == 200
143 status, _, payload = raw_request(f"/api/files/{file_id}")
144 assert status == 200 and payload == b"file bytes here"
145
146 # Another user cannot delete it; the owner can, and bytes go away.
147 other_token, _ = create_identity_session({"user_id": "mallory"})
148 status, _, _ = request(
149 f"/api/files/{file_id}",
150 method="DELETE",
151 headers=[("authorization", f"Bearer {other_token}")],
152 )
153 assert status == 403
154 status, _, deleted = request(f"/api/files/{file_id}", method="DELETE", headers=bearer)
155 assert status == 200 and deleted["deleted"] is True
156 assert object_user_files.usage_bytes("dan", base_dir=data_dir) == 0
157 status, _, _ = request(f"/api/files/{file_id}", headers=bearer)
158 assert status == 404
159
160
161def test_upload_respects_quota_and_session(tmp_path, monkeypatch):
162 files_env(tmp_path, monkeypatch)
163 monkeypatch.setenv(object_server.USER_FILES_QUOTA_ENV, "10")
164 token, _ = create_identity_session({"user_id": "dan"})
165 bearer = [("authorization", f"Bearer {token}")]
166
167 body, content_type = multipart_body("file", "big.txt", b"x" * 11)
168 status, _, over = request(
169 "/api/files", method="POST", body=body,
170 headers=bearer + [("content-type", content_type)],
171 )
172 assert status == 413 and over["code"] == "quota_exceeded"
173
174 status, _, _ = request(
175 "/api/files", method="POST", body=body, headers=[("content-type", content_type)]
176 )
177 assert status == 401