maskingmasking / test_core.py
zombee11's picture
Upload 20 files
4de67a0 verified
Raw
History Blame Contribute Delete
3.98 kB
"""
Tests for Aadhaar Masking API.
Run: pytest tests/ -v
"""
import io
import re
import numpy as np
import pytest
# ── Verhoeff tests ─────────────────────────────────────────────────────────────
from app.utils.verhoeff import validate
def test_verhoeff_valid_known():
# Known valid Aadhaar-format numbers (public test vectors)
assert validate("234123412346") is True # example that passes Verhoeff
def test_verhoeff_invalid():
assert validate("123456789012") is False
def test_verhoeff_length_guard():
# Verhoeff works on any digit string; 12-digit gate is at detection layer
assert isinstance(validate("0"), bool)
# ── Regex pattern tests ────────────────────────────────────────────────────────
import re
AADHAAR_RE = re.compile(
r"(?<!\d)([2-9]\d{3})[\s\-]?(\d{4})[\s\-]?(\d{4})(?!\d)"
)
def test_aadhaar_regex_matches_spaced():
text = "Your Aadhaar: 2341 2341 2346"
m = AADHAAR_RE.search(text)
assert m is not None
def test_aadhaar_regex_no_match_phone():
# 10-digit phone starting with 9 β€” should NOT match 12-digit pattern
text = "Call us at 9876543210"
raw = text.replace(" ", "")
# Phone is 10 digits; our regex needs 12, so no full match expected
matches = [
m.group(0).replace(" ", "").replace("-", "")
for m in AADHAAR_RE.finditer(text)
if len(m.group(0).replace(" ", "").replace("-", "")) == 12
]
assert matches == []
VID_RE = re.compile(
r"(?<!\d)(9\d{3})[\s\-]?(\d{4})[\s\-]?(\d{4})[\s\-]?(\d{4})(?!\d)"
)
def test_vid_regex_matches():
text = "VID: 9876 5432 1098 7654"
assert VID_RE.search(text) is not None
def test_vid_regex_no_match_short():
text = "9876 5432 1098" # only 12 digits
assert VID_RE.search(text) is None
# ── Masking text tests ─────────────────────────────────────────────────────────
from app.models.schemas import MaskType
from app.services.masking_service import _mask_aadhaar_text, _mask_vid_text
def test_mask_aadhaar_partial():
assert _mask_aadhaar_text("2341 2341 2346", MaskType.partial) == "XXXX XXXX 2346"
def test_mask_aadhaar_full():
assert _mask_aadhaar_text("2341 2341 2346", MaskType.full) == "XXXX XXXX XXXX"
def test_mask_vid_partial():
result = _mask_vid_text("9876 5432 1098 7654", MaskType.partial)
assert result == "XXXX XXXX XXXX 7654"
def test_mask_vid_full():
result = _mask_vid_text("9876 5432 1098 7654", MaskType.full)
assert result == "XXXX XXXX XXXX XXXX"
# ── File service tests ─────────────────────────────────────────────────────────
from app.services.file_service import decode_image
import cv2
def test_decode_png_bytes():
img = np.zeros((100, 100, 3), dtype=np.uint8)
_, buf = cv2.imencode(".png", img)
decoded = decode_image(bytes(buf), "png")
assert decoded.shape == (100, 100, 3)
def test_decode_bad_bytes():
with pytest.raises(ValueError):
decode_image(b"not an image", "jpg")
# ── Cleanup / storage tests ────────────────────────────────────────────────────
from app.utils.cleanup import save_file, get_file_path, delete_file
def test_save_and_retrieve():
fid = save_file(b"hello", "txt")
path = get_file_path(fid)
assert path is not None
assert path.read_bytes() == b"hello"
def test_delete_file():
fid = save_file(b"bye", "txt")
delete_file(fid)
assert get_file_path(fid) is None