-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_storage_compat.py
More file actions
293 lines (236 loc) · 10.7 KB
/
test_storage_compat.py
File metadata and controls
293 lines (236 loc) · 10.7 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
"""
Storage-service compatibility unit tests.
Validates that Python SDK storage query classes remain compatible with the
response shapes produced by storage-service (the Rainbow replacement). These
tests mock at the HTTP level and require no running service.
Covered flows:
* UploadDocument: POST /storage/files/store, LegacyUploadResponseItem shape
* CreateStorageURLs: indico-file:///storage<path> URI construction
* RetrieveStorageObject: indico-file:// prefix stripping and GET path
"""
import io
import json
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
from pathlib import Path
import pytest
from indico.client import IndicoClient
from indico.client.request import HTTPMethod
from indico.config import IndicoConfig
from indico.errors import IndicoRequestError
from indico.queries.model_import import _UploadSMExport
from indico.queries.storage import (
CreateStorageURLs,
RetrieveStorageObject,
UploadDocument,
)
# ---------------------------------------------------------------------------
# Response shape produced by storage-service /files/store endpoint
# (mirrors LegacyUploadResponseItem from storage_service/routes/blob_routes.py)
# ---------------------------------------------------------------------------
STORAGE_SERVICE_UPLOAD_RESPONSE = [
{
"path": "/uploads/42/abc-uuid",
"name": "document.pdf",
"size": 12345,
"upload_type": "user",
}
]
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture
def cfg():
return IndicoConfig(protocol="mock", host="mock", api_token="test-token")
@pytest.fixture
def mock_request(requests_mock, cfg):
"""Register a URL on requests_mock using the test config base URL."""
def _register(method, path, **kwargs):
url = f"{cfg.protocol}://{cfg.host}{path}"
getattr(requests_mock, method)(
url, **kwargs, headers={"Content-Type": "application/json"}
)
return _register
@pytest.fixture
def client(mock_request, cfg):
mock_request("post", "/auth/users/refresh_token", json={"auth_token": "tok"})
return IndicoClient(config=cfg)
# ---------------------------------------------------------------------------
# UploadDocument — request shape and response parsing
# ---------------------------------------------------------------------------
def test_upload_document_posts_to_storage_files_store(mock_request, client):
"""UploadDocument sends POST to /storage/files/store."""
captured = []
def capture(request, context):
captured.append(request.path)
context.status_code = 200
context.headers["Content-Type"] = "application/json"
import json as _json
return _json.dumps(STORAGE_SERVICE_UPLOAD_RESPONSE)
mock_request("post", "/storage/files/store", text=capture)
client.call(UploadDocument(streams={"test.pdf": io.BytesIO(b"data")}))
assert captured == ["/storage/files/store"]
def test_upload_document_processes_path_name_upload_type(mock_request, client):
"""UploadDocument.process_response reads path/name/upload_type from storage-service."""
mock_request("post", "/storage/files/store", json=STORAGE_SERVICE_UPLOAD_RESPONSE)
result = client.call(UploadDocument(streams={"test.pdf": io.BytesIO(b"data")}))
assert len(result) == 1
assert result[0]["filename"] == "document.pdf"
meta = json.loads(result[0]["filemeta"])
assert meta["path"] == "/uploads/42/abc-uuid"
assert meta["name"] == "document.pdf"
assert meta["uploadType"] == "user"
def test_upload_document_handles_multiple_files(mock_request, client):
"""Multiple files in one upload are each parsed correctly."""
multi_response = [
{
"path": "/uploads/42/uuid-1",
"name": "a.pdf",
"size": 100,
"upload_type": "user",
},
{
"path": "/uploads/42/uuid-2",
"name": "b.pdf",
"size": 200,
"upload_type": "user",
},
]
mock_request("post", "/storage/files/store", json=multi_response)
result = client.call(
UploadDocument(
streams={
"a.pdf": io.BytesIO(b"aaa"),
"b.pdf": io.BytesIO(b"bbb"),
}
)
)
assert len(result) == 2
assert result[0]["filename"] == "a.pdf"
assert result[1]["filename"] == "b.pdf"
# ---------------------------------------------------------------------------
# CreateStorageURLs — indico-file URI construction
# ---------------------------------------------------------------------------
def test_create_storage_urls_builds_indico_file_uris(mock_request, client):
"""CreateStorageURLs returns indico-file:///storage<path> from storage-service response."""
mock_request("post", "/storage/files/store", json=STORAGE_SERVICE_UPLOAD_RESPONSE)
result = client.call(CreateStorageURLs(streams={"test.pdf": io.BytesIO(b"data")}))
assert result == ["indico-file:///storage/uploads/42/abc-uuid"]
def test_create_storage_urls_round_trips_through_retrieve():
"""A URI from CreateStorageURLs can be fed directly into RetrieveStorageObject."""
uri = "indico-file:///storage/uploads/42/abc-uuid"
req = RetrieveStorageObject(uri)
assert req.path == "/storage/uploads/42/abc-uuid"
assert req.method == HTTPMethod.GET
# ---------------------------------------------------------------------------
# RetrieveStorageObject — path construction
# ---------------------------------------------------------------------------
def test_retrieve_storage_object_strips_indico_file_scheme():
"""indico-file:// prefix is stripped; remaining path becomes the GET path."""
req = RetrieveStorageObject("indico-file:///storage/submissions/1/2/result.json")
assert req.path == "/storage/submissions/1/2/result.json"
assert req.method == HTTPMethod.GET
def test_retrieve_storage_object_accepts_dict_with_url_key():
"""Accepts a dict with 'url' key (as returned by GraphQL result objects)."""
req = RetrieveStorageObject({"url": "indico-file:///storage/extractions/99.json"})
assert req.path == "/storage/extractions/99.json"
def test_retrieve_storage_object_fetches_content(mock_request, client):
"""GET /storage/<path> is issued and the response body is returned."""
payload = {"status": "complete", "results": [{"text": "hello"}]}
mock_request("get", "/storage/submissions/1/2/result.json", json=payload)
result = client.call(
RetrieveStorageObject("indico-file:///storage/submissions/1/2/result.json")
)
assert result == payload
def test_retrieve_storage_object_follows_redirects():
"""Storage GET requests follow redirects in redirect-mode deployments."""
payload = {"status": "complete", "results": [{"text": "redirected"}]}
refresh_path = "/auth/users/refresh_token"
source_path = "/storage/submissions/1/2/result.json"
redirected_path = "/storage/signed/submissions/1/2/result.json"
class Handler(BaseHTTPRequestHandler):
def do_POST(self): # noqa: N802
if self.path != refresh_path:
self.send_response(404)
self.end_headers()
return
body = json.dumps({"auth_token": "tok"}).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_GET(self): # noqa: N802
if self.path == source_path:
self.send_response(302)
self.send_header(
"Location",
f"http://{self.server.server_address[0]}:{self.server.server_address[1]}{redirected_path}",
)
self.end_headers()
return
if self.path == redirected_path:
body = json.dumps(payload).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
return
self.send_response(404)
self.end_headers()
def log_message(self, format, *args): # noqa: A003
return
with HTTPServer(("127.0.0.1", 0), Handler) as server:
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
host = f"{server.server_address[0]}:{server.server_address[1]}"
client = IndicoClient(
config=IndicoConfig(protocol="http", host=host, api_token="test-token")
)
result = client.call(
RetrieveStorageObject(
"indico-file:///storage/submissions/1/2/result.json"
)
)
assert result == payload
finally:
server.shutdown()
thread.join(timeout=5)
def test_upload_static_model_export_puts_zip_to_signed_url(tmp_path, requests_mock):
"""Static model export upload uses the signed URL with zip content-type."""
export_path = tmp_path / "model.zip"
export_bytes = b"zip-bytes"
export_path.write_bytes(export_bytes)
signed_url = "https://signed.example/upload"
storage_uri = "indico-file:///storage/exports/model.zip"
requests_mock.put(signed_url, status_code=200, text="")
request = _UploadSMExport(str(export_path))
result = request.process_response(
{"data": {"exportUpload": {"signedUrl": signed_url, "storageUri": storage_uri}}}
)
assert result == storage_uri
assert len(requests_mock.request_history) == 1
put_call = requests_mock.request_history[0]
assert put_call.method == "PUT"
assert put_call.headers["Content-Type"] == "application/zip"
assert put_call.body == export_bytes
def test_upload_static_model_export_raises_on_put_failure(tmp_path, requests_mock):
"""A failing signed-url PUT raises IndicoRequestError."""
export_path = tmp_path / "model.zip"
export_path.write_bytes(b"zip-bytes")
signed_url = "https://signed.example/upload"
requests_mock.put(signed_url, status_code=403, text="forbidden")
request = _UploadSMExport(str(Path(export_path)))
with pytest.raises(IndicoRequestError):
request.process_response(
{
"data": {
"exportUpload": {
"signedUrl": signed_url,
"storageUri": "indico-file:///storage/exports/model.zip",
}
}
}
)