-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_provider_request.py
More file actions
237 lines (186 loc) · 7.14 KB
/
test_provider_request.py
File metadata and controls
237 lines (186 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Unit tests for :mod:`gateway_api.provider_request`.
This module contains unit tests for the `GpProviderClient` class, which is responsible
for interacting with the GPProvider FHIR API.
"""
from typing import Any
import pytest
from requests import Response
from requests.structures import CaseInsensitiveDict
from stubs.stub_provider import GpProviderStub
from gateway_api import provider_request
from gateway_api.provider_request import ExternalServiceError, GpProviderClient
ars_interactionId = (
"urn:nhs:names:services:gpconnect:structured"
":fhir:operation:gpc.getstructuredrecord-1"
)
@pytest.fixture
def stub() -> GpProviderStub:
return GpProviderStub()
@pytest.fixture
def mock_request_post(
monkeypatch: pytest.MonkeyPatch, stub: GpProviderStub
) -> dict[str, Any]:
"""
Fixture to patch the `requests.post` method for testing.
This fixture intercepts calls to `requests.post` and routes them to the
stub provider. It also captures the most recent request details, such as
headers, body, and URL, for verification in tests.
Returns:
dict[str, Any]: A dictionary containing the captured request details.
"""
capture: dict[str, Any] = {}
def _fake_post(
url: str,
headers: CaseInsensitiveDict[str],
data: str,
timeout: int,
) -> Response:
"""A fake requests.post implementation."""
capture["headers"] = dict(headers)
capture["data"] = data
capture["url"] = url
return stub.access_record_structured()
monkeypatch.setattr(provider_request, "post", _fake_post)
return capture
def test_valid_gpprovider_access_structured_record_makes_request_correct_url_post_200(
mock_request_post: dict[str, Any],
stub: GpProviderStub,
) -> None:
"""
Test that the `access_structured_record` method constructs the correct URL
for the GPProvider FHIR API request and receives a 200 OK response.
This test verifies that the URL includes the correct FHIR base path and
operation for accessing a structured patient record.
"""
provider_asid = "200000001154"
consumer_asid = "200000001152"
provider_endpoint = "https://test.com"
trace_id = "some_uuid_value"
client = GpProviderClient(
provider_endpoint=provider_endpoint,
provider_asid=provider_asid,
consumer_asid=consumer_asid,
)
result = client.access_structured_record(trace_id, "body")
captured_url = mock_request_post.get("url", provider_endpoint)
assert (
captured_url
== provider_endpoint + "/FHIR/STU3/patient/$gpc.getstructuredrecord"
)
assert result.status_code == 200
def test_valid_gpprovider_access_structured_record_with_correct_headers_post_200(
mock_request_post: dict[str, Any],
stub: GpProviderStub,
) -> None:
"""
Test that the `access_structured_record` method includes the correct headers
in the GPProvider FHIR API request and receives a 200 OK response.
This test verifies that the headers include:
- Content-Type and Accept headers for FHIR+JSON.
- Ssp-TraceID, Ssp-From, Ssp-To, and Ssp-InteractionID for GPConnect.
"""
provider_asid = "200000001154"
consumer_asid = "200000001152"
provider_endpoint = "https://test.com"
trace_id = "some_uuid_value"
client = GpProviderClient(
provider_endpoint=provider_endpoint,
provider_asid=provider_asid,
consumer_asid=consumer_asid,
)
expected_headers = {
"Content-Type": "application/fhir+json",
"Accept": "application/fhir+json",
"Ssp-TraceID": str(trace_id),
"Ssp-From": consumer_asid,
"Ssp-To": provider_asid,
"Ssp-InteractionID": ars_interactionId,
}
result = client.access_structured_record(trace_id, "body")
captured_headers = mock_request_post["headers"]
assert expected_headers == captured_headers
assert result.status_code == 200
def test_valid_gpprovider_access_structured_record_with_correct_body_200(
mock_request_post: dict[str, Any],
stub: GpProviderStub,
) -> None:
"""
Test that the `access_structured_record` method includes the correct body
in the GPProvider FHIR API request and receives a 200 OK response.
This test verifies that the request body matches the expected FHIR parameters
resource sent to the GPProvider API.
"""
provider_asid = "200000001154"
consumer_asid = "200000001152"
provider_endpoint = "https://test.com"
trace_id = "some_uuid_value"
request_body = "some_FHIR_request_params"
client = GpProviderClient(
provider_endpoint=provider_endpoint,
provider_asid=provider_asid,
consumer_asid=consumer_asid,
)
result = client.access_structured_record(trace_id, request_body)
captured_body = mock_request_post["data"]
assert result.status_code == 200
assert captured_body == request_body
def test_valid_gpprovider_access_structured_record_returns_stub_response_200(
mock_request_post: dict[str, Any],
stub: GpProviderStub,
) -> None:
"""
Test that the `access_structured_record` method returns the same response
as provided by the stub provider.
This test verifies that the response from the GPProvider FHIR API matches
the expected response, including the status code and content.
"""
provider_asid = "200000001154"
consumer_asid = "200000001152"
provider_endpoint = "https://test.com"
trace_id = "some_uuid_value"
client = GpProviderClient(
provider_endpoint=provider_endpoint,
provider_asid=provider_asid,
consumer_asid=consumer_asid,
)
expected_response = stub.access_record_structured()
result = client.access_structured_record(trace_id, "body")
assert result.status_code == expected_response.status_code
assert result.content == expected_response.content
def test_access_structured_record_raises_external_service_error(
mock_request_post: dict[str, Any],
stub: GpProviderStub,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""
Test that the `access_structured_record` method raises an `ExternalServiceError`
when the GPProvider FHIR API request fails with an HTTP error.
"""
provider_asid = "200000001154"
consumer_asid = "200000001152"
provider_endpoint = "https://test.com"
trace_id = "some_uuid_value"
client = GpProviderClient(
provider_endpoint=provider_endpoint,
provider_asid=provider_asid,
consumer_asid=consumer_asid,
)
# Simulate an error response from the stub
def _fake_post_error(
url: str,
headers: CaseInsensitiveDict[str],
data: str,
timeout: int,
) -> Response:
response = Response()
response.status_code = 500
response._content = b"Internal Server Error" # noqa: SLF001 TODO: push this back into the stub?
response.reason = "Internal Server Error"
return response
monkeypatch.setattr(provider_request, "post", _fake_post_error)
with pytest.raises(
ExternalServiceError,
match="GPProvider FHIR API request failed:Internal Server Error",
):
client.access_structured_record(trace_id, "body")