Skip to content

Commit 7752ebb

Browse files
authored
[PRMP-1081] Refactor access policy tests (#955)
1 parent 0d9e537 commit 7752ebb

File tree

1 file changed

+61
-48
lines changed

1 file changed

+61
-48
lines changed

lambdas/tests/unit/services/test_authoriser_service.py

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
def mock_auth_service(set_env, mocker):
3131
mock_test_auth_service = AuthoriserService()
3232
mocker.patch.object(mock_test_auth_service, "manage_user_session_service")
33+
mock_test_auth_service.allowed_nhs_numbers = []
34+
mock_test_auth_service.deceased_nhs_numbers = []
3335
yield mock_test_auth_service
34-
36+
mock_test_auth_service.allowed_nhs_numbers = []
37+
mock_test_auth_service.deceased_nhs_numbers = []
3538

3639
def build_decoded_token_for_role(role: str) -> dict:
3740
return {
@@ -74,6 +77,7 @@ def mocked_decode_method(auth_token: str, *_args, **_kwargs):
7477
"/DocumentStatus",
7578
"/UploadState",
7679
"/VirusScan",
80+
7781
],
7882
)
7983
def test_deny_access_policy_returns_true_for_gp_clinical_on_paths(
@@ -86,7 +90,6 @@ def test_deny_access_policy_returns_true_for_gp_clinical_on_paths(
8690
test_path, RepositoryRole.GP_CLINICAL.value, "900000001"
8791
)
8892
assert actual == expected
89-
mock_auth_service.allowed_nhs_numbers = []
9093

9194

9295
@pytest.mark.parametrize("test_path", ["/DocumentManifest", "/DocumentDelete", "Any"])
@@ -100,7 +103,6 @@ def test_deny_access_policy_returns_true_for_nhs_number_not_in_allowed(
100103
test_path, RepositoryRole.GP_ADMIN.value, "900000001"
101104
)
102105
assert actual == expected
103-
mock_auth_service.allowed_nhs_numbers = []
104106

105107

106108
@pytest.mark.parametrize("test_path", ["/DocumentManifest", "/DocumentDelete", "Any"])
@@ -114,49 +116,34 @@ def test_deny_access_policy_returns_false_for_nhs_number_in_allowed(
114116
test_path, RepositoryRole.GP_ADMIN.value, "900000002"
115117
)
116118
assert actual == expected
117-
mock_auth_service.allowed_nhs_numbers = []
118119

119120

120121
@pytest.mark.parametrize(
121-
"path",
122+
["path", "role", "expected"],
122123
[
123-
"/DocumentReference",
124-
"/DocumentReference/6b6417b5-58ed-45db-8359-bd78891e67b7",
125-
f"DocumentReview/{TEST_UUID}/1"
124+
("/DocumentReference", RepositoryRole.GP_ADMIN.value, False),
125+
("/DocumentReference", RepositoryRole.GP_CLINICAL.value, False),
126+
("/DocumentReference", RepositoryRole.PCSE.value, True),
127+
("/DocumentReference/6b6417b5-58ed-45db-8359-bd78891e67b7", RepositoryRole.GP_ADMIN.value, False),
128+
("/DocumentReference/6b6417b5-58ed-45db-8359-bd78891e67b7", RepositoryRole.GP_CLINICAL.value, False),
129+
("/DocumentReference/6b6417b5-58ed-45db-8359-bd78891e67b7", RepositoryRole.PCSE.value, True),
130+
(f"/DocumentReview/{TEST_UUID}/1", RepositoryRole.GP_ADMIN.value, False),
131+
(f"/DocumentReview/{TEST_UUID}/1", RepositoryRole.GP_CLINICAL.value, False),
132+
(f"/DocumentReview/{TEST_UUID}/1", RepositoryRole.PCSE.value, False),
133+
("/LloydGeorgeStitch", RepositoryRole.GP_ADMIN.value, False),
134+
("/LloydGeorgeStitch", RepositoryRole.GP_CLINICAL.value, False),
135+
("/LloydGeorgeStitch", RepositoryRole.PCSE.value, True),
126136
],
127137
)
128-
def test_deny_document_reference_as_gp_admin_or_clinical_returns_false(
129-
mock_auth_service: AuthoriserService,
130-
path: str,
138+
def test_deny_access_policy_for_various_paths_and_roles(
139+
mock_auth_service: AuthoriserService,
140+
path: str,
141+
role: str,
142+
expected: bool,
131143
):
132144
mock_auth_service.allowed_nhs_numbers.append("122222222")
133145

134-
expected = False
135-
136-
for role in (RepositoryRole.GP_CLINICAL.value, RepositoryRole.GP_ADMIN.value):
137-
actual = mock_auth_service.deny_access_policy(path, role, "122222222")
138-
assert actual == expected
139-
140-
141-
@pytest.mark.parametrize(
142-
"path",
143-
[
144-
"/DocumentReference",
145-
"/DocumentReference/6b6417b5-58ed-45db-8359-bd78891e67b7"
146-
],
147-
)
148-
def test_deny_document_reference_as_pcse_returns_true(
149-
mock_auth_service: AuthoriserService,
150-
path: str,
151-
):
152-
mock_auth_service.allowed_nhs_numbers.append("122222222")
153-
154-
expected = True
155-
156-
actual = mock_auth_service.deny_access_policy(
157-
path, RepositoryRole.PCSE.value, "122222222"
158-
)
159-
146+
actual = mock_auth_service.deny_access_policy(path, role, "122222222")
160147
assert actual == expected
161148

162149

@@ -184,28 +171,54 @@ def test_deny_document_reference_as_any_role_on_deceased_patient_returns_true(
184171
actual = mock_auth_service.deny_access_policy(path, role, "122222222")
185172
assert actual == expected
186173

187-
188-
def test_allow_access_policy_returns_false_for_nhs_number_not_in_allowed_on_search_path(
174+
@pytest.mark.parametrize(
175+
["test_path", "nhs_number"],
176+
[
177+
("/DocumentManifest", "900000001"),
178+
("/DocumentDelete", "900000001"),
179+
("Any", "900000001"),
180+
("/DocumentManifest", ""),
181+
("/DocumentManifest", None),
182+
],
183+
)
184+
def test_deny_access_policy_returns_true_for_invalid_nhs_number(
185+
test_path,
186+
nhs_number,
189187
mock_auth_service: AuthoriserService,
190188
):
191-
expected = False
189+
expected = True
192190
mock_auth_service.allowed_nhs_numbers = ["900000002"]
193-
194191
actual = mock_auth_service.deny_access_policy(
195-
"/SearchPatient", RepositoryRole.GP_ADMIN.value, "122222222"
192+
test_path, RepositoryRole.GP_ADMIN.value, nhs_number
196193
)
197194
assert actual == expected
198-
mock_auth_service.allowed_nhs_numbers = []
199195

200196

201-
def test_deny_access_policy_returns_false_for_gp_clinical_on_search_path(
197+
@pytest.mark.parametrize(
198+
"test_path",
199+
[
200+
"/SearchPatient",
201+
"/OdsReport",
202+
"/FeatureFlags",
203+
"/Feedback",
204+
"/DocumentReview",
205+
f"/DocumentReview/{TEST_UUID}/1/Status",
206+
],
207+
)
208+
def test_endpoints_allow_access_regardless_of_nhs_number(
209+
test_path: str,
202210
mock_auth_service: AuthoriserService,
203211
):
204212
expected = False
205-
actual = mock_auth_service.deny_access_policy(
206-
"/SearchPatient", RepositoryRole.GP_CLINICAL.value, "122222222"
207-
)
208-
assert expected == actual
213+
mock_auth_service.allowed_nhs_numbers = ["900000002"]
214+
215+
for role in (
216+
RepositoryRole.PCSE.value,
217+
RepositoryRole.GP_CLINICAL.value,
218+
RepositoryRole.GP_ADMIN.value,
219+
):
220+
actual = mock_auth_service.deny_access_policy(test_path, role, "122222222")
221+
assert actual == expected
209222

210223

211224
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)