import hashlib from io import BytesIO from zipfile import ZipFile import pytest from fastapi import HTTPException, UploadFile from core.domains import Domain from persistance import config from webapi.models import import_models @pytest.mark.parametrize( ("kind", "status", "detail"), [ ("wrong_sha", 422, "not the one given"), ("non_zip", 422, "not a bundle"), ("untrusted", 403, "not trusted on this server"), ], ) def test_import_rejects_invalid_uploads(tmp_path, monkeypatch, kind, status, detail): monkeypatch.setattr(config, "DATA_DIR", tmp_path) if kind == "non_zip": data = b"hello" else: buffer = BytesIO() with ZipFile(buffer, "w") as archive: archive.writestr("manifest.json", "{}") data = buffer.getvalue() digest = hashlib.sha256(data).hexdigest() sent = "0" * 64 if kind == "wrong_sha" else digest with pytest.raises(HTTPException) as rejected: import_models(Domain.TEA, sent, UploadFile(BytesIO(data), filename="bundle.zip")) assert rejected.value.status_code == status assert detail in rejected.value.detail assert not (tmp_path / "bundles" / f"{digest}.zip").exists()