Skip to content

Commit 65f5aee

Browse files
committed
NPA-5534: Updated sandbox tests
Signed-off-by: adamclarkson <[email protected]>
1 parent 8ffeefe commit 65f5aee

File tree

6 files changed

+53
-22
lines changed

6 files changed

+53
-22
lines changed

sandbox/api/app.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from .patch_consent import patch_consent_response
1111
from .post_consent import post_consent_response
1212
from .post_questionnaire_response import post_questionnaire_response_response
13+
from .utils import generate_response_from_example
14+
from .constants import METHOD_NOT_ALLOWED
1315

1416
app = Flask(__name__)
1517
basicConfig(level=INFO, format="%(asctime)s - %(message)s")
@@ -40,13 +42,24 @@ def get_related_persons() -> Union[dict, tuple]:
4042

4143

4244
@app.route(f"/{COMMON_PATH}/QuestionnaireResponse", methods=["GET"])
45+
@app.route(f"/{COMMON_PATH}/QuestionnaireResponse/", methods=["GET"])
4346
def get_questionnaire_response() -> Union[dict, tuple]:
4447
"""Sandbox API for GET /QuestionnaireResponse
4548
4649
Returns:
4750
Union[dict, tuple]: Response for GET /QuestionnaireResponse
4851
"""
49-
return get_questionnaire_response_response()
52+
return generate_response_from_example(METHOD_NOT_ALLOWED, 405)
53+
54+
55+
@app.route(f"/{COMMON_PATH}/QuestionnaireResponse/<identifier>", methods=["GET"])
56+
def get_questionnaire_response_id(identifier: str) -> Union[dict, tuple]:
57+
"""Sandbox API for GET /QuestionnaireResponse
58+
59+
Returns:
60+
Union[dict, tuple]: Response for GET /QuestionnaireResponse
61+
"""
62+
return get_questionnaire_response_response(identifier)
5063

5164

5265
@app.route(f"/{COMMON_PATH}/QuestionnaireResponse", methods=["POST"])

sandbox/api/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
INVALIDATED_RESOURCE = "./api/examples/errors/invalidated-resource.yaml"
1515
MISSING_IDENTIFIER = "./api/examples/errors/missing-identifier.yaml"
1616
INVALID_IDENTIFIER = "./api/examples/errors/invalid-identifier.yaml"
17+
METHOD_NOT_ALLOWED = "./api/examples/errors/method-not-allowed.yaml"
1718

1819
# GET Consent examples
1920
GET_CONSENT__DIRECTORY = "./api/examples/GET_Consent/"

sandbox/api/get_questionnaire_response.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@
55

66
from .constants import (
77
GET_QUESTIONNAIRE_RESPONSE__INVALID,
8-
GET_QUESTIONNAIRE_RESPONSE__MISSING,
98
GET_QUESTIONNAIRE_RESPONSE__NOT_FOUND,
109
GET_QUESTIONNAIRE_RESPONSE__SUCCESS,
1110
INTERNAL_SERVER_ERROR_EXAMPLE,
11+
METHOD_NOT_ALLOWED
1212
)
1313
from .utils import generate_response_from_example
1414

1515
basicConfig(level=INFO, format="%(asctime)s - %(message)s")
1616
logger = getLogger(__name__)
1717

1818

19-
def get_questionnaire_response_response() -> Union[dict, tuple]:
20-
"""Sandbox API for GET /QuestionnaireResponse
19+
def get_questionnaire_response_response(access_request_id: str) -> Union[dict, tuple]:
20+
"""Sandbox API for GET /QuestionnaireResponse/{id}
2121
2222
Returns:
23-
Union[dict, tuple]: Response for GET /QuestionnaireResponse
23+
Union[dict, tuple]: Response for GET /QuestionnaireResponse/{id}
2424
"""
2525
try:
26-
access_request_id = request.args.get("ID")
2726
if access_request_id == "156e1560-e532-4e2a-85ad-5aeff03dc43e":
2827
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__SUCCESS, 200)
2928
elif access_request_id == "INVALID":
3029
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__INVALID, 400)
3130
elif access_request_id == "" or access_request_id is None:
32-
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__MISSING, 400)
31+
return generate_response_from_example(METHOD_NOT_ALLOWED, 405)
3332
elif access_request_id == "60d09b82-f4bb-41f9-b41e-767999b4ac9b":
3433
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__NOT_FOUND, 404)
3534
else:

sandbox/api/tests/test_get_questionnaire_response.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,74 @@
33
from flask import Response
44
from json import dumps, loads
55

6+
from pycparser.plyparser import parameterized
7+
68
GET_QUESTIONNAIRE_RESPONSE_API_ENDPOINT = "/FHIR/R4/QuestionnaireResponse"
79

810

911
@pytest.mark.parametrize(
10-
("request_args", "response_file_name", "status_code"),
12+
("path", "response_file_name", "status_code"),
1113
[
1214
(
13-
"ID=156e1560-e532-4e2a-85ad-5aeff03dc43e",
15+
"/156e1560-e532-4e2a-85ad-5aeff03dc43e",
1416
"./api/examples/GET_QuestionnaireResponse/success.yaml",
1517
200,
1618
),
1719
(
18-
"ID=INVALID",
20+
"/INVALID",
1921
"./api/examples/GET_QuestionnaireResponse/errors/invalid_access_request_id.yaml",
2022
400,
2123
),
2224
(
23-
"ID=",
24-
"./api/examples/GET_QuestionnaireResponse/errors/missing_access_request_id.yaml",
25-
400,
26-
),
27-
(
28-
"ID=60d09b82-f4bb-41f9-b41e-767999b4ac9b",
25+
"/60d09b82-f4bb-41f9-b41e-767999b4ac9b",
2926
"./api/examples/GET_QuestionnaireResponse/errors/questionnaire_response_not_found.yaml",
3027
404,
3128
),
3229
(
33-
"ID=INVALID_CODE",
30+
"/INVALID_CODE",
3431
"./api/examples/errors/internal-server-error.yaml",
3532
500,
3633
),
3734
],
3835
)
3936
@patch("sandbox.api.get_questionnaire_response.generate_response_from_example")
40-
def test_get_questionnaire_response_returns_expected_responses__mocked_utils(
37+
def test_get_questionnaire_response_id_returns_expected_responses__mocked_utils(
4138
mock_generate_response_from_example: MagicMock,
42-
request_args: str,
39+
path: str,
4340
response_file_name: str,
4441
status_code: int,
4542
client: object,
4643
) -> None:
47-
"""Test GET Consent endpoint."""
44+
"""Test GET /QuestionnaireResponse/{id} endpoint."""
4845
mock_generate_response_from_example.return_value = mocked_response = Response(
4946
dumps({"data": "mocked"}),
5047
status=status_code,
5148
content_type="application/json",
5249
)
5350
# Act
54-
response = client.get(f"{GET_QUESTIONNAIRE_RESPONSE_API_ENDPOINT}?{request_args}")
51+
response = client.get(f"{GET_QUESTIONNAIRE_RESPONSE_API_ENDPOINT}{path}")
5552
# Assert
5653
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
5754
assert response.status_code == status_code
5855
assert response.json == loads(mocked_response.get_data(as_text=True))
56+
57+
58+
@pytest.mark.parametrize("path", ["/",""])
59+
@patch("sandbox.api.app.generate_response_from_example")
60+
def test_get_questionnaire_response_without_path_params_return_405_errors(
61+
mock_generate_response_from_example: MagicMock,
62+
path: str,
63+
client: object,
64+
) -> None:
65+
"""Test the GET /QuestionnaireResponse endpoint with no path params."""
66+
mock_generate_response_from_example.return_value = mocked_response = Response(
67+
dumps({"data": "mocked"}),
68+
status=405,
69+
content_type="application/json",
70+
)
71+
# Act
72+
response = client.get(f"{GET_QUESTIONNAIRE_RESPONSE_API_ENDPOINT}{path}")
73+
# Assert
74+
mock_generate_response_from_example.assert_called_once_with("./api/examples/errors/method-not-allowed.yaml", 405)
75+
assert response.status_code == 405
76+
assert response.json == loads(mocked_response.get_data(as_text=True))

specification/examples/responses/errors/method_not_allowed.yaml renamed to specification/examples/responses/errors/method-not-allowed.yaml

File renamed without changes.

specification/validated-relationships-service-api.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ paths:
370370
invalidAccessRequestID:
371371
$ref: "./examples/responses/GET_QuestionnaireResponse/errors/invalid_access_request_id.yaml#/InvalidAccessRequestID"
372372
missingAccessRequestID:
373-
$ref: "./examples/responses/errors/method_not_allowed.yaml#/MethodNotAllowedError"
373+
$ref: "./examples/responses/errors/method-not-allowed.yaml#/MethodNotAllowedError"
374374
questionnaireResponseNotFound:
375375
$ref: "./examples/responses/GET_QuestionnaireResponse/errors/questionnaire_response_not_found.yaml#/QuestionnaireResponseNotFound"
376376
"5XX":

0 commit comments

Comments
 (0)