Files
WebApi/tests/test_cama_runtime.py
T
2026-11-13 17:47:37 -06:00

39 lines
1.7 KiB
Python

"""Exercise persistent cohort setup and its optional patient access boundary."""
from flask import Flask, request
from webapi.cama_runtime import configure_cama_cohort
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 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):
"""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}):
assert app.config['CAMA_COHORT_ACCESS'](request)
with app.test_request_context(headers={'X-Cama-Cohort-Token': 'incorrect'}):
assert not app.config['CAMA_COHORT_ACCESS'](request)
def test_application_starts_with_retained_obsolete_configuration(monkeypatch):
"""Old synthetic settings or a bad token cannot prevent the health endpoint starting."""
monkeypatch.setenv('CAMA_COHORT_SYNTHETIC', '1')
monkeypatch.delenv('CAMA_COHORT_SECRET', raising=False)
monkeypatch.setenv('CAMA_COHORT_ACCESS_TOKEN', 'invalid')
from webapi import create_app
client = create_app().test_client()
assert client.get('/health').get_json() == {'status': 'ok'}
assert client.get('/cama/seguimiento').status_code == 403