Delete tests/test_docclass.py
Browse files- tests/test_docclass.py +0 -174
tests/test_docclass.py
DELETED
|
@@ -1,174 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Document classification path — the serving contract plus the HTTP layer.
|
| 3 |
-
|
| 4 |
-
DOCCLASS_MODEL_PATH=<path to layer3_25class.joblib> python -m pytest tests -q
|
| 5 |
-
|
| 6 |
-
Runs docclass.py's router on its own (no PII model, no Hub), against the real
|
| 7 |
-
model file. Every input is generated here: no sample secrets or ID documents
|
| 8 |
-
live in the repository.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
from __future__ import annotations
|
| 12 |
-
|
| 13 |
-
import io
|
| 14 |
-
import os
|
| 15 |
-
import sys
|
| 16 |
-
import zipfile
|
| 17 |
-
|
| 18 |
-
import pytest
|
| 19 |
-
|
| 20 |
-
_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 21 |
-
sys.path.insert(0, _ROOT)
|
| 22 |
-
|
| 23 |
-
pytestmark = pytest.mark.skipif(not os.getenv("DOCCLASS_MODEL_PATH"),
|
| 24 |
-
reason="set DOCCLASS_MODEL_PATH to the model file")
|
| 25 |
-
|
| 26 |
-
SECRET = "test-secret"
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
@pytest.fixture(scope="module")
|
| 30 |
-
def client():
|
| 31 |
-
from fastapi import FastAPI, HTTPException, Request
|
| 32 |
-
from fastapi.testclient import TestClient
|
| 33 |
-
import docclass
|
| 34 |
-
|
| 35 |
-
docclass.load()
|
| 36 |
-
assert docclass.MODEL is not None, docclass.LOAD_ERROR
|
| 37 |
-
|
| 38 |
-
def verify(request: Request):
|
| 39 |
-
if request.headers.get("Authorization") != f"Bearer {SECRET}":
|
| 40 |
-
raise HTTPException(status_code=401, detail="Invalid token")
|
| 41 |
-
|
| 42 |
-
app = FastAPI()
|
| 43 |
-
app.include_router(docclass.build_router(verify))
|
| 44 |
-
return TestClient(app)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
AUTH = {"Authorization": f"Bearer {SECRET}"}
|
| 48 |
-
|
| 49 |
-
CREDENTIALS = (
|
| 50 |
-
"CONFIDENTIAL - Employee Onboarding Credentials\n"
|
| 51 |
-
"Domain Username: a.sharma\n"
|
| 52 |
-
"Temporary Password: " + "Xk9$mQ2vLp8w!Tz" + "\n"
|
| 53 |
-
"API Key: " + "sk_live_" + "4eJbQ7mXpZ2tRvNd8KfHwLcY" + "\n"
|
| 54 |
-
"AWS_SECRET_ACCESS_KEY=" + "wJal5XUtnFEMI+K7MDENG/bPxRfiCYEX" + "\n"
|
| 55 |
-
"-----BEGIN OPENSSH PRIVATE KEY-----\n"
|
| 56 |
-
)
|
| 57 |
-
AADHAAR = (
|
| 58 |
-
"GOVERNMENT OF INDIA\nUNIQUE IDENTIFICATION AUTHORITY OF INDIA\n\n"
|
| 59 |
-
"Aadhaar Number: 5821 9043 7761\nName: Meera Reddy\nDate of Birth: 14/08/1991\n"
|
| 60 |
-
"Gender: Female\nAddress: 42 SV Road, Chennai, Tamil Nadu 600042\n"
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
def _text_image(text: str) -> bytes:
|
| 65 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 66 |
-
img = Image.new("RGB", (1100, 60 + 44 * text.count("\n")), "white")
|
| 67 |
-
draw = ImageDraw.Draw(img)
|
| 68 |
-
try:
|
| 69 |
-
font = ImageFont.truetype("arial.ttf", 30)
|
| 70 |
-
except OSError:
|
| 71 |
-
font = ImageFont.load_default(size=30)
|
| 72 |
-
draw.multiline_text((30, 30), text, fill="black", font=font, spacing=12)
|
| 73 |
-
buf = io.BytesIO()
|
| 74 |
-
img.save(buf, format="PNG")
|
| 75 |
-
return buf.getvalue()
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# ── Contract (from the handover's tests) ───────────────────────────────────────
|
| 79 |
-
|
| 80 |
-
def test_every_class_has_a_policy_row(client):
|
| 81 |
-
import docclass
|
| 82 |
-
assert docclass.validate_policy(list(docclass.MODEL.classes)) == []
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def test_auth_required(client):
|
| 86 |
-
assert client.post("/file/classify", json={"text": "x"}).status_code == 401
|
| 87 |
-
assert client.post("/file/analyze", content=b"x").status_code == 401
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
def test_empty_input_abstains_and_fails_closed(client):
|
| 91 |
-
for body in [{}, {"text": ""}, {"text": " ", "column_headers": []}]:
|
| 92 |
-
r = client.post("/file/classify", json=body, headers=AUTH).json()
|
| 93 |
-
assert r["abstained"] is True
|
| 94 |
-
assert r["document_type"] == "unknown"
|
| 95 |
-
assert r["action"] == "ROUTE_TO_REVIEW"
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def test_credentials_blocked(client):
|
| 99 |
-
r = client.post("/file/classify", json={"text": CREDENTIALS}, headers=AUTH).json()
|
| 100 |
-
assert r["document_type"] == "credentials"
|
| 101 |
-
assert r["action"] == "BLOCK_ALL"
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
def test_aadhaar_is_government_identity(client):
|
| 105 |
-
r = client.post("/file/classify", json={"text": AADHAAR}, headers=AUTH).json()
|
| 106 |
-
assert r["document_type"] == "government_identity"
|
| 107 |
-
assert r["action"] == "BLOCK_EXTERNAL"
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
def test_low_confidence_fails_closed(client):
|
| 111 |
-
r = client.post("/file/classify", json={"text": "a b c d e f g h i j k l m n o p"}, headers=AUTH).json()
|
| 112 |
-
if not r["confident"]:
|
| 113 |
-
assert r["action"] == "ROUTE_TO_REVIEW"
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
def test_column_headers_alone_classify(client):
|
| 117 |
-
r = client.post("/file/classify", json={"column_headers": ["customer_name", "email", "phone", "address", "account_id"]},
|
| 118 |
-
headers=AUTH).json()
|
| 119 |
-
assert r["abstained"] is False
|
| 120 |
-
assert 0.0 <= r["confidence"] <= 1.0
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# ── Files: layers 1 + 2 + OCR ─────────────────────────────────────────────────
|
| 124 |
-
|
| 125 |
-
def test_analyze_text_file(client):
|
| 126 |
-
r = client.post("/file/analyze", content=CREDENTIALS.encode(), headers={**AUTH, "X-Filename": "notes.txt"}).json()
|
| 127 |
-
assert r["document_type"] == "credentials"
|
| 128 |
-
assert r["ocr_used"] is False
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
def test_image_is_ocrd_and_rename_is_flagged(client):
|
| 132 |
-
# A PNG pretending to be a PDF: Layer 1 reads the bytes, so OCR still runs
|
| 133 |
-
# and the lie is reported.
|
| 134 |
-
png = _text_image(AADHAAR)
|
| 135 |
-
r = client.post("/file/analyze", content=png, headers={**AUTH, "X-Filename": "report.pdf"}).json()
|
| 136 |
-
assert r["actual_type"] == "png"
|
| 137 |
-
assert r["type_mismatch"] is True
|
| 138 |
-
assert r["ocr_used"] is True
|
| 139 |
-
assert r["abstained"] is False
|
| 140 |
-
assert r["document_type"] == "government_identity", r
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
def test_scanned_pdf_is_ocrd(client):
|
| 144 |
-
from PIL import Image
|
| 145 |
-
buf = io.BytesIO()
|
| 146 |
-
Image.open(io.BytesIO(_text_image(AADHAAR))).save(buf, format="PDF")
|
| 147 |
-
r = client.post("/file/analyze", content=buf.getvalue(), headers={**AUTH, "X-Filename": "scan.pdf"}).json()
|
| 148 |
-
assert r["actual_type"] == "pdf"
|
| 149 |
-
assert r["ocr_used"] is True
|
| 150 |
-
assert r["extraction_method"] == "pdf_ocr_rapidocr"
|
| 151 |
-
assert r["document_type"] == "government_identity", r
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
def test_archive_contents_are_classified(client):
|
| 155 |
-
buf = io.BytesIO()
|
| 156 |
-
with zipfile.ZipFile(buf, "w") as z:
|
| 157 |
-
z.writestr("holiday/notes.txt", CREDENTIALS)
|
| 158 |
-
z.writestr("holiday/id.txt", AADHAAR)
|
| 159 |
-
r = client.post("/file/analyze", content=buf.getvalue(), headers={**AUTH, "X-Filename": "photos.zip"}).json()
|
| 160 |
-
inside = {e["path"].split("/")[-1]: e for e in r["nested_entries"]}
|
| 161 |
-
assert inside["notes.txt"]["document_type"] == "credentials"
|
| 162 |
-
assert inside["id.txt"]["document_type"] == "government_identity"
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
def test_oversize_rejected(client):
|
| 166 |
-
import docclass
|
| 167 |
-
big = b"a" * (docclass.MAX_FILE_BYTES + 1)
|
| 168 |
-
assert client.post("/file/analyze", content=big, headers={**AUTH, "X-Filename": "big.txt"}).status_code == 413
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
def test_empty_file_abstains(client):
|
| 172 |
-
r = client.post("/file/analyze", content=b"", headers={**AUTH, "X-Filename": "empty.txt"}).json()
|
| 173 |
-
assert r["abstained"] is True
|
| 174 |
-
assert r["action"] == "ROUTE_TO_REVIEW"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|