from __future__ import annotations import json import time from io import BytesIO from pathlib import Path import pytest from app.web import create_app from .conftest import write_task from .test_runner import wait_for def start_review(settings, *, sensitive=False, fail=False): task = write_task(settings.task_root, "imports", "apply.py", ''' import json import os from pathlib import Path values = json.loads(Path(os.environ["SERVER_MAINTENANCE_INPUT"]).read_text()) print(json.dumps({"mode": values["mode"], "csv": Path(values["csv"]).read_text()})) ''' + ("raise SystemExit(1)\n" if fail else "")) (task.parent / "task-inputs.json").write_text(json.dumps({ "version": 1, "tasks": {"apply.py": { "inputs": [ {"name": "csv", "label": "CSV", "type": "file", "required": True, "accept": [".csv"], "maximum_bytes": 1024, "sensitive": sensitive}, {"name": "choices", "label": "Choices", "type": "multi_choice", "options": ["one"], "default": ["one"]}, ], "execution": {"field": "mode", "dry_run_value": "review", "dry_run_label": "Review", "execute_value": "apply", "execute_label": "Execute"}, }}, })) app = create_app(settings) client = app.test_client() response = client.post("/tasks/imports/apply.py/run", data={ "csv": (BytesIO(b"answer\nsame\n"), "decisions.csv"), "choices": "", }) run_id = int(response.location.rsplit("/", 1)[1]) manager = app.extensions["run_manager"] assert wait_for(manager, run_id, {"failed" if fail else "succeeded"}) == ("failed" if fail else "succeeded") manager.cleanup_transient_files() return app, client, run_id def test_uploaded_review_can_execute_with_the_same_csv_and_keeps_its_output(settings): app, client, review_id = start_review(settings) store = app.extensions["run_store"] original = Path(json.loads(store.get(review_id).input_json)["csv"]) assert original.is_file() assert original.stat().st_mode & 0o777 == 0o600 assert f'action="/runs/{review_id}/execute"'.encode() in client.get(f"/runs/{review_id}").data response = client.post(f"/runs/{review_id}/execute") assert response.status_code == 302 executed_id = int(response.location.rsplit("/", 1)[1]) assert executed_id != review_id assert wait_for(app.extensions["run_manager"], executed_id, {"succeeded"}) == "succeeded" app.extensions["run_manager"].cleanup_transient_files() review_output = json.loads(client.get(f"/runs/{review_id}/output").get_json()["output"]) execute_output = json.loads(client.get(f"/runs/{executed_id}/output").get_json()["output"]) assert review_output == {"mode": "review", "csv": "answer\nsame\n"} assert execute_output == {"mode": "apply", "csv": "answer\nsame\n"} assert json.loads(store.get(executed_id).input_json)["choices"] == [] assert not original.parent.exists() assert not Path(json.loads(store.get(executed_id).input_json)["csv"]).parent.exists() assert client.post(f"/runs/{review_id}/execute").status_code == 404 def test_review_upload_survives_application_restart(settings): app, _, run_id = start_review(settings) original = Path(json.loads(app.extensions["run_store"].get(run_id).input_json)["csv"]) restarted = create_app(settings) assert original.is_file() assert f'action="/runs/{run_id}/execute"'.encode() in restarted.test_client().get(f"/runs/{run_id}").data def test_unused_review_upload_expires_after_one_day(settings, monkeypatch): app, client, run_id = start_review(settings) original = Path(json.loads(app.extensions["run_store"].get(run_id).input_json)["csv"]) future = time.time() + 86401 monkeypatch.setattr("app.runner.time.time", lambda: future) assert client.post(f"/runs/{run_id}/execute").status_code == 404 app.extensions["run_manager"].cleanup_transient_files() assert not original.parent.exists() @pytest.mark.parametrize("sensitive,fail", [(True, False), (False, True)]) def test_sensitive_or_failed_review_does_not_retain_uploads(settings, sensitive, fail): _, client, run_id = start_review(settings, sensitive=sensitive, fail=fail) assert not (settings.state_root / "runs" / f"{run_id}.uploads").exists() assert client.post(f"/runs/{run_id}/execute").status_code == 404 @pytest.mark.parametrize("invalid_upload", ["missing", "symlink", "outside"]) def test_missing_or_unsafe_review_upload_cannot_execute(settings, invalid_upload): app, client, run_id = start_review(settings) store = app.extensions["run_store"] values = json.loads(store.get(run_id).input_json) original = Path(values["csv"]) original.unlink() if invalid_upload != "missing": outside = settings.state_root / "other.csv" outside.write_text("not the reviewed upload") if invalid_upload == "symlink": original.symlink_to(outside) else: values["csv"] = str(outside) store.set_input(run_id, values) assert client.post(f"/runs/{run_id}/execute").status_code == 404 assert f'action="/runs/{run_id}/execute"'.encode() not in client.get(f"/runs/{run_id}").data def test_failed_execution_enqueue_keeps_original_review_upload(settings, monkeypatch): app, client, run_id = start_review(settings) manager = app.extensions["run_manager"] original = Path(json.loads(manager.store.get(run_id).input_json)["csv"]) def cannot_save(*_): raise OSError("test: disk full") monkeypatch.setattr(manager, "_save_uploads", cannot_save) response = client.post(f"/runs/{run_id}/execute") failed_id = int(response.location.rsplit("/", 1)[1]) assert manager.store.get(failed_id).status == "failed" assert original.is_file() assert f'action="/runs/{run_id}/execute"'.encode() in client.get(f"/runs/{run_id}").data def test_removing_file_input_from_task_cleans_up_unused_review_upload(settings): app, _, run_id = start_review(settings) original = Path(json.loads(app.extensions["run_store"].get(run_id).input_json)["csv"]) manifest = settings.task_root / "imports" / "task-inputs.json" definition = json.loads(manifest.read_text()) definition["tasks"]["apply.py"]["inputs"] = [] manifest.write_text(json.dumps(definition)) app.extensions["run_manager"].cleanup_transient_files() assert not original.parent.exists()