Skip to content

Commit 7004d12

Browse files
committed
npa-5087 - Added sandbox tests for get consent by id
1 parent 267b450 commit 7004d12

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
from json import dumps, loads
2+
from unittest.mock import MagicMock, patch
3+
4+
import pytest
5+
from flask import Response
6+
7+
CONSENT_API_ENDPOINT = "/FHIR/R4/Consent"
8+
GET_CONSENT_BY_ID_FILE_PATH = "sandbox.api.get_consent_by_id"
9+
10+
11+
@pytest.mark.parametrize(
12+
("consent_id", "include_params", "response_file_name", "status_code"),
13+
[
14+
(
15+
"a0922245-1072-40c3-8f4e-a7490c10d365", # Invalid parameters
16+
"_invalid=test",
17+
"./api/examples/errors/invalid-include-parameter.yaml",
18+
422,
19+
),
20+
(
21+
"a0922245-1072-40c3-8f4e-a7490c10d365", # No proxy-role record found error
22+
"",
23+
"./api/examples/errors/invalidated-resource.yaml",
24+
404,
25+
),
26+
(
27+
" ", # Missing consent ID
28+
"",
29+
"./api/examples/GET_Consent/ID/errors/missing-id.yaml",
30+
400,
31+
),
32+
(
33+
"test", # Invalid consent ID
34+
"",
35+
"./api/examples/GET_Consent/ID/errors/invalid-id.yaml",
36+
400,
37+
),
38+
],
39+
)
40+
@patch("sandbox.api.get_consent_by_id.generate_response_from_example")
41+
def test_get_consent_by_id_returns_expected_responses__mocked_get_consent_by_id(
42+
mock_generate_response_from_example: MagicMock,
43+
consent_id: str,
44+
include_params: str,
45+
response_file_name: str,
46+
status_code: int,
47+
client: object,
48+
) -> None:
49+
"""Test GET /Consent/{ID} endpoint."""
50+
mock_generate_response_from_example.return_value = mocked_response = Response(
51+
dumps({"data": "mocked"}),
52+
status=status_code,
53+
content_type="application/json",
54+
)
55+
# Act
56+
response = client.get(f"{CONSENT_API_ENDPOINT}/{consent_id}?{include_params}")
57+
# import pdb; pdb.set_trace()
58+
# Assert
59+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
60+
assert response.status_code == status_code
61+
assert response.json == loads(mocked_response.get_data(as_text=True))
62+
63+
64+
@pytest.mark.parametrize(
65+
("consent_id", "include_params", "response_file_name", "status_code"),
66+
[
67+
(
68+
"74eed847-ca25-4e76-8cf2-f2c2d7842a7a", # Single consenting adult relationship no include
69+
"",
70+
"./api/examples/GET_Consent/single-consenting-adult-relationship.yaml",
71+
200,
72+
),
73+
(
74+
"74eed847-ca25-4e76-8cf2-f2c2d7842a7a", # Single consenting adult relationship with include performer
75+
"_include=Consent:performer",
76+
"./api/examples/GET_Consent/single-consenting-adult-relationship-include-performer.yaml",
77+
200,
78+
),
79+
(
80+
"74eed847-ca25-4e76-8cf2-f2c2d7842a7a", # Single consenting adult relationship with include patient
81+
"_include=Consent:patient",
82+
"./api/examples/GET_Consent/single-consenting-adult-relationship-include-patient.yaml",
83+
200,
84+
),
85+
(
86+
"74eed847-ca25-4e76-8cf2-f2c2d7842a7a", # Single consenting adult relationship with include performer and patient
87+
"_include=Consent:performer&_include=Consent:patient",
88+
"./api/examples/GET_Consent/single-consenting-adult-relationship-include-performer-patient.yaml",
89+
200,
90+
),
91+
(
92+
"39df03a2-1b14-4d19-b1dc-d5d8cbf96948", # Single adult-child relationship no include
93+
"",
94+
"./api/examples/GET_Consent/single-mother-child-relationship.yaml",
95+
200,
96+
),
97+
(
98+
"39df03a2-1b14-4d19-b1dc-d5d8cbf96948", # Single adult-child relationship with include performer
99+
"_include=Consent:performer",
100+
"./api/examples/GET_Consent/single-mother-child-relationship-include-performer.yaml",
101+
200,
102+
),
103+
(
104+
"39df03a2-1b14-4d19-b1dc-d5d8cbf96948", # Single adult-child relationship with include patient
105+
"_include=Consent:patient",
106+
"./api/examples/GET_Consent/single-mother-child-relationship-include-patient.yaml",
107+
200,
108+
),
109+
(
110+
"39df03a2-1b14-4d19-b1dc-d5d8cbf96948", # Single adult-child relationship with include performer and patient
111+
"_include=Consent:performer&_include=Consent:patient",
112+
"./api/examples/GET_Consent/single-mother-child-relationship-include-performer-patient.yaml",
113+
200,
114+
),
115+
],
116+
)
117+
@patch("sandbox.api.utils.generate_response_from_example")
118+
def test_get_consent_by_id_returns_expected_responses__mocked_utils(
119+
mock_generate_response_from_example: MagicMock,
120+
consent_id: str,
121+
include_params: str,
122+
response_file_name: str,
123+
status_code: int,
124+
client: object,
125+
) -> None:
126+
"""Test GET /Consent/{ID} endpoint."""
127+
mock_generate_response_from_example.return_value = mocked_response = Response(
128+
dumps({"data": "mocked"}),
129+
status=status_code,
130+
content_type="application/json",
131+
)
132+
# Act
133+
response = client.get(f"{CONSENT_API_ENDPOINT}/{consent_id}?{include_params}")
134+
# import pdb; pdb.set_trace()
135+
# Assert
136+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
137+
assert response.status_code == status_code
138+
assert response.json == loads(mocked_response.get_data(as_text=True))
139+
140+
141+
@patch(f"{GET_CONSENT_BY_ID_FILE_PATH}.check_for_consent_include_params")
142+
@patch(f"{GET_CONSENT_BY_ID_FILE_PATH}.generate_response_from_example")
143+
def test_get_consent_by_id__500_internal_server_error(
144+
mock_generate_response_from_example: MagicMock,
145+
mock_check_for_consent_include_params: MagicMock,
146+
client: object,
147+
) -> None:
148+
"""Test GET /Consent/{ID} endpoint."""
149+
mock_check_for_consent_include_params.side_effect = Exception("Test exception")
150+
# Act
151+
client.get(f"{CONSENT_API_ENDPOINT}/74eed847-ca25-4e76-8cf2-f2c2d7842a7a")
152+
# Assert
153+
mock_generate_response_from_example.assert_called_once_with("./api/examples/errors/internal-server-error.yaml", 500)

0 commit comments

Comments
 (0)