import numpy as np def test_upload_rejects_non_mz_file(client): response = client.post( "/api/upload/analyze", files={"file": ("note.txt", b"hello", "text/plain")}, ) assert response.status_code == 400 assert "Invalid PE file" in response.json()["detail"] def test_upload_rejects_size_limit(client): oversized = b"MZ" + (b"0" * (50 * 1024 * 1024 + 1)) response = client.post( "/api/upload/analyze", files={"file": ("big.exe", oversized, "application/octet-stream")}, ) assert response.status_code == 400 assert "File too large" in response.json()["detail"] def test_upload_analyze_happy_path_with_mocked_lief(client, monkeypatch): from app.routers import upload monkeypatch.setattr(upload, "LIEF_AVAILABLE", True) monkeypatch.setattr( upload, "extract_features_and_lief", lambda content: (np.zeros(2381, dtype=float), "unused", object()), ) response = client.post( "/api/upload/analyze?include_explanation=false", files={"file": ("sample.exe", b"MZfake-pe", "application/octet-stream")}, ) assert response.status_code == 200 body = response.json() assert body["features_extracted"] is True assert body["feature_count"] == 2381 assert body["features"] is None assert body["pe_headers"] is None assert body["prediction"]["prediction"] == "Malicious"