Use source patient identity for breast follow-up

This commit is contained in:
Angel Capetillo Zavala
2026-11-13 17:38:19 -06:00
parent 2ca86232c9
commit eeb55c4255
11 changed files with 73 additions and 110 deletions
+12 -13
View File
@@ -57,7 +57,7 @@ def client(cohort_db):
app.register_blueprint(blueprint_seguimiento_cama(), url_prefix='/cama')
def read(cutoff):
with Session(cohort_db) as session:
return CamaCohort(session, b'x' * 32).rows(cutoff)
return CamaCohort(session).rows(cutoff)
app.config.update(CAMA_COHORT_SYNTHETIC=True, CAMA_COHORT_READER=read, CAMA_COHORT_ACCESS=lambda req: True)
return app.test_client()
@@ -84,7 +84,7 @@ def test_aggregate_list_detail_and_export_have_one_selection(client):
exported = client.get('/cama/seguimiento/exportacion' + selection).get_json()
assert exported['items'] == items and exported['total_items'] == 6
for item in items:
detail = client.get('/cama/pacientes/' + item['id_seudonimo'] + selection).get_json()
detail = client.get('/cama/pacientes/' + item['pac_id'] + selection).get_json()
assert detail['item'] == item and detail['corte'] == aggregate['corte']
assert sum(item['biopsia'] == 'BENIGNO' for item in items) == 1
assert client.get('/cama/seguimiento' + selection).get_json()['items'] == items
@@ -115,16 +115,15 @@ def test_cutoff_excludes_later_clinical_and_load_dates(client):
def test_links_and_load_dates_are_distinct(client, cohort_db):
"""A missing link and a late load cannot supply a diagnosis before cutoff."""
with Session(cohort_db) as session:
ids = {i: CamaCohort(session, b'x' * 32)._id(i) for i in range(1, 5)}
ids = {i: str(i) for i in range(1, 5)}
data = client.get('/cama/seguimiento' + query(client)).get_json()
rows = {item['id_seudonimo']: item for item in data['items']}
rows = {item['pac_id']: item for item in data['items']}
assert rows[ids[1]]['biopsia'] == 'BENIGNO'
assert rows[ids[2]]['biopsia'] is None and rows[ids[2]]['biopsia_fecha'] is None
assert rows[ids[3]]['biopsia'] is None and rows[ids[3]]['biopsia_fecha'] == '2025-02-15'
assert rows[ids[4]]['biopsia'] is None and rows[ids[4]]['biopsia_fecha'] is None
later = client.get('/cama/seguimiento' + query(client, '2027-12-01')).get_json()
rows_later = {item['id_seudonimo']: item for item in later['items']}
rows_later = {item['pac_id']: item for item in later['items']}
assert rows_later[ids[3]]['biopsia'] == 'BENIGNO'
assert rows_later[ids[4]]['biopsia_fecha'] == '2027-02-15'
assert set(rows) == set(rows_later)
@@ -143,11 +142,11 @@ def test_full_application_registers_cohort_and_regular_followup(client):
def test_windows_and_disabled_provider(client):
"""Regular windows remain available and the reader is opt-in."""
"""Regular windows remain available and a missing reader returns unavailable."""
selection = query(client).replace('&meses=0', '&meses=1').replace('&birads=BIRADS_5', '')
body = client.get('/cama/seguimiento' + selection).get_json()
assert body['items'] == [] and body['ventana']['desde'] == '2026-09-21'
client.application.config['CAMA_COHORT_SYNTHETIC'] = False
client.application.config['CAMA_COHORT_READER'] = None
assert client.get('/cama/analisis/indicadores?corte=2026-10-20').status_code == 503
@@ -178,8 +177,8 @@ def test_cross_patient_links_and_observations_are_not_used(cohort_db):
# The BI-RADS observation belongs to a different patient.
session.get(TABLAS.Observacion, 6).pac_id = 5
session.commit()
reader = CamaCohort(session, b'x' * 32)
rows = {row['id_seudonimo']: row for row in reader.rows(date(2027, 12, 1))}
assert reader._id(6) not in rows
assert rows[reader._id(1)]['biopsia'] is None
assert rows[reader._id(3)]['biopsia'] is None
reader = CamaCohort(session)
rows = {row['pac_id']: row for row in reader.rows(date(2027, 12, 1))}
assert '6' not in rows
assert rows['1']['biopsia'] is None
assert rows['3']['biopsia'] is None
+9 -21
View File
@@ -1,38 +1,26 @@
"""Verify the synthetic cohort's opt-in startup and server-side access check."""
"""Exercise persistent cohort setup and its optional patient access boundary."""
import pytest
from flask import Flask
from flask import Flask, request
from webapi.cama_runtime import configure_cama_cohort
def test_synthetic_cohort_requires_explicit_configuration(monkeypatch):
"""Missing or short secrets must not silently enable patient access."""
monkeypatch.delenv('CAMA_COHORT_SYNTHETIC', raising=False)
def test_missing_or_invalid_token_does_not_prevent_startup(monkeypatch):
"""A malformed optional credential leaves the reader registered and access denied."""
monkeypatch.setenv('CAMA_COHORT_ACCESS_TOKEN', 'short')
app = Flask(__name__)
configure_cama_cohort(app)
assert 'CAMA_COHORT_READER' not in app.config
monkeypatch.setenv('CAMA_COHORT_SYNTHETIC', '1')
monkeypatch.setenv('CAMA_COHORT_SECRET', 'short')
monkeypatch.setenv('CAMA_COHORT_ACCESS_TOKEN', 't' * 32)
with pytest.raises(ValueError):
configure_cama_cohort(app)
monkeypatch.setenv('CAMA_COHORT_SECRET', 's' * 32)
monkeypatch.setenv('CAMA_COHORT_ACCESS_TOKEN', 't' * 32 + '";')
with pytest.raises(ValueError):
configure_cama_cohort(app)
assert callable(app.config['CAMA_COHORT_READER'])
with app.test_request_context(headers={'X-Cama-Cohort-Token': 'short'}):
assert not app.config['CAMA_COHORT_ACCESS'](request)
def test_access_token_is_checked_on_the_server(monkeypatch):
"""Only requests bearing the configured token can use the cohort patient routes."""
monkeypatch.setenv('CAMA_COHORT_SYNTHETIC', '1')
monkeypatch.setenv('CAMA_COHORT_SECRET', 's' * 32)
"""The retained proxy credential gates patient routes until a shared boundary is available."""
monkeypatch.setenv('CAMA_COHORT_ACCESS_TOKEN', 't' * 32)
app = Flask(__name__)
configure_cama_cohort(app)
with app.test_request_context(headers={'X-Cama-Cohort-Token': 't' * 32}):
from flask import request
assert app.config['CAMA_COHORT_ACCESS'](request)
with app.test_request_context(headers={'X-Cama-Cohort-Token': 'incorrect'}):
from flask import request
assert not app.config['CAMA_COHORT_ACCESS'](request)
+15 -3
View File
@@ -12,7 +12,7 @@ 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(id_seudonimo=f'patient-{n}', mastografia_fecha='2026-10-15',
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__)
@@ -36,7 +36,7 @@ def test_regular_list_detail_and_export_use_the_same_selection(client):
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]['id_seudonimo'] + query + context, headers=headers).get_json()['item'] == rows[0]
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'
@@ -54,5 +54,17 @@ def test_disabled_provider_and_empty_window(client):
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_SYNTHETIC'] = False
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'}