review-malformed-verdict.py
"""Actual ASGI extraction must not substitute cache placeholders for validation."""
from pathlib import Path
import os
import subprocess
import sys
def test_cache_miss_requires_validation_before_forwarding(tmp_path):
root = Path('/tmp/mizoki-readiness-fr41')
script = r'''
import os, socket, sys
from unittest.mock import patch, AsyncMock
import google.auth
from google.auth.exceptions import DefaultCredentialsError
attempts=[]
def denied(*args, **kwargs):
attempts.append('network')
raise AssertionError('external access forbidden in fixture')
socket.socket.connect=denied
socket.create_connection=denied
socket.getaddrinfo=denied
sys.path[:0]=[sys.argv[1]+'/src/cells/cell02',sys.argv[1]+'/src']
os.environ['GCP_PROJECT_ID']=''
os.environ['MIZOKI_SRPVDAL_ENABLED']='false'
os.environ['SRPVDAL_ENABLED']='false'
with patch.object(google.auth,'default',side_effect=DefaultCredentialsError('offline fixture')):
import Cell2, httpx, pandas as pd
from fastapi.testclient import TestClient
assert Cell2.Cell2Config.model_fields['enable_cell4_cache'].default is True
with TestClient(Cell2.app) as client:
service=Cell2.app.state.extraction_service
payload={'source':'fixture-only','format':'json','ontology_id':'fixture-only'}
calls=[]
responses=[
httpx.Response(200,json={'status':'error','error':'Unknown message type: cmd.ontology.validate_schema.v1'}),
httpx.Response(200,json={'status':'success','data':{'is_valid':'false','errors':['fixture denial']}}),
httpx.Response(200,json={'status':'success','data':{}}),
httpx.Response(503,json={'status':'not_configured'}),
]
selected=responses[0]
async def post(url, **kwargs):
calls.append(kwargs['json'])
return selected
with patch.object(Cell2.CONFIG,'exclude_pii_fields',True), patch.object(service,'_load_data',new=AsyncMock(return_value=pd.DataFrame([{'value':7}]))), patch.object(service.schema_service,'get_cell4_endpoint',new=AsyncMock(return_value='http://fixture.invalid/tasks/send')), patch.object(service.http_client,'post',new=post):
for enabled in [True,False]:
with patch.object(Cell2.CONFIG,'enable_cell4_cache',enabled):
for selected in responses:
for repeat in range(2):
calls.clear()
service.schema_service.circuit_breaker.record_success()
response=client.post('/api/v1/extract',json=payload)
assert response.status_code==200, response.text
result=response.json()
assert result['status']=='validation_failed', result
assert result['kg_ingestion_id'] is None, result
assert len(calls)==1, calls
assert calls[0]['message_type']=='cmd.ontology.validate_schema.v1', calls
assert client.post('/api/v1/extract',json={}).status_code==422
assert not attempts, 'external attempt was swallowed'
print('16 actual ASGI denials: cache true/false, repeated Cell4 error/invalid/missing verdict/HTTP503; no KG forwarding')
'''
env = {k: v for k, v in os.environ.items()
if not k.startswith(('GOOGLE_', 'GCP_', 'GCLOUD_', 'FIRESTORE_', 'BIGQUERY_'))}
result = subprocess.run([sys.executable, '-I', '-c', script, str(root)],
cwd=tmp_path, env=env, capture_output=True, text=True, timeout=40)
print(result.stdout)
assert result.returncode == 0, result.stdout + result.stderr