Skip to content

Commit

Permalink
Add Sandbox check GitHub Action
Browse files Browse the repository at this point in the history
  • Loading branch information
JackPlowman committed Apr 3, 2024
1 parent fa76ab2 commit 1aab16d
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .dependabot/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
version: 1
update_configs:
- package-ecosystem: "github-actions"
directory: "/"
groups:
github-action-dependencies:
patterns:
- "*"
- package_manager: "javascript"
directory: "/"
update_schedule: "live"
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/sandbox-checks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Sandbox Check
on: pull_request
jobs:
sandbox-check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v5
with:
python-version: '3.8'
cache: 'poetry'
- name: Install dependencies
run: make install
working-directory: ./sandbox
- name: Check Sandbox Formatting
run: make check-format
- name: Run Sandbox Linting
run: make lint
working-directory: ./sandbox
- name: Run Sandbox Unit Tests
run: make test
working-directory: ./sandbox
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ install: install-node install-python .git/hooks/pre-commit

#Run the npm linting script (specified in package.json). Used to check the syntax and formatting of files.
lint:
# TODO: Python linting
find . -name '*.py' -not -path '**/.venv/*' | xargs poetry run flake8

format:
find . -name '*.py' -not -path '**/.venv/*' | xargs poetry run black --check

#Removes build/ + dist/ directories
clean:
rm -rf build
Expand Down
2 changes: 1 addition & 1 deletion sandbox/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WORKDIR /sandbox

RUN pip install poetry

RUN poetry install --only sandbox
RUN poetry install --without dev

EXPOSE 9000

Expand Down
11 changes: 10 additions & 1 deletion sandbox/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ SHELL := /bin/bash
dirname := $(notdir $(patsubst %/,%,$(CURDIR)))

install:
poetry install
poetry install --no-root

list:
@grep '^[^#[:space:]].*:' Makefile

build:
docker build -t validate-relationships-service-api-sandbox .

check-format:
poetry run black --check .

format:
poetry run black .

lint:
poetry run flake8 .

start:
poetry run flask --app api.app:app run -p 9000

Expand Down
24 changes: 19 additions & 5 deletions sandbox/api/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from flask import Flask
from .utils import get_response
from logging import getLogger, basicConfig, INFO
from logging import INFO, basicConfig, getLogger
from typing import Union

from flask import Flask, request

from .utils import get_response

app = Flask(__name__)
basicConfig(level=INFO, format="%(asctime)s - %(message)s")
logger = getLogger(__name__)
Expand All @@ -17,7 +19,19 @@ def related_persons() -> Union[dict, tuple]:
"""

try:
return get_response("./api/responses/RelatedPerson_identifier_and_patient.json")
if not request.args.get("identifier"):
print(f"Args not found: {request.args}")
return {"error": "Missing required parameter 'identifier'"}, 400

if request.args.get("identifier") and request.args.get("patient"):
return get_response(
"./api/responses/RelatedPerson_identifier_and_patient.json"
)
elif request.args.get("identifier"):
return get_response("./api/responses/RelatedPerson.json")
else:
raise Exception("Invalid request")

except Exception as e:
logger.error(e)
return {"error": "Internal Server Error"}, 500
return {"error": "Sandbox Internal Server Error"}, 500
Empty file added sandbox/api/tests/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions sandbox/api/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from unittest.mock import MagicMock, patch

import pytest

from ..app import app


@pytest.fixture()
def client() -> object:
"""Create a test client for the app."""
return app.test_client()


@patch("sandbox.api.app.get_response")
def test_related_person(mock_get_response: MagicMock, client: object) -> None:
""""""
# Arrange
mock_get_response.return_value = {"data": "mocked"}
# Act
client.get("/RelatedPerson?identifier=1234")
# Assert
mock_get_response.assert_called_once_with("./api/responses/RelatedPerson.json")
2 changes: 1 addition & 1 deletion sandbox/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions sandbox/poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[virtualenvs]
in-project = true
4 changes: 1 addition & 3 deletions sandbox/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ package-mode = false

[tool.poetry.dependencies]
python = "^3.8"

[tool.poetry.group.sandbox.dependencies]
flask = "^3.0.2"
gunicorn = "^21.2.0"
flask = "^3.0.2"

[tool.poetry.dev-dependencies]
flake8 = "^3.7.9"
Expand Down
2 changes: 1 addition & 1 deletion scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
make lint
make format lint
31 changes: 20 additions & 11 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://github.com/NHSDigital/pytest-nhsd-apim/blob/main/tests/test_examples.py
for more ideas on how to test the authorization of your API.
"""

import requests
import pytest
from os import getenv
Expand All @@ -20,9 +21,11 @@ def test_wait_for_ping(nhsd_apim_proxy_url):
resp = requests.get(f"{nhsd_apim_proxy_url}/_ping")
deployed_commitId = resp.json().get("commitId")

while (deployed_commitId != getenv('SOURCE_COMMIT_ID')
and retries <= 30
and resp.status_code == 200):
while (
deployed_commitId != getenv("SOURCE_COMMIT_ID")
and retries <= 30
and resp.status_code == 200
):
resp = requests.get(f"{nhsd_apim_proxy_url}/_ping")
deployed_commitId = resp.json().get("commitId")
retries += 1
Expand All @@ -32,7 +35,7 @@ def test_wait_for_ping(nhsd_apim_proxy_url):
elif retries >= 30:
pytest.fail("Timeout Error - max retries")

assert deployed_commitId == getenv('SOURCE_COMMIT_ID')
assert deployed_commitId == getenv("SOURCE_COMMIT_ID")


@pytest.mark.smoketest
Expand All @@ -47,14 +50,20 @@ def test_status(nhsd_apim_proxy_url, status_endpoint_auth_headers):
@pytest.mark.smoketest
def test_wait_for_status(nhsd_apim_proxy_url, status_endpoint_auth_headers):
retries = 0
resp = requests.get(f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers)
resp = requests.get(
f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers
)
deployed_commitId = resp.json().get("commitId")

while (deployed_commitId != getenv('SOURCE_COMMIT_ID')
and retries <= 30
and resp.status_code == 200
and resp.json().get("version")):
resp = requests.get(f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers)
while (
deployed_commitId != getenv("SOURCE_COMMIT_ID")
and retries <= 30
and resp.status_code == 200
and resp.json().get("version")
):
resp = requests.get(
f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers
)
deployed_commitId = resp.json().get("commitId")
retries += 1

Expand All @@ -65,7 +74,7 @@ def test_wait_for_status(nhsd_apim_proxy_url, status_endpoint_auth_headers):
elif not resp.json().get("version"):
pytest.fail("version not found")

assert deployed_commitId == getenv('SOURCE_COMMIT_ID')
assert deployed_commitId == getenv("SOURCE_COMMIT_ID")


@pytest.mark.nhsd_apim_authorization({"access": "application", "level": "level0"})
Expand Down

0 comments on commit 1aab16d

Please sign in to comment.