39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
"""Verify the synthetic cohort's opt-in startup and server-side access check."""
|
|
|
|
import pytest
|
|
from flask import Flask
|
|
|
|
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)
|
|
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)
|
|
|
|
|
|
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)
|
|
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)
|