Files
cms-SiteForge/tests/legacy.py
T

25 lines
1.1 KiB
Python
Raw Normal View History

"""Legacy tests assert a synchronous response. Adapt ONLY tests via the actual worker.
Production /builds always returns HTTP 202. This testing adapter submits to the
real queue, drains it through the worker executor and returns the legacy report.
It must never be imported from app/ or the production frontend.
"""
import json
import re
from fastapi.testclient import TestClient
from httpx import Response
from app import builder,db,worker
class LegacyTestClient(TestClient):
def post(self,url,*args,**kwargs):
r=super().post(url,*args,**kwargs)
if r.status_code==202 and re.fullmatch(r'/api/sites/[^/]+/builds',str(url)):
result=r.json()
job=result['id']
worker.run_once(executor=lambda sid,opts: builder.build(sid,bool(opts.get('mirror_images',False))))
record=db.one('SELECT * FROM build_jobs WHERE id=?',(job,))
if record['state']=='done':
return Response(200,json=json.loads(record['result_json']),request=r.request)
return Response(400,json={'detail':record['error']},request=r.request)
return r