|
| 1 | +"""The Trainer's LoRA download route: the browser has no filesystem, so a finished run's |
| 2 | +.safetensors is fetched over GET /download/lora/{run_id} rather than by copying a path.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import sqlite3 |
| 7 | + |
| 8 | +import pytest |
| 9 | +from fastapi.testclient import TestClient |
| 10 | + |
| 11 | +from inline_core.graph.registry import build_default_registry |
| 12 | +from inline_core.server.app import create_app |
| 13 | +from inline_core.studio import training_store as ts |
| 14 | +from inline_core.studio.store import StudioStore |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def client(tmp_path, monkeypatch): |
| 19 | + # The route resolves the file under config.models_dir(); point that at the test models root so |
| 20 | + # it agrees with where a run's LoRA would actually be written. |
| 21 | + models = tmp_path / "models" |
| 22 | + (models / "loras").mkdir(parents=True) |
| 23 | + monkeypatch.setenv("INLINE_MODELS_DIR", str(models)) |
| 24 | + |
| 25 | + store = StudioStore(tmp_path / "appdata", tmp_path / "workspace") |
| 26 | + app = create_app( |
| 27 | + registry=build_default_registry(), |
| 28 | + studio_store=store, |
| 29 | + asset_dir=str(tmp_path / "assets"), |
| 30 | + models_root=str(models), |
| 31 | + takes_dir=str(tmp_path / "takes"), |
| 32 | + ) |
| 33 | + with TestClient(app) as c: |
| 34 | + yield c, store, models |
| 35 | + |
| 36 | + |
| 37 | +def _finished_run(store: StudioStore, rel: str) -> str: |
| 38 | + # The project connection lives on the server thread (opened by project:create), so write the |
| 39 | + # run row over our own connection to the same project.db to avoid SQLite's thread affinity. |
| 40 | + conn = sqlite3.connect(str(store.folder() / "project.db"), isolation_level=None) |
| 41 | + conn.row_factory = sqlite3.Row |
| 42 | + dataset = ts.create_dataset(conn, "chars", "sks") |
| 43 | + run = ts.create_run(conn, dataset["id"], "my-run", {"baseMode": "raw"}) |
| 44 | + ts.update_run(conn, run["id"], {"status": "done", "outputLoraPath": rel}) |
| 45 | + conn.close() |
| 46 | + return run["id"] |
| 47 | + |
| 48 | + |
| 49 | +def test_download_streams_the_lora_as_an_attachment(client) -> None: |
| 50 | + c, store, models = client |
| 51 | + args = [{"name": "F", "parentDir": None}] |
| 52 | + assert c.post("/rpc", json={"channel": "project:create", "args": args}).json()["ok"] is True |
| 53 | + |
| 54 | + (models / "loras" / "my-run.safetensors").write_bytes(b"LORA-BYTES") |
| 55 | + run_id = _finished_run(store, "loras/my-run.safetensors") |
| 56 | + |
| 57 | + res = c.get(f"/download/lora/{run_id}") |
| 58 | + assert res.status_code == 200 |
| 59 | + assert res.content == b"LORA-BYTES" |
| 60 | + assert "attachment" in res.headers.get("content-disposition", "") |
| 61 | + assert "my-run.safetensors" in res.headers.get("content-disposition", "") |
| 62 | + |
| 63 | + |
| 64 | +def test_unknown_run_is_404(client) -> None: |
| 65 | + c, _store, _models = client |
| 66 | + c.post("/rpc", json={"channel": "project:create", "args": [{"name": "F", "parentDir": None}]}) |
| 67 | + assert c.get("/download/lora/does-not-exist").status_code == 404 |
| 68 | + |
| 69 | + |
| 70 | +def test_path_traversal_is_refused(client) -> None: |
| 71 | + c, store, _models = client |
| 72 | + c.post("/rpc", json={"channel": "project:create", "args": [{"name": "F", "parentDir": None}]}) |
| 73 | + # A run whose stored path tries to escape loras/ must not serve an arbitrary file. |
| 74 | + run_id = _finished_run(store, "loras/../../secret.txt") |
| 75 | + assert c.get(f"/download/lora/{run_id}").status_code in (403, 404) |
0 commit comments