Skip to content

Commit 4f76eb5

Browse files
Merge pull request #19 from NHSDigital/feature/eli-132-empty-endpoint-for-eligibility
ELI-132 Empty endpoint for eligibility
2 parents 8d1f746 + 7f860db commit 4f76eb5

File tree

11 files changed

+101
-13
lines changed

11 files changed

+101
-13
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pythonVersion = "3.13"
7878

7979
[tool.pytest.ini_options]
8080
log_cli = true
81-
log_cli_level = "INFO"
81+
log_cli_level = "DEBUG"
8282
log_format = "%(asctime)s %(levelname)s %(message)s"
8383
log_date_format = "%Y-%m-%d %H:%M:%S"
8484

src/eligibility_signposting_api/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from eligibility_signposting_api import repos, services
1111
from eligibility_signposting_api.config import LOG_LEVEL, config, init_logging
1212
from eligibility_signposting_api.error_handler import handle_exception
13+
from eligibility_signposting_api.views.eligibility import eligibility
1314
from eligibility_signposting_api.views.hello import hello
1415

1516

@@ -32,7 +33,8 @@ def create_app() -> Flask:
3233
app.logger.info("app created")
3334

3435
# Register views & error handler
35-
app.register_blueprint(hello)
36+
app.register_blueprint(eligibility, url_prefix="/eligibility")
37+
app.register_blueprint(hello, url_prefix="/hello")
3638
app.register_error_handler(Exception, handle_exception)
3739

3840
# Set up dependency injection using wireup

src/eligibility_signposting_api/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@ def init_logging() -> None:
3636
"wsgi": {"class": "logging.StreamHandler", "stream": "ext://sys.stdout", "formatter": "default"}
3737
},
3838
"root": {"level": level, "handlers": ["wsgi"]},
39+
"loggers": {
40+
"eligibility_signposting_api.app": {"level": level, "handlers": ["wsgi"], "propagate": False},
41+
},
3942
}
4043
)

src/eligibility_signposting_api/error_handler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
from flask import make_response
66
from flask.typing import ResponseReturnValue
7+
from werkzeug.exceptions import HTTPException
78

89
from eligibility_signposting_api.views.response_models import Problem
910

1011
logger = logging.getLogger(__name__)
1112

1213

13-
def handle_exception(e: BaseException) -> ResponseReturnValue:
14+
def handle_exception(e: Exception) -> ResponseReturnValue | HTTPException:
1415
logger.exception("Unexpected Exception", exc_info=e)
16+
17+
# Let Flask handle its own exceptions for now.
18+
if isinstance(e, HTTPException):
19+
return e
20+
1521
problem = Problem(
1622
title="Unexpected Exception",
1723
type=str(type(e)),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import NewType
2+
3+
NHSNumber = NewType("NHSNumber", str)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import logging
2+
from http import HTTPStatus
3+
4+
from flask import Blueprint, make_response
5+
from flask.typing import ResponseReturnValue
6+
7+
from eligibility_signposting_api.model.eligibility import NHSNumber
8+
9+
logger = logging.getLogger(__name__)
10+
11+
eligibility = Blueprint("eligibility", __name__)
12+
13+
14+
@eligibility.get("/<nhs_number>")
15+
def check_eligibility(nhs_number: NHSNumber) -> ResponseReturnValue:
16+
logger.info("nhs_number: %s", nhs_number)
17+
return make_response({}, HTTPStatus.OK)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from http import HTTPStatus
2+
3+
from brunns.matchers.data import json_matching as is_json_that
4+
from brunns.matchers.werkzeug import is_werkzeug_response as is_response
5+
from flask.testing import FlaskClient
6+
from hamcrest import assert_that, empty, is_
7+
8+
9+
def test_nhs_number_given(client: FlaskClient):
10+
# Given
11+
12+
# When
13+
response = client.get("/eligibility/12345")
14+
15+
# Then
16+
assert_that(response, is_response().with_status_code(HTTPStatus.OK).and_text(is_json_that(is_(empty()))))
17+
18+
19+
def test_no_nhs_number_given(client: FlaskClient):
20+
# Given
21+
22+
# When
23+
response = client.get("/eligibility/")
24+
25+
# Then
26+
assert_that(response, is_response().with_status_code(HTTPStatus.NOT_FOUND))

tests/integration/test_flask_app.py renamed to tests/integration/in_process/test_hello_world_endpoint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_no_name_given(client: FlaskClient):
2525
# Given
2626

2727
# When
28-
response = client.get("/")
28+
response = client.get("/hello/")
2929

3030
# Then
3131
assert_that(
@@ -40,7 +40,7 @@ def test_app_for_name_with_nickname(client: FlaskClient):
4040
# Given
4141

4242
# When
43-
response = client.get("/simon")
43+
response = client.get("/hello/simon")
4444

4545
# Then
4646
assert_that(
@@ -55,7 +55,7 @@ def test_app_for_nonexistent_name(client: FlaskClient):
5555
# Given
5656

5757
# When
58-
response = client.get("/fred")
58+
response = client.get("/hello/fred")
5959

6060
# Then
6161
assert_that(

tests/integration/test_app_running_as_lambda.py renamed to tests/integration/lambda/test_app_running_as_lambda.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def test_install_and_call_lambda_flask(lambda_client: BaseClient, flask_function
3838
"rawPath": "/",
3939
"rawQueryString": "",
4040
"headers": {"accept": "application/json", "content-type": "application/json"},
41-
"requestContext": {"http": {"sourceIp": "192.0.0.1", "method": "GET", "path": "/", "protocol": "HTTP/1.1"}},
41+
"requestContext": {
42+
"http": {"sourceIp": "192.0.0.1", "method": "GET", "path": "/hello/", "protocol": "HTTP/1.1"}
43+
},
4244
"body": None,
4345
"isBase64Encoded": False,
4446
}
@@ -60,7 +62,7 @@ def test_install_and_call_flask_lambda_over_http(flask_function_url: URL):
6062
# Given
6163

6264
# When
63-
response = httpx.get(str(flask_function_url))
65+
response = httpx.get(str(flask_function_url / "hello" / ""))
6466

6567
# Then
6668
assert_that(
@@ -76,7 +78,7 @@ def test_install_and_call_flask_lambda_with_nickname_over_http(flask_function_ur
7678
# Given
7779

7880
# When
79-
response = httpx.get(str(flask_function_url / "ayesh"), timeout=30)
81+
response = httpx.get(str(flask_function_url / "hello" / "ayesh"), timeout=30)
8082

8183
# Then
8284
assert_that(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import logging
2+
from http import HTTPStatus
3+
4+
from brunns.matchers.data import json_matching as is_json_that
5+
from brunns.matchers.werkzeug import is_werkzeug_response as is_response
6+
from flask.testing import FlaskClient
7+
from hamcrest import assert_that, empty, is_
8+
9+
logger = logging.getLogger(__name__)
10+
11+
12+
def test_nhs_number_given(client: FlaskClient):
13+
# Given
14+
15+
# When
16+
response = client.get("/eligibility/12345")
17+
18+
# Then
19+
assert_that(response, is_response().with_status_code(HTTPStatus.OK).and_text(is_json_that(is_(empty()))))
20+
21+
22+
def test_no_nhs_number_given(client: FlaskClient):
23+
# Given
24+
25+
# When
26+
response = client.get("/eligibility/")
27+
28+
# Then
29+
assert_that(response, is_response().with_status_code(HTTPStatus.NOT_FOUND))

0 commit comments

Comments
 (0)