Skip to content

Commit 91b4239

Browse files
first attempt at integration test
1 parent 89f96aa commit 91b4239

File tree

3 files changed

+97
-6
lines changed

3 files changed

+97
-6
lines changed

.env

Whitespace-only changes.

tests/conftest.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ def asid(is_mocked_environment):
7979
)
8080

8181

82+
@pytest.fixture(scope="session")
83+
def apim_app_flow_vars(allowListodsCode=None):
84+
if allowListodsCode is not None:
85+
return {"ers": {"allowListodsCode": allowListodsCode}}
86+
87+
8288
@pytest.fixture(scope="session")
8389
def referring_clinician(is_mocked_environment):
8490
return Actor.RC_DEV if is_mocked_environment else Actor.RC
@@ -127,7 +133,8 @@ async def user_restricted_product(client, make_product):
127133
[
128134
"urn:nhsd:apim:user-nhs-id:aal3:e-referrals-service-api",
129135
"urn:nhsd:apim:user-nhs-id:aal2:e-referrals-service-api",
130-
]
136+
],
137+
additional_attributes=[{"name": "EUOAllowlist", "value": "false"}],
131138
)
132139

133140
print(f"product created: {productName}")
@@ -240,7 +247,7 @@ def _update_function(attr, value):
240247

241248
@pytest.fixture
242249
def make_product(client, environment, service_name):
243-
async def _make_product(product_scopes):
250+
async def _make_product(product_scopes, additional_attributes=None):
244251
product = ApiProductsAPI(client=client)
245252

246253
proxies = [f"identity-service-mock-{environment}"]
@@ -251,9 +258,12 @@ async def _make_product(product_scopes):
251258
product_name = f"apim-auto-{uuid4()}"
252259
attributes = [
253260
{"name": "access", "value": "public"},
254-
{"name": "EUOAllowlistRequired", "value": "false"},
255261
{"name": "ratelimit", "value": "10ps"},
256262
]
263+
264+
if additional_attributes is not None:
265+
attributes.extend(additional_attributes)
266+
257267
body = {
258268
"proxies": proxies,
259269
"scopes": product_scopes,
@@ -273,9 +283,17 @@ async def _make_product(product_scopes):
273283

274284

275285
@pytest_asyncio.fixture
276-
async def user_restricted_app(client, make_app, user_restricted_product, asid):
286+
async def user_restricted_app(
287+
client, make_app, user_restricted_product, asid, apim_app_flow_vars
288+
):
277289
# Setup
278-
app = await make_app(user_restricted_product, {"asid": asid})
290+
if apim_app_flow_vars is not None:
291+
app = await make_app(
292+
user_restricted_product,
293+
{"asid": asid, "apim-app-flow-vars": apim_app_flow_vars},
294+
)
295+
else:
296+
app = await make_app(user_restricted_product, {"asid": asid})
279297

280298
appName = app["name"]
281299
print(f"App created: {appName}")
@@ -318,7 +336,7 @@ async def _make_app(product, custom_attributes={}):
318336

319337
@pytest.fixture
320338
def authenticate_user(client, user_restricted_app, environment, oauth_url):
321-
async def _auth(actor: Actor):
339+
async def _auth(actor: Actor, apim_app_flow_vars=None):
322340
print(f"Attempting to authenticate: {actor}")
323341

324342
credentials = user_restricted_app["credentials"][0]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import pytest
2+
import requests
3+
from requests import Response
4+
from tests.data import RenamedHeader, Actor, UserAuthenticationLevel
5+
from tests.asserts import assert_ok_response
6+
7+
_HEADER_AUTHORIZATION = "Authorization"
8+
_HEADER_ECHO = "echo" # enable echo target
9+
_HEADER_BASE_URL = "x-ers-network-baseurl"
10+
_HEADER_USER_ID = "x-ers-user-id"
11+
_HEADER_REQUEST_ID = "x-request-id"
12+
_HEADER_ASID = "xapi_asid"
13+
_HEADER_ACCESS_MODE = "x-ers-access-mode"
14+
_HEADER_AAL = "x-ers-authentication-assurance-level"
15+
_HEADER_AMR = "x-ers-amr"
16+
_HEADER_ID_ASSURANCE_LEVEL = "x-ers-id-assurance-level"
17+
18+
_EXPECTED_REFERRAL_ID = "000000040032"
19+
_EXPECTED_CORRELATION_ID = "123123-123123-123123-123123"
20+
_EXPECTED_FILENAME = "mysuperfilename.txt"
21+
_EXPECTED_COMMA_FILENAME = "mysuper,filename.txt"
22+
_EXPECTED_COMM_RULE_ORG = "R100"
23+
_EXPECTED_OBO_USER_ID = "0123456789000"
24+
_EXPECTED_ACCESS_MODE = "user-restricted"
25+
26+
_SPECIALTY_REF_DATA_URL = "/FHIR/STU3/CodeSystem/SPECIALTY"
27+
_SEARCH_HEALTHCARE_SERVICE_R4_URL = "/FHIR/R4/HealthcareService"
28+
29+
30+
@pytest.mark.integration_test
31+
class TestUserRestricted:
32+
@pytest.mark.asyncio
33+
@pytest.mark.parametrize(
34+
"endpoint_url, is_fhir_4",
35+
[("", False), ("/FHIR/R4/", True), ("/FHIR/STU3/", False)],
36+
)
37+
async def test_user_restricted_invalid_ods_code(
38+
self,
39+
authenticate_user,
40+
endpoint_url,
41+
is_fhir_4,
42+
service_url,
43+
update_user_restricted_product,
44+
):
45+
access_code = await authenticate_user(
46+
referring_clinician_insufficient_ial, ["invalid_code"]
47+
)
48+
49+
client_request_headers = {
50+
_HEADER_ECHO: "", # enable echo target
51+
_HEADER_AUTHORIZATION: "Bearer " + access_code,
52+
_HEADER_REQUEST_ID: "DUMMY-VALUE",
53+
RenamedHeader.REFERRAL_ID.original: _EXPECTED_REFERRAL_ID,
54+
RenamedHeader.CORRELATION_ID.original: _EXPECTED_CORRELATION_ID,
55+
RenamedHeader.BUSINESS_FUNCTION.original: referring_clinician_insufficient_ial.business_function,
56+
RenamedHeader.ODS_CODE.original: referring_clinician_insufficient_ial.org_code,
57+
RenamedHeader.FILENAME.original: _EXPECTED_FILENAME,
58+
RenamedHeader.COMM_RULE_ORG.original: _EXPECTED_COMM_RULE_ORG,
59+
RenamedHeader.OBO_USER_ID.original: _EXPECTED_OBO_USER_ID,
60+
}
61+
62+
# Make the API call
63+
64+
# Make request with user with ODS code not in allow list (e.g. R69)
65+
response = requests.get(
66+
f"{service_url}{endpoint_url}", headers=client_request_headers
67+
)
68+
69+
# Verify the status
70+
# Verify 403 response with appropriate error message
71+
assert (
72+
response.status_code == 403
73+
), "Expected a 403 when accessing the api but got " + str(response.status_code)

0 commit comments

Comments
 (0)