73 lines
3.8 KiB
Python
73 lines
3.8 KiB
Python
"""Exercise regular and cohort follow-up with an authorized synthetic reader."""
|
|
|
|
from datetime import date
|
|
|
|
import pytest
|
|
from flask import Flask
|
|
|
|
from webapi.cama_analysis import analysis, revision_of
|
|
from webapi.seguimiento_cama import blueprint_seguimiento_cama
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
"""A fixed six-person cohort and a server-controlled access callback."""
|
|
rows = [dict(pac_id=str(n + 1), mastografia_fecha='2026-10-15',
|
|
birads='BIRADS_5', grupo_edad='50-59', biopsia=None,
|
|
biopsia_fecha=None) for n in range(6)]
|
|
app = Flask(__name__)
|
|
app.register_blueprint(analysis, url_prefix='/cama')
|
|
app.register_blueprint(blueprint_seguimiento_cama(), url_prefix='/cama')
|
|
app.config.update(CAMA_COHORT_SYNTHETIC=True, CAMA_COHORT_READER=lambda cutoff: rows,
|
|
CAMA_COHORT_ACCESS=lambda request: request.headers.get('X-Test') == 'yes')
|
|
return app.test_client()
|
|
|
|
|
|
def test_regular_list_detail_and_export_use_the_same_selection(client):
|
|
"""Pagination, detail and export resolve the same filtered persistent cohort."""
|
|
headers = {'X-Test': 'yes'}
|
|
query = '?corte=2026-10-20&meses=3&birads=BIRADS_5'
|
|
page = client.get('/cama/seguimiento' + query + '&page_size=2', headers=headers).get_json()
|
|
assert page['paginacion']['total_items'] == 6
|
|
assert page['cohorte'] == 'cama_birads_v1'
|
|
context = '&cohorte=' + page['cohorte'] + '&escenario=' + page['escenario'] + '&revision=' + page['revision']
|
|
rows = []
|
|
for number in (1, 2, 3):
|
|
rows.extend(client.get('/cama/seguimiento' + query + context + f'&page_size=2&page={number}', headers=headers).get_json()['items'])
|
|
exported = client.get('/cama/seguimiento/exportacion' + query + context, headers=headers).get_json()
|
|
assert exported['items'] == rows
|
|
assert client.get('/cama/pacientes/' + rows[0]['pac_id'] + query + context, headers=headers).get_json()['item'] == rows[0]
|
|
assert client.get('/cama/seguimiento' + query, headers=headers).headers['Cache-Control'] == 'no-store'
|
|
|
|
|
|
def test_regular_followup_checks_access_and_suppression(client):
|
|
"""The ordinary route cannot bypass authorization or suppression."""
|
|
assert client.get('/cama/seguimiento').status_code == 403
|
|
headers = {'X-Test': 'yes'}
|
|
assert client.get('/cama/seguimiento?corte=2026-10-20&birads=BIRADS_4', headers=headers).status_code == 403
|
|
assert client.get('/cama/seguimiento?corte=2026-10-20&estatus=BENIGNO', headers=headers).status_code == 400
|
|
assert client.get('/cama/seguimiento?revision=' + '0' * 64, headers=headers).status_code == 400
|
|
client.application.config['CAMA_COHORT_ACCESS'] = 'invalid'
|
|
assert client.get('/cama/seguimiento', headers=headers).status_code == 403
|
|
|
|
|
|
def test_disabled_provider_and_empty_window(client):
|
|
"""Unavailable sources fail closed and an empty unfiltered window stays empty."""
|
|
headers = {'X-Test': 'yes'}
|
|
body = client.get('/cama/seguimiento?corte=2025-01-01&meses=3', headers=headers).get_json()
|
|
assert body['items'] == [] and body['paginacion']['total_items'] == 0
|
|
client.application.config['CAMA_COHORT_READER'] = None
|
|
assert client.get('/cama/seguimiento', headers=headers).status_code == 503
|
|
|
|
|
|
def test_provider_failure_does_not_break_other_routes(client):
|
|
"""A failing cohort source returns unavailable while the app can serve other requests."""
|
|
def fail(_cutoff):
|
|
raise RuntimeError('provider unavailable')
|
|
|
|
client.application.add_url_rule('/health', 'health', lambda: {'status': 'ok'})
|
|
client.application.config['CAMA_COHORT_READER'] = fail
|
|
assert client.get('/cama/analisis/indicadores?corte=2026-10-20').status_code == 503
|
|
assert client.get('/cama/seguimiento?corte=2026-10-20', headers={'X-Test': 'yes'}).status_code == 503
|
|
assert client.get('/health').get_json() == {'status': 'ok'}
|