|
| 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