Skip to content

Commit a50ffff

Browse files
tests passing, added 400 error case
1 parent 29ea4f7 commit a50ffff

File tree

2 files changed

+176
-20
lines changed

2 files changed

+176
-20
lines changed

tests/conftest.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def asid(is_mocked_environment):
8383
@pytest.fixture(scope="session")
8484
def apim_app_flow_vars(allowListodsCode=None):
8585
if allowListodsCode is not None:
86-
print(f"Using allowListodsCode: {allowListodsCode}")
87-
return {"ers": {"allowListodsCode": str(allowListodsCode)}}
86+
return {"ers": {"allowListodsCode": allowListodsCode}}
8887

8988

9089
@pytest.fixture(scope="session")
@@ -264,9 +263,7 @@ async def _make_product(product_scopes, additional_attributes=None):
264263
]
265264

266265
if additional_attributes is not None:
267-
print(f"Adding additional attributes: {additional_attributes}")
268266
attributes.extend(additional_attributes)
269-
print(f"Attributes now: {attributes}")
270267

271268
body = {
272269
"proxies": proxies,
@@ -292,8 +289,7 @@ async def user_restricted_app(
292289
):
293290
# Setup
294291
if apim_app_flow_vars is not None:
295-
odslist = json.dumps({"ers": {"allowListodsCode": str(apim_app_flow_vars)}})
296-
print(f"Using apim_app_flow_vars: {odslist}")
292+
odslist = json.dumps({"ers": {"allowListodsCode": apim_app_flow_vars}})
297293
app = await make_app(
298294
user_restricted_product,
299295
{"asid": asid, "apim-app-flow-vars": odslist},
@@ -318,8 +314,6 @@ async def _make_app(product, custom_attributes={}):
318314
devAppAPI = DeveloperAppsAPI(client=client)
319315
app_name = f"apim-auto-{uuid4()}"
320316

321-
print(f"Custom attributes: {custom_attributes}")
322-
323317
attributes = [
324318
{"name": key, "value": value} for key, value in custom_attributes.items()
325319
]

tests/integration/test_user_restricted.py

Lines changed: 174 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ class TestUserRestricted:
4242
async def test_user_restricted_valid_ods_code(
4343
self,
4444
authenticate_user,
45+
service_url,
4546
user: Actor,
47+
asid,
4648
endpoint_url,
4749
is_fhir_4,
48-
service_url,
4950
apim_app_flow_vars,
50-
update_user_restricted_product,
5151
):
5252
access_code = await authenticate_user(user)
5353

@@ -65,14 +65,11 @@ async def test_user_restricted_valid_ods_code(
6565
}
6666

6767
# Make the API call
68-
69-
# Make request with user with ODS code not in allow list (e.g. R69)
7068
response = requests.get(
7169
f"{service_url}{endpoint_url}", headers=client_request_headers
7270
)
7371

7472
# Verify the status
75-
# Verify 403 response with appropriate error message
7673
assert (
7774
response.status_code == 200
7875
), "Expected a 200 when accessing the api but got " + str(response.status_code)
@@ -89,12 +86,12 @@ async def test_user_restricted_valid_ods_code(
8986
async def test_user_restricted_invalid_ods_code(
9087
self,
9188
authenticate_user,
92-
endpoint_url,
89+
service_url,
9390
user: Actor,
91+
asid,
92+
endpoint_url,
9493
is_fhir_4,
95-
service_url,
9694
apim_app_flow_vars,
97-
update_user_restricted_product,
9895
):
9996
access_code = await authenticate_user(user)
10097

@@ -112,14 +109,179 @@ async def test_user_restricted_invalid_ods_code(
112109
}
113110

114111
# Make the API call
115-
116-
# Make request with user with ODS code not in allow list (e.g. R69)
117112
response = requests.get(
118113
f"{service_url}{endpoint_url}", headers=client_request_headers
119114
)
120-
121115
# Verify the status
122-
# Verify 403 response with appropriate error message
123116
assert (
124117
response.status_code == 403
125118
), "Expected a 403 when accessing the api but got " + str(response.status_code)
119+
# Verify the OperationOutcome payload
120+
response_data = response.json()
121+
assert response_data["resourceType"] == "OperationOutcome"
122+
assert response_data["meta"]["lastUpdated"] is not None
123+
assert len(response_data["meta"]["profile"]) == 1
124+
assert response_data["meta"]["profile"][0] == (
125+
"https://www.hl7.org/fhir/R4/operationoutcome.html"
126+
if is_fhir_4
127+
else "https://fhir.nhs.uk/STU3/StructureDefinition/eRS-OperationOutcome-1"
128+
)
129+
assert len(response_data["issue"]) == 1
130+
issue = response_data["issue"][0]
131+
assert issue["severity"] == "error"
132+
assert issue["code"] == "security" if is_fhir_4 else "forbidden"
133+
assert issue["diagnostics"] == (
134+
"Unauthorised ODS code provided in NHSD-End-User-Organisation-ODS header"
135+
)
136+
assert len(issue["details"]["coding"]) == 1
137+
issue_details = issue["details"]["coding"][0]
138+
assert (
139+
issue_details["system"]
140+
== "https://fhir.nhs.uk/CodeSystem/NHSD-API-ErrorOrWarningCode"
141+
if is_fhir_4
142+
else "https://fhir.nhs.uk/STU3/CodeSystem/eRS-APIErrorCode-1"
143+
)
144+
assert (
145+
issue_details["code"] == "ACCESS_DENIED" if is_fhir_4 else "ACCESS_DENIED"
146+
)
147+
148+
@pytest.mark.asyncio
149+
@pytest.mark.parametrize(
150+
"endpoint_url, is_fhir_4, user ,apim_app_flow_vars",
151+
[
152+
("", False, Actor.RC_DEV, [Actor.RC_DEV.org_code]),
153+
("/FHIR/R4/", True, Actor.RC_DEV, [Actor.RC_DEV.org_code]),
154+
("/FHIR/STU3/", False, Actor.RC_DEV, [Actor.RC_DEV.org_code]),
155+
],
156+
)
157+
async def test_user_restricted_missing_ods_header(
158+
self,
159+
authenticate_user,
160+
service_url,
161+
user: Actor,
162+
asid,
163+
endpoint_url,
164+
is_fhir_4,
165+
apim_app_flow_vars,
166+
):
167+
access_code = await authenticate_user(user)
168+
169+
client_request_headers = {
170+
_HEADER_ECHO: "", # enable echo target
171+
_HEADER_AUTHORIZATION: "Bearer " + access_code,
172+
_HEADER_REQUEST_ID: "DUMMY-VALUE",
173+
RenamedHeader.REFERRAL_ID.original: _EXPECTED_REFERRAL_ID,
174+
RenamedHeader.CORRELATION_ID.original: _EXPECTED_CORRELATION_ID,
175+
RenamedHeader.BUSINESS_FUNCTION.original: user.business_function,
176+
RenamedHeader.FILENAME.original: _EXPECTED_FILENAME,
177+
RenamedHeader.COMM_RULE_ORG.original: _EXPECTED_COMM_RULE_ORG,
178+
RenamedHeader.OBO_USER_ID.original: _EXPECTED_OBO_USER_ID,
179+
}
180+
181+
# Make the API call
182+
response = requests.get(
183+
f"{service_url}{endpoint_url}", headers=client_request_headers
184+
)
185+
# Verify the status
186+
assert (
187+
response.status_code == 400
188+
), "Expected a 400 when accessing the api but got " + str(response.status_code)
189+
# Verify the OperationOutcome payload
190+
response_data = response.json()
191+
assert response_data["resourceType"] == "OperationOutcome"
192+
assert response_data["meta"]["lastUpdated"] is not None
193+
assert len(response_data["meta"]["profile"]) == 1
194+
assert response_data["meta"]["profile"][0] == (
195+
"https://www.hl7.org/fhir/R4/operationoutcome.html"
196+
if is_fhir_4
197+
else "https://fhir.nhs.uk/STU3/StructureDefinition/eRS-OperationOutcome-1"
198+
)
199+
assert len(response_data["issue"]) == 1
200+
issue = response_data["issue"][0]
201+
assert issue["severity"] == "error"
202+
assert issue["code"] == "invalid" if is_fhir_4 else "invalid"
203+
assert issue["diagnostics"] == (
204+
"Missing or Empty NHSD-End-User-Organisation-ODS header."
205+
)
206+
assert len(issue["details"]["coding"]) == 1
207+
issue_details = issue["details"]["coding"][0]
208+
assert (
209+
issue_details["system"]
210+
== "https://fhir.nhs.uk/CodeSystem/NHSD-API-ErrorOrWarningCode"
211+
if is_fhir_4
212+
else "https://fhir.nhs.uk/STU3/CodeSystem/eRS-APIErrorCode-1"
213+
)
214+
assert (
215+
issue_details["code"] == "MISSING_HEADER" if is_fhir_4 else "MISSING_HEADER"
216+
)
217+
218+
@pytest.mark.asyncio
219+
@pytest.mark.parametrize(
220+
"endpoint_url, is_fhir_4, user ,apim_app_flow_vars",
221+
[
222+
("", False, Actor.RC_DEV, [Actor.RC_DEV.org_code]),
223+
("/FHIR/R4/", True, Actor.RC_DEV, [Actor.RC_DEV.org_code]),
224+
("/FHIR/STU3/", False, Actor.RC_DEV, [Actor.RC_DEV.org_code]),
225+
],
226+
)
227+
async def test_user_restricted_missing_ods_code(
228+
self,
229+
authenticate_user,
230+
service_url,
231+
user: Actor,
232+
asid,
233+
endpoint_url,
234+
is_fhir_4,
235+
apim_app_flow_vars,
236+
):
237+
access_code = await authenticate_user(user)
238+
239+
client_request_headers = {
240+
_HEADER_ECHO: "", # enable echo target
241+
_HEADER_AUTHORIZATION: "Bearer " + access_code,
242+
_HEADER_REQUEST_ID: "DUMMY-VALUE",
243+
RenamedHeader.REFERRAL_ID.original: _EXPECTED_REFERRAL_ID,
244+
RenamedHeader.CORRELATION_ID.original: _EXPECTED_CORRELATION_ID,
245+
RenamedHeader.BUSINESS_FUNCTION.original: user.business_function,
246+
RenamedHeader.ODS_CODE.original: "",
247+
RenamedHeader.FILENAME.original: _EXPECTED_FILENAME,
248+
RenamedHeader.COMM_RULE_ORG.original: _EXPECTED_COMM_RULE_ORG,
249+
RenamedHeader.OBO_USER_ID.original: _EXPECTED_OBO_USER_ID,
250+
}
251+
252+
# Make the API call
253+
response = requests.get(
254+
f"{service_url}{endpoint_url}", headers=client_request_headers
255+
)
256+
# Verify the status
257+
assert (
258+
response.status_code == 400
259+
), "Expected a 400 when accessing the api but got " + str(response.status_code)
260+
# Verify the OperationOutcome payload
261+
response_data = response.json()
262+
assert response_data["resourceType"] == "OperationOutcome"
263+
assert response_data["meta"]["lastUpdated"] is not None
264+
assert len(response_data["meta"]["profile"]) == 1
265+
assert response_data["meta"]["profile"][0] == (
266+
"https://www.hl7.org/fhir/R4/operationoutcome.html"
267+
if is_fhir_4
268+
else "https://fhir.nhs.uk/STU3/StructureDefinition/eRS-OperationOutcome-1"
269+
)
270+
assert len(response_data["issue"]) == 1
271+
issue = response_data["issue"][0]
272+
assert issue["severity"] == "error"
273+
assert issue["code"] == "invalid" if is_fhir_4 else "invalid"
274+
assert issue["diagnostics"] == (
275+
"Missing or Empty NHSD-End-User-Organisation-ODS header."
276+
)
277+
assert len(issue["details"]["coding"]) == 1
278+
issue_details = issue["details"]["coding"][0]
279+
assert (
280+
issue_details["system"]
281+
== "https://fhir.nhs.uk/CodeSystem/NHSD-API-ErrorOrWarningCode"
282+
if is_fhir_4
283+
else "https://fhir.nhs.uk/STU3/CodeSystem/eRS-APIErrorCode-1"
284+
)
285+
assert (
286+
issue_details["code"] == "MISSING_HEADER" if is_fhir_4 else "MISSING_HEADER"
287+
)

0 commit comments

Comments
 (0)