-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
131 lines (112 loc) · 4.11 KB
/
Copy pathtest_app.py
File metadata and controls
131 lines (112 loc) · 4.11 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
Minimal runnable tests — py -m pytest test_app.py or python test_app.py
"""
from fastapi.testclient import TestClient
from app import app, _verhoeff_validate, _check_pan, _check_aadhaar
client = TestClient(app)
# --- unit: PAN ---------------------------------------------------------------
def test_pan_valid():
r = _check_pan("ABCDE1234F")
assert r.passed, r.detail
def test_pan_invalid_format():
r = _check_pan("123456789A")
assert not r.passed
def test_pan_lowercase_rejected():
r = _check_pan("abcde1234f")
assert not r.passed
# --- unit: Aadhaar / Verhoeff ------------------------------------------------
def test_verhoeff_known_valid():
# check digit of "271" is 8 → "2718" is valid
assert _verhoeff_validate("2718")
def test_verhoeff_known_invalid():
# one digit off from a valid number
assert not _verhoeff_validate("2717")
def test_aadhaar_wrong_length():
r = _check_aadhaar("12345")
assert not r.passed
def test_aadhaar_non_digits():
r = _check_aadhaar("12345678901X")
assert not r.passed
# --- integration: /verify endpoint ------------------------------------------
def test_verify_clean_pan():
resp = client.post("/verify", json={
"doc_type": "PAN",
"doc_number": "ABCDE1234F",
"name": "Mohan Kumar",
"dob": "1990-06-15",
})
assert resp.status_code == 200
data = resp.json()
assert data["verdict"] == "CLEAR"
pan_check = next(c for c in data["checks"] if c["name"] == "pan_format")
assert pan_check["passed"]
def test_verify_bad_pan_not_clear():
"""Bad PAN format should at minimum flag REVIEW (not CLEAR)."""
resp = client.post("/verify", json={
"doc_type": "PAN",
"doc_number": "BADPAN",
"name": "Test User",
"dob": "1985-01-01",
})
assert resp.status_code == 200
data = resp.json()
assert data["verdict"] != "CLEAR"
pan_check = next(c for c in data["checks"] if c["name"] == "pan_format")
assert not pan_check["passed"]
def test_verify_multiple_failures_reject():
"""Bad PAN + underage + name mismatch should REJECT."""
resp = client.post("/verify", json={
"doc_type": "PAN",
"doc_number": "BADPAN",
"name": "Test User",
"dob": "2020-01-01",
"name_on_doc": "Completely Different",
})
assert resp.status_code == 200
assert resp.json()["verdict"] == "REJECT"
def test_verify_underage_review():
resp = client.post("/verify", json={
"doc_type": "PAN",
"doc_number": "ABCDE1234F",
"name": "Young User",
"dob": "2015-01-01",
})
assert resp.status_code == 200
data = resp.json()
assert data["verdict"] != "CLEAR"
def test_name_mismatch_increases_risk():
resp_match = client.post("/verify", json={
"doc_type": "PAN", "doc_number": "ABCDE1234F",
"name": "Mohan Kumar", "dob": "1988-03-10",
"name_on_doc": "Mohan Kumar",
})
resp_mismatch = client.post("/verify", json={
"doc_type": "PAN", "doc_number": "ABCDE1234F",
"name": "Mohan Kumar", "dob": "1988-03-10",
"name_on_doc": "Totally Different Person",
})
assert resp_match.json()["risk_score"] < resp_mismatch.json()["risk_score"]
def test_health():
assert client.get("/health").json() == {"status": "ok"}
# --- self-check when run directly --------------------------------------------
if __name__ == "__main__":
tests = [
test_pan_valid, test_pan_invalid_format, test_pan_lowercase_rejected,
test_verhoeff_known_valid, test_verhoeff_known_invalid,
test_aadhaar_wrong_length, test_aadhaar_non_digits,
test_verify_clean_pan, test_verify_bad_pan_not_clear, test_verify_multiple_failures_reject,
test_verify_underage_review, test_name_mismatch_increases_risk,
test_health,
]
passed = failed = 0
for t in tests:
try:
t()
print(f" PASS {t.__name__}")
passed += 1
except Exception as e:
print(f" FAIL {t.__name__}: {e}")
failed += 1
print(f"\n{passed}/{passed+failed} passed")
if failed:
raise SystemExit(1)