import io from datetime import date, timedelta from unittest.mock import patch import pytest from PIL import Image from app import app def _valid_png_bytes() -> bytes: buf = io.BytesIO() Image.new("RGB", (10, 10), color=(0, 128, 255)).save(buf, format="PNG") return buf.getvalue() @pytest.fixture def client(tmp_path): app.config["TESTING"] = True app.config["UPLOAD_FOLDER"] = str(tmp_path / "uploads") with app.test_client() as c: yield c def _valid_payload(): """Return a complete, valid fake registration payload.""" return { "title": "1", "lname": "DOE", "fname": "JOHN", "mname": "A", "pan_no": "ABCDE1234F", "aadhar_no": "123456789012", "gender": "1", "father": "RICHARD DOE", "dob": "15/05/1990", "age": "36", "ageUnit": "0", "approx_dob": "0", "marital_status": "", "dom": "", "bmname": "", "patient_income": "25000", "address1": "123 FAKE STREET", "address2": "NEAR PARK", "post_office": "PUNE", "country": "38", "state": "12", "city": "289", "pincode": "411004", "phone": "0201234567", "office_phone": "0207654321", "cell_phone": "9876543210", "email": "john.doe@example.com", "p_address1": "123 FAKE STREET", "p_address2": "NEAR PARK", "p_post_office": "PUNE", "p_country": "38", "p_state": "12", "p_city": "289", "p_pincode": "411004", "p_phone": "0201234567", "emg_person": "DOE", "emg_person1": "JANE", "emg_person2": "", "relation": "2", "emg_no": "9876500000", "re_persone": "JANE DOE", "re_relation": "2", "doc_type": "1570", "txtcaptcha": "AbCd", "SaveDB": "Save", "honeypot": "", "ptnPhoto": (io.BytesIO(_valid_png_bytes()), "photo.png"), "id_file": (io.BytesIO(b"%PDF-1.4\n1 0 obj\n<<>>\nendobj\ntrailer\n<<>>\n%%EOF"), "aadhar.pdf"), } def test_index_routes(client): for path in ("/", "/index.php"): resp = client.get(path) assert resp.status_code == 200 assert b"Patient Registration Form" in resp.data def test_state_and_city_endpoints_india(client): r1 = client.post("/getState.php?countryId=38&stateId=12") assert r1.status_code == 200 assert b'name="state"' in r1.data assert b"Maharashtra" in r1.data r2 = client.post("/getCity.php?countryId=38&stateId=12&cityId=289") assert r2.status_code == 200 assert b'name="city"' in r2.data assert b"Pune" in r2.data r3 = client.post("/p_getState.php?countryId=38&stateId=12") assert r3.status_code == 200 assert b'name="p_state"' in r3.data r4 = client.post("/p_getCity.php?countryId=38&stateId=12&cityId=289") assert r4.status_code == 200 assert b'name="p_city"' in r4.data def test_state_and_city_endpoints_non_india(client): r1 = client.post("/getState.php?countryId=81&other_state=California") assert r1.status_code == 200 assert b'name="other_state"' in r1.data assert b'value="California"' in r1.data r2 = client.post("/getCity.php?countryId=81&other_city=San%20Jose") assert r2.status_code == 200 assert b'name="other_city"' in r2.data assert b'value="San Jose"' in r2.data r3 = client.post("/p_getState.php?countryId=81&other_state=Texas") assert r3.status_code == 200 assert b'name="other_p_state"' in r3.data r4 = client.post("/p_getCity.php?countryId=81&other_city=Austin") assert r4.status_code == 200 assert b'name="other_p_city"' in r4.data def test_get_dob_endpoint(client): r_days = client.post("/getDOB.php?age=10&ageUnit=days") assert r_days.status_code == 200 expected = (date.today() - timedelta(days=10)).strftime("%d/%m/%Y").encode() assert r_days.data == expected r_weeks = client.post("/getDOB.php?age=2&ageUnit=weeks") assert r_weeks.status_code == 200 expected_w = (date.today() - timedelta(weeks=2)).strftime("%d/%m/%Y").encode() assert r_weeks.data == expected_w def test_valid_submission_saves_and_shows_reference_id(client): with patch("db.save_registration", return_value="250043") as mock_save: resp = client.post( "/uploadProxy.php", data=_valid_payload(), content_type="multipart/form-data", ) assert resp.status_code == 200 assert b"Registration Form Saved Successfully.. Your Reference Form ID : 250043" in resp.data mock_save.assert_called_once() def test_honeypot_rejected(client): data = _valid_payload() data["honeypot"] = "bot-filled" resp = client.post("/uploadProxy.php", data=data, content_type="multipart/form-data") assert resp.status_code == 400 assert b"Invalid submission detected." in resp.data @pytest.mark.parametrize( "field,bad_val,expected_msg", [ ("title", "", b"Please Select Title"), ("lname", "", b"Please Enter Last Name"), ("fname", "", b"Please Enter First Name"), ("pan_no", "INVALID123", b"Enter Valid Pan Card Number"), ("aadhar_no", "12345abc", b"Please Enter Numbers In Aadhar Number"), ("aadhar_no", "12345678901", b"Please Enter 12 Digit Aadhar Number"), ("patient_income", "abc", b"Please Enter Numbers In Monthly Income"), ("address1", "", b"Please Enter House Name/Appt.No Details"), ("country", "", b"Please Select Country"), ("state", "", b"Please Select State"), ("city", "", b"Please Select City"), ("pincode", "41100A", b"Please Enter Numbers In Pin Number"), ("cell_phone", "987654321a", b"Please Enter Numbers In Cell Number"), ("cell_phone", "98765", b"Please Enter 10 Digit Cell Number"), ("email", "bad-email", b"You have entered an invalid email address!"), ("email", "user@domain.info", b"You have entered an invalid email address!"), ("emg_person", "", b"Please Enter Emergency Contact Person"), ("emg_person1", "", b"Please Enter Emergency Contact Person"), ("relation", "", b"Please Select Relation With Patient"), ("emg_no", "", b"Please Enter Emergency Contact Number"), ("re_persone", "", b"Please Enter Name Of Representative"), ("re_relation", "", b"Please Select Representative Relation To Patient"), ("txtcaptcha", "", b"Enter captcha text."), ("txtcaptcha", "12", b"Invalid Captcha Text!"), ], ) def test_field_validations(client, field, bad_val, expected_msg): data = _valid_payload() data[field] = bad_val resp = client.post("/uploadProxy.php", data=data, content_type="multipart/form-data") assert resp.status_code == 400 assert expected_msg in resp.data def test_female_marital_status_rules(client): data = _valid_payload() data["title"] = "3" data["gender"] = "2" data["marital_status"] = "" resp = client.post("/uploadProxy.php", data=data, content_type="multipart/form-data") assert resp.status_code == 400 assert b"Please Choose Marital Status." in resp.data data2 = _valid_payload() data2["title"] = "3" data2["gender"] = "2" data2["marital_status"] = "Married" data2["dom"] = "" data2["bmname"] = "" resp2 = client.post("/uploadProxy.php", data=data2, content_type="multipart/form-data") assert resp2.status_code == 400 assert b"Please Enter Date Of Marriage." in resp2.data assert b"Please Enter Before Marriage Name." in resp2.data def test_non_india_free_text_state_city_accepted(client): data = _valid_payload() data["country"] = "81" data.pop("state") data.pop("city") data["other_state"] = "California" data["other_city"] = "San Jose" with patch("db.save_registration", return_value="250099"): resp = client.post("/uploadProxy.php", data=data, content_type="multipart/form-data") assert resp.status_code == 200 assert b"250099" in resp.data def test_invalid_id_file_type_rejected(client): data = _valid_payload() data["id_file"] = (io.BytesIO(b"bad"), "malware.exe") resp = client.post("/uploadProxy.php", data=data, content_type="multipart/form-data") assert resp.status_code == 400 assert b"Invalid file type" in resp.data