From eca0631a388d5c65b3d189dd78efa2ec4d6ad050 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 14 Jan 2026 13:05:39 +0000 Subject: [PATCH 01/27] [GPCAPIM-254]: Lift and shift of lambda functionality in to a Flask app. --- Makefile | 3 +- gateway-api/openapi.yaml | 241 +----------------- gateway-api/poetry.lock | 129 +++++++++- gateway-api/pyproject.toml | 2 + gateway-api/src/gateway_api/app.py | 51 ++++ ...GatewayAPIConsumer-GatewayAPIProvider.json | 12 +- .../tests/contract/test_consumer_contract.py | 2 +- gateway-api/tests/integration/test_main.py | 2 +- infrastructure/images/gateway-api/Dockerfile | 12 +- 9 files changed, 198 insertions(+), 256 deletions(-) create mode 100644 gateway-api/src/gateway_api/app.py diff --git a/Makefile b/Makefile index 3e634518..598014d2 100644 --- a/Makefile +++ b/Makefile @@ -34,9 +34,8 @@ build-gateway-api: dependencies @poetry run mypy --no-namespace-packages . @echo "Packaging dependencies..." @poetry build --format=wheel - @pip install "dist/gateway_api-0.1.0-py3-none-any.whl" --target "./target/gateway-api" --platform musllinux_1_1_x86_64 --only-binary=:all: + @pip install "dist/gateway_api-0.1.0-py3-none-any.whl" --target "./target/gateway-api" --platform musllinux_1_2_x86_64 --only-binary=:all: # Copy main file separately as it is not included within the package. - @cp lambda_handler.py ./target/gateway-api/ @rm -rf ../infrastructure/images/gateway-api/resources/build/ @mkdir ../infrastructure/images/gateway-api/resources/build/ @cp -r ./target/gateway-api ../infrastructure/images/gateway-api/resources/build/ diff --git a/gateway-api/openapi.yaml b/gateway-api/openapi.yaml index b6799f7c..df06978c 100644 --- a/gateway-api/openapi.yaml +++ b/gateway-api/openapi.yaml @@ -15,7 +15,7 @@ paths: description: Returns a simple hello world message operationId: postHelloWorld requestBody: - required: false + required: true content: application/json: schema: @@ -24,247 +24,12 @@ paths: payload: type: string description: The payload to be processed + example: "Alex" responses: '200': description: Successful response content: - text/plain: - schema: - type: object - properties: - status_code: - type: integer - description: Status code of the interaction - body: - type: string - description: The output of the interaction - errorMessage: - type: string - description: Any error messages relating to errors encountered with the interaction - errorType: - type: string - description: The type of error encountered during the interaction, if an error has occurred - requestId: - type: string - format: uuid - description: The unique request ID for the interaction - stacktrace: - type: array - items: - type: string - description: The stack trace of the error, if an error has occurred - get: - summary: Get hello world message - description: Returns a simple hello world message - operationId: postHelloWorld - requestBody: - required: false - content: - application/json: - schema: - type: object - properties: - payload: - type: string - description: The payload to be processed - responses: - '200': - description: Successful response - content: - text/plain: - schema: - type: object - properties: - status_code: - type: integer - description: Status code of the interaction - body: - type: string - description: The output of the interaction - errorMessage: - type: string - description: Any error messages relating to errors encountered with the interaction - errorType: - type: string - description: The type of error encountered during the interaction, if an error has occurred - requestId: - type: string - format: uuid - description: The unique request ID for the interaction - stacktrace: - type: array - items: - type: string - description: The stack trace of the error, if an error has occurred - - '404': - description: Route not found - content: - text/html: - schema: - type: string - put: - summary: Get hello world message - description: Returns a simple hello world message - operationId: postHelloWorld - requestBody: - required: false - content: - application/json: - schema: - type: object - properties: - payload: - type: string - description: The payload to be processed - responses: - '200': - description: Successful response - content: - text/plain: - schema: - type: object - properties: - status_code: - type: integer - description: Status code of the interaction - body: - type: string - description: The output of the interaction - errorMessage: - type: string - description: Any error messages relating to errors encountered with the interaction - errorType: - type: string - description: The type of error encountered during the interaction, if an error has occurred - requestId: - type: string - format: uuid - description: The unique request ID for the interaction - stacktrace: - type: array - items: - type: string - description: The stack trace of the error, if an error has occurred - - '404': - description: Route not found - content: - text/html: - schema: - type: string - patch: - summary: Get hello world message - description: Returns a simple hello world message - operationId: postHelloWorld - requestBody: - required: false - content: - application/json: - schema: - type: object - properties: - payload: - type: string - description: The payload to be processed - responses: - '200': - description: Successful response - content: - text/plain: - schema: - type: object - properties: - status_code: - type: integer - description: Status code of the interaction - body: - type: string - description: The output of the interaction - errorMessage: - type: string - description: Any error messages relating to errors encountered with the interaction - errorType: - type: string - description: The type of error encountered during the interaction, if an error has occurred - requestId: - type: string - format: uuid - description: The unique request ID for the interaction - stacktrace: - type: array - items: - type: string - description: The stack trace of the error, if an error has occurred - - '404': - description: Route not found - content: - text/html: - schema: - type: string - delete: - summary: Get hello world message - description: Returns a simple hello world message - operationId: postHelloWorld - requestBody: - required: false - content: - application/json: - schema: - type: object - properties: - payload: - type: string - description: The payload to be processed - responses: - '200': - description: Successful response - content: - text/plain: - schema: - type: object - properties: - status_code: - type: integer - description: Status code of the interaction - body: - type: string - description: The output of the interaction - errorMessage: - type: string - description: Any error messages relating to errors encountered with the interaction - errorType: - type: string - description: The type of error encountered during the interaction, if an error has occurred - requestId: - type: string - format: uuid - description: The unique request ID for the interaction - stacktrace: - type: array - items: - type: string - description: The stack trace of the error, if an error has occurred - trace: - summary: Get hello world message - description: Returns a simple hello world message - operationId: postHelloWorld - requestBody: - required: false - content: - application/json: - schema: - type: object - properties: - payload: - type: string - description: The payload to be processed - responses: - '200': - description: Successful response - content: - text/plain: + application/json: schema: type: object properties: diff --git a/gateway-api/poetry.lock b/gateway-api/poetry.lock index 338577d4..8ec2ddde 100644 --- a/gateway-api/poetry.lock +++ b/gateway-api/poetry.lock @@ -63,6 +63,18 @@ files = [ {file = "backoff-2.2.1.tar.gz", hash = "sha256:03f829f5bb1923180821643f8753b0502c3b682293992485b0eef2807afa5cba"}, ] +[[package]] +name = "blinker" +version = "1.9.0" +description = "Fast, simple object-to-object and broadcast signaling" +optional = false +python-versions = ">=3.9" +groups = ["main"] +files = [ + {file = "blinker-1.9.0-py3-none-any.whl", hash = "sha256:ba0efaa9080b619ff2f3459d1d500c57bddea4a6b424b60a91141db6fd2f08bc"}, + {file = "blinker-1.9.0.tar.gz", hash = "sha256:b4ce2265a7abece45e7cc896e98dbebe6cead56bcf805a3d23136d145f5445bf"}, +] + [[package]] name = "certifi" version = "2025.11.12" @@ -301,7 +313,7 @@ version = "8.3.0" description = "Composable command line interface toolkit" optional = false python-versions = ">=3.10" -groups = ["dev"] +groups = ["main", "dev"] files = [ {file = "click-8.3.0-py3-none-any.whl", hash = "sha256:9b9f285302c6e3064f4330c05f05b81945b2a39544279343e6e7c5f27a9baddc"}, {file = "click-8.3.0.tar.gz", hash = "sha256:e7b8232224eba16f4ebe410c25ced9f7875cb5f3263ffc93cc3e8da705e229c4"}, @@ -332,11 +344,12 @@ version = "0.4.6" description = "Cross-platform colored terminal text." optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" -groups = ["dev"] +groups = ["main", "dev"] files = [ {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +markers = {main = "platform_system == \"Windows\""} [[package]] name = "coverage" @@ -443,6 +456,30 @@ files = [ [package.extras] toml = ["tomli ; python_full_version <= \"3.11.0a6\""] +[[package]] +name = "flask" +version = "3.1.2" +description = "A simple framework for building complex web applications." +optional = false +python-versions = ">=3.9" +groups = ["main"] +files = [ + {file = "flask-3.1.2-py3-none-any.whl", hash = "sha256:ca1d8112ec8a6158cc29ea4858963350011b5c846a414cdb7a954aa9e967d03c"}, + {file = "flask-3.1.2.tar.gz", hash = "sha256:bf656c15c80190ed628ad08cdfd3aaa35beb087855e2f494910aa3774cc4fd87"}, +] + +[package.dependencies] +blinker = ">=1.9.0" +click = ">=8.1.3" +itsdangerous = ">=2.2.0" +jinja2 = ">=3.1.2" +markupsafe = ">=2.1.1" +werkzeug = ">=3.1.0" + +[package.extras] +async = ["asgiref (>=3.2)"] +dotenv = ["python-dotenv"] + [[package]] name = "fqdn" version = "1.5.1" @@ -668,13 +705,25 @@ files = [ [package.dependencies] arrow = ">=0.15.0" +[[package]] +name = "itsdangerous" +version = "2.2.0" +description = "Safely pass data to untrusted environments and back." +optional = false +python-versions = ">=3.8" +groups = ["main"] +files = [ + {file = "itsdangerous-2.2.0-py3-none-any.whl", hash = "sha256:c6242fc49e35958c8b15141343aa660db5fc54d4f13a1db01a3f5891b98700ef"}, + {file = "itsdangerous-2.2.0.tar.gz", hash = "sha256:e0050c0b7da1eea53ffaf149c0cfbb5c6e2e2b69c4bef22c81fa6eb73e5f6173"}, +] + [[package]] name = "jinja2" version = "3.1.6" description = "A very fast and expressive template engine." optional = false python-versions = ">=3.7" -groups = ["dev"] +groups = ["main", "dev"] files = [ {file = "jinja2-3.1.6-py3-none-any.whl", hash = "sha256:85ece4451f492d0c13c5dd7c13a64681a86afae63a5f347908daf103ce6d2f67"}, {file = "jinja2-3.1.6.tar.gz", hash = "sha256:0137fb05990d35f1275a587e9aee6d56da821fc83491a0fb838183be43f66d6d"}, @@ -808,7 +857,7 @@ version = "3.0.3" description = "Safely add untrusted strings to HTML/XML markup." optional = false python-versions = ">=3.9" -groups = ["dev"] +groups = ["main", "dev"] files = [ {file = "markupsafe-3.0.3-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:2f981d352f04553a7171b8e44369f2af4055f888dfb147d55e42d29e29e74559"}, {file = "markupsafe-3.0.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:e1c1493fb6e50ab01d20a22826e57520f1284df32f2d8601fdd90b6304601419"}, @@ -1981,6 +2030,62 @@ files = [ requests = "*" starlette = ">=0.20.1" +[[package]] +name = "types-click" +version = "7.1.8" +description = "Typing stubs for click" +optional = false +python-versions = "*" +groups = ["main"] +files = [ + {file = "types-click-7.1.8.tar.gz", hash = "sha256:b6604968be6401dc516311ca50708a0a28baa7a0cb840efd7412f0dbbff4e092"}, + {file = "types_click-7.1.8-py3-none-any.whl", hash = "sha256:8cb030a669e2e927461be9827375f83c16b8178c365852c060a34e24871e7e81"}, +] + +[[package]] +name = "types-flask" +version = "1.1.6" +description = "Typing stubs for Flask" +optional = false +python-versions = "*" +groups = ["main"] +files = [ + {file = "types-Flask-1.1.6.tar.gz", hash = "sha256:aac777b3abfff9436e6b01f6d08171cf23ea6e5be71cbf773aaabb1c5763e9cf"}, + {file = "types_Flask-1.1.6-py3-none-any.whl", hash = "sha256:6ab8a9a5e258b76539d652f6341408867298550b19b81f0e41e916825fc39087"}, +] + +[package.dependencies] +types-click = "*" +types-Jinja2 = "*" +types-Werkzeug = "*" + +[[package]] +name = "types-jinja2" +version = "2.11.9" +description = "Typing stubs for Jinja2" +optional = false +python-versions = "*" +groups = ["main"] +files = [ + {file = "types-Jinja2-2.11.9.tar.gz", hash = "sha256:dbdc74a40aba7aed520b7e4d89e8f0fe4286518494208b35123bcf084d4b8c81"}, + {file = "types_Jinja2-2.11.9-py3-none-any.whl", hash = "sha256:60a1e21e8296979db32f9374d8a239af4cb541ff66447bb915d8ad398f9c63b2"}, +] + +[package.dependencies] +types-MarkupSafe = "*" + +[[package]] +name = "types-markupsafe" +version = "1.1.10" +description = "Typing stubs for MarkupSafe" +optional = false +python-versions = "*" +groups = ["main"] +files = [ + {file = "types-MarkupSafe-1.1.10.tar.gz", hash = "sha256:85b3a872683d02aea3a5ac2a8ef590193c344092032f58457287fbf8e06711b1"}, + {file = "types_MarkupSafe-1.1.10-py3-none-any.whl", hash = "sha256:ca2bee0f4faafc45250602567ef38d533e877d2ddca13003b319c551ff5b3cc5"}, +] + [[package]] name = "types-pyyaml" version = "6.0.12.20250915" @@ -2008,6 +2113,18 @@ files = [ [package.dependencies] urllib3 = ">=2" +[[package]] +name = "types-werkzeug" +version = "1.0.9" +description = "Typing stubs for Werkzeug" +optional = false +python-versions = "*" +groups = ["main"] +files = [ + {file = "types-Werkzeug-1.0.9.tar.gz", hash = "sha256:5cc269604c400133d452a40cee6397655f878fc460e03fde291b9e3a5eaa518c"}, + {file = "types_Werkzeug-1.0.9-py3-none-any.whl", hash = "sha256:194bd5715a13c598f05c63e8a739328657590943bce941e8a3619a6b5d4a54ec"}, +] + [[package]] name = "typing-extensions" version = "4.15.0" @@ -2083,7 +2200,7 @@ version = "3.1.5" description = "The comprehensive WSGI web application library." optional = false python-versions = ">=3.9" -groups = ["dev"] +groups = ["main", "dev"] files = [ {file = "werkzeug-3.1.5-py3-none-any.whl", hash = "sha256:5111e36e91086ece91f93268bb39b4a35c1e6f1feac762c9c822ded0a4e322dc"}, {file = "werkzeug-3.1.5.tar.gz", hash = "sha256:6a548b0e88955dd07ccb25539d7d0cc97417ee9e179677d22c7041c8f078ce67"}, @@ -2243,4 +2360,4 @@ propcache = ">=0.2.1" [metadata] lock-version = "2.1" python-versions = ">3.13,<4.0.0" -content-hash = "67e8839de72625c8f7c4d42aea6ea55afaf9f738aef2267bb4dac2f83a389f8e" +content-hash = "30cdb09db37902c7051aa190c1e4c374dbfa6a14ca0c69131c0295ee33e7338f" diff --git a/gateway-api/pyproject.toml b/gateway-api/pyproject.toml index 2242551f..87f86635 100644 --- a/gateway-api/pyproject.toml +++ b/gateway-api/pyproject.toml @@ -10,6 +10,8 @@ requires-python = ">3.13,<4.0.0" [tool.poetry.dependencies] clinical-data-common = { git = "https://github.com/NHSDigital/clinical-data-common.git", tag = "v0.1.0" } +flask = "^3.1.2" +types-flask = "^1.1.6" [tool.poetry] packages = [{include = "gateway_api", from = "src"}, diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py new file mode 100644 index 00000000..b769ad61 --- /dev/null +++ b/gateway-api/src/gateway_api/app.py @@ -0,0 +1,51 @@ +from typing import Any, TypedDict + +from flask import Flask, request + +from gateway_api.handler import User, greet + +app = Flask(__name__) + + +class APIMResponse[T](TypedDict): + """A API Management response including a body with a generic type.""" + + statusCode: int + headers: dict[str, str] + body: T + + +@app.route("/2015-03-31/functions/function/invocations", methods=["POST"]) +def greet_endpoint() -> APIMResponse[str | dict[str, str]]: + """Greet endpoint that replicates the lambda handler functionality.""" + data = request.get_json(force=True) + if "payload" not in data: + return _with_default_headers(status_code=400, body="Name is required") + + name = data["payload"] + if not name: + return _with_default_headers(status_code=400, body="Name cannot be empty") + user = User(name=name) + + try: + return _with_default_headers(status_code=200, body=f"{greet(user)}") + except ValueError: + return _with_default_headers( + status_code=404, body=f"Provided name cannot be found. name={name}" + ) + + +def _with_default_headers[T](status_code: int, body: T) -> APIMResponse[T]: + return APIMResponse( + statusCode=status_code, headers={"Content-Type": "application/json"}, body=body + ) + + +@app.route("/health", methods=["GET"]) +def health_check() -> APIMResponse[dict[str, Any]]: + """Health check endpoint.""" + return _with_default_headers(status_code=200, body={"status": "healthy"}) + + +if __name__ == "__main__": + app.run(host="gateway-api", port=8080) diff --git a/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json b/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json index 681c19d7..dd9af038 100644 --- a/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json +++ b/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json @@ -36,13 +36,19 @@ }, "response": { "body": { - "content": "{\"statusCode\": 200, \"headers\": {\"Content-Type\": \"application/json\"}, \"body\": \"Hello, World!\"}", - "contentType": "text/plain;charset=utf-8", + "content": { + "body": "Hello, World!", + "headers": { + "Content-Type": "application/json" + }, + "statusCode": 200 + }, + "contentType": "application/json", "encoded": false }, "headers": { "Content-Type": [ - "text/plain;charset=utf-8" + "application/json" ] }, "status": 200 diff --git a/gateway-api/tests/contract/test_consumer_contract.py b/gateway-api/tests/contract/test_consumer_contract.py index ac0d11d1..e8b46c1c 100644 --- a/gateway-api/tests/contract/test_consumer_contract.py +++ b/gateway-api/tests/contract/test_consumer_contract.py @@ -36,7 +36,7 @@ def test_get_hello_world(self) -> None: "headers": {"Content-Type": "application/json"}, "body": "Hello, World!", }, - content_type="text/plain;charset=utf-8", + content_type="application/json", ) ) diff --git a/gateway-api/tests/integration/test_main.py b/gateway-api/tests/integration/test_main.py index 18c71e09..0ccb3786 100644 --- a/gateway-api/tests/integration/test_main.py +++ b/gateway-api/tests/integration/test_main.py @@ -19,7 +19,7 @@ def test_hello_world_returns_correct_message(self, client: Client) -> None: def test_hello_world_content_type(self, client: Client) -> None: """Test that the response has the correct content type.""" response = client.send("world") - assert "text/plain" in response.headers["Content-Type"] + assert "application/json" in response.headers["Content-Type"] def test_nonexistent_returns_error(self, client: Client) -> None: """Test that non-existent routes return 404.""" diff --git a/infrastructure/images/gateway-api/Dockerfile b/infrastructure/images/gateway-api/Dockerfile index 121dc611..a3caaf61 100644 --- a/infrastructure/images/gateway-api/Dockerfile +++ b/infrastructure/images/gateway-api/Dockerfile @@ -1,11 +1,13 @@ # Retrieve the python version from build arguments, deliberately set to "invalid" by default to highlight when no version is provided when building the container. ARG PYTHON_VERSION=invalid -# Use the specified python version to retrieve the required base lambda image. -ARG url=public.ecr.aws/lambda/python:${PYTHON_VERSION} -FROM $url +FROM python:${PYTHON_VERSION}-slim AS gateway-api COPY resources/ /resources -COPY /resources/build/gateway-api ${LAMBDA_TASK_ROOT} +WORKDIR /resources/build/gateway-api + +ENV PYTHONPATH=/resources/build/gateway-api + +ENTRYPOINT ["python"] +CMD ["gateway_api/app.py"] -CMD [ "lambda_handler.handler" ] From 11f39d33f0809de1c612646f032b7d3013705393 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 14 Jan 2026 13:37:35 +0000 Subject: [PATCH 02/27] [GPCAPIM-254]: Lambda is no longer being used; move actions away from it. --- .github/actions/start-app/action.yaml | 50 +++++++++++++++++++++++++++ .github/workflows/stage-2-test.yaml | 16 ++++----- 2 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 .github/actions/start-app/action.yaml diff --git a/.github/actions/start-app/action.yaml b/.github/actions/start-app/action.yaml new file mode 100644 index 00000000..414e52f6 --- /dev/null +++ b/.github/actions/start-app/action.yaml @@ -0,0 +1,50 @@ +name: "Start local app" +description: "Start Flask app that will handle requests" +inputs: + deploy-command: + description: "Command to start app" + required: false + default: "make deploy" + health-path: + description: "Health probe path to POST" + required: false + default: "/2015-03-31/functions/function/invocations" + max-seconds: + description: "Maximum seconds to wait for readiness" + required: false + default: "60" +python-version: + description: "Python version to install" + required: true +runs: + using: "composite" + steps: + - name: "Start app" + shell: bash + env: + PYTHON_VERSION: ${{ inputs.python-version }} + run: | + set -euo pipefail + echo "Starting app: '${{ inputs.deploy-command }}'" + nohup ${{ inputs.deploy-command }} >/tmp/app.log 2>&1 & + echo $! > /tmp/app.pid + echo "PID: $(cat /tmp/app.pid)" + - name: "Wait for app to be ready" + shell: bash + run: | + set -euo pipefail + BASE_URL="${BASE_URL:-http://localhost:5000}" + HEALTH_URL="${BASE_URL}${{ inputs.health-path }}" + MAX="${{ inputs.max-seconds }}" + echo "Waiting for app at ${HEALTH_URL} (max ${MAX}s)..." + for i in $(seq 1 "${MAX}"); do + if curl -sSf -X POST "${HEALTH_URL}" -d '{}' >/dev/null; then + echo "App is ready" + exit 0 + fi + sleep 1 + done + echo "App did not become ready in time" + echo "---- recent app log ----" + tail -n 200 /tmp/app.log || true + exit 1 diff --git a/.github/workflows/stage-2-test.yaml b/.github/workflows/stage-2-test.yaml index 66e4a240..32a5fd2b 100644 --- a/.github/workflows/stage-2-test.yaml +++ b/.github/workflows/stage-2-test.yaml @@ -68,8 +68,8 @@ jobs: uses: ./.github/actions/setup-python-project with: python-version: ${{ inputs.python_version }} - - name: "Start local Lambda" - uses: ./.github/actions/start-local-lambda + - name: "Start app" + uses: ./.github/actions/start-app with: python-version: ${{ inputs.python_version }} - name: "Run contract tests" @@ -98,8 +98,8 @@ jobs: uses: ./.github/actions/setup-python-project with: python-version: ${{ inputs.python_version }} - - name: "Start local Lambda" - uses: ./.github/actions/start-local-lambda + - name: "Start app" + uses: ./.github/actions/start-app with: python-version: ${{ inputs.python_version }} - name: "Run schema validation tests" @@ -128,8 +128,8 @@ jobs: uses: ./.github/actions/setup-python-project with: python-version: ${{ inputs.python_version }} - - name: "Start local Lambda" - uses: ./.github/actions/start-local-lambda + - name: "Start app" + uses: ./.github/actions/start-app with: python-version: ${{ inputs.python_version }} - name: "Run integration test" @@ -158,8 +158,8 @@ jobs: uses: ./.github/actions/setup-python-project with: python-version: ${{ inputs.python_version }} - - name: "Start local Lambda" - uses: ./.github/actions/start-local-lambda + - name: "Start app" + uses: ./.github/actions/start-app with: python-version: ${{ inputs.python_version }} max-seconds: 90 From 671538411ea3276672b97cffec9a10b2054b6c9e Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 14 Jan 2026 13:47:15 +0000 Subject: [PATCH 03/27] [GPCAPIM-254]: Github not picking up unindented input --- .github/actions/start-app/action.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/actions/start-app/action.yaml b/.github/actions/start-app/action.yaml index 414e52f6..97a88061 100644 --- a/.github/actions/start-app/action.yaml +++ b/.github/actions/start-app/action.yaml @@ -13,9 +13,9 @@ inputs: description: "Maximum seconds to wait for readiness" required: false default: "60" -python-version: - description: "Python version to install" - required: true + python-version: + description: "Python version to install" + required: true runs: using: "composite" steps: From 5e15c15fa8cfa1daba49e84f575771e885e0096f Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 14 Jan 2026 13:47:15 +0000 Subject: [PATCH 04/27] [GPCAPIM-254]: Github not picking up unindented input --- .github/actions/start-app/action.yaml | 6 +++--- gateway-api/src/gateway_api/app.py | 6 +++++- infrastructure/images/gateway-api/Dockerfile | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/actions/start-app/action.yaml b/.github/actions/start-app/action.yaml index 414e52f6..97a88061 100644 --- a/.github/actions/start-app/action.yaml +++ b/.github/actions/start-app/action.yaml @@ -13,9 +13,9 @@ inputs: description: "Maximum seconds to wait for readiness" required: false default: "60" -python-version: - description: "Python version to install" - required: true + python-version: + description: "Python version to install" + required: true runs: using: "composite" steps: diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index b769ad61..ce881c77 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -1,3 +1,4 @@ +import os from typing import Any, TypedDict from flask import Flask, request @@ -48,4 +49,7 @@ def health_check() -> APIMResponse[dict[str, Any]]: if __name__ == "__main__": - app.run(host="gateway-api", port=8080) + host = os.getenv("FLASK_HOST") + if host is None: + raise RuntimeError("FLASK_HOST environment variable is not set.") + app.run(host=host, port=8080) diff --git a/infrastructure/images/gateway-api/Dockerfile b/infrastructure/images/gateway-api/Dockerfile index a3caaf61..f3ce577f 100644 --- a/infrastructure/images/gateway-api/Dockerfile +++ b/infrastructure/images/gateway-api/Dockerfile @@ -7,6 +7,7 @@ COPY resources/ /resources WORKDIR /resources/build/gateway-api ENV PYTHONPATH=/resources/build/gateway-api +ENV FLASK_HOST="0.0.0.0" ENTRYPOINT ["python"] CMD ["gateway_api/app.py"] From f0be4f018e63fb907f099315022b76df1388db94 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 14 Jan 2026 16:29:42 +0000 Subject: [PATCH 05/27] [GPCAPIM-254]: Github not picking up unindented input --- gateway-api/src/gateway_api/test_app.py | 122 ++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 gateway-api/src/gateway_api/test_app.py diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py new file mode 100644 index 00000000..23dd2d96 --- /dev/null +++ b/gateway-api/src/gateway_api/test_app.py @@ -0,0 +1,122 @@ +"""Unit tests for the Flask app endpoints.""" + +import pytest +from flask.testing import FlaskClient + +from gateway_api.app import app + + +@pytest.fixture +def client() -> FlaskClient: + """Create a Flask test client.""" + app.config["TESTING"] = True + with app.test_client() as client: + yield client + + +class TestGreetEndpoint: + """Unit tests for the greet_endpoint function.""" + + def test_greet_endpoint_returns_greeting_for_valid_name( + self, client: FlaskClient + ) -> None: + """Test that greet_endpoint returns a greeting for a valid name.""" + response = client.post( + "/2015-03-31/functions/function/invocations", + json={"payload": "Alice"}, + ) + + assert response.status_code == 200 + data = response.get_json() + assert data["statusCode"] == 200 + assert data["headers"]["Content-Type"] == "application/json" + assert "Alice" in data["body"] + assert data["body"].endswith("!") + + def test_greet_endpoint_returns_400_when_payload_missing( + self, client: FlaskClient + ) -> None: + """Test that greet_endpoint returns 400 when payload is missing.""" + response = client.post( + "/2015-03-31/functions/function/invocations", + json={}, + ) + + assert response.status_code == 200 + data = response.get_json() + assert data["statusCode"] == 400 + assert data["body"] == "Name is required" + assert data["headers"]["Content-Type"] == "application/json" + + def test_greet_endpoint_returns_400_when_name_is_empty( + self, client: FlaskClient + ) -> None: + """Test that greet_endpoint returns 400 when name is empty.""" + response = client.post( + "/2015-03-31/functions/function/invocations", + json={"payload": ""}, + ) + + assert response.status_code == 200 + data = response.get_json() + assert data["statusCode"] == 400 + assert data["body"] == "Name cannot be empty" + assert data["headers"]["Content-Type"] == "application/json" + + def test_greet_endpoint_returns_404_for_nonexistent_user( + self, client: FlaskClient + ) -> None: + """Test that greet_endpoint returns 404 for nonexistent user.""" + response = client.post( + "/2015-03-31/functions/function/invocations", + json={"payload": "nonexistent"}, + ) + + assert response.status_code == 200 + data = response.get_json() + assert data["statusCode"] == 404 + assert "cannot be found" in data["body"] + assert "nonexistent" in data["body"] + assert data["headers"]["Content-Type"] == "application/json" + + def test_greet_endpoint_returns_400_when_name_is_none( + self, client: FlaskClient + ) -> None: + """Test that greet_endpoint returns 400 when name is None.""" + response = client.post( + "/2015-03-31/functions/function/invocations", + json={"payload": None}, + ) + + assert response.status_code == 200 + data = response.get_json() + assert data["statusCode"] == 400 + assert data["body"] == "Name cannot be empty" + assert data["headers"]["Content-Type"] == "application/json" + + +class TestHealthCheck: + """Unit tests for the health_check function.""" + + def test_health_check_returns_200_and_healthy_status( + self, client: FlaskClient + ) -> None: + """Test that health_check returns 200 with healthy status.""" + response = client.get("/health") + + assert response.status_code == 200 + data = response.get_json() + assert data["statusCode"] == 200 + assert data["body"]["status"] == "healthy" + assert data["headers"]["Content-Type"] == "application/json" + + def test_health_check_only_accepts_get_method(self, client: FlaskClient) -> None: + """Test that health_check only accepts GET method.""" + response = client.post("/health") + assert response.status_code == 405 # Method Not Allowed + + response = client.put("/health") + assert response.status_code == 405 + + response = client.delete("/health") + assert response.status_code == 405 From 7c2ca4a513e7593dc5e1eb6969d9e770cbf9b92b Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 14 Jan 2026 16:43:57 +0000 Subject: [PATCH 06/27] [GPCAPIM-254]: Add type hinting --- gateway-api/src/gateway_api/test_app.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py index 23dd2d96..9ba66301 100644 --- a/gateway-api/src/gateway_api/test_app.py +++ b/gateway-api/src/gateway_api/test_app.py @@ -1,13 +1,16 @@ """Unit tests for the Flask app endpoints.""" +from collections.abc import Generator + import pytest +from flask import Flask from flask.testing import FlaskClient from gateway_api.app import app @pytest.fixture -def client() -> FlaskClient: +def client() -> Generator[FlaskClient[Flask], None, None]: """Create a Flask test client.""" app.config["TESTING"] = True with app.test_client() as client: @@ -18,7 +21,7 @@ class TestGreetEndpoint: """Unit tests for the greet_endpoint function.""" def test_greet_endpoint_returns_greeting_for_valid_name( - self, client: FlaskClient + self, client: FlaskClient[Flask] ) -> None: """Test that greet_endpoint returns a greeting for a valid name.""" response = client.post( @@ -34,7 +37,7 @@ def test_greet_endpoint_returns_greeting_for_valid_name( assert data["body"].endswith("!") def test_greet_endpoint_returns_400_when_payload_missing( - self, client: FlaskClient + self, client: FlaskClient[Flask] ) -> None: """Test that greet_endpoint returns 400 when payload is missing.""" response = client.post( @@ -49,7 +52,7 @@ def test_greet_endpoint_returns_400_when_payload_missing( assert data["headers"]["Content-Type"] == "application/json" def test_greet_endpoint_returns_400_when_name_is_empty( - self, client: FlaskClient + self, client: FlaskClient[Flask] ) -> None: """Test that greet_endpoint returns 400 when name is empty.""" response = client.post( @@ -64,7 +67,7 @@ def test_greet_endpoint_returns_400_when_name_is_empty( assert data["headers"]["Content-Type"] == "application/json" def test_greet_endpoint_returns_404_for_nonexistent_user( - self, client: FlaskClient + self, client: FlaskClient[Flask] ) -> None: """Test that greet_endpoint returns 404 for nonexistent user.""" response = client.post( @@ -80,7 +83,7 @@ def test_greet_endpoint_returns_404_for_nonexistent_user( assert data["headers"]["Content-Type"] == "application/json" def test_greet_endpoint_returns_400_when_name_is_none( - self, client: FlaskClient + self, client: FlaskClient[Flask] ) -> None: """Test that greet_endpoint returns 400 when name is None.""" response = client.post( @@ -99,7 +102,7 @@ class TestHealthCheck: """Unit tests for the health_check function.""" def test_health_check_returns_200_and_healthy_status( - self, client: FlaskClient + self, client: FlaskClient[Flask] ) -> None: """Test that health_check returns 200 with healthy status.""" response = client.get("/health") @@ -110,7 +113,9 @@ def test_health_check_returns_200_and_healthy_status( assert data["body"]["status"] == "healthy" assert data["headers"]["Content-Type"] == "application/json" - def test_health_check_only_accepts_get_method(self, client: FlaskClient) -> None: + def test_health_check_only_accepts_get_method( + self, client: FlaskClient[Flask] + ) -> None: """Test that health_check only accepts GET method.""" response = client.post("/health") assert response.status_code == 405 # Method Not Allowed From 8eb80d053c91561cbc61497266822f27ddb6bcf5 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Thu, 15 Jan 2026 18:28:34 +0000 Subject: [PATCH 07/27] [GPCAPIM-254]: Beginning of /patient/$gpc.getstructuredrecord endpoint. --- gateway-api/openapi.yaml | 132 ++++++++++++++++++ gateway-api/src/gateway_api/app.py | 73 ++++++++++ gateway-api/src/gateway_api/test_app.py | 35 +++++ gateway-api/tests/conftest.py | 39 ++++-- ...GatewayAPIConsumer-GatewayAPIProvider.json | 74 ++++++++++ .../tests/contract/test_consumer_contract.py | 102 ++++++++++++++ 6 files changed, 447 insertions(+), 8 deletions(-) diff --git a/gateway-api/openapi.yaml b/gateway-api/openapi.yaml index df06978c..a318d4d9 100644 --- a/gateway-api/openapi.yaml +++ b/gateway-api/openapi.yaml @@ -9,6 +9,138 @@ servers: - url: http://localhost:5000 description: Local development server paths: + /patient/$gpc.getstructuredrecord: + post: + summary: Get structured record + description: Returns a FHIR Bundle containing patient structured record + operationId: getStructuredRecord + parameters: + - in: header + name: Content-Type + schema: + type: string + enum: [application/json] + required: true + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + resourceType: + type: string + example: "Parameters" + parameter: + type: array + items: + type: object + properties: + name: + type: string + example: "patientNHSNumber" + valueIdentifier: + type: object + properties: + system: + type: string + example: "https://fhir.nhs.uk/Id/nhs-number" + value: + type: string + example: "9999999999" + responses: + '200': + description: Successful response + parameters: + - in: header + name: Content-Type + schema: + type: string + enum: [application/json] + required: true + content: + application/json: + schema: + type: object + properties: + statusCode: + type: integer + description: Status code of the interaction + example: 200 + headers: + type: object + properties: + Content-Type: + type: string + example: "application/json" + body: + type: object + description: FHIR Bundle containing patient data + properties: + resourceType: + type: string + example: "Bundle" + id: + type: string + example: "example-patient-bundle" + type: + type: string + example: "collection" + timestamp: + type: string + format: date-time + example: "2026-01-12T10:00:00Z" + entry: + type: array + items: + type: object + properties: + fullUrl: + type: string + example: "urn:uuid:123e4567-e89b-12d3-a456-426614174000" + resource: + type: object + properties: + resourceType: + type: string + example: "Patient" + id: + type: string + example: "9999999999" + identifier: + type: array + items: + type: object + properties: + system: + type: string + example: "https://fhir.nhs.uk/Id/nhs-number" + value: + type: string + example: "9999999999" + name: + type: array + items: + type: object + properties: + use: + type: string + example: "official" + family: + type: string + example: "Doe" + given: + type: array + items: + type: string + example: ["John"] + gender: + type: string + example: "male" + birthDate: + type: string + format: date + example: "1985-04-12" /2015-03-31/functions/function/invocations: post: summary: Get hello world message diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index ce881c77..f6fa3fcd 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -16,6 +16,79 @@ class APIMResponse[T](TypedDict): body: T +class Identifier(TypedDict): + """FHIR Identifier type.""" + + system: str + value: str + + +class HumanName(TypedDict): + """FHIR HumanName type.""" + + use: str + family: str + given: list[str] + + +class Patient(TypedDict): + """FHIR Patient resource.""" + + resourceType: str + id: str + identifier: list[Identifier] + name: list[HumanName] + gender: str + birthDate: str + + +class BundleEntry(TypedDict): + """FHIR Bundle entry.""" + + fullUrl: str + resource: Patient + + +class Bundle(TypedDict): + """FHIR Bundle resource.""" + + resourceType: str + id: str + type: str + timestamp: str + entry: list[BundleEntry] + + +@app.route("/patient/$gpc.getstructuredrecord", methods=["POST"]) +def get_structured_record() -> Bundle: + """Endpoint to get structured record, replicating lambda handler functionality.""" + bundle: Bundle = { + "resourceType": "Bundle", + "id": "example-patient-bundle", + "type": "collection", + "timestamp": "2026-01-12T10:00:00Z", + "entry": [ + { + "fullUrl": "urn:uuid:123e4567-e89b-12d3-a456-426614174000", + "resource": { + "resourceType": "Patient", + "id": "9999999999", + "identifier": [ + { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + } + ], + "name": [{"use": "official", "family": "Doe", "given": ["John"]}], + "gender": "male", + "birthDate": "1985-04-12", + }, + } + ], + } + return bundle + + @app.route("/2015-03-31/functions/function/invocations", methods=["POST"]) def greet_endpoint() -> APIMResponse[str | dict[str, str]]: """Greet endpoint that replicates the lambda handler functionality.""" diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py index 9ba66301..15d864c4 100644 --- a/gateway-api/src/gateway_api/test_app.py +++ b/gateway-api/src/gateway_api/test_app.py @@ -17,6 +17,41 @@ def client() -> Generator[FlaskClient[Flask], None, None]: yield client +class TestGetStructuredRecord: + """Unit tests for the get_structured_record function.""" + + def test_get_structured_record_returns_200_with_bundle( + self, client: FlaskClient[Flask] + ) -> None: + """Test that get_structured_record returns 200 with a bundle.""" + body = { + "resourceType": "Parameters", + "parameter": [ + { + "name": "patientNHSNumber", + "valueIdentifier": { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + }, + }, + ], + } + response = client.post("/patient/$gpc.getstructuredrecord", json=body) + + assert response.status_code == 200 + data = response.get_json() + assert isinstance(data, dict) + assert data.get("resourceType") == "Bundle" + assert data.get("id") == "example-patient-bundle" + assert data.get("type") == "collection" + assert "entry" in data + assert isinstance(data["entry"], list) + assert len(data["entry"]) > 0 + assert data["entry"][0]["resource"]["resourceType"] == "Patient" + assert data["entry"][0]["resource"]["id"] == "9999999999" + assert data["entry"][0]["resource"]["identifier"][0]["value"] == "9999999999" + + class TestGreetEndpoint: """Unit tests for the greet_endpoint function.""" diff --git a/gateway-api/tests/conftest.py b/gateway-api/tests/conftest.py index d5fba218..997b044d 100644 --- a/gateway-api/tests/conftest.py +++ b/gateway-api/tests/conftest.py @@ -21,7 +21,28 @@ def __init__(self, lambda_url: str, timeout: timedelta = timedelta(seconds=1)): self._lambda_url = lambda_url self._timeout = timeout.total_seconds() - def send(self, data: str) -> requests.Response: + def get_structured_record(self, nhs_number: str) -> requests.Response: + """ + Send a request to the get_structured_record endpoint with the given NHS number. + """ + payload = json.dumps( + { + "resourceType": "Parameters", + "parameter": [ + { + "name": "patientNHSNumber", + "valueIdentifier": { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": nhs_number, + }, + }, + ], + } + ) + url = f"{self._lambda_url}/patient/$gpc.getstructuredrecord" + return self._send(url=url, payload=payload) + + def send(self, message: str) -> requests.Response: """ Send a request to the APIs with some given parameters. Args: @@ -29,7 +50,9 @@ def send(self, data: str) -> requests.Response: Returns: Response object from the request """ - return self._send(data=data, include_payload=True) + payload = json.dumps({"payload": message}) + url = f"{self._lambda_url}/2015-03-31/functions/function/invocations" + return self._send(url=url, payload=payload) def send_without_payload(self) -> requests.Response: """ @@ -37,14 +60,14 @@ def send_without_payload(self) -> requests.Response: Returns: Response object from the request """ - return self._send(data=None, include_payload=False) - - def _send(self, data: str | None, include_payload: bool) -> requests.Response: - json_data = {"payload": data} if include_payload else {} + empty_payload = json.dumps({}) + url = f"{self._lambda_url}/2015-03-31/functions/function/invocations" + return self._send(url=url, payload=empty_payload) + def _send(self, url: str, payload: str) -> requests.Response: return requests.post( - f"{self._lambda_url}/2015-03-31/functions/function/invocations", - data=json.dumps(json_data), + url=url, + data=payload, timeout=self._timeout, ) diff --git a/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json b/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json index dd9af038..47af75f5 100644 --- a/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json +++ b/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json @@ -15,6 +15,80 @@ }, "type": "Synchronous/HTTP" }, + { + "description": "a request for structured record", + "pending": false, + "request": { + "body": { + "content": { + "parameter": [ + { + "name": "patientNHSNumber", + "valueIdentifier": { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999" + } + } + ], + "resourceType": "Parameters" + }, + "contentType": "application/json", + "encoded": false + }, + "headers": { + "Content-Type": [ + "application/json" + ] + }, + "method": "POST", + "path": "/patient/$gpc.getstructuredrecord" + }, + "response": { + "body": { + "content": { + "entry": [ + { + "fullUrl": "urn:uuid:123e4567-e89b-12d3-a456-426614174000", + "resource": { + "birthDate": "1985-04-12", + "gender": "male", + "id": "9999999999", + "identifier": [ + { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999" + } + ], + "name": [ + { + "family": "Doe", + "given": [ + "John" + ], + "use": "official" + } + ], + "resourceType": "Patient" + } + } + ], + "id": "example-patient-bundle", + "resourceType": "Bundle", + "timestamp": "2026-01-12T10:00:00Z", + "type": "collection" + }, + "contentType": "application/json", + "encoded": false + }, + "headers": { + "Content-Type": [ + "application/json" + ] + }, + "status": 200 + }, + "type": "Synchronous/HTTP" + }, { "description": "a request for the hello world message", "pending": false, diff --git a/gateway-api/tests/contract/test_consumer_contract.py b/gateway-api/tests/contract/test_consumer_contract.py index e8b46c1c..68b33aaa 100644 --- a/gateway-api/tests/contract/test_consumer_contract.py +++ b/gateway-api/tests/contract/test_consumer_contract.py @@ -4,6 +4,8 @@ interactions with the provider (the Flask API). """ +import json + import requests from pact import Pact @@ -59,6 +61,106 @@ def test_get_hello_world(self) -> None: # Write the pact file after the test pact.write_file("tests/contract/pacts") + def test_get_structured_record(self) -> None: + """Test the consumer's expectation of the get structured record endpoint. + + This test defines the contract: when the consumer requests + POST to the /patient/$gpc.getstructuredrecord endpoint, + a 200 response containing a FHIR Bundle is returned. + """ + pact = Pact(consumer="GatewayAPIConsumer", provider="GatewayAPIProvider") + + expected_bundle = { + "resourceType": "Bundle", + "id": "example-patient-bundle", + "type": "collection", + "timestamp": "2026-01-12T10:00:00Z", + "entry": [ + { + "fullUrl": "urn:uuid:123e4567-e89b-12d3-a456-426614174000", + "resource": { + "resourceType": "Patient", + "id": "9999999999", + "identifier": [ + { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + } + ], + "name": [ + {"use": "official", "family": "Doe", "given": ["John"]} + ], + "gender": "male", + "birthDate": "1985-04-12", + }, + } + ], + } + + # Define the expected interaction + ( + pact.upon_receiving("a request for structured record") + .with_body( + { + "resourceType": "Parameters", + "parameter": [ + { + "name": "patientNHSNumber", + "valueIdentifier": { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + }, + }, + ], + }, + content_type="application/json", + ) + .with_header("Content-Type", "application/json") + .with_request( + method="POST", + path="/patient/$gpc.getstructuredrecord", + ) + .will_respond_with(status=200) + .with_body(expected_bundle, content_type="application/json") + .with_header("Content-Type", "application/json") + ) + + # Start the mock server and execute the test + with pact.serve() as server: + # Make the actual request to the mock provider + response = requests.post( + f"{server.url}/patient/$gpc.getstructuredrecord", + data=json.dumps( + { + "resourceType": "Parameters", + "parameter": [ + { + "name": "patientNHSNumber", + "valueIdentifier": { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + }, + }, + ], + } + ), + headers={"Content-Type": "application/json"}, + timeout=10, + ) + + # Verify the response matches expectations + assert response.status_code == 200 + body = response.json() + assert body["resourceType"] == "Bundle" + assert body["id"] == "example-patient-bundle" + assert body["type"] == "collection" + assert len(body["entry"]) == 1 + assert body["entry"][0]["resource"]["resourceType"] == "Patient" + assert body["entry"][0]["resource"]["id"] == "9999999999" + + # Write the pact file after the test + pact.write_file("tests/contract/pacts") + def test_get_nonexistent_route(self) -> None: """Test the consumer's expectation when requesting a non-existent route. From 6f72813be5487ad0bedcd9129f95e234342240f6 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Mon, 19 Jan 2026 13:12:53 +0000 Subject: [PATCH 08/27] [GPCAPIM-254]: Handle logic in request-specific class --- gateway-api/pyproject.toml | 3 +- gateway-api/src/fhir/__init__.py | 8 +++ gateway-api/src/fhir/bundle.py | 22 ++++++ gateway-api/src/fhir/human_name.py | 11 +++ gateway-api/src/fhir/identifier.py | 10 +++ gateway-api/src/fhir/patient.py | 17 +++++ gateway-api/src/gateway_api/app.py | 71 ++----------------- .../get_structed_record/__init__.py | 0 .../get_structed_record/request.py | 36 ++++++++++ 9 files changed, 110 insertions(+), 68 deletions(-) create mode 100644 gateway-api/src/fhir/__init__.py create mode 100644 gateway-api/src/fhir/bundle.py create mode 100644 gateway-api/src/fhir/human_name.py create mode 100644 gateway-api/src/fhir/identifier.py create mode 100644 gateway-api/src/fhir/patient.py create mode 100644 gateway-api/src/gateway_api/get_structed_record/__init__.py create mode 100644 gateway-api/src/gateway_api/get_structed_record/request.py diff --git a/gateway-api/pyproject.toml b/gateway-api/pyproject.toml index 87f86635..fa79be03 100644 --- a/gateway-api/pyproject.toml +++ b/gateway-api/pyproject.toml @@ -15,7 +15,8 @@ types-flask = "^1.1.6" [tool.poetry] packages = [{include = "gateway_api", from = "src"}, - {include = "stubs", from = "stubs"}] + {include = "stubs", from = "stubs"}, + {include = "fhir", from = "src"}] [tool.coverage.run] relative_files = true diff --git a/gateway-api/src/fhir/__init__.py b/gateway-api/src/fhir/__init__.py new file mode 100644 index 00000000..efc9349c --- /dev/null +++ b/gateway-api/src/fhir/__init__.py @@ -0,0 +1,8 @@ +"""FHIR data types and resources.""" + +from fhir.bundle import Bundle, BundleEntry +from fhir.human_name import HumanName +from fhir.identifier import Identifier +from fhir.patient import Patient + +__all__ = ["Bundle", "BundleEntry", "Identifier", "Patient", "HumanName"] diff --git a/gateway-api/src/fhir/bundle.py b/gateway-api/src/fhir/bundle.py new file mode 100644 index 00000000..afe49f94 --- /dev/null +++ b/gateway-api/src/fhir/bundle.py @@ -0,0 +1,22 @@ +"""FHIR Bundle resource.""" + +from typing import TypedDict + +from fhir.patient import Patient + + +class BundleEntry(TypedDict): + """FHIR Bundle entry.""" + + fullUrl: str + resource: Patient + + +class Bundle(TypedDict): + """FHIR Bundle resource.""" + + resourceType: str + id: str + type: str + timestamp: str + entry: list[BundleEntry] diff --git a/gateway-api/src/fhir/human_name.py b/gateway-api/src/fhir/human_name.py new file mode 100644 index 00000000..bc56d529 --- /dev/null +++ b/gateway-api/src/fhir/human_name.py @@ -0,0 +1,11 @@ +"""FHIR HumanName type.""" + +from typing import TypedDict + + +class HumanName(TypedDict): + """FHIR HumanName type.""" + + use: str + family: str + given: list[str] diff --git a/gateway-api/src/fhir/identifier.py b/gateway-api/src/fhir/identifier.py new file mode 100644 index 00000000..feb62aee --- /dev/null +++ b/gateway-api/src/fhir/identifier.py @@ -0,0 +1,10 @@ +"""FHIR Identifier type.""" + +from typing import TypedDict + + +class Identifier(TypedDict): + """FHIR Identifier type.""" + + system: str + value: str diff --git a/gateway-api/src/fhir/patient.py b/gateway-api/src/fhir/patient.py new file mode 100644 index 00000000..e23eb449 --- /dev/null +++ b/gateway-api/src/fhir/patient.py @@ -0,0 +1,17 @@ +"""FHIR Patient resource.""" + +from typing import TypedDict + +from fhir.human_name import HumanName +from fhir.identifier import Identifier + + +class Patient(TypedDict): + """FHIR Patient resource.""" + + resourceType: str + id: str + identifier: list[Identifier] + name: list[HumanName] + gender: str + birthDate: str diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index f6fa3fcd..2d2bf91d 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -1,8 +1,10 @@ import os from typing import Any, TypedDict +from fhir import Bundle from flask import Flask, request +from gateway_api.get_structed_record.request import GetStructuredRecordRequest from gateway_api.handler import User, greet app = Flask(__name__) @@ -16,76 +18,11 @@ class APIMResponse[T](TypedDict): body: T -class Identifier(TypedDict): - """FHIR Identifier type.""" - - system: str - value: str - - -class HumanName(TypedDict): - """FHIR HumanName type.""" - - use: str - family: str - given: list[str] - - -class Patient(TypedDict): - """FHIR Patient resource.""" - - resourceType: str - id: str - identifier: list[Identifier] - name: list[HumanName] - gender: str - birthDate: str - - -class BundleEntry(TypedDict): - """FHIR Bundle entry.""" - - fullUrl: str - resource: Patient - - -class Bundle(TypedDict): - """FHIR Bundle resource.""" - - resourceType: str - id: str - type: str - timestamp: str - entry: list[BundleEntry] - - @app.route("/patient/$gpc.getstructuredrecord", methods=["POST"]) def get_structured_record() -> Bundle: """Endpoint to get structured record, replicating lambda handler functionality.""" - bundle: Bundle = { - "resourceType": "Bundle", - "id": "example-patient-bundle", - "type": "collection", - "timestamp": "2026-01-12T10:00:00Z", - "entry": [ - { - "fullUrl": "urn:uuid:123e4567-e89b-12d3-a456-426614174000", - "resource": { - "resourceType": "Patient", - "id": "9999999999", - "identifier": [ - { - "system": "https://fhir.nhs.uk/Id/nhs-number", - "value": "9999999999", - } - ], - "name": [{"use": "official", "family": "Doe", "given": ["John"]}], - "gender": "male", - "birthDate": "1985-04-12", - }, - } - ], - } + get_structured_record_request = GetStructuredRecordRequest(request) + bundle = get_structured_record_request.fulfil() return bundle diff --git a/gateway-api/src/gateway_api/get_structed_record/__init__.py b/gateway-api/src/gateway_api/get_structed_record/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/gateway-api/src/gateway_api/get_structed_record/request.py b/gateway-api/src/gateway_api/get_structed_record/request.py new file mode 100644 index 00000000..4b22296b --- /dev/null +++ b/gateway-api/src/gateway_api/get_structed_record/request.py @@ -0,0 +1,36 @@ +from fhir import Bundle +from flask.wrappers import Request + + +class GetStructuredRecordRequest: + def __init__(self, request: Request) -> None: + self._http_request = request + + def fulfil(self) -> Bundle: + bundle: Bundle = { + "resourceType": "Bundle", + "id": "example-patient-bundle", + "type": "collection", + "timestamp": "2026-01-12T10:00:00Z", + "entry": [ + { + "fullUrl": "urn:uuid:123e4567-e89b-12d3-a456-426614174000", + "resource": { + "resourceType": "Patient", + "id": "9999999999", + "identifier": [ + { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + } + ], + "name": [ + {"use": "official", "family": "Doe", "given": ["John"]} + ], + "gender": "male", + "birthDate": "1985-04-12", + }, + } + ], + } + return bundle From 34ceab7d808a0eee02db6bda3d41b67c83fa5a94 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Mon, 19 Jan 2026 14:16:56 +0000 Subject: [PATCH 09/27] [GPCAPIM-254]: Move to handler class --- gateway-api/src/fhir/__init__.py | 11 +++- gateway-api/src/fhir/parameters.py | 19 +++++++ gateway-api/src/gateway_api/app.py | 5 +- .../get_structed_record/handler.py | 35 ++++++++++++ .../get_structed_record/request.py | 55 +++++++++---------- 5 files changed, 93 insertions(+), 32 deletions(-) create mode 100644 gateway-api/src/fhir/parameters.py create mode 100644 gateway-api/src/gateway_api/get_structed_record/handler.py diff --git a/gateway-api/src/fhir/__init__.py b/gateway-api/src/fhir/__init__.py index efc9349c..ea58d5c8 100644 --- a/gateway-api/src/fhir/__init__.py +++ b/gateway-api/src/fhir/__init__.py @@ -3,6 +3,15 @@ from fhir.bundle import Bundle, BundleEntry from fhir.human_name import HumanName from fhir.identifier import Identifier +from fhir.parameters import Parameter, Parameters from fhir.patient import Patient -__all__ = ["Bundle", "BundleEntry", "Identifier", "Patient", "HumanName"] +__all__ = [ + "Bundle", + "BundleEntry", + "HumanName", + "Identifier", + "Parameter", + "Parameters", + "Patient", +] diff --git a/gateway-api/src/fhir/parameters.py b/gateway-api/src/fhir/parameters.py new file mode 100644 index 00000000..eef46ca6 --- /dev/null +++ b/gateway-api/src/fhir/parameters.py @@ -0,0 +1,19 @@ +"""FHIR Parameters resource.""" + +from typing import TypedDict + +from fhir.identifier import Identifier + + +class Parameter(TypedDict): + """FHIR Parameter type.""" + + name: str + valueIdentifier: Identifier + + +class Parameters(TypedDict): + """FHIR Parameters resource.""" + + resourceType: str + parameter: list[Parameter] diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index 2d2bf91d..5a449268 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -4,6 +4,7 @@ from fhir import Bundle from flask import Flask, request +from gateway_api.get_structed_record.handler import GetStructuredRecordHandler from gateway_api.get_structed_record.request import GetStructuredRecordRequest from gateway_api.handler import User, greet @@ -22,8 +23,8 @@ class APIMResponse[T](TypedDict): def get_structured_record() -> Bundle: """Endpoint to get structured record, replicating lambda handler functionality.""" get_structured_record_request = GetStructuredRecordRequest(request) - bundle = get_structured_record_request.fulfil() - return bundle + response = GetStructuredRecordHandler.handle(get_structured_record_request) + return response @app.route("/2015-03-31/functions/function/invocations", methods=["POST"]) diff --git a/gateway-api/src/gateway_api/get_structed_record/handler.py b/gateway-api/src/gateway_api/get_structed_record/handler.py new file mode 100644 index 00000000..5a301bd4 --- /dev/null +++ b/gateway-api/src/gateway_api/get_structed_record/handler.py @@ -0,0 +1,35 @@ +from fhir import Bundle + +from gateway_api.get_structed_record.request import GetStructuredRecordRequest + + +class GetStructuredRecordHandler: + @classmethod + def handle(cls, request: GetStructuredRecordRequest) -> Bundle: + bundle: Bundle = { + "resourceType": "Bundle", + "id": "example-patient-bundle", + "type": "collection", + "timestamp": "2026-01-12T10:00:00Z", + "entry": [ + { + "fullUrl": "urn:uuid:123e4567-e89b-12d3-a456-426614174000", + "resource": { + "resourceType": "Patient", + "id": "9999999999", + "identifier": [ + { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + } + ], + "name": [ + {"use": "official", "family": "Doe", "given": ["John"]} + ], + "gender": "male", + "birthDate": "1985-04-12", + }, + } + ], + } + return bundle diff --git a/gateway-api/src/gateway_api/get_structed_record/request.py b/gateway-api/src/gateway_api/get_structed_record/request.py index 4b22296b..7a0f7311 100644 --- a/gateway-api/src/gateway_api/get_structed_record/request.py +++ b/gateway-api/src/gateway_api/get_structed_record/request.py @@ -1,36 +1,33 @@ -from fhir import Bundle +from typing import TYPE_CHECKING + from flask.wrappers import Request +if TYPE_CHECKING: + from fhir import Parameters + class GetStructuredRecordRequest: def __init__(self, request: Request) -> None: self._http_request = request + self._headers = request.headers + self._request_body: Parameters = request.get_json() + + @property + def trace_id(self) -> str: + trace_id: str = self._headers["Ssp-TraceID"] + return trace_id + + @property + def nhs_number(self) -> str: + nhs_number: str = self._request_body["parameter"][0]["valueIdentifier"]["value"] + return nhs_number + + @property + def consumer_asid(self) -> str: + consumer_asid: str = self._headers["X-Consumer-ASID"] + return consumer_asid - def fulfil(self) -> Bundle: - bundle: Bundle = { - "resourceType": "Bundle", - "id": "example-patient-bundle", - "type": "collection", - "timestamp": "2026-01-12T10:00:00Z", - "entry": [ - { - "fullUrl": "urn:uuid:123e4567-e89b-12d3-a456-426614174000", - "resource": { - "resourceType": "Patient", - "id": "9999999999", - "identifier": [ - { - "system": "https://fhir.nhs.uk/Id/nhs-number", - "value": "9999999999", - } - ], - "name": [ - {"use": "official", "family": "Doe", "given": ["John"]} - ], - "gender": "male", - "birthDate": "1985-04-12", - }, - } - ], - } - return bundle + @property + def provider_asid(self) -> str: + provider_asid: str = self._headers["X-Provider-ASID"] + return provider_asid From 1fc652859e0be0874c472f5889c2eb1c1735d535 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Tue, 20 Jan 2026 11:06:09 +0000 Subject: [PATCH 10/27] [GPCAPIM-254]: Update healthcheck endpoint to return simplier body. --- gateway-api/src/gateway_api/app.py | 10 +++++++--- gateway-api/src/gateway_api/test_app.py | 15 ++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index 5a449268..240dc9a6 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -1,5 +1,5 @@ import os -from typing import Any, TypedDict +from typing import TypedDict from fhir import Bundle from flask import Flask, request @@ -19,6 +19,10 @@ class APIMResponse[T](TypedDict): body: T +class HealthCheckResponse(TypedDict): + status: str + + @app.route("/patient/$gpc.getstructuredrecord", methods=["POST"]) def get_structured_record() -> Bundle: """Endpoint to get structured record, replicating lambda handler functionality.""" @@ -54,9 +58,9 @@ def _with_default_headers[T](status_code: int, body: T) -> APIMResponse[T]: @app.route("/health", methods=["GET"]) -def health_check() -> APIMResponse[dict[str, Any]]: +def health_check() -> HealthCheckResponse: """Health check endpoint.""" - return _with_default_headers(status_code=200, body={"status": "healthy"}) + return {"status": "healthy"} if __name__ == "__main__": diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py index 15d864c4..00d6a460 100644 --- a/gateway-api/src/gateway_api/test_app.py +++ b/gateway-api/src/gateway_api/test_app.py @@ -144,19 +144,12 @@ def test_health_check_returns_200_and_healthy_status( assert response.status_code == 200 data = response.get_json() - assert data["statusCode"] == 200 - assert data["body"]["status"] == "healthy" - assert data["headers"]["Content-Type"] == "application/json" + assert data["status"] == "healthy" + @pytest.mark.parametrize("method", ["POST", "PUT", "DELETE", "PATCH"]) def test_health_check_only_accepts_get_method( - self, client: FlaskClient[Flask] + self, client: FlaskClient[Flask], method: str ) -> None: """Test that health_check only accepts GET method.""" - response = client.post("/health") - assert response.status_code == 405 # Method Not Allowed - - response = client.put("/health") - assert response.status_code == 405 - - response = client.delete("/health") + response = client.open("/health", method=method) assert response.status_code == 405 From 7b9e59c1bf2394f7df77f404861a13b79bbcb81b Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Tue, 20 Jan 2026 18:11:20 +0000 Subject: [PATCH 11/27] [GPCAPIM-254]: Remove the lambda. --- gateway-api/lambda_handler.py | 38 -------- gateway-api/openapi.yaml | 48 +++------- gateway-api/src/fhir/py.typed | 0 gateway-api/src/gateway_api/app.py | 36 -------- gateway-api/src/gateway_api/test_app.py | 81 ----------------- gateway-api/test_lambda_handler.py | 63 ------------- .../acceptance/features/happy_path.feature | 16 ++++ ...test_hello_world.py => test_happy_path.py} | 8 +- .../tests/acceptance/steps/happy_path.py | 73 +++++++++++++++ gateway-api/tests/conftest.py | 89 ++++++++++++++----- ...GatewayAPIConsumer-GatewayAPIProvider.json | 40 --------- .../tests/contract/test_consumer_contract.py | 48 ---------- .../integration/test_get_structured_record.py | 42 +++++++++ gateway-api/tests/integration/test_main.py | 65 ++++++-------- 14 files changed, 243 insertions(+), 404 deletions(-) delete mode 100644 gateway-api/lambda_handler.py create mode 100644 gateway-api/src/fhir/py.typed delete mode 100644 gateway-api/test_lambda_handler.py create mode 100644 gateway-api/tests/acceptance/features/happy_path.feature rename gateway-api/tests/acceptance/scenarios/{test_hello_world.py => test_happy_path.py} (52%) create mode 100644 gateway-api/tests/acceptance/steps/happy_path.py create mode 100644 gateway-api/tests/integration/test_get_structured_record.py diff --git a/gateway-api/lambda_handler.py b/gateway-api/lambda_handler.py deleted file mode 100644 index 554f6e28..00000000 --- a/gateway-api/lambda_handler.py +++ /dev/null @@ -1,38 +0,0 @@ -from typing import TypedDict - -from gateway_api.handler import User, greet - - -class LambdaResponse[T](TypedDict): - """A lambda response including a body with a generic type.""" - - statusCode: int - headers: dict[str, str] - body: T - - -def _with_default_headers[T](status_code: int, body: T) -> LambdaResponse[T]: - return { - "statusCode": status_code, - "headers": {"Content-Type": "application/json"}, - "body": body, - } - - -def handler(event: dict[str, str], context: dict[str, str]) -> LambdaResponse[str]: - print(f"Received event: {event}") - - if "payload" not in event: - return _with_default_headers(status_code=400, body="Name is required") - - name = event["payload"] - if not name: - return _with_default_headers(status_code=400, body="Name cannot be empty") - user = User(name=name) - - try: - return _with_default_headers(status_code=200, body=f"{greet(user)}") - except ValueError: - return _with_default_headers( - status_code=404, body=f"Provided name cannot be found. name={name}" - ) diff --git a/gateway-api/openapi.yaml b/gateway-api/openapi.yaml index a318d4d9..8a52ceab 100644 --- a/gateway-api/openapi.yaml +++ b/gateway-api/openapi.yaml @@ -141,48 +141,22 @@ paths: type: string format: date example: "1985-04-12" - /2015-03-31/functions/function/invocations: - post: - summary: Get hello world message - description: Returns a simple hello world message - operationId: postHelloWorld - requestBody: - required: true - content: - application/json: - schema: - type: object - properties: - payload: - type: string - description: The payload to be processed - example: "Alex" + /health: + get: + summary: Health check + description: Returns the health status of the API + operationId: healthCheck responses: '200': - description: Successful response + description: Service is healthy content: application/json: schema: type: object properties: - status_code: - type: integer - description: Status code of the interaction - body: - type: string - description: The output of the interaction - errorMessage: - type: string - description: Any error messages relating to errors encountered with the interaction - errorType: - type: string - description: The type of error encountered during the interaction, if an error has occurred - requestId: + status: type: string - format: uuid - description: The unique request ID for the interaction - stacktrace: - type: array - items: - type: string - description: The stack trace of the error, if an error has occurred + example: "healthy" + required: + - status + diff --git a/gateway-api/src/fhir/py.typed b/gateway-api/src/fhir/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index 240dc9a6..f6ef72f3 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -6,57 +6,21 @@ from gateway_api.get_structed_record.handler import GetStructuredRecordHandler from gateway_api.get_structed_record.request import GetStructuredRecordRequest -from gateway_api.handler import User, greet app = Flask(__name__) -class APIMResponse[T](TypedDict): - """A API Management response including a body with a generic type.""" - - statusCode: int - headers: dict[str, str] - body: T - - class HealthCheckResponse(TypedDict): status: str @app.route("/patient/$gpc.getstructuredrecord", methods=["POST"]) def get_structured_record() -> Bundle: - """Endpoint to get structured record, replicating lambda handler functionality.""" get_structured_record_request = GetStructuredRecordRequest(request) response = GetStructuredRecordHandler.handle(get_structured_record_request) return response -@app.route("/2015-03-31/functions/function/invocations", methods=["POST"]) -def greet_endpoint() -> APIMResponse[str | dict[str, str]]: - """Greet endpoint that replicates the lambda handler functionality.""" - data = request.get_json(force=True) - if "payload" not in data: - return _with_default_headers(status_code=400, body="Name is required") - - name = data["payload"] - if not name: - return _with_default_headers(status_code=400, body="Name cannot be empty") - user = User(name=name) - - try: - return _with_default_headers(status_code=200, body=f"{greet(user)}") - except ValueError: - return _with_default_headers( - status_code=404, body=f"Provided name cannot be found. name={name}" - ) - - -def _with_default_headers[T](status_code: int, body: T) -> APIMResponse[T]: - return APIMResponse( - statusCode=status_code, headers={"Content-Type": "application/json"}, body=body - ) - - @app.route("/health", methods=["GET"]) def health_check() -> HealthCheckResponse: """Health check endpoint.""" diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py index 00d6a460..a382ccc1 100644 --- a/gateway-api/src/gateway_api/test_app.py +++ b/gateway-api/src/gateway_api/test_app.py @@ -52,87 +52,6 @@ def test_get_structured_record_returns_200_with_bundle( assert data["entry"][0]["resource"]["identifier"][0]["value"] == "9999999999" -class TestGreetEndpoint: - """Unit tests for the greet_endpoint function.""" - - def test_greet_endpoint_returns_greeting_for_valid_name( - self, client: FlaskClient[Flask] - ) -> None: - """Test that greet_endpoint returns a greeting for a valid name.""" - response = client.post( - "/2015-03-31/functions/function/invocations", - json={"payload": "Alice"}, - ) - - assert response.status_code == 200 - data = response.get_json() - assert data["statusCode"] == 200 - assert data["headers"]["Content-Type"] == "application/json" - assert "Alice" in data["body"] - assert data["body"].endswith("!") - - def test_greet_endpoint_returns_400_when_payload_missing( - self, client: FlaskClient[Flask] - ) -> None: - """Test that greet_endpoint returns 400 when payload is missing.""" - response = client.post( - "/2015-03-31/functions/function/invocations", - json={}, - ) - - assert response.status_code == 200 - data = response.get_json() - assert data["statusCode"] == 400 - assert data["body"] == "Name is required" - assert data["headers"]["Content-Type"] == "application/json" - - def test_greet_endpoint_returns_400_when_name_is_empty( - self, client: FlaskClient[Flask] - ) -> None: - """Test that greet_endpoint returns 400 when name is empty.""" - response = client.post( - "/2015-03-31/functions/function/invocations", - json={"payload": ""}, - ) - - assert response.status_code == 200 - data = response.get_json() - assert data["statusCode"] == 400 - assert data["body"] == "Name cannot be empty" - assert data["headers"]["Content-Type"] == "application/json" - - def test_greet_endpoint_returns_404_for_nonexistent_user( - self, client: FlaskClient[Flask] - ) -> None: - """Test that greet_endpoint returns 404 for nonexistent user.""" - response = client.post( - "/2015-03-31/functions/function/invocations", - json={"payload": "nonexistent"}, - ) - - assert response.status_code == 200 - data = response.get_json() - assert data["statusCode"] == 404 - assert "cannot be found" in data["body"] - assert "nonexistent" in data["body"] - assert data["headers"]["Content-Type"] == "application/json" - - def test_greet_endpoint_returns_400_when_name_is_none( - self, client: FlaskClient[Flask] - ) -> None: - """Test that greet_endpoint returns 400 when name is None.""" - response = client.post( - "/2015-03-31/functions/function/invocations", - json={"payload": None}, - ) - - assert response.status_code == 200 - data = response.get_json() - assert data["statusCode"] == 400 - assert data["body"] == "Name cannot be empty" - assert data["headers"]["Content-Type"] == "application/json" - - class TestHealthCheck: """Unit tests for the health_check function.""" diff --git a/gateway-api/test_lambda_handler.py b/gateway-api/test_lambda_handler.py deleted file mode 100644 index df38367d..00000000 --- a/gateway-api/test_lambda_handler.py +++ /dev/null @@ -1,63 +0,0 @@ -import pytest -from lambda_handler import handler - - -class TestHandler: - """Unit tests for the Lambda handler function.""" - - @pytest.mark.parametrize( - ("name", "expected_greeting"), - [ - ("Alice", "Hello, Alice!"), - ("Bob", "Hello, Bob!"), - ("John Doe", "Hello, John Doe!"), - ("user123", "Hello, user123!"), - ], - ids=["simple_name_alice", "simple_name_bob", "name_with_space", "alphanumeric"], - ) - def test_handler_success(self, name: str, expected_greeting: str) -> None: - """Test handler returns 200 with greeting for valid names.""" - # Arrange - event = {"payload": name} - context: dict[str, str] = {} - - # Act - response = handler(event, context) - - # Assert - assert response["statusCode"] == 200 - assert response["body"] == expected_greeting - assert response["headers"] == {"Content-Type": "application/json"} - - @pytest.mark.parametrize( - ("event", "expected_status", "expected_body"), - [ - ({"other_key": "value"}, 400, "Name is required"), - ({"payload": ""}, 400, "Name cannot be empty"), - ({"payload": None}, 400, "Name cannot be empty"), - ( - {"payload": "nonexistent"}, - 404, - "Provided name cannot be found. name=nonexistent", - ), - ], - ids=[ - "missing_payload_key", - "empty_payload", - "none_payload", - "nonexistent_user", - ], - ) - def test_handler_error_cases( - self, event: dict[str, str], expected_status: int, expected_body: str - ) -> None: - """Test handler returns appropriate error responses for invalid or - nonexistent input. - """ - # Act - response = handler(event, {}) - - # Assert - assert response["statusCode"] == expected_status - assert response["body"] == expected_body - assert response["headers"] == {"Content-Type": "application/json"} diff --git a/gateway-api/tests/acceptance/features/happy_path.feature b/gateway-api/tests/acceptance/features/happy_path.feature new file mode 100644 index 00000000..b4f5b757 --- /dev/null +++ b/gateway-api/tests/acceptance/features/happy_path.feature @@ -0,0 +1,16 @@ +Feature: Gateway API Hello World + As an API consumer + I want to interact with the Gateway API + So that I can verify it responds correctly to valid and invalid requests + + Background: The API is running + Given the API is running new + + Scenario: Get structured record request + When I send a valid Parameters resource to the endpoint + Then the response status code should be 200 + And the response should contain a valid Bundle resource + + Scenario: Accessing a non-existent endpoint returns a 404 + When I send a valid Parameters resource to a nonexistent endpoint + Then the response status code should be 404 diff --git a/gateway-api/tests/acceptance/scenarios/test_hello_world.py b/gateway-api/tests/acceptance/scenarios/test_happy_path.py similarity index 52% rename from gateway-api/tests/acceptance/scenarios/test_hello_world.py rename to gateway-api/tests/acceptance/scenarios/test_happy_path.py index 93ed4a1a..e175a034 100644 --- a/gateway-api/tests/acceptance/scenarios/test_hello_world.py +++ b/gateway-api/tests/acceptance/scenarios/test_happy_path.py @@ -4,16 +4,16 @@ from pytest_bdd import scenario -from tests.acceptance.steps.hello_world_steps import * # noqa: F403,S2208 - Required to import all hello world steps. +from tests.acceptance.steps.happy_path import * # noqa: F403,S2208 - Required to import all happy path steps. -@scenario("hello_world.feature", "Get hello world message") -def test_hello_world() -> None: +@scenario("happy_path.feature", "Get structured record request") +def test_structured_record_request() -> None: # No body required here as this method simply provides a binding to the BDD step pass -@scenario("hello_world.feature", "Accessing a non-existent endpoint returns a 404") +@scenario("happy_path.feature", "Accessing a non-existent endpoint returns a 404") def test_nonexistent_route() -> None: # No body required here as this method simply provides a binding to the BDD step pass diff --git a/gateway-api/tests/acceptance/steps/happy_path.py b/gateway-api/tests/acceptance/steps/happy_path.py new file mode 100644 index 00000000..b1f64404 --- /dev/null +++ b/gateway-api/tests/acceptance/steps/happy_path.py @@ -0,0 +1,73 @@ +"""Step definitions for Gateway API hello world feature.""" + +import json +from datetime import timedelta + +import requests +from fhir.bundle import Bundle +from fhir.parameters import Parameters +from pytest_bdd import given, parsers, then, when + +from tests.acceptance.conftest import ResponseContext +from tests.conftest import Client + + +@given("the API is running new") +def check_api_is_running(client: Client) -> None: + response = client.send_health_check() + assert response.status_code == 200 + + +@when("I send a valid Parameters resource to the endpoint") +def send_get_request( + client: Client, + response_context: ResponseContext, + simple_request_payload: Parameters, +) -> None: + response_context.response = client.send_to_get_structured_record_endpoint( + json.dumps(simple_request_payload) + ) + + +@when("I send a valid Parameters resource to a nonexistent endpoint") +def send_to_nonexistent_endpoint( + client: Client, + response_context: ResponseContext, + simple_request_payload: Parameters, +) -> None: + nonexistent_endpoint = f"{client.base_url}/nonexistent" + response_context.response = requests.post( + url=nonexistent_endpoint, + data=json.dumps(simple_request_payload), + timeout=timedelta(seconds=1).total_seconds(), + ) + + +@then( + parsers.cfparse( + "the response status code should be {expected_status:d}", + extra_types={"expected_status": int}, + ) +) +def check_status_code(response_context: ResponseContext, expected_status: int) -> None: + assert response_context.response is not None, "Response has not been set." + assert response_context.response.status_code == expected_status, ( + f"Expected status {expected_status}, " + f"got {response_context.response.status_code}" + ) + + +@then("the response should contain a valid Bundle resource") +def check_response_contains( + response_context: ResponseContext, expected_response_payload: Bundle +) -> None: + """Verify the response contains the expected text. + + Args: + context: Behave context containing the response + expected_text: Text that should be in the response + """ + assert response_context.response, "Response has not been set." + assert response_context.response.json() == expected_response_payload, ( + "Expected response payload does not match actual response payload." + ) diff --git a/gateway-api/tests/conftest.py b/gateway-api/tests/conftest.py index 997b044d..a8f6759c 100644 --- a/gateway-api/tests/conftest.py +++ b/gateway-api/tests/conftest.py @@ -8,6 +8,8 @@ import pytest import requests from dotenv import find_dotenv, load_dotenv +from fhir.bundle import Bundle +from fhir.parameters import Parameters # Load environment variables from .env file in the workspace root # find_dotenv searches upward from current directory for .env file @@ -17,30 +19,31 @@ class Client: """A simple HTTP client for testing purposes.""" - def __init__(self, lambda_url: str, timeout: timedelta = timedelta(seconds=1)): - self._lambda_url = lambda_url + def __init__(self, base_url: str, timeout: timedelta = timedelta(seconds=1)): + self.base_url = base_url self._timeout = timeout.total_seconds() - def get_structured_record(self, nhs_number: str) -> requests.Response: + def send_to_get_structured_record_endpoint(self, payload: str) -> requests.Response: """ Send a request to the get_structured_record endpoint with the given NHS number. """ - payload = json.dumps( - { - "resourceType": "Parameters", - "parameter": [ - { - "name": "patientNHSNumber", - "valueIdentifier": { - "system": "https://fhir.nhs.uk/Id/nhs-number", - "value": nhs_number, - }, - }, - ], - } + url = f"{self.base_url}/patient/$gpc.getstructuredrecord" + headers = {"Content-Type": "application/json"} + return requests.post( + url=url, + data=payload, + headers=headers, + timeout=self._timeout, ) - url = f"{self._lambda_url}/patient/$gpc.getstructuredrecord" - return self._send(url=url, payload=payload) + + def send_health_check(self) -> requests.Response: + """ + Send a health check request to the API. + Returns: + Response object from the request + """ + url = f"{self.base_url}/health" + return requests.get(url=url, timeout=self._timeout) def send(self, message: str) -> requests.Response: """ @@ -51,7 +54,7 @@ def send(self, message: str) -> requests.Response: Response object from the request """ payload = json.dumps({"payload": message}) - url = f"{self._lambda_url}/2015-03-31/functions/function/invocations" + url = f"{self.base_url}/2015-03-31/functions/function/invocations" return self._send(url=url, payload=payload) def send_without_payload(self) -> requests.Response: @@ -61,7 +64,7 @@ def send_without_payload(self) -> requests.Response: Response object from the request """ empty_payload = json.dumps({}) - url = f"{self._lambda_url}/2015-03-31/functions/function/invocations" + url = f"{self.base_url}/2015-03-31/functions/function/invocations" return self._send(url=url, payload=empty_payload) def _send(self, url: str, payload: str) -> requests.Response: @@ -72,10 +75,54 @@ def _send(self, url: str, payload: str) -> requests.Response: ) +@pytest.fixture +def simple_request_payload() -> Parameters: + return { + "resourceType": "Parameters", + "parameter": [ + { + "name": "patientNHSNumber", + "valueIdentifier": { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + }, + }, + ], + } + + +@pytest.fixture +def expected_response_payload() -> Bundle: + return { + "resourceType": "Bundle", + "id": "example-patient-bundle", + "type": "collection", + "timestamp": "2026-01-12T10:00:00Z", + "entry": [ + { + "fullUrl": "urn:uuid:123e4567-e89b-12d3-a456-426614174000", + "resource": { + "resourceType": "Patient", + "id": "9999999999", + "identifier": [ + { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + } + ], + "name": [{"use": "official", "family": "Doe", "given": ["John"]}], + "gender": "male", + "birthDate": "1985-04-12", + }, + } + ], + } + + @pytest.fixture(scope="module") def client(base_url: str) -> Client: """Create a test client for the application.""" - return Client(lambda_url=base_url) + return Client(base_url=base_url) @pytest.fixture(scope="module") diff --git a/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json b/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json index 47af75f5..43863082 100644 --- a/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json +++ b/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json @@ -88,46 +88,6 @@ "status": 200 }, "type": "Synchronous/HTTP" - }, - { - "description": "a request for the hello world message", - "pending": false, - "request": { - "body": { - "content": { - "payload": "World" - }, - "contentType": "application/json", - "encoded": false - }, - "headers": { - "Content-Type": [ - "application/json" - ] - }, - "method": "POST", - "path": "/2015-03-31/functions/function/invocations" - }, - "response": { - "body": { - "content": { - "body": "Hello, World!", - "headers": { - "Content-Type": "application/json" - }, - "statusCode": 200 - }, - "contentType": "application/json", - "encoded": false - }, - "headers": { - "Content-Type": [ - "application/json" - ] - }, - "status": 200 - }, - "type": "Synchronous/HTTP" } ], "metadata": { diff --git a/gateway-api/tests/contract/test_consumer_contract.py b/gateway-api/tests/contract/test_consumer_contract.py index 68b33aaa..12e51a17 100644 --- a/gateway-api/tests/contract/test_consumer_contract.py +++ b/gateway-api/tests/contract/test_consumer_contract.py @@ -13,54 +13,6 @@ class TestConsumerContract: """Consumer contract tests to define expected API behavior.""" - def test_get_hello_world(self) -> None: - """Test the consumer's expectation of the hello world endpoint. - - This test defines the contract: when the consumer requests - GET/PUT/POST/PATCH/TRACE/DELETE to the - /2015-03-31/functions/function/invocations endpoint, with a payload of "World", - a 200 response containing "Hello, World!" is returned. - """ - pact = Pact(consumer="GatewayAPIConsumer", provider="GatewayAPIProvider") - - # Define the expected interaction - ( - pact.upon_receiving("a request for the hello world message") - .with_body({"payload": "World"}) - .with_request( - method="POST", - path="/2015-03-31/functions/function/invocations", - ) - .will_respond_with(status=200) - .with_body( - { - "statusCode": 200, - "headers": {"Content-Type": "application/json"}, - "body": "Hello, World!", - }, - content_type="application/json", - ) - ) - - # Start the mock server and execute the test - with pact.serve() as server: - # Make the actual request to the mock provider - response = requests.post( - f"{server.url}/2015-03-31/functions/function/invocations", - json={"payload": "World"}, - timeout=10, - ) - - # Verify the response matches expectations - assert response.status_code == 200 - body = response.json() - assert body["body"] == "Hello, World!" - assert body["statusCode"] == 200 - assert body["headers"] == {"Content-Type": "application/json"} - - # Write the pact file after the test - pact.write_file("tests/contract/pacts") - def test_get_structured_record(self) -> None: """Test the consumer's expectation of the get structured record endpoint. diff --git a/gateway-api/tests/integration/test_get_structured_record.py b/gateway-api/tests/integration/test_get_structured_record.py new file mode 100644 index 00000000..b32460eb --- /dev/null +++ b/gateway-api/tests/integration/test_get_structured_record.py @@ -0,0 +1,42 @@ +"""Integration tests for the gateway API using pytest.""" + +import json + +from fhir.bundle import Bundle +from fhir.parameters import Parameters + +from tests.conftest import Client + + +class TestGetStructuredRecord: + """Test suite for the hello world endpoint.""" + + def test_happy_path_returns_200( + self, client: Client, simple_request_payload: Parameters + ) -> None: + """Test that the root endpoint returns a 200 status code.""" + response = client.send_to_get_structured_record_endpoint( + json.dumps(simple_request_payload) + ) + assert response.status_code == 200 + + def test_happy_path_returns_correct_message( + self, + client: Client, + simple_request_payload: Parameters, + expected_response_payload: Bundle, + ) -> None: + """Test that the root endpoint returns the correct message.""" + response = client.send_to_get_structured_record_endpoint( + json.dumps(simple_request_payload) + ) + assert response.json() == expected_response_payload + + def test_happy_path_content_type( + self, client: Client, simple_request_payload: Parameters + ) -> None: + """Test that the response has the correct content type.""" + response = client.send_to_get_structured_record_endpoint( + json.dumps(simple_request_payload) + ) + assert "application/json" in response.headers["Content-Type"] diff --git a/gateway-api/tests/integration/test_main.py b/gateway-api/tests/integration/test_main.py index 0ccb3786..49f22d9a 100644 --- a/gateway-api/tests/integration/test_main.py +++ b/gateway-api/tests/integration/test_main.py @@ -1,49 +1,42 @@ """Integration tests for the gateway API using pytest.""" +import json + +from fhir.bundle import Bundle +from fhir.parameters import Parameters + from tests.conftest import Client -class TestHelloWorld: +class TestGetStructuredRecord: """Test suite for the hello world endpoint.""" - def test_hello_world_returns_200(self, client: Client) -> None: + def test_happy_path_returns_200( + self, client: Client, simple_request_payload: Parameters + ) -> None: """Test that the root endpoint returns a 200 status code.""" - response = client.send("world") + response = client.send_to_get_structured_record_endpoint( + json.dumps(simple_request_payload) + ) assert response.status_code == 200 - def test_hello_world_returns_correct_message(self, client: Client) -> None: + def test_happy_path_returns_correct_message( + self, + client: Client, + simple_request_payload: Parameters, + expected_response_payload: Bundle, + ) -> None: """Test that the root endpoint returns the correct message.""" - response = client.send("World") - assert response.json()["body"] == "Hello, World!" - - def test_hello_world_content_type(self, client: Client) -> None: + response = client.send_to_get_structured_record_endpoint( + json.dumps(simple_request_payload) + ) + assert response.json() == expected_response_payload + + def test_happy_path_content_type( + self, client: Client, simple_request_payload: Bundle + ) -> None: """Test that the response has the correct content type.""" - response = client.send("world") + response = client.send_to_get_structured_record_endpoint( + json.dumps(simple_request_payload) + ) assert "application/json" in response.headers["Content-Type"] - - def test_nonexistent_returns_error(self, client: Client) -> None: - """Test that non-existent routes return 404.""" - response = client.send("nonexistent") - assert response.status_code == 200 - - body = response.json().get("body") - assert body == "Provided name cannot be found. name=nonexistent" - - status_code = response.json().get("statusCode") - assert status_code == 404 - - def test_no_payload_returns_error(self, client: Client) -> None: - """Test that an error is returned when no payload is provided.""" - response = client.send_without_payload() - assert response.status_code == 200 - - body = response.json().get("body") - assert body == "Name is required" - - def test_empty_name_returns_error(self, client: Client) -> None: - """Test that an error is returned when an empty name is provided.""" - response = client.send("") - assert response.status_code == 200 - - body = response.json().get("body") - assert body == "Name cannot be empty" From a0d594e172056f5d0cca31f449827a7ddcc3c5f9 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Tue, 20 Jan 2026 19:18:39 +0000 Subject: [PATCH 12/27] [GPCAPIM-254]: Clean up github actions. --- .github/actions/start-app/action.yaml | 4 +- .../actions/start-local-lambda/action.yaml | 50 ------------------- 2 files changed, 2 insertions(+), 52 deletions(-) delete mode 100644 .github/actions/start-local-lambda/action.yaml diff --git a/.github/actions/start-app/action.yaml b/.github/actions/start-app/action.yaml index 97a88061..ec2afd77 100644 --- a/.github/actions/start-app/action.yaml +++ b/.github/actions/start-app/action.yaml @@ -8,7 +8,7 @@ inputs: health-path: description: "Health probe path to POST" required: false - default: "/2015-03-31/functions/function/invocations" + default: "/health" max-seconds: description: "Maximum seconds to wait for readiness" required: false @@ -38,7 +38,7 @@ runs: MAX="${{ inputs.max-seconds }}" echo "Waiting for app at ${HEALTH_URL} (max ${MAX}s)..." for i in $(seq 1 "${MAX}"); do - if curl -sSf -X POST "${HEALTH_URL}" -d '{}' >/dev/null; then + if curl -sSf -X GET "${HEALTH_URL}" >/dev/null; then echo "App is ready" exit 0 fi diff --git a/.github/actions/start-local-lambda/action.yaml b/.github/actions/start-local-lambda/action.yaml deleted file mode 100644 index 49d77405..00000000 --- a/.github/actions/start-local-lambda/action.yaml +++ /dev/null @@ -1,50 +0,0 @@ -name: "Start local Lambda environment" -description: "Start a local AWS Lambda environment for testing" -inputs: - deploy-command: - description: "Command to start local Lambda" - required: false - default: "make deploy" - health-path: - description: "Health probe path to POST" - required: false - default: "/2015-03-31/functions/function/invocations" - max-seconds: - description: "Maximum seconds to wait for readiness" - required: false - default: "60" -python-version: - description: "Python version to install" - required: true -runs: - using: "composite" - steps: - - name: "Start local Lambda environment" - shell: bash - env: - PYTHON_VERSION: ${{ inputs.python-version }} - run: | - set -euo pipefail - echo "Starting local Lambda: '${{ inputs.deploy-command }}'" - nohup ${{ inputs.deploy-command }} >/tmp/lambda.log 2>&1 & - echo $! > /tmp/lambda.pid - echo "PID: $(cat /tmp/lambda.pid)" - - name: "Wait for Lambda to be ready" - shell: bash - run: | - set -euo pipefail - BASE_URL="${BASE_URL:-http://localhost:5000}" - HEALTH_URL="${BASE_URL}${{ inputs.health-path }}" - MAX="${{ inputs.max-seconds }}" - echo "Waiting for Lambda at ${HEALTH_URL} (max ${MAX}s)..." - for i in $(seq 1 "${MAX}"); do - if curl -sSf -X POST "${HEALTH_URL}" -d '{}' >/dev/null; then - echo "Lambda is ready" - exit 0 - fi - sleep 1 - done - echo "Lambda did not become ready in time" - echo "---- recent lambda log ----" - tail -n 200 /tmp/lambda.log || true - exit 1 From a21dcee8e31767dd91030c0d5f147681fdb549be Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 10:16:03 +0000 Subject: [PATCH 13/27] [GPCAPIM-254]: Clean up. --- .github/actions/start-app/action.yaml | 4 +- gateway-api/src/fhir/bundle.py | 4 -- gateway-api/src/fhir/human_name.py | 2 - gateway-api/src/fhir/identifier.py | 2 - gateway-api/src/fhir/parameters.py | 4 -- gateway-api/src/fhir/patient.py | 2 - gateway-api/src/gateway_api/app.py | 11 ++- .../get_structed_record/__init__.py | 6 ++ .../get_structed_record/request.py | 8 ++- gateway-api/src/gateway_api/handler.py | 17 ----- gateway-api/src/gateway_api/test_app.py | 13 ++-- gateway-api/src/gateway_api/test_handler.py | 54 -------------- .../tests/acceptance/steps/happy_path.py | 8 +-- .../acceptance/steps/hello_world_steps.py | 70 ------------------- gateway-api/tests/conftest.py | 30 -------- .../integration/test_get_structured_record.py | 2 - gateway-api/tests/integration/test_main.py | 42 ----------- infrastructure/images/gateway-api/Dockerfile | 1 + 18 files changed, 29 insertions(+), 251 deletions(-) delete mode 100644 gateway-api/src/gateway_api/handler.py delete mode 100644 gateway-api/src/gateway_api/test_handler.py delete mode 100644 gateway-api/tests/acceptance/steps/hello_world_steps.py delete mode 100644 gateway-api/tests/integration/test_main.py diff --git a/.github/actions/start-app/action.yaml b/.github/actions/start-app/action.yaml index ec2afd77..0f6d6d20 100644 --- a/.github/actions/start-app/action.yaml +++ b/.github/actions/start-app/action.yaml @@ -6,7 +6,7 @@ inputs: required: false default: "make deploy" health-path: - description: "Health probe path to POST" + description: "Health check path" required: false default: "/health" max-seconds: @@ -26,7 +26,7 @@ runs: run: | set -euo pipefail echo "Starting app: '${{ inputs.deploy-command }}'" - nohup ${{ inputs.deploy-command }} >/tmp/app.log 2>&1 & + nohup ${{ inputs.deploy-command }} > /tmp/app.log 2>&1 & echo $! > /tmp/app.pid echo "PID: $(cat /tmp/app.pid)" - name: "Wait for app to be ready" diff --git a/gateway-api/src/fhir/bundle.py b/gateway-api/src/fhir/bundle.py index afe49f94..5fbc9a3b 100644 --- a/gateway-api/src/fhir/bundle.py +++ b/gateway-api/src/fhir/bundle.py @@ -6,15 +6,11 @@ class BundleEntry(TypedDict): - """FHIR Bundle entry.""" - fullUrl: str resource: Patient class Bundle(TypedDict): - """FHIR Bundle resource.""" - resourceType: str id: str type: str diff --git a/gateway-api/src/fhir/human_name.py b/gateway-api/src/fhir/human_name.py index bc56d529..2a73deb0 100644 --- a/gateway-api/src/fhir/human_name.py +++ b/gateway-api/src/fhir/human_name.py @@ -4,8 +4,6 @@ class HumanName(TypedDict): - """FHIR HumanName type.""" - use: str family: str given: list[str] diff --git a/gateway-api/src/fhir/identifier.py b/gateway-api/src/fhir/identifier.py index feb62aee..4e59908d 100644 --- a/gateway-api/src/fhir/identifier.py +++ b/gateway-api/src/fhir/identifier.py @@ -4,7 +4,5 @@ class Identifier(TypedDict): - """FHIR Identifier type.""" - system: str value: str diff --git a/gateway-api/src/fhir/parameters.py b/gateway-api/src/fhir/parameters.py index eef46ca6..30b7cce8 100644 --- a/gateway-api/src/fhir/parameters.py +++ b/gateway-api/src/fhir/parameters.py @@ -6,14 +6,10 @@ class Parameter(TypedDict): - """FHIR Parameter type.""" - name: str valueIdentifier: Identifier class Parameters(TypedDict): - """FHIR Parameters resource.""" - resourceType: str parameter: list[Parameter] diff --git a/gateway-api/src/fhir/patient.py b/gateway-api/src/fhir/patient.py index e23eb449..33d0ce41 100644 --- a/gateway-api/src/fhir/patient.py +++ b/gateway-api/src/fhir/patient.py @@ -7,8 +7,6 @@ class Patient(TypedDict): - """FHIR Patient resource.""" - resourceType: str id: str identifier: list[Identifier] diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index f6ef72f3..ba1ca820 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -4,8 +4,10 @@ from fhir import Bundle from flask import Flask, request -from gateway_api.get_structed_record.handler import GetStructuredRecordHandler -from gateway_api.get_structed_record.request import GetStructuredRecordRequest +from gateway_api.get_structed_record import ( + GetStructuredRecordHandler, + GetStructuredRecordRequest, +) app = Flask(__name__) @@ -29,6 +31,9 @@ def health_check() -> HealthCheckResponse: if __name__ == "__main__": host = os.getenv("FLASK_HOST") + port = os.getenv("FLASK_PORT") if host is None: raise RuntimeError("FLASK_HOST environment variable is not set.") - app.run(host=host, port=8080) + if port is None: + raise RuntimeError("FLASK_PORT environment variable is not set.") + app.run(host=host, port=int(port)) diff --git a/gateway-api/src/gateway_api/get_structed_record/__init__.py b/gateway-api/src/gateway_api/get_structed_record/__init__.py index e69de29b..9861ad49 100644 --- a/gateway-api/src/gateway_api/get_structed_record/__init__.py +++ b/gateway-api/src/gateway_api/get_structed_record/__init__.py @@ -0,0 +1,6 @@ +"""Get Structured Record module.""" + +from gateway_api.get_structed_record.handler import GetStructuredRecordHandler +from gateway_api.get_structed_record.request import GetStructuredRecordRequest + +__all__ = ["GetStructuredRecordHandler", "GetStructuredRecordRequest"] diff --git a/gateway-api/src/gateway_api/get_structed_record/request.py b/gateway-api/src/gateway_api/get_structed_record/request.py index 7a0f7311..9f054b76 100644 --- a/gateway-api/src/gateway_api/get_structed_record/request.py +++ b/gateway-api/src/gateway_api/get_structed_record/request.py @@ -7,6 +7,10 @@ class GetStructuredRecordRequest: + interaction_id: str = "urn:nhs:names:services:gpconnect:gpc.getstructuredrecord-1" + resource: str = "patient" + fhir_operation: str = "$gpc.getstructuredrecord" + def __init__(self, request: Request) -> None: self._http_request = request self._headers = request.headers @@ -24,10 +28,10 @@ def nhs_number(self) -> str: @property def consumer_asid(self) -> str: - consumer_asid: str = self._headers["X-Consumer-ASID"] + consumer_asid: str = self._headers["Ssp-from"] return consumer_asid @property def provider_asid(self) -> str: - provider_asid: str = self._headers["X-Provider-ASID"] + provider_asid: str = self._headers["Ssp-to"] return provider_asid diff --git a/gateway-api/src/gateway_api/handler.py b/gateway-api/src/gateway_api/handler.py deleted file mode 100644 index a3f66b94..00000000 --- a/gateway-api/src/gateway_api/handler.py +++ /dev/null @@ -1,17 +0,0 @@ -from clinical_data_common import get_hello - - -class User: - def __init__(self, name: str): - self._name = name - - @property - def name(self) -> str: - return self._name - - -def greet(user: User) -> str: - if user.name == "nonexistent": - raise ValueError("nonexistent user provided.") - hello = get_hello() - return f"{hello}{user.name}!" diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py index a382ccc1..f05537f9 100644 --- a/gateway-api/src/gateway_api/test_app.py +++ b/gateway-api/src/gateway_api/test_app.py @@ -1,6 +1,7 @@ """Unit tests for the Flask app endpoints.""" from collections.abc import Generator +from typing import TYPE_CHECKING import pytest from flask import Flask @@ -8,23 +9,22 @@ from gateway_api.app import app +if TYPE_CHECKING: + from fhir.parameters import Parameters + @pytest.fixture def client() -> Generator[FlaskClient[Flask], None, None]: - """Create a Flask test client.""" app.config["TESTING"] = True with app.test_client() as client: yield client class TestGetStructuredRecord: - """Unit tests for the get_structured_record function.""" - def test_get_structured_record_returns_200_with_bundle( self, client: FlaskClient[Flask] ) -> None: - """Test that get_structured_record returns 200 with a bundle.""" - body = { + body: Parameters = { "resourceType": "Parameters", "parameter": [ { @@ -53,12 +53,9 @@ def test_get_structured_record_returns_200_with_bundle( class TestHealthCheck: - """Unit tests for the health_check function.""" - def test_health_check_returns_200_and_healthy_status( self, client: FlaskClient[Flask] ) -> None: - """Test that health_check returns 200 with healthy status.""" response = client.get("/health") assert response.status_code == 200 diff --git a/gateway-api/src/gateway_api/test_handler.py b/gateway-api/src/gateway_api/test_handler.py deleted file mode 100644 index f2092af7..00000000 --- a/gateway-api/src/gateway_api/test_handler.py +++ /dev/null @@ -1,54 +0,0 @@ -import pytest - -from gateway_api.handler import User, greet - - -class TestUser: - """Test suite for the User class.""" - - @pytest.mark.parametrize( - "name", - [ - "Alice", - "Bob", - "", - "O'Brien", - ], - ) - def test_user_initialization(self, name: str) -> None: - """Test that a User can be initialized with various names.""" - user = User(name) - assert user.name == name - - def test_user_name_is_immutable(self) -> None: - """Test that the name property cannot be directly modified.""" - user = User("Charlie") - with pytest.raises(AttributeError): - user.name = "David" # type: ignore[misc] - - -class TestGreet: - """Test suite for the greet function.""" - - @pytest.mark.parametrize( - ("name", "expected_greeting"), - [ - ("Alice", "Hello, Alice!"), - ("Bob", "Hello, Bob!"), - ("", "Hello, !"), - ("O'Brien", "Hello, O'Brien!"), - ("Nonexistent", "Hello, Nonexistent!"), - ("nonexistent ", "Hello, nonexistent !"), - ], - ) - def test_greet_with_valid_users(self, name: str, expected_greeting: str) -> None: - """Test that greet returns the correct greeting for various valid users.""" - user = User(name) - result = greet(user) - assert result == expected_greeting - - def test_greet_with_nonexistent_user_raises_value_error(self) -> None: - """Test that greet raises ValueError for nonexistent user.""" - user = User("nonexistent") - with pytest.raises(ValueError, match="nonexistent user provided."): - greet(user) diff --git a/gateway-api/tests/acceptance/steps/happy_path.py b/gateway-api/tests/acceptance/steps/happy_path.py index b1f64404..016ccb82 100644 --- a/gateway-api/tests/acceptance/steps/happy_path.py +++ b/gateway-api/tests/acceptance/steps/happy_path.py @@ -1,4 +1,4 @@ -"""Step definitions for Gateway API hello world feature.""" +"""Step definitions for Gateway API happy path feature.""" import json from datetime import timedelta @@ -61,12 +61,6 @@ def check_status_code(response_context: ResponseContext, expected_status: int) - def check_response_contains( response_context: ResponseContext, expected_response_payload: Bundle ) -> None: - """Verify the response contains the expected text. - - Args: - context: Behave context containing the response - expected_text: Text that should be in the response - """ assert response_context.response, "Response has not been set." assert response_context.response.json() == expected_response_payload, ( "Expected response payload does not match actual response payload." diff --git a/gateway-api/tests/acceptance/steps/hello_world_steps.py b/gateway-api/tests/acceptance/steps/hello_world_steps.py deleted file mode 100644 index a7439725..00000000 --- a/gateway-api/tests/acceptance/steps/hello_world_steps.py +++ /dev/null @@ -1,70 +0,0 @@ -"""Step definitions for Gateway API hello world feature.""" - -from pytest_bdd import given, parsers, then, when - -from tests.acceptance.conftest import ResponseContext -from tests.conftest import Client - - -@given("the API is running") -def step_api_is_running(client: Client) -> None: - """Verify the API test client is available. - - Args: - client: Test client from conftest.py - """ - response = client.send("test") - assert response.text is not None - assert response.status_code == 200 - - -@when(parsers.cfparse('I send "{message}" to the endpoint')) -def step_send_get_request( - client: Client, message: str, response_context: ResponseContext -) -> None: - """Send a GET request to the specified endpoint. - - Args: - client: Test client - endpoint: The API endpoint path to request - """ - response_context.response = client.send(message) - - -# fmt: off -@then(parsers.cfparse("the response status code should be {expected_status:d}",extra_types={"expected_status": int})) # noqa: E501 - BDD steps must be declared on a singular line. -# fmt: on -def step_check_status_code( - response_context: ResponseContext, expected_status: int -) -> None: - """Verify the response status code matches expected value. - - Args: - context: Behave context containing the response - expected_status: Expected HTTP status code - """ - assert response_context.response, "Response has not been set." - - data = response_context.response.json() - - assert data["statusCode"] == expected_status, ( - f"Expected status {expected_status}, " - f"got {response_context.response.status_code}" - ) - - -@then(parsers.cfparse('the response should contain "{expected_text}"')) -def step_check_response_contains( - response_context: ResponseContext, expected_text: str -) -> None: - """Verify the response contains the expected text. - - Args: - context: Behave context containing the response - expected_text: Text that should be in the response - """ - assert response_context.response, "Response has not been set." - - assert expected_text in response_context.response.text, ( - f"Expected '{expected_text}' in response, got: {response_context.response.text}" - ) diff --git a/gateway-api/tests/conftest.py b/gateway-api/tests/conftest.py index a8f6759c..7c982b8c 100644 --- a/gateway-api/tests/conftest.py +++ b/gateway-api/tests/conftest.py @@ -1,6 +1,5 @@ """Pytest configuration and shared fixtures for gateway API tests.""" -import json import os from datetime import timedelta from typing import cast @@ -45,35 +44,6 @@ def send_health_check(self) -> requests.Response: url = f"{self.base_url}/health" return requests.get(url=url, timeout=self._timeout) - def send(self, message: str) -> requests.Response: - """ - Send a request to the APIs with some given parameters. - Args: - data: The data to send in the request payload - Returns: - Response object from the request - """ - payload = json.dumps({"payload": message}) - url = f"{self.base_url}/2015-03-31/functions/function/invocations" - return self._send(url=url, payload=payload) - - def send_without_payload(self) -> requests.Response: - """ - Send a request to the APIs without a payload. - Returns: - Response object from the request - """ - empty_payload = json.dumps({}) - url = f"{self.base_url}/2015-03-31/functions/function/invocations" - return self._send(url=url, payload=empty_payload) - - def _send(self, url: str, payload: str) -> requests.Response: - return requests.post( - url=url, - data=payload, - timeout=self._timeout, - ) - @pytest.fixture def simple_request_payload() -> Parameters: diff --git a/gateway-api/tests/integration/test_get_structured_record.py b/gateway-api/tests/integration/test_get_structured_record.py index b32460eb..d0f8dc99 100644 --- a/gateway-api/tests/integration/test_get_structured_record.py +++ b/gateway-api/tests/integration/test_get_structured_record.py @@ -9,8 +9,6 @@ class TestGetStructuredRecord: - """Test suite for the hello world endpoint.""" - def test_happy_path_returns_200( self, client: Client, simple_request_payload: Parameters ) -> None: diff --git a/gateway-api/tests/integration/test_main.py b/gateway-api/tests/integration/test_main.py deleted file mode 100644 index 49f22d9a..00000000 --- a/gateway-api/tests/integration/test_main.py +++ /dev/null @@ -1,42 +0,0 @@ -"""Integration tests for the gateway API using pytest.""" - -import json - -from fhir.bundle import Bundle -from fhir.parameters import Parameters - -from tests.conftest import Client - - -class TestGetStructuredRecord: - """Test suite for the hello world endpoint.""" - - def test_happy_path_returns_200( - self, client: Client, simple_request_payload: Parameters - ) -> None: - """Test that the root endpoint returns a 200 status code.""" - response = client.send_to_get_structured_record_endpoint( - json.dumps(simple_request_payload) - ) - assert response.status_code == 200 - - def test_happy_path_returns_correct_message( - self, - client: Client, - simple_request_payload: Parameters, - expected_response_payload: Bundle, - ) -> None: - """Test that the root endpoint returns the correct message.""" - response = client.send_to_get_structured_record_endpoint( - json.dumps(simple_request_payload) - ) - assert response.json() == expected_response_payload - - def test_happy_path_content_type( - self, client: Client, simple_request_payload: Bundle - ) -> None: - """Test that the response has the correct content type.""" - response = client.send_to_get_structured_record_endpoint( - json.dumps(simple_request_payload) - ) - assert "application/json" in response.headers["Content-Type"] diff --git a/infrastructure/images/gateway-api/Dockerfile b/infrastructure/images/gateway-api/Dockerfile index f3ce577f..67cbb1d3 100644 --- a/infrastructure/images/gateway-api/Dockerfile +++ b/infrastructure/images/gateway-api/Dockerfile @@ -8,6 +8,7 @@ WORKDIR /resources/build/gateway-api ENV PYTHONPATH=/resources/build/gateway-api ENV FLASK_HOST="0.0.0.0" +ENV FLASK_PORT="8080" ENTRYPOINT ["python"] CMD ["gateway_api/app.py"] From 3157ddbb86f84ddd2a67d804cea00853f6d2d290 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 10:16:55 +0000 Subject: [PATCH 14/27] [GPCAPIM-254]: Unit tests no loonger exist in the top level. --- scripts/tests/run-test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/tests/run-test.sh b/scripts/tests/run-test.sh index d2c3177c..7d7fd4e1 100755 --- a/scripts/tests/run-test.sh +++ b/scripts/tests/run-test.sh @@ -25,7 +25,7 @@ cd "$(git rev-parse --show-toplevel)" # Determine test path based on test type if [[ "$TEST_TYPE" = "unit" ]]; then - TEST_PATH="test_*.py src/*/test_*.py" + TEST_PATH="src/*/test_*.py" else TEST_PATH="tests/${TEST_TYPE}/" fi From e882bcf6a51d667893c8cbdcb70c88b83aa22aba Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 11:46:21 +0000 Subject: [PATCH 15/27] [GPCAPIM-254]: Correct content-type header. --- gateway-api/openapi.yaml | 12 ++++++------ gateway-api/tests/conftest.py | 2 +- gateway-api/tests/contract/test_consumer_contract.py | 10 +++++----- .../tests/integration/test_get_structured_record.py | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/gateway-api/openapi.yaml b/gateway-api/openapi.yaml index 8a52ceab..e91ff7bd 100644 --- a/gateway-api/openapi.yaml +++ b/gateway-api/openapi.yaml @@ -19,12 +19,12 @@ paths: name: Content-Type schema: type: string - enum: [application/json] + enum: [application/fhir+json] required: true requestBody: required: true content: - application/json: + application/fhir+json: schema: type: object properties: @@ -56,10 +56,10 @@ paths: name: Content-Type schema: type: string - enum: [application/json] + enum: [application/fhir+json] required: true content: - application/json: + application/fhir+json: schema: type: object properties: @@ -72,7 +72,7 @@ paths: properties: Content-Type: type: string - example: "application/json" + example: "application/fhir+json" body: type: object description: FHIR Bundle containing patient data @@ -150,7 +150,7 @@ paths: '200': description: Service is healthy content: - application/json: + application/fhir+json: schema: type: object properties: diff --git a/gateway-api/tests/conftest.py b/gateway-api/tests/conftest.py index 7c982b8c..5facb089 100644 --- a/gateway-api/tests/conftest.py +++ b/gateway-api/tests/conftest.py @@ -27,7 +27,7 @@ def send_to_get_structured_record_endpoint(self, payload: str) -> requests.Respo Send a request to the get_structured_record endpoint with the given NHS number. """ url = f"{self.base_url}/patient/$gpc.getstructuredrecord" - headers = {"Content-Type": "application/json"} + headers = {"Content-Type": "application/fhir+json"} return requests.post( url=url, data=payload, diff --git a/gateway-api/tests/contract/test_consumer_contract.py b/gateway-api/tests/contract/test_consumer_contract.py index 12e51a17..2f828234 100644 --- a/gateway-api/tests/contract/test_consumer_contract.py +++ b/gateway-api/tests/contract/test_consumer_contract.py @@ -65,16 +65,16 @@ def test_get_structured_record(self) -> None: }, ], }, - content_type="application/json", + content_type="application/fhir+json", ) - .with_header("Content-Type", "application/json") + .with_header("Content-Type", "application/fhir+json") .with_request( method="POST", path="/patient/$gpc.getstructuredrecord", ) .will_respond_with(status=200) - .with_body(expected_bundle, content_type="application/json") - .with_header("Content-Type", "application/json") + .with_body(expected_bundle, content_type="application/fhir+json") + .with_header("Content-Type", "application/fhir+json") ) # Start the mock server and execute the test @@ -96,7 +96,7 @@ def test_get_structured_record(self) -> None: ], } ), - headers={"Content-Type": "application/json"}, + headers={"Content-Type": "application/fhir+json"}, timeout=10, ) diff --git a/gateway-api/tests/integration/test_get_structured_record.py b/gateway-api/tests/integration/test_get_structured_record.py index d0f8dc99..0215d840 100644 --- a/gateway-api/tests/integration/test_get_structured_record.py +++ b/gateway-api/tests/integration/test_get_structured_record.py @@ -37,4 +37,4 @@ def test_happy_path_content_type( response = client.send_to_get_structured_record_endpoint( json.dumps(simple_request_payload) ) - assert "application/json" in response.headers["Content-Type"] + assert "application/fhir+json" in response.headers["Content-Type"] From ba7fa309277359d49dcfb91a3ea264dd2bce5c01 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 11:47:22 +0000 Subject: [PATCH 16/27] [GPCAPIM-254]: Handle response object, rather than just pass back dict. --- gateway-api/src/fhir/__init__.py | 3 ++ gateway-api/src/fhir/operation_outcome.py | 14 ++++++++ gateway-api/src/gateway_api/app.py | 8 ++--- .../get_structed_record/handler.py | 9 +++-- .../get_structed_record/request.py | 36 ++++++++++++++++--- 5 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 gateway-api/src/fhir/operation_outcome.py diff --git a/gateway-api/src/fhir/__init__.py b/gateway-api/src/fhir/__init__.py index ea58d5c8..4ad915ee 100644 --- a/gateway-api/src/fhir/__init__.py +++ b/gateway-api/src/fhir/__init__.py @@ -3,6 +3,7 @@ from fhir.bundle import Bundle, BundleEntry from fhir.human_name import HumanName from fhir.identifier import Identifier +from fhir.operation_outcome import OperationOutcome, OperationOutcomeIssue from fhir.parameters import Parameter, Parameters from fhir.patient import Patient @@ -11,6 +12,8 @@ "BundleEntry", "HumanName", "Identifier", + "OperationOutcome", + "OperationOutcomeIssue", "Parameter", "Parameters", "Patient", diff --git a/gateway-api/src/fhir/operation_outcome.py b/gateway-api/src/fhir/operation_outcome.py new file mode 100644 index 00000000..d25765f5 --- /dev/null +++ b/gateway-api/src/fhir/operation_outcome.py @@ -0,0 +1,14 @@ +"""FHIR OperationOutcome resource.""" + +from typing import TypedDict + + +class OperationOutcomeIssue(TypedDict): + severity: str + code: str + diagnostics: str + + +class OperationOutcome(TypedDict): + resourceType: str + issue: list[OperationOutcomeIssue] diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index ba1ca820..8858888e 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -1,8 +1,8 @@ import os from typing import TypedDict -from fhir import Bundle from flask import Flask, request +from flask.wrappers import Response from gateway_api.get_structed_record import ( GetStructuredRecordHandler, @@ -17,10 +17,10 @@ class HealthCheckResponse(TypedDict): @app.route("/patient/$gpc.getstructuredrecord", methods=["POST"]) -def get_structured_record() -> Bundle: +def get_structured_record() -> Response: get_structured_record_request = GetStructuredRecordRequest(request) - response = GetStructuredRecordHandler.handle(get_structured_record_request) - return response + GetStructuredRecordHandler.handle(get_structured_record_request) + return get_structured_record_request.build_response() @app.route("/health", methods=["GET"]) diff --git a/gateway-api/src/gateway_api/get_structed_record/handler.py b/gateway-api/src/gateway_api/get_structed_record/handler.py index 5a301bd4..eb692fae 100644 --- a/gateway-api/src/gateway_api/get_structed_record/handler.py +++ b/gateway-api/src/gateway_api/get_structed_record/handler.py @@ -1,11 +1,14 @@ -from fhir import Bundle +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from fhir import Bundle from gateway_api.get_structed_record.request import GetStructuredRecordRequest class GetStructuredRecordHandler: @classmethod - def handle(cls, request: GetStructuredRecordRequest) -> Bundle: + def handle(cls, request: GetStructuredRecordRequest) -> None: bundle: Bundle = { "resourceType": "Bundle", "id": "example-patient-bundle", @@ -32,4 +35,4 @@ def handle(cls, request: GetStructuredRecordRequest) -> Bundle: } ], } - return bundle + request.set_positive_response(bundle) diff --git a/gateway-api/src/gateway_api/get_structed_record/request.py b/gateway-api/src/gateway_api/get_structed_record/request.py index 9f054b76..3d6ecdaa 100644 --- a/gateway-api/src/gateway_api/get_structed_record/request.py +++ b/gateway-api/src/gateway_api/get_structed_record/request.py @@ -1,9 +1,9 @@ -from typing import TYPE_CHECKING +import json -from flask.wrappers import Request - -if TYPE_CHECKING: - from fhir import Parameters +from fhir import OperationOutcome, Parameters +from fhir.bundle import Bundle +from fhir.operation_outcome import OperationOutcomeIssue +from flask.wrappers import Request, Response class GetStructuredRecordRequest: @@ -15,6 +15,8 @@ def __init__(self, request: Request) -> None: self._http_request = request self._headers = request.headers self._request_body: Parameters = request.get_json() + self._response_body: Bundle | OperationOutcome | None = None + self._status_code: int | None = None @property def trace_id(self) -> str: @@ -35,3 +37,27 @@ def consumer_asid(self) -> str: def provider_asid(self) -> str: provider_asid: str = self._headers["Ssp-to"] return provider_asid + + def build_response(self) -> Response: + return Response( + response=json.dumps(self._response_body), + status=self._status_code, + mimetype="application/fhir+json", + ) + + def set_positive_response(self, bundle: Bundle) -> None: + self._status_code = 200 + self._response_body = bundle + + def set_negative_response(self, error: str) -> None: + self._status_code = 500 + self._response_body = OperationOutcome( + resourceType="OperationOutcome", + issue=[ + OperationOutcomeIssue( + severity="error", + code="exception", + diagnostics=error, + ) + ], + ) From 8dfb142394ea44a7b5290f841470c995684e1faa Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 11:53:26 +0000 Subject: [PATCH 17/27] [GPCAPIM-254]: Correct content-type header for healthcheck. --- gateway-api/openapi.yaml | 2 +- .../pacts/GatewayAPIConsumer-GatewayAPIProvider.json | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gateway-api/openapi.yaml b/gateway-api/openapi.yaml index e91ff7bd..96b3f30e 100644 --- a/gateway-api/openapi.yaml +++ b/gateway-api/openapi.yaml @@ -150,7 +150,7 @@ paths: '200': description: Service is healthy content: - application/fhir+json: + application/json: schema: type: object properties: diff --git a/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json b/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json index 43863082..6d60fef5 100644 --- a/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json +++ b/gateway-api/tests/contract/pacts/GatewayAPIConsumer-GatewayAPIProvider.json @@ -32,12 +32,12 @@ ], "resourceType": "Parameters" }, - "contentType": "application/json", + "contentType": "application/fhir+json", "encoded": false }, "headers": { "Content-Type": [ - "application/json" + "application/fhir+json" ] }, "method": "POST", @@ -77,12 +77,12 @@ "timestamp": "2026-01-12T10:00:00Z", "type": "collection" }, - "contentType": "application/json", + "contentType": "application/fhir+json", "encoded": false }, "headers": { "Content-Type": [ - "application/json" + "application/fhir+json" ] }, "status": 200 From bceea35731a1269c8f256e6e6a3b4264f4a6663b Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 12:12:51 +0000 Subject: [PATCH 18/27] [GPCAPIM-254]: Add error hanlding in app. --- gateway-api/src/gateway_api/app.py | 7 +++-- gateway-api/src/gateway_api/conftest.py | 20 +++++++++++++++ gateway-api/src/gateway_api/test_app.py | 34 +++++++++++++++---------- 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 gateway-api/src/gateway_api/conftest.py diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index 8858888e..885d3350 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -18,8 +18,11 @@ class HealthCheckResponse(TypedDict): @app.route("/patient/$gpc.getstructuredrecord", methods=["POST"]) def get_structured_record() -> Response: - get_structured_record_request = GetStructuredRecordRequest(request) - GetStructuredRecordHandler.handle(get_structured_record_request) + try: + get_structured_record_request = GetStructuredRecordRequest(request) + GetStructuredRecordHandler.handle(get_structured_record_request) + except Exception as e: + get_structured_record_request.set_negative_response(str(e)) return get_structured_record_request.build_response() diff --git a/gateway-api/src/gateway_api/conftest.py b/gateway-api/src/gateway_api/conftest.py new file mode 100644 index 00000000..4042053d --- /dev/null +++ b/gateway-api/src/gateway_api/conftest.py @@ -0,0 +1,20 @@ +"""Pytest configuration and shared fixtures for gateway API tests.""" + +import pytest +from fhir.parameters import Parameters + + +@pytest.fixture +def simple_request_payload() -> Parameters: + return { + "resourceType": "Parameters", + "parameter": [ + { + "name": "patientNHSNumber", + "valueIdentifier": { + "system": "https://fhir.nhs.uk/Id/nhs-number", + "value": "9999999999", + }, + }, + ], + } diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py index f05537f9..f61fbc0a 100644 --- a/gateway-api/src/gateway_api/test_app.py +++ b/gateway-api/src/gateway_api/test_app.py @@ -22,21 +22,11 @@ def client() -> Generator[FlaskClient[Flask], None, None]: class TestGetStructuredRecord: def test_get_structured_record_returns_200_with_bundle( - self, client: FlaskClient[Flask] + self, client: FlaskClient[Flask], simple_request_payload: "Parameters" ) -> None: - body: Parameters = { - "resourceType": "Parameters", - "parameter": [ - { - "name": "patientNHSNumber", - "valueIdentifier": { - "system": "https://fhir.nhs.uk/Id/nhs-number", - "value": "9999999999", - }, - }, - ], - } - response = client.post("/patient/$gpc.getstructuredrecord", json=body) + response = client.post( + "/patient/$gpc.getstructuredrecord", json=simple_request_payload + ) assert response.status_code == 200 data = response.get_json() @@ -51,6 +41,22 @@ def test_get_structured_record_returns_200_with_bundle( assert data["entry"][0]["resource"]["id"] == "9999999999" assert data["entry"][0]["resource"]["identifier"][0]["value"] == "9999999999" + def test_get_structured_record_handles_exception( + self, + client: FlaskClient[Flask], + monkeypatch: pytest.MonkeyPatch, + simple_request_payload: "Parameters", + ) -> None: + monkeypatch.setattr( + "gateway_api.get_structed_record.GetStructuredRecordHandler.handle", + Exception(), + ) + + response = client.post( + "/patient/$gpc.getstructuredrecord", json=simple_request_payload + ) + assert response.status_code == 500 + class TestHealthCheck: def test_health_check_returns_200_and_healthy_status( From f1a3fad6ece7dd5b714bbb0cb1d4f9c2b98861f6 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 15:18:04 +0000 Subject: [PATCH 19/27] [GPCAPIM-254]: Force new deployment of ecs task in preview environment. --- infrastructure/environments/preview/main.tf | 4 ---- 1 file changed, 4 deletions(-) diff --git a/infrastructure/environments/preview/main.tf b/infrastructure/environments/preview/main.tf index 30dfdbb2..f09c9fdf 100644 --- a/infrastructure/environments/preview/main.tf +++ b/infrastructure/environments/preview/main.tf @@ -228,10 +228,6 @@ resource "aws_ecs_service" "branch" { container_port = var.container_port } - lifecycle { - ignore_changes = [task_definition] - } - depends_on = [aws_lb_listener_rule.branch] } From d9ee75defc78462f4ec228d1435cde69fc6ba479 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 16:42:45 +0000 Subject: [PATCH 20/27] Revert "[GPCAPIM-254]: Force new deployment of ecs task in preview environment." This reverts commit f1a3fad6ece7dd5b714bbb0cb1d4f9c2b98861f6. --- infrastructure/environments/preview/main.tf | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/infrastructure/environments/preview/main.tf b/infrastructure/environments/preview/main.tf index f09c9fdf..30dfdbb2 100644 --- a/infrastructure/environments/preview/main.tf +++ b/infrastructure/environments/preview/main.tf @@ -228,6 +228,10 @@ resource "aws_ecs_service" "branch" { container_port = var.container_port } + lifecycle { + ignore_changes = [task_definition] + } + depends_on = [aws_lb_listener_rule.branch] } From d3797235583385c784f3918a743f7081c9655dfd Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 16:50:40 +0000 Subject: [PATCH 21/27] [GPCAPIM-254]: Make it clear which version is deployed in the health endpoint. --- Makefile | 4 +++- gateway-api/src/gateway_api/app.py | 14 ++++++++++++-- infrastructure/images/gateway-api/Dockerfile | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 598014d2..40b5f7fe 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,8 @@ IMAGE_REPOSITORY := ${ECR_URL} endif IMAGE_NAME := ${IMAGE_REPOSITORY}:${IMAGE_TAG} +COMMIT_VERSION := $(shell git rev-parse --short HEAD) +BUILD_DATE := $(shell date -u +"%Y%m%d") # ============================================================================== # Example CI/CD targets are: dependencies, build, publish, deploy, clean, etc. @@ -45,7 +47,7 @@ build-gateway-api: dependencies .PHONY: build build: build-gateway-api # Build the project artefact @Pipeline @echo "Building Docker x86 image using Docker. Utilising python version: ${PYTHON_VERSION} ..." - @$(docker) buildx build --platform linux/amd64 --load --provenance=false --build-arg PYTHON_VERSION=${PYTHON_VERSION} -t ${IMAGE_NAME} infrastructure/images/gateway-api + @$(docker) buildx build --platform linux/amd64 --load --provenance=false --build-arg PYTHON_VERSION=${PYTHON_VERSION} --build-arg COMMIT_VERSION=${COMMIT_VERSION} --build-arg BUILD_DATE=${BUILD_DATE} -t ${IMAGE_NAME} infrastructure/images/gateway-api @echo "Docker image '${IMAGE_NAME}' built successfully!" publish: # Publish the project artefact @Pipeline diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index 885d3350..1065861e 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -14,6 +14,7 @@ class HealthCheckResponse(TypedDict): status: str + version: str @app.route("/patient/$gpc.getstructuredrecord", methods=["POST"]) @@ -29,14 +30,23 @@ def get_structured_record() -> Response: @app.route("/health", methods=["GET"]) def health_check() -> HealthCheckResponse: """Health check endpoint.""" - return {"status": "healthy"} + version: str = "unkonwn" + + commit_version: str | None = os.getenv("COMMIT_VERSION") + build_date: str | None = os.getenv("BUILD_DATE") + if commit_version and build_date: + version = f"{build_date}.{commit_version}" + + return {"status": "healthy", "version": version} if __name__ == "__main__": host = os.getenv("FLASK_HOST") - port = os.getenv("FLASK_PORT") if host is None: raise RuntimeError("FLASK_HOST environment variable is not set.") + port = os.getenv("FLASK_PORT") if port is None: raise RuntimeError("FLASK_PORT environment variable is not set.") + print(f"Starting Gateway API on {host}:{port}") + print(f"Version: {os.getenv('COMMIT_VERSION')}") app.run(host=host, port=int(port)) diff --git a/infrastructure/images/gateway-api/Dockerfile b/infrastructure/images/gateway-api/Dockerfile index 67cbb1d3..ffcd00df 100644 --- a/infrastructure/images/gateway-api/Dockerfile +++ b/infrastructure/images/gateway-api/Dockerfile @@ -10,6 +10,11 @@ ENV PYTHONPATH=/resources/build/gateway-api ENV FLASK_HOST="0.0.0.0" ENV FLASK_PORT="8080" +ARG COMMIT_VERSION +ENV COMMIT_VERSION=$COMMIT_VERSION +ARG BUILD_DATE +ENV BUILD_DATE=$BUILD_DATE + ENTRYPOINT ["python"] CMD ["gateway_api/app.py"] From 12dfae8bd527edd118066f71b91f6d62fc867e29 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 16:58:42 +0000 Subject: [PATCH 22/27] [GPCAPIM-254]: Correct module name. --- gateway-api/src/gateway_api/app.py | 2 +- gateway-api/src/gateway_api/get_structed_record/__init__.py | 6 ------ .../src/gateway_api/get_structured_record/__init__.py | 6 ++++++ .../handler.py | 2 +- .../request.py | 0 gateway-api/src/gateway_api/test_app.py | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) delete mode 100644 gateway-api/src/gateway_api/get_structed_record/__init__.py create mode 100644 gateway-api/src/gateway_api/get_structured_record/__init__.py rename gateway-api/src/gateway_api/{get_structed_record => get_structured_record}/handler.py (93%) rename gateway-api/src/gateway_api/{get_structed_record => get_structured_record}/request.py (100%) diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index 1065861e..94bb8b07 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -4,7 +4,7 @@ from flask import Flask, request from flask.wrappers import Response -from gateway_api.get_structed_record import ( +from gateway_api.get_structured_record import ( GetStructuredRecordHandler, GetStructuredRecordRequest, ) diff --git a/gateway-api/src/gateway_api/get_structed_record/__init__.py b/gateway-api/src/gateway_api/get_structed_record/__init__.py deleted file mode 100644 index 9861ad49..00000000 --- a/gateway-api/src/gateway_api/get_structed_record/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Get Structured Record module.""" - -from gateway_api.get_structed_record.handler import GetStructuredRecordHandler -from gateway_api.get_structed_record.request import GetStructuredRecordRequest - -__all__ = ["GetStructuredRecordHandler", "GetStructuredRecordRequest"] diff --git a/gateway-api/src/gateway_api/get_structured_record/__init__.py b/gateway-api/src/gateway_api/get_structured_record/__init__.py new file mode 100644 index 00000000..c279cb73 --- /dev/null +++ b/gateway-api/src/gateway_api/get_structured_record/__init__.py @@ -0,0 +1,6 @@ +"""Get Structured Record module.""" + +from gateway_api.get_structured_record.handler import GetStructuredRecordHandler +from gateway_api.get_structured_record.request import GetStructuredRecordRequest + +__all__ = ["GetStructuredRecordHandler", "GetStructuredRecordRequest"] diff --git a/gateway-api/src/gateway_api/get_structed_record/handler.py b/gateway-api/src/gateway_api/get_structured_record/handler.py similarity index 93% rename from gateway-api/src/gateway_api/get_structed_record/handler.py rename to gateway-api/src/gateway_api/get_structured_record/handler.py index eb692fae..15479f28 100644 --- a/gateway-api/src/gateway_api/get_structed_record/handler.py +++ b/gateway-api/src/gateway_api/get_structured_record/handler.py @@ -3,7 +3,7 @@ if TYPE_CHECKING: from fhir import Bundle -from gateway_api.get_structed_record.request import GetStructuredRecordRequest +from gateway_api.get_structured_record.request import GetStructuredRecordRequest class GetStructuredRecordHandler: diff --git a/gateway-api/src/gateway_api/get_structed_record/request.py b/gateway-api/src/gateway_api/get_structured_record/request.py similarity index 100% rename from gateway-api/src/gateway_api/get_structed_record/request.py rename to gateway-api/src/gateway_api/get_structured_record/request.py diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py index f61fbc0a..97c9c3e2 100644 --- a/gateway-api/src/gateway_api/test_app.py +++ b/gateway-api/src/gateway_api/test_app.py @@ -48,7 +48,7 @@ def test_get_structured_record_handles_exception( simple_request_payload: "Parameters", ) -> None: monkeypatch.setattr( - "gateway_api.get_structed_record.GetStructuredRecordHandler.handle", + "gateway_api.get_structured_record.GetStructuredRecordHandler.handle", Exception(), ) From 791ecc42e189df6e294412c538505e2420bb2e12 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Wed, 21 Jan 2026 17:06:14 +0000 Subject: [PATCH 23/27] [GPCAPIM-254]: Use tech radars preferred alpine and run thorugh non-root user. --- infrastructure/images/gateway-api/Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/infrastructure/images/gateway-api/Dockerfile b/infrastructure/images/gateway-api/Dockerfile index ffcd00df..54824a4b 100644 --- a/infrastructure/images/gateway-api/Dockerfile +++ b/infrastructure/images/gateway-api/Dockerfile @@ -1,6 +1,9 @@ # Retrieve the python version from build arguments, deliberately set to "invalid" by default to highlight when no version is provided when building the container. ARG PYTHON_VERSION=invalid -FROM python:${PYTHON_VERSION}-slim AS gateway-api +FROM python:${PYTHON_VERSION}-alpine3.23 AS gateway-api + +RUN addgroup -S nonroot \ + && adduser -S gateway_api_user -G nonroot COPY resources/ /resources @@ -15,6 +18,7 @@ ENV COMMIT_VERSION=$COMMIT_VERSION ARG BUILD_DATE ENV BUILD_DATE=$BUILD_DATE +USER gateway_api_user ENTRYPOINT ["python"] CMD ["gateway_api/app.py"] From 2fca0d7694b62c9acc75663bb574a5b535044629 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Thu, 22 Jan 2026 14:07:08 +0000 Subject: [PATCH 24/27] [GPCAPIM-254]: APIM handles CSRF through its auth design; We don't have to. --- gateway-api/src/gateway_api/app.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index 94bb8b07..5e6234b6 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -10,6 +10,9 @@ ) app = Flask(__name__) +# This is a RESTful API, behind the proxy on APIM, which itself handles CSRF. +# We shall not handle CSRF +app.config["WTF_CSRF_ENABLED"] = False class HealthCheckResponse(TypedDict): From 0bff6ce18352feb4990a636ac21805845b85a086 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Thu, 22 Jan 2026 15:42:52 +0000 Subject: [PATCH 25/27] [GPCAPIM-254]: Reduce fragility of code by pushing environment variable getting in to their own testable methods. --- gateway-api/src/gateway_api/app.py | 24 ++++++++++++++------ gateway-api/src/gateway_api/test_app.py | 29 ++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/gateway-api/src/gateway_api/app.py b/gateway-api/src/gateway_api/app.py index 5e6234b6..d9a6a756 100644 --- a/gateway-api/src/gateway_api/app.py +++ b/gateway-api/src/gateway_api/app.py @@ -20,6 +20,20 @@ class HealthCheckResponse(TypedDict): version: str +def get_app_host() -> str: + host = os.getenv("FLASK_HOST") + if host is None: + raise RuntimeError("FLASK_HOST environment variable is not set.") + return host + + +def get_app_port() -> int: + port = os.getenv("FLASK_PORT") + if port is None: + raise RuntimeError("FLASK_PORT environment variable is not set.") + return int(port) + + @app.route("/patient/$gpc.getstructuredrecord", methods=["POST"]) def get_structured_record() -> Response: try: @@ -44,12 +58,8 @@ def health_check() -> HealthCheckResponse: if __name__ == "__main__": - host = os.getenv("FLASK_HOST") - if host is None: - raise RuntimeError("FLASK_HOST environment variable is not set.") - port = os.getenv("FLASK_PORT") - if port is None: - raise RuntimeError("FLASK_PORT environment variable is not set.") + host = get_app_host() + port = get_app_port() print(f"Starting Gateway API on {host}:{port}") print(f"Version: {os.getenv('COMMIT_VERSION')}") - app.run(host=host, port=int(port)) + app.run(host=host, port=port) diff --git a/gateway-api/src/gateway_api/test_app.py b/gateway-api/src/gateway_api/test_app.py index 97c9c3e2..5ccb583a 100644 --- a/gateway-api/src/gateway_api/test_app.py +++ b/gateway-api/src/gateway_api/test_app.py @@ -1,5 +1,6 @@ """Unit tests for the Flask app endpoints.""" +import os from collections.abc import Generator from typing import TYPE_CHECKING @@ -7,7 +8,7 @@ from flask import Flask from flask.testing import FlaskClient -from gateway_api.app import app +from gateway_api.app import app, get_app_host, get_app_port if TYPE_CHECKING: from fhir.parameters import Parameters @@ -20,6 +21,32 @@ def client() -> Generator[FlaskClient[Flask], None, None]: yield client +class TestAppInitialization: + def test_get_app_host_returns_set_host_name(self) -> None: + os.environ["FLASK_HOST"] = "host_is_set" + + actual = get_app_host() + assert actual == "host_is_set" + + def test_get_app_host_raises_runtime_error_if_host_name_not_set(self) -> None: + del os.environ["FLASK_HOST"] + + with pytest.raises(RuntimeError): + _ = get_app_host() + + def test_get_app_port_returns_set_port_number(self) -> None: + os.environ["FLASK_PORT"] = "8080" + + actual = get_app_port() + assert actual == 8080 + + def test_get_app_port_raises_runtime_error_if_port_not_set(self) -> None: + del os.environ["FLASK_PORT"] + + with pytest.raises(RuntimeError): + _ = get_app_port() + + class TestGetStructuredRecord: def test_get_structured_record_returns_200_with_bundle( self, client: FlaskClient[Flask], simple_request_payload: "Parameters" From 07418b960a02fdf0586fd9efd17387d213490ec3 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Thu, 22 Jan 2026 16:00:07 +0000 Subject: [PATCH 26/27] [GPCAPIM-254]: Reduce fragility of code by ensuring headers are correctly read. --- .../get_structured_record/test_request.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 gateway-api/src/gateway_api/get_structured_record/test_request.py diff --git a/gateway-api/src/gateway_api/get_structured_record/test_request.py b/gateway-api/src/gateway_api/get_structured_record/test_request.py new file mode 100644 index 00000000..316960c8 --- /dev/null +++ b/gateway-api/src/gateway_api/get_structured_record/test_request.py @@ -0,0 +1,57 @@ +import pytest +from flask import Request + +from gateway_api.get_structured_record.request import GetStructuredRecordRequest + + +class MockRequest: + def __init__(self, headers: dict[str, str]) -> None: + self.headers = headers + + def get_json(self) -> dict[str, str]: + return {} + + +@pytest.fixture +def mock_request_with_headers() -> MockRequest: + headers = { + "Ssp-TraceID": "test-trace-id", + "Ssp-from": "test-consumer-asid", + "Ssp-to": "test-provider-asid", + } + return MockRequest(headers) + + +class TestGetStructuredRecordRequest: + def test_trace_id_is_pulled_from_ssp_traceid_header( + self, mock_request_with_headers: Request + ) -> None: + get_structured_record_request = GetStructuredRecordRequest( + request=mock_request_with_headers + ) + + actual = get_structured_record_request.trace_id + expected = "test-trace-id" + assert actual == expected + + def test_consumer_asid_is_pulled_from_ssp_from_header( + self, mock_request_with_headers: Request + ) -> None: + get_structured_record_request = GetStructuredRecordRequest( + request=mock_request_with_headers + ) + + actual = get_structured_record_request.consumer_asid + expected = "test-consumer-asid" + assert actual == expected + + def test_provider_asid_is_pulled_from_ssp_to_header( + self, mock_request_with_headers: Request + ) -> None: + get_structured_record_request = GetStructuredRecordRequest( + request=mock_request_with_headers + ) + + actual = get_structured_record_request.provider_asid + expected = "test-provider-asid" + assert actual == expected From a844d578a2f963b3ec0552701252a415c03c49f9 Mon Sep 17 00:00:00 2001 From: David Hamill <109090521+davidhamill1-nhs@users.noreply.github.com> Date: Thu, 22 Jan 2026 16:09:52 +0000 Subject: [PATCH 27/27] [GPCAPIM-254]: Reduce fragility of code by ensuring NHS number is correctly read. --- .../get_structured_record/test_request.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/gateway-api/src/gateway_api/get_structured_record/test_request.py b/gateway-api/src/gateway_api/get_structured_record/test_request.py index 316960c8..3ad3b12d 100644 --- a/gateway-api/src/gateway_api/get_structured_record/test_request.py +++ b/gateway-api/src/gateway_api/get_structured_record/test_request.py @@ -1,25 +1,27 @@ import pytest +from fhir.parameters import Parameters from flask import Request from gateway_api.get_structured_record.request import GetStructuredRecordRequest class MockRequest: - def __init__(self, headers: dict[str, str]) -> None: + def __init__(self, headers: dict[str, str], body: Parameters) -> None: self.headers = headers + self.body = body def get_json(self) -> dict[str, str]: return {} @pytest.fixture -def mock_request_with_headers() -> MockRequest: +def mock_request_with_headers(simple_request_payload: Parameters) -> MockRequest: headers = { "Ssp-TraceID": "test-trace-id", "Ssp-from": "test-consumer-asid", "Ssp-to": "test-provider-asid", } - return MockRequest(headers) + return MockRequest(headers, simple_request_payload) class TestGetStructuredRecordRequest: @@ -55,3 +57,14 @@ def test_provider_asid_is_pulled_from_ssp_to_header( actual = get_structured_record_request.provider_asid expected = "test-provider-asid" assert actual == expected + + def test_nhs_number_is_pulled_from_request_body( + self, mock_request_with_headers: Request + ) -> None: + get_structured_record_request = GetStructuredRecordRequest( + request=mock_request_with_headers + ) + + actual = get_structured_record_request.nhs_number + expected = "9999999999" + assert actual == expected