"""Direct tests of the supplied server logic (output/site/server.py Handler.do_POST). The sandbox forbids socket binding (PermissionError on bind), so requests are simulated: do_POST is invoked with a real byte-stream body/headers and stubbed response methods. This exercises the server's validation, status codes, response bodies and submissions.jsonl writes exactly as written. Live HTTP POST /api/leads behavior is separately exercised by the browser tool's trusted server. Stdlib only. Safe to re-run: any pre-existing submissions.jsonl (e.g. records written by browser tests) is backed up in memory and restored afterwards; only this script's own test records are removed. """ import importlib.util, io, json from pathlib import Path from email.message import Message site = Path('output/site').resolve() sub = site / 'submissions.jsonl' preexisting = sub.read_bytes() if sub.exists() else None # preserve real records spec = importlib.util.spec_from_file_location('workshop_server', site / 'server.py') mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) # module chdirs to site/ class SimHandler(mod.Handler): def __init__(self, path, payload): body = json.dumps(payload).encode() self.path = path self.rfile = io.BytesIO(body) self.wfile = io.BytesIO() self.headers = Message() self.headers['Content-Length'] = str(len(body)) self.status = None self.sent_headers = {} def send_response(self, code, *a): self.status = code def send_header(self, k, v): self.sent_headers[k] = v def end_headers(self): pass def send_error(self, code, *a): self.status = code def post(payload, path='/api/leads'): h = SimHandler(path, payload) h.do_POST() return h.status, h.wfile.getvalue().decode() valid = [ {'name': 'Ada Test', 'email': 'ada@example.test', 'seats': 1}, {'name': 'Bo Test', 'email': 'bo@example.test', 'seats': 3}, {'name': 'Cy Test', 'email': 'cy@example.test', 'seats': 8}, ] invalid = [ ('empty name', {'name': '', 'email': 'x@example.test', 'seats': 2}), ('whitespace name', {'name': ' ', 'email': 'x@example.test', 'seats': 2}), ('bad email', {'name': 'A', 'email': 'not-an-email', 'seats': 2}), ('email no dot', {'name': 'A', 'email': 'a@b', 'seats': 2}), ('0 seats', {'name': 'A', 'email': 'x@example.test', 'seats': 0}), ('-1 seats', {'name': 'A', 'email': 'x@example.test', 'seats': -1}), ('9 seats', {'name': 'A', 'email': 'x@example.test', 'seats': 9}), ('2.5 seats', {'name': 'A', 'email': 'x@example.test', 'seats': 2.5}), ('string seats "3"', {'name': 'A', 'email': 'x@example.test', 'seats': '3'}), ('missing seats', {'name': 'A', 'email': 'x@example.test'}), ] results = [] try: if sub.exists(): sub.unlink() # isolate this run's records from any pre-existing ones for p in valid: code, body = post(p) results.append({'case': 'VALID ' + p['name'], 'code': code, 'body': body, 'pass': code == 201}) for label, p in invalid: code, body = post(p) results.append({'case': 'INVALID ' + label, 'code': code, 'body': body, 'pass': code == 400}) # the original bug: wrong endpoint must 404, never store code, body = post(valid[0], path='/api/lead') results.append({'case': 'old endpoint /api/lead', 'code': code, 'pass': code == 404}) stored = [json.loads(l) for l in sub.read_text().splitlines()] if sub.exists() else [] print(json.dumps({'cases': results, 'all_cases_pass': all(r['pass'] for r in results), 'stored_records': stored, 'stored_equals_valid_payloads_only': stored == valid}, indent=1)) finally: # restore the pre-existing state exactly; drop only this run's test records if sub.exists(): sub.unlink() if preexisting is not None: sub.write_bytes(preexisting) print('pre-existing submissions.jsonl restored unchanged')