Spaces:
Running
Running
Download backend/tests/test_upload.py from HeshamXOR/MalwareGuard: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/spaces/HeshamXOR/MalwareGuard/resolve/main/backend/tests/test_upload.py
- Command line
-
hf download hf://spaces/HeshamXOR/MalwareGuard/backend/tests/test_upload.py
-
curl -L -o test_upload.py https://huggingface.co/spaces/HeshamXOR/MalwareGuard/resolve/main/backend/tests/test_upload.py
1.42 kB
| 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" | |