"""Test the actual supplied POST handler without sockets, using temporary storage. The execution sandbox prohibits socket.bind, so these are NOT HTTP transport tests. """ from pathlib import Path import hashlib, io, json, os, runpy, tempfile root = Path(__file__).resolve().parent original_cwd = Path.cwd() namespace = runpy.run_path(str(root / 'site/server.py'), run_name='handler_test') os.chdir(original_cwd) Handler = namespace['Handler'] results = [] with tempfile.TemporaryDirectory() as tmp: os.chdir(tmp) def request(raw, path='/api/leads'): payload = raw.encode('utf-8') handler = Handler.__new__(Handler) handler.path = path handler.headers = {'Content-Length':str(len(payload))} handler.rfile = io.BytesIO(payload) handler.wfile = io.BytesIO() captured = {} handler.send_response = lambda code: captured.update(status=code) handler.send_header = lambda name, value: None handler.end_headers = lambda: None Handler.do_POST(handler) return captured['status'], handler.wfile.getvalue() def records(): path = Path('submissions.jsonl') return [json.loads(line) for line in path.read_text().splitlines()] if path.exists() else [] try: for seats in [1, 2, 3, 8]: data = {'name':'Local Tester', 'email':f'seats{seats}@example.test', 'seats':seats} before = len(records()) status, body = request(json.dumps(data)) assert status == 201 and json.loads(body) == {'ok':True} assert len(records()) == before + 1 and records()[-1] == data results.append({'case':f'valid seats {seats}', 'status':status, 'records_before':before, 'records_after':len(records()), 'record':records()[-1], 'pass':True}) base = {'name':'Invalid Tester', 'email':'invalid@example.test', 'seats':1} cases = [('blank name', {'name':''}), ('whitespace name', {'name':' '}), ('null name', {'name':None}), ('blank email', {'email':''}), ('malformed email', {'email':'not-email'}), ('no dotted email domain', {'email':'name@example'}), ('email spaces', {'email':'a b@example.test'})] cases += [(f'invalid seats {value!r}', {'seats':value}) for value in ['', 0, -1, 1.5, 9, None, True, '3']] for label, update in cases: before = len(records()) status, body = request(json.dumps({**base, **update})) assert status == 400 and 'error' in json.loads(body) and len(records()) == before results.append({'case':label, 'status':status, 'records_before':before, 'records_after':len(records()), 'pass':True}) for raw in ['{', '[]', '{}']: before = len(records()) status, body = request(raw) assert status == 400 and len(records()) == before results.append({'case':'invalid payload ' + raw, 'status':status, 'records_before':before, 'records_after':len(records()), 'pass':True}) finally: os.chdir(original_cwd) report = {'server_sha256':hashlib.sha256((root/'site/server.py').read_bytes()).hexdigest(), 'scope':'Actual supplied Handler.do_POST invoked directly with BytesIO request/response and temporary disk storage. No live HTTP transport or server restart check; socket.bind was denied by the sandbox.', 'checks':results} (root / 'server-checks.json').write_text(json.dumps(report, indent=2) + '\n') print(json.dumps(report, indent=2))