81 lines
3.1 KiB
Python
81 lines
3.1 KiB
Python
"""The CapabilityStatement is built from the routes, so a route it cannot describe breaks it for
|
|
every domain. The experimental NCI operation (`$reference-risk`, behind CACU_NCI_REFERENCE) is
|
|
registered whether or not the flag is on, and its `cutoff` and `sample` are operation parameters,
|
|
not search parameters: the statement is served either way, and names no search parameter that
|
|
is not one."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from webapi import fhir
|
|
|
|
DOMAINS = ("cacu", "cama", "tea")
|
|
OPERATION_PARAMETERS = {"cutoff", "sample"}
|
|
|
|
|
|
@pytest.fixture
|
|
def client() -> TestClient:
|
|
app = FastAPI()
|
|
app.include_router(fhir.router) # the router alone: metadata reads the routes, not the data
|
|
return TestClient(app)
|
|
|
|
|
|
def _statement(client: TestClient, domain: str) -> dict:
|
|
response = client.get(f"/{domain}/fhir/metadata")
|
|
assert response.status_code == 200, response.text
|
|
assert response.headers["content-type"].startswith("application/fhir+json")
|
|
body = response.json()
|
|
assert body["resourceType"] == "CapabilityStatement"
|
|
return body
|
|
|
|
|
|
def _resources(statement: dict) -> dict[str, dict]:
|
|
return {r["type"]: r for r in statement["rest"][0]["resource"]}
|
|
|
|
|
|
@pytest.mark.parametrize("flag", [None, "0", "1"])
|
|
@pytest.mark.parametrize("domain", DOMAINS)
|
|
def test_metadata_is_served_with_the_nci_reference_on_or_off(client, monkeypatch, domain, flag):
|
|
if flag is None:
|
|
monkeypatch.delenv(fhir.NCI_REFERENCE, raising=False)
|
|
else:
|
|
monkeypatch.setenv(fhir.NCI_REFERENCE, flag)
|
|
resources = _resources(_statement(client, domain))
|
|
assert "MeasureReport" in resources
|
|
for resource in resources.values():
|
|
codes = [i["code"] for i in resource["interaction"]]
|
|
assert len(codes) == len(set(codes)), f"{resource['type']}: repeated interactions {codes}"
|
|
for param in resource.get("searchParam", []):
|
|
assert param["type"], param
|
|
assert param["name"] not in OPERATION_PARAMETERS, (
|
|
f"{resource['type']}: {param['name']} belongs to the operation, not to a search")
|
|
|
|
|
|
def test_the_flag_does_not_change_what_the_statement_declares(client, monkeypatch):
|
|
def declared(flag: str | None) -> dict:
|
|
if flag is None:
|
|
monkeypatch.delenv(fhir.NCI_REFERENCE, raising=False)
|
|
else:
|
|
monkeypatch.setenv(fhir.NCI_REFERENCE, flag)
|
|
body = _statement(client, "cacu")
|
|
body.pop("date") # the day it was served
|
|
return json.loads(json.dumps(body, sort_keys=True))
|
|
|
|
assert declared(None) == declared("1")
|
|
|
|
|
|
@pytest.mark.parametrize("domain", ["cama", "tea"])
|
|
def test_the_nci_operation_stays_cacu_only_when_enabled(client, monkeypatch, domain):
|
|
monkeypatch.setenv(fhir.NCI_REFERENCE, "1")
|
|
response = client.get(f"/{domain}/fhir/Patient/any/$reference-risk")
|
|
assert response.status_code == 404
|
|
|
|
|
|
def test_the_nci_operation_is_refused_while_the_flag_is_off(client, monkeypatch):
|
|
monkeypatch.delenv(fhir.NCI_REFERENCE, raising=False)
|
|
response = client.get("/cacu/fhir/Patient/any/$reference-risk")
|
|
assert response.status_code == 404
|