3232from parameter_parser import patient_identifier_system , process_search_params
3333from tests .utils .generic_utils import load_json_data
3434from tests .utils .values_for_tests import ValidValues
35+ from utils .mock_redis import MOCK_REDIS_V2D_RESPONSE
3536
36- "test"
37+ class TestFhirControllerBase (unittest .TestCase ):
38+ """Base class for all tests to set up common fixtures"""
3739
38-
39- class TestFhirController (unittest .TestCase ):
4040 def setUp (self ):
41+ super ().setUp ()
42+ self .redis_patcher = patch ("parameter_parser.redis_client" )
43+ self .mock_redis_client = self .redis_patcher .start ()
44+ self .logger_info_patcher = patch ("logging.Logger.info" )
45+ self .mock_logger_info = self .logger_info_patcher .start ()
46+
47+ def tearDown (self ):
48+ self .redis_patcher .stop ()
49+ self .logger_info_patcher .stop ()
50+ super ().tearDown ()
51+
52+ class TestFhirController (TestFhirControllerBase ):
53+ def setUp (self ):
54+ super ().setUp ()
4155 self .service = create_autospec (FhirService )
4256 self .repository = create_autospec (ImmunizationRepository )
4357 self .authorizer = create_autospec (Authorization )
4458 self .controller = FhirController (self .authorizer , self .service )
4559
60+ def tearDown (self ):
61+ self .redis_patcher .stop ()
62+ super ().tearDown ()
63+
4664 def test_create_response (self ):
4765 """it should return application/fhir+json with correct status code"""
4866 body = {"message" : "a body" }
@@ -133,7 +151,6 @@ def test_get_imms_by_identifer_header_missing(self):
133151 response = self .controller .get_immunization_by_identifier (lambda_event )
134152
135153 self .assertEqual (response ["statusCode" ], 403 )
136-
137154 @patch ("fhir_controller.get_supplier_permissions" )
138155 def test_not_found_for_identifier (self , mock_get_permissions ):
139156 """it should return not-found OperationOutcome if it doesn't exist"""
@@ -165,7 +182,6 @@ def test_not_found_for_identifier(self, mock_get_permissions):
165182
166183 imms = identifier .replace ("|" , "#" )
167184 # When
168-
169185 response = self .controller .get_immunization_by_identifier (lambda_event )
170186
171187 # Then
@@ -200,7 +216,6 @@ def test_get_imms_by_identifer_patient_identifier_and_element_present(self, mock
200216 self .assertEqual (response ["statusCode" ], 400 )
201217 body = json .loads (response ["body" ])
202218 self .assertEqual (body ["resourceType" ], "OperationOutcome" )
203-
204219 @patch ("fhir_controller.get_supplier_permissions" )
205220 def test_get_imms_by_identifer_both_body_and_query_params_present (self , mock_get_supplier_permissions ):
206221 """it should return Immunization Id if it exists"""
@@ -422,7 +437,6 @@ def test_validate_immunization_identifier_having_whitespace(self,mock_get_suppli
422437 self .assertEqual (response ["statusCode" ], 400 )
423438 outcome = json .loads (response ["body" ])
424439 self .assertEqual (outcome ["resourceType" ], "OperationOutcome" )
425-
426440 @patch ("fhir_controller.get_supplier_permissions" )
427441 def test_validate_imms_id_invalid_vaccinetype (self , mock_get_permissions ):
428442 """it should validate lambda's Immunization id"""
@@ -738,7 +752,6 @@ def test_validate_immunization_identifier_having_whitespace(self,mock_get_suppli
738752 self .assertEqual (response ["statusCode" ], 400 )
739753 outcome = json .loads (response ["body" ])
740754 self .assertEqual (outcome ["resourceType" ], "OperationOutcome" )
741-
742755 @patch ("fhir_controller.get_supplier_permissions" )
743756 def test_validate_imms_id_invalid_vaccinetype (self , mock_get_permissions ):
744757 """it should validate lambda's Immunization id"""
@@ -820,7 +833,6 @@ def test_get_imms_by_id_unauthorised_vax_error(self,mock_permissions):
820833 # Then
821834 mock_permissions .assert_called_once_with ("test" )
822835 self .assertEqual (response ["statusCode" ], 403 )
823-
824836 @patch ("fhir_controller.get_supplier_permissions" )
825837 def test_get_imms_by_id_no_vax_permission (self , mock_permissions ):
826838 """it should return Immunization Id if it exists"""
@@ -1142,7 +1154,6 @@ def test_update_immunization_UnauthorizedVaxError(self, mock_get_supplier_permis
11421154 response = self .controller .update_immunization (aws_event )
11431155 mock_get_supplier_permissions .assert_called_once_with ("Test" )
11441156 self .assertEqual (response ["statusCode" ], 403 )
1145-
11461157 @patch ("fhir_controller.get_supplier_permissions" )
11471158 def test_update_immunization_UnauthorizedVaxError_check_for_non_batch (self , mock_get_supplier_permissions ):
11481159 """it should not update the Immunization record"""
@@ -1573,7 +1584,6 @@ def test_immunization_exception_not_found(self, mock_get_permissions):
15731584 body = json .loads (response ["body" ])
15741585 self .assertEqual (body ["resourceType" ], "OperationOutcome" )
15751586 self .assertEqual (body ["issue" ][0 ]["code" ], "not-found" )
1576-
15771587 @patch ("fhir_controller.get_supplier_permissions" )
15781588 def test_immunization_unhandled_error (self , mock_get_supplier_permissions ):
15791589 """it should return server-error OperationOutcome if service throws UnhandledResponseError"""
@@ -1596,8 +1606,9 @@ def test_immunization_unhandled_error(self, mock_get_supplier_permissions):
15961606 self .assertEqual (body ["resourceType" ], "OperationOutcome" )
15971607 self .assertEqual (body ["issue" ][0 ]["code" ], "exception" )
15981608
1599- class TestSearchImmunizations (unittest . TestCase ):
1609+ class TestSearchImmunizations (TestFhirControllerBase ):
16001610 def setUp (self ):
1611+ super ().setUp ()
16011612 self .service = create_autospec (FhirService )
16021613 self .authorizer = create_autospec (Authorization )
16031614 self .controller = FhirController (self .authorizer , self .service )
@@ -1607,11 +1618,14 @@ def setUp(self):
16071618 self .date_to_key = "-date.to"
16081619 self .nhs_number_valid_value = "9000000009"
16091620 self .patient_identifier_valid_value = f"{ patient_identifier_system } |{ self .nhs_number_valid_value } "
1621+ self .mock_redis_client .hkeys .return_value = MOCK_REDIS_V2D_RESPONSE
1622+
1623+ def tearDown (self ):
1624+ return super ().tearDown ()
16101625
16111626 @patch ("fhir_controller.get_supplier_permissions" )
16121627 def test_get_search_immunizations (self , mock_get_supplier_permissions ):
16131628 """it should search based on patient_identifier and immunization_target"""
1614-
16151629 mock_get_supplier_permissions .return_value = ["COVID19.S" ]
16161630 search_result = Bundle .construct ()
16171631 self .service .search_immunizations .return_value = search_result
@@ -1795,7 +1809,6 @@ def test_post_search_immunizations(self,mock_get_supplier_permissions):
17951809 "headers" : {"Content-Type" : "application/x-www-form-urlencoded" , "SupplierSystem" : "Test" },
17961810 "body" : base64_encoded_body ,
17971811 }
1798-
17991812 # When
18001813 response = self .controller .search_immunizations (lambda_event )
18011814 # Then
@@ -1906,6 +1919,7 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search_403(self
19061919
19071920 @patch ("fhir_controller.process_search_params" , wraps = process_search_params )
19081921 def test_uses_parameter_parser (self , process_search_params : Mock ):
1922+ self .mock_redis_client .hkeys .return_value = MOCK_REDIS_V2D_RESPONSE
19091923 lambda_event = {
19101924 "multiValueQueryStringParameters" : {
19111925 self .patient_identifier_key : ["https://fhir.nhs.uk/Id/nhs-number|9000000009" ],
0 commit comments