[verified] add exam-board signed URL endpoint
This commit is contained in:
@@ -143,6 +143,9 @@ class _FakeStorageAdmin:
|
||||
def download_file(self, bucket_id, file_path):
|
||||
return b"%PDF-1.7 fake"
|
||||
|
||||
def create_signed_url(self, bucket_id, file_path, expires_in=3600):
|
||||
return {"signedURL": f"https://storage.test/{bucket_id}/{file_path}?token=fake&expires_in={expires_in}"}
|
||||
|
||||
|
||||
class _FakeServiceRoleClient:
|
||||
def __init__(self, store):
|
||||
@@ -171,6 +174,65 @@ def test_requires_auth_when_not_overridden():
|
||||
assert resp.status_code in (401, 403) # unauthenticated, not processed
|
||||
|
||||
|
||||
def test_catalogue_requires_auth_when_not_overridden():
|
||||
app = FastAPI()
|
||||
app.include_router(router, prefix="/api/exam")
|
||||
resp = TestClient(app).get("/api/exam/catalogue")
|
||||
assert resp.status_code in (401, 403)
|
||||
|
||||
|
||||
def test_list_catalogue_papers_uses_as_user_metadata():
|
||||
store = {
|
||||
"eb_exams": [
|
||||
{"id": "e1", "exam_code": "AQA-1", "type_code": "QP", "storage_loc": "cc.examboards/aqa/p.pdf"},
|
||||
{"id": "e2", "exam_code": "AQA-MS", "type_code": "MS", "storage_loc": "cc.examboards/aqa/ms.pdf"},
|
||||
]
|
||||
}
|
||||
client, _ = make_client(store=store)
|
||||
resp = client.get("/api/exam/catalogue")
|
||||
assert resp.status_code == 200
|
||||
assert [p["id"] for p in resp.json()["papers"]] == ["e1"]
|
||||
|
||||
|
||||
def test_catalogue_signed_url_requires_auth_and_signs_examboard_pdf(monkeypatch):
|
||||
store = {
|
||||
"eb_exams": [
|
||||
{"id": "e1", "exam_code": "AQA-1", "type_code": "QP", "storage_loc": "cc.examboards/aqa/physics/qp.pdf"},
|
||||
]
|
||||
}
|
||||
client, _ = make_client(store=store)
|
||||
monkeypatch.setattr(templates_mod, "StorageAdmin", _FakeStorageAdmin)
|
||||
resp = client.get("/api/exam/catalogue/e1/signed-url?expires_in=120")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["bucket"] == "cc.examboards"
|
||||
assert body["path"] == "aqa/physics/qp.pdf"
|
||||
assert body["expires_in"] == 120
|
||||
assert "token=fake" in body["signed_url"]
|
||||
|
||||
|
||||
def test_catalogue_signed_url_rejects_non_examboard_storage(monkeypatch):
|
||||
store = {
|
||||
"eb_exams": [
|
||||
{"id": "e1", "exam_code": "AQA-1", "type_code": "QP", "storage_loc": "cc.public/aqa/physics/qp.pdf"},
|
||||
]
|
||||
}
|
||||
client, _ = make_client(store=store)
|
||||
monkeypatch.setattr(templates_mod, "StorageAdmin", _FakeStorageAdmin)
|
||||
assert client.get("/api/exam/catalogue/e1/signed-url").status_code == 404
|
||||
|
||||
|
||||
def test_catalogue_signed_url_rejects_non_catalogue_doc_type(monkeypatch):
|
||||
store = {
|
||||
"eb_exams": [
|
||||
{"id": "e1", "exam_code": "AQA-MS", "type_code": "MS", "storage_loc": "cc.examboards/aqa/physics/ms.pdf"},
|
||||
]
|
||||
}
|
||||
client, _ = make_client(store=store)
|
||||
monkeypatch.setattr(templates_mod, "StorageAdmin", _FakeStorageAdmin)
|
||||
assert client.get("/api/exam/catalogue/e1/signed-url").status_code == 404
|
||||
|
||||
|
||||
def test_create_template_sets_owner_and_institute():
|
||||
client, store = make_client()
|
||||
resp = client.post("/api/exam/templates", json={"title": "AQA Physics 1H", "subject": "Physics"})
|
||||
|
||||
Reference in New Issue
Block a user