"""Local stack integration checks; creates and retains clearly named test orders. Run: python3 -m tests.smoke_test. Standard library only. Honors .env/environment. """ from concurrent.futures import ThreadPoolExecutor import hashlib import http.cookiejar import json import os import struct import zlib from pathlib import Path import time from urllib.error import HTTPError from urllib.request import build_opener, HTTPCookieProcessor, Request, urlopen from uuid import uuid4 def final_png_bytes(label='final'): """A tiny real RGBA PNG tagged 300 DPI for operator final-file tests.""" def chunk(kind, body): return struct.pack('>I', len(body)) + kind + body + struct.pack('>I', zlib.crc32(kind + body) & 0xffffffff) red = hashlib.sha256(label.encode()).digest()[0] row = b'\x00' + bytes((red, 30, 60, 255, 0, 0, 0, 0)) return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', 2, 1, 8, 6, 0, 0, 0)) + chunk(b'pHYs', struct.pack('>IIB', 11811, 11811, 1)) + chunk(b'IDAT', zlib.compress(row)) + chunk(b'IEND', b'')) if Path('.env').exists(): for line in Path('.env').read_text().splitlines(): if line.strip() and not line.startswith('#') and '=' in line: key,value=line.split('=',1) os.environ.setdefault(key,value) # CI runs these from a container on the stack's own network, because a runner # container cannot reach ports published on the host's loopback. SITE_BASE_URL # points at the gateway by service name; SITE_HOST_HEADER keeps the Host the # gateway and TrustedHostMiddleware expect, so the security configuration under # test stays identical to a developer's localhost run. BASE=os.environ.get('SITE_BASE_URL') or 'http://localhost:'+os.environ.get('SITE_PORT','8080') HOST_HEADER=os.environ.get('SITE_HOST_HEADER') def with_host(headers=None): headers=dict(headers or {}) if HOST_HEADER and not any(k.lower()=='host' for k in headers): headers['Host']=HOST_HEADER return headers class Client: def __init__(self): self.jar=http.cookiejar.CookieJar() self.opener=build_opener(HTTPCookieProcessor(self.jar)) self.operator_client=None def call(self,path,body=None,operator=False,expected=200,method=None): headers=with_host({'Content-Type':'application/json'}) if operator: if self.operator_client is None: self.operator_client=Client() self.operator_client.call('/operator/login',{'email':os.environ.get('OPERATOR_EMAIL','operator@example.test'),'password':os.environ.get('OPERATOR_PASSWORD','local-operator-only')}) return self.operator_client.call(path,body,expected=expected,method=method) for attempt in range(20): request=Request(BASE+'/api'+path,data=None if body is None else json.dumps(body).encode(), headers=headers,method=method) try: with self.opener.open(request,timeout=30) as response: assert response.status==expected,(path,response.status,expected) return json.load(response) except HTTPError as exc: if exc.code==expected:return json.load(exc) text=exc.read().decode() # The gateway's own limit (nginx, 20 r/s per address, burst 100) is # an HTML page, not the API's JSON: a fast suite waits for it. if exc.code==429 and text.lstrip().startswith('