File size: 1,420 Bytes
1a73f90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf70023
 
1a73f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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"