3333from tests .utils .generic_utils import load_json_data
3434from tests .utils .values_for_tests import ValidValues
3535
36- "test"
36+ class TestFhirControllerBase (unittest .TestCase ):
37+ """Base class for all tests to set up common fixtures"""
3738
38-
39- class TestFhirController (unittest .TestCase ):
4039 def setUp (self ):
40+ super ().setUp ()
41+ self .redis_patcher = patch ("parameter_parser.redis_client" )
42+ self .mock_redis_client = self .redis_patcher .start ()
43+ self .logger_info_patcher = patch ("logging.Logger.info" )
44+ self .mock_logger_info = self .logger_info_patcher .start ()
45+
46+ def tearDown (self ):
47+ self .redis_patcher .stop ()
48+ self .logger_info_patcher .stop ()
49+ super ().tearDown ()
50+
51+ class TestFhirController (TestFhirControllerBase ):
52+ def setUp (self ):
53+ super ().setUp ()
4154 self .service = create_autospec (FhirService )
4255 self .repository = create_autospec (ImmunizationRepository )
4356 self .authorizer = create_autospec (Authorization )
4457 self .controller = FhirController (self .authorizer , self .service )
4558
59+ def tearDown (self ):
60+ self .redis_patcher .stop ()
61+ super ().tearDown ()
62+
4663 def test_create_response (self ):
4764 """it should return application/fhir+json with correct status code"""
4865 body = {"message" : "a body" }
@@ -133,7 +150,6 @@ def test_get_imms_by_identifer_header_missing(self):
133150 response = self .controller .get_immunization_by_identifier (lambda_event )
134151
135152 self .assertEqual (response ["statusCode" ], 403 )
136-
137153 @patch ("fhir_controller.get_supplier_permissions" )
138154 def test_not_found_for_identifier (self , mock_get_permissions ):
139155 """it should return not-found OperationOutcome if it doesn't exist"""
@@ -165,7 +181,6 @@ def test_not_found_for_identifier(self, mock_get_permissions):
165181
166182 imms = identifier .replace ("|" , "#" )
167183 # When
168-
169184 response = self .controller .get_immunization_by_identifier (lambda_event )
170185
171186 # Then
@@ -200,7 +215,6 @@ def test_get_imms_by_identifer_patient_identifier_and_element_present(self, mock
200215 self .assertEqual (response ["statusCode" ], 400 )
201216 body = json .loads (response ["body" ])
202217 self .assertEqual (body ["resourceType" ], "OperationOutcome" )
203-
204218 @patch ("fhir_controller.get_supplier_permissions" )
205219 def test_get_imms_by_identifer_both_body_and_query_params_present (self , mock_get_supplier_permissions ):
206220 """it should return Immunization Id if it exists"""
@@ -422,7 +436,6 @@ def test_validate_immunization_identifier_having_whitespace(self,mock_get_suppli
422436 self .assertEqual (response ["statusCode" ], 400 )
423437 outcome = json .loads (response ["body" ])
424438 self .assertEqual (outcome ["resourceType" ], "OperationOutcome" )
425-
426439 @patch ("fhir_controller.get_supplier_permissions" )
427440 def test_validate_imms_id_invalid_vaccinetype (self , mock_get_permissions ):
428441 """it should validate lambda's Immunization id"""
@@ -738,7 +751,6 @@ def test_validate_immunization_identifier_having_whitespace(self,mock_get_suppli
738751 self .assertEqual (response ["statusCode" ], 400 )
739752 outcome = json .loads (response ["body" ])
740753 self .assertEqual (outcome ["resourceType" ], "OperationOutcome" )
741-
742754 @patch ("fhir_controller.get_supplier_permissions" )
743755 def test_validate_imms_id_invalid_vaccinetype (self , mock_get_permissions ):
744756 """it should validate lambda's Immunization id"""
@@ -820,7 +832,6 @@ def test_get_imms_by_id_unauthorised_vax_error(self,mock_permissions):
820832 # Then
821833 mock_permissions .assert_called_once_with ("test" )
822834 self .assertEqual (response ["statusCode" ], 403 )
823-
824835 @patch ("fhir_controller.get_supplier_permissions" )
825836 def test_get_imms_by_id_no_vax_permission (self , mock_permissions ):
826837 """it should return Immunization Id if it exists"""
@@ -1142,7 +1153,6 @@ def test_update_immunization_UnauthorizedVaxError(self, mock_get_supplier_permis
11421153 response = self .controller .update_immunization (aws_event )
11431154 mock_get_supplier_permissions .assert_called_once_with ("Test" )
11441155 self .assertEqual (response ["statusCode" ], 403 )
1145-
11461156 @patch ("fhir_controller.get_supplier_permissions" )
11471157 def test_update_immunization_UnauthorizedVaxError_check_for_non_batch (self , mock_get_supplier_permissions ):
11481158 """it should not update the Immunization record"""
@@ -1573,7 +1583,6 @@ def test_immunization_exception_not_found(self, mock_get_permissions):
15731583 body = json .loads (response ["body" ])
15741584 self .assertEqual (body ["resourceType" ], "OperationOutcome" )
15751585 self .assertEqual (body ["issue" ][0 ]["code" ], "not-found" )
1576-
15771586 @patch ("fhir_controller.get_supplier_permissions" )
15781587 def test_immunization_unhandled_error (self , mock_get_supplier_permissions ):
15791588 """it should return server-error OperationOutcome if service throws UnhandledResponseError"""
@@ -1596,8 +1605,22 @@ def test_immunization_unhandled_error(self, mock_get_supplier_permissions):
15961605 self .assertEqual (body ["resourceType" ], "OperationOutcome" )
15971606 self .assertEqual (body ["issue" ][0 ]["code" ], "exception" )
15981607
1599- class TestSearchImmunizations (unittest .TestCase ):
1608+ class TestSearchImmunizations (TestFhirControllerBase ):
1609+ MOCK_REDIS_V2D_RESPONSE = {
1610+ "PERTUSSIS" : "[{\" code\" : \" 27836007\" , \" term\" : \" Pertussis (disorder)\" }]" ,
1611+ "RSV" : "[{\" code\" : \" 55735004\" , \" term\" : \" Respiratory syncytial virus infection (disorder)\" }]" ,
1612+ "3in1" : "[{\" code\" : \" 398102009\" , \" term\" : \" Acute poliomyelitis\" }, {\" code\" : \" 397430003\" , \" term\" : \" Diphtheria caused by Corynebacterium diphtheriae\" }, {\" code\" : \" 76902006\" , \" term\" : \" Tetanus (disorder)\" }]" ,
1613+ "MMR" : "[{\" code\" : \" 14189004\" , \" term\" : \" Measles (disorder)\" }, {\" code\" : \" 36989005\" , \" term\" : \" Mumps (disorder)\" }, {\" code\" : \" 36653000\" , \" term\" : \" Rubella (disorder)\" }]" ,
1614+ "HPV" : "[{\" code\" : \" 240532009\" , \" term\" : \" Human papillomavirus infection\" }]" ,
1615+ "MMRV" : "[{\" code\" : \" 14189004\" , \" term\" : \" Measles (disorder)\" }, {\" code\" : \" 36989005\" , \" term\" : \" Mumps (disorder)\" }, {\" code\" : \" 36653000\" , \" term\" : \" Rubella (disorder)\" }, {\" code\" : \" 38907003\" , \" term\" : \" Varicella (disorder)\" }]" ,
1616+ "PCV13" : "[{\" code\" : \" 16814004\" , \" term\" : \" Pneumococcal infectious disease\" }]" ,
1617+ "SHINGLES" : "[{\" code\" : \" 4740000\" , \" term\" : \" Herpes zoster\" }]" ,
1618+ "COVID19" : "[{\" code\" : \" 840539006\" , \" term\" : \" Disease caused by severe acute respiratory syndrome coronavirus 2\" }]" ,
1619+ "FLU" : "[{\" code\" : \" 6142004\" , \" term\" : \" Influenza caused by seasonal influenza virus (disorder)\" }]" ,
1620+ "MENACWY" : "[{\" code\" : \" 23511006\" , \" term\" : \" Meningococcal infectious disease\" }]"
1621+ }
16001622 def setUp (self ):
1623+ super ().setUp ()
16011624 self .service = create_autospec (FhirService )
16021625 self .authorizer = create_autospec (Authorization )
16031626 self .controller = FhirController (self .authorizer , self .service )
@@ -1607,11 +1630,14 @@ def setUp(self):
16071630 self .date_to_key = "-date.to"
16081631 self .nhs_number_valid_value = "9000000009"
16091632 self .patient_identifier_valid_value = f"{ patient_identifier_system } |{ self .nhs_number_valid_value } "
1633+ self .mock_redis_client .hkeys .return_value = self .MOCK_REDIS_V2D_RESPONSE
1634+
1635+ def tearDown (self ):
1636+ return super ().tearDown ()
16101637
16111638 @patch ("fhir_controller.get_supplier_permissions" )
16121639 def test_get_search_immunizations (self , mock_get_supplier_permissions ):
16131640 """it should search based on patient_identifier and immunization_target"""
1614-
16151641 mock_get_supplier_permissions .return_value = ["COVID19.S" ]
16161642 search_result = Bundle .construct ()
16171643 self .service .search_immunizations .return_value = search_result
@@ -1795,7 +1821,6 @@ def test_post_search_immunizations(self,mock_get_supplier_permissions):
17951821 "headers" : {"Content-Type" : "application/x-www-form-urlencoded" , "SupplierSystem" : "Test" },
17961822 "body" : base64_encoded_body ,
17971823 }
1798-
17991824 # When
18001825 response = self .controller .search_immunizations (lambda_event )
18011826 # Then
@@ -1906,6 +1931,7 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search_403(self
19061931
19071932 @patch ("fhir_controller.process_search_params" , wraps = process_search_params )
19081933 def test_uses_parameter_parser (self , process_search_params : Mock ):
1934+ self .mock_redis_client .hkeys .return_value = self .MOCK_REDIS_V2D_RESPONSE
19091935 lambda_event = {
19101936 "multiValueQueryStringParameters" : {
19111937 self .patient_identifier_key : ["https://fhir.nhs.uk/Id/nhs-number|9000000009" ],
0 commit comments