Skip to content

Commit 4b81071

Browse files
committed
Added unit tests for the new authoriser
1 parent 1d60b43 commit 4b81071

File tree

4 files changed

+108
-5
lines changed

4 files changed

+108
-5
lines changed

backend/src/authorisation/authoriser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Authoriser class"""
12
import json
23

34
from authorisation.api_operation_code import ApiOperationCode
@@ -6,6 +7,7 @@
67

78

89
class Authoriser:
10+
"""Authoriser class. Used for authorising operations of FHIR vaccinations."""
911
def __init__(self):
1012
self._cache_client = redis_client
1113

backend/src/fhir_repository.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
import boto3
1010
import botocore.exceptions
1111
from boto3.dynamodb.conditions import Attr, Key
12-
from models.utils.permission_checker import ApiOperationCode, validate_permissions
1312
from botocore.config import Config
1413
from models.errors import (
1514
ResourceNotFoundError,
1615
UnhandledResponseError,
1716
IdentifierDuplicationError,
18-
UnauthorizedVaxError,
1917
)
2018
from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource, Table
2119

@@ -248,9 +246,6 @@ def update_reinstated_immunization(
248246
update_reinstated=True,
249247
)
250248

251-
def _handle_permissions(self, imms_vax_type_perms: list[str], attr: RecordAttributes):
252-
validate_permissions(imms_vax_type_perms, ApiOperationCode.UPDATE, [attr.vaccine_type])
253-
254249
def _build_update_expression(self, is_reinstate: bool) -> str:
255250
if is_reinstate:
256251
return (

backend/tests/authorisation/__init__.py

Whitespace-only changes.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import unittest
2+
from unittest.mock import patch
3+
4+
from poetry.console.commands import self
5+
6+
from authorisation.api_operation_code import ApiOperationCode
7+
from authorisation.authoriser import Authoriser
8+
9+
10+
class TestAuthoriser(unittest.TestCase):
11+
MOCK_SUPPLIER_NAME = "TestSupplier"
12+
13+
def setUp(self):
14+
self.cache_client_patcher = patch("authorisation.authoriser.redis_client")
15+
self.mock_cache_client = self.cache_client_patcher.start()
16+
17+
self.logger_patcher = patch("authorisation.authoriser.logger")
18+
self.mock_logger = self.logger_patcher.start()
19+
20+
self.test_authoriser = Authoriser()
21+
22+
def tearDown(self):
23+
patch.stopall()
24+
25+
def test_authorise_returns_true_if_supplier_has_permissions(self):
26+
"""Authoriser().authorise should return true if the supplier has the required permissions"""
27+
self.mock_cache_client.hget.return_value = '["COVID19.RS"]'
28+
29+
result = self.test_authoriser.authorise(self.MOCK_SUPPLIER_NAME, ApiOperationCode.READ, {"COVID19"})
30+
31+
self.assertTrue(result)
32+
self.mock_cache_client.hget.assert_called_once_with("supplier_permissions", self.MOCK_SUPPLIER_NAME)
33+
self.mock_logger.info.assert_called_once_with(
34+
"operation: r, supplier_permissions: {'covid19': ['r', 's']}, vaccine_types: {'COVID19'}"
35+
)
36+
37+
def test_authorise_returns_false_if_supplier_does_not_have_any_permissions(self):
38+
"""Authoriser().authorise should return false if the supplier does not have any permissions in the cache"""
39+
self.mock_cache_client.hget.return_value = ''
40+
41+
result = self.test_authoriser.authorise(self.MOCK_SUPPLIER_NAME, ApiOperationCode.CREATE, {"COVID19"})
42+
43+
self.assertFalse(result)
44+
self.mock_cache_client.hget.assert_called_once_with("supplier_permissions", self.MOCK_SUPPLIER_NAME)
45+
self.mock_logger.info.assert_called_once_with(
46+
"operation: c, supplier_permissions: {}, vaccine_types: {'COVID19'}"
47+
)
48+
49+
def test_authorise_returns_false_if_supplier_does_not_have_permission_for_operation(self):
50+
"""Authoriser().authorise should return false if the supplier does not have permission for the operation"""
51+
self.mock_cache_client.hget.return_value = '["COVID19.RS"]'
52+
53+
result = self.test_authoriser.authorise(self.MOCK_SUPPLIER_NAME, ApiOperationCode.CREATE, {"COVID19"})
54+
55+
self.assertFalse(result)
56+
self.mock_cache_client.hget.assert_called_once_with("supplier_permissions", self.MOCK_SUPPLIER_NAME)
57+
self.mock_logger.info.assert_called_once_with(
58+
"operation: c, supplier_permissions: {'covid19': ['r', 's']}, vaccine_types: {'COVID19'}"
59+
)
60+
61+
def test_authorise_returns_false_if_no_permission_for_vaccination_type(self):
62+
"""Authoriser().authorise should return false if the supplier does not have permission for the vaccination
63+
type"""
64+
self.mock_cache_client.hget.return_value = '["COVID19.RS"]'
65+
66+
result = self.test_authoriser.authorise(self.MOCK_SUPPLIER_NAME, ApiOperationCode.READ, {"FLU"})
67+
68+
self.assertFalse(result)
69+
self.mock_cache_client.hget.assert_called_once_with("supplier_permissions", self.MOCK_SUPPLIER_NAME)
70+
self.mock_logger.info.assert_called_once_with(
71+
"operation: r, supplier_permissions: {'covid19': ['r', 's']}, vaccine_types: {'FLU'}"
72+
)
73+
74+
def test_authorise_returns_false_multiple_vaccs_scenario(self):
75+
"""Authoriser().authorise should return false if the supplier is missing a permission for any of the vaccs in
76+
the list provided"""
77+
self.mock_cache_client.hget.return_value = '["COVID19.RS", "FLU.CRUDS"]'
78+
79+
result = self.test_authoriser.authorise(self.MOCK_SUPPLIER_NAME, ApiOperationCode.READ, {
80+
"FLU", "COVID19", "RSV"})
81+
82+
self.assertFalse(result)
83+
self.mock_cache_client.hget.assert_called_once_with("supplier_permissions", self.MOCK_SUPPLIER_NAME)
84+
85+
def test_filter_permitted_vacc_types_returns_all_if_supplier_has_perms_for_all(self):
86+
"""The same set of vaccination types will be returned if the supplier has the required permissions"""
87+
self.mock_cache_client.hget.return_value = '["COVID19.RS", "FLU.CRUDS", "RSV.CRUDS"]'
88+
requested_vacc_types = {"FLU", "COVID19", "RSV"}
89+
90+
result = self.test_authoriser.filter_permitted_vacc_types(self.MOCK_SUPPLIER_NAME, ApiOperationCode.SEARCH,
91+
requested_vacc_types)
92+
93+
self.assertSetEqual(result, requested_vacc_types)
94+
self.mock_cache_client.hget.assert_called_once_with("supplier_permissions", self.MOCK_SUPPLIER_NAME)
95+
self.assertNotEqual(id(requested_vacc_types), id(result))
96+
97+
def test_filter_permitted_vacc_types_removes_any_vacc_types_that_the_supplier_cannot_interact_with(self):
98+
"""Filter permitted vacc types method will filter out any vaccination types that the user cannot interact
99+
with"""
100+
self.mock_cache_client.hget.return_value = '["COVID19.RS", "FLU.CRUDS", "RSV.R"]'
101+
102+
result = self.test_authoriser.filter_permitted_vacc_types(self.MOCK_SUPPLIER_NAME, ApiOperationCode.SEARCH, {
103+
"FLU", "COVID19", "RSV"})
104+
105+
self.assertSetEqual(result, {"FLU", "COVID19"})
106+
self.mock_cache_client.hget.assert_called_once_with("supplier_permissions", self.MOCK_SUPPLIER_NAME)

0 commit comments

Comments
 (0)