44import json
55import unittest
66import uuid
7+ import datetime
78
89from unittest .mock import patch
910from fhir .resources .R4B .bundle import Bundle
2627 IdentifierDuplicationError ,
2728)
2829from tests .utils .immunization_utils import create_covid_19_immunization
29- from parameter_parser import patient_identifier_system , process_search_params
30+ from parameter_parser import patient_identifier_system , process_search_params , SearchParams
3031from tests .utils .generic_utils import load_json_data
3132from utils .mock_redis import mock_redis_hkeys
3233
@@ -38,7 +39,6 @@ def setUp(self):
3839 super ().setUp ()
3940 self .redis_patcher = patch ("parameter_parser.redis_client" )
4041 self .mock_redis_client = self .redis_patcher .start ()
41- self .mock_redis_client .hkeys .side_effect = mock_redis_hkeys
4242 self .logger_info_patcher = patch ("logging.Logger.info" )
4343 self .mock_logger_info = self .logger_info_patcher .start ()
4444
@@ -54,6 +54,11 @@ def setUp(self):
5454 self .repository = create_autospec (ImmunizationRepository )
5555 self .authorizer = create_autospec (Authorization )
5656 self .controller = FhirController (self .authorizer , self .service )
57+ def tearDown (self ):
58+ self .service .reset_mock ()
59+ self .repository .reset_mock ()
60+ self .authorizer .reset_mock ()
61+ self .controller = None
5762
5863 def test_create_response (self ):
5964 """it should return application/fhir+json with correct status code"""
@@ -1588,7 +1593,10 @@ def setUp(self):
15881593 self .patient_identifier_valid_value = f"{ patient_identifier_system } |{ self .nhs_number_valid_value } "
15891594
15901595 def tearDown (self ):
1591- return super ().tearDown ()
1596+ self .service .reset_mock ()
1597+ self .authorizer .reset_mock ()
1598+ self .controller = None
1599+
15921600
15931601 @patch ("fhir_controller.get_supplier_permissions" )
15941602 def test_get_search_immunizations (self , mock_get_supplier_permissions ):
@@ -1599,6 +1607,7 @@ def test_get_search_immunizations(self, mock_get_supplier_permissions):
15991607 self .service .search_immunizations .return_value = search_result
16001608
16011609 vaccine_type = "COVID19"
1610+ self .mock_redis_client .hkeys .return_value = [vaccine_type ]
16021611 params = f"{ self .immunization_target_key } ={ vaccine_type } &" + urllib .parse .urlencode (
16031612 [(f"{ self .patient_identifier_key } " , f"{ self .patient_identifier_valid_value } " )]
16041613 )
@@ -1633,6 +1642,7 @@ def test_get_search_immunizations_vax_permission_check(self, mock_get_supplier_p
16331642 self .service .search_immunizations .return_value = search_result
16341643
16351644 vaccine_type = "COVID19"
1645+ self .mock_redis_client .hkeys .return_value = [vaccine_type ]
16361646 lambda_event = {
16371647 "SupplierSystem" : "test" ,
16381648 "multiValueQueryStringParameters" : {
@@ -1657,7 +1667,7 @@ def test_get_search_immunizations_for_unauthorized_vaccine_type_search(self, moc
16571667
16581668 vaccine_type = "COVID19" , "FLU"
16591669 vaccine_type = "," .join (vaccine_type )
1660-
1670+ self . mock_redis_client . hkeys . return_value = [ "COVID19" , "FLU" ]
16611671 lambda_event = {
16621672 "headers" : {"Content-Type" : "application/x-www-form-urlencoded" , "SupplierSystem" : "test" ,},
16631673 "multiValueQueryStringParameters" : {
@@ -1686,7 +1696,7 @@ def test_get_search_immunizations_for_unauthorized_vaccine_type_search_400(self,
16861696 self .service .search_immunizations .return_value = bundle
16871697
16881698 vaccine_type = "FLUE"
1689-
1699+ self . mock_redis_client . hkeys . return_value = [ "NOT-FLUUE" ]
16901700 lambda_event = {
16911701 "headers" : {"Content-Type" : "application/x-www-form-urlencoded" , "SupplierSystem" : "test" },
16921702 "multiValueQueryStringParameters" : {
@@ -1711,7 +1721,7 @@ def test_get_search_immunizations_for_unauthorized_vaccine_type_search_403(self,
17111721
17121722 vaccine_type = "COVID19" , "FLU"
17131723 vaccine_type = "," .join (vaccine_type )
1714-
1724+ self . mock_redis_client . hkeys . return_value = [ "COVID19" , "FLU" ]
17151725 lambda_event = {
17161726 "headers" : {"Content-Type" : "application/x-www-form-urlencoded" , "SupplierSystem" : "test" ,},
17171727 "multiValueQueryStringParameters" : {
@@ -1735,6 +1745,7 @@ def test_get_search_immunizations_unauthorized(self, mock_get_supplier_permissio
17351745 self .service .search_immunizations .return_value = search_result
17361746
17371747 vaccine_type = "COVID19"
1748+ self .mock_redis_client .hkeys .return_value = [vaccine_type ]
17381749 params = f"{ self .immunization_target_key } ={ vaccine_type } &" + urllib .parse .urlencode (
17391750 [(f"{ self .patient_identifier_key } " , f"{ self .patient_identifier_valid_value } " )]
17401751 )
@@ -1759,6 +1770,7 @@ def test_post_search_immunizations(self,mock_get_supplier_permissions):
17591770 self .service .search_immunizations .return_value = search_result
17601771
17611772 vaccine_type = "COVID19"
1773+ self .mock_redis_client .hkeys .return_value = [vaccine_type ]
17621774 params = f"{ self .immunization_target_key } ={ vaccine_type } &" + urllib .parse .urlencode (
17631775 [(f"{ self .patient_identifier_key } " , f"{ self .patient_identifier_valid_value } " )]
17641776 )
@@ -1799,6 +1811,7 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search(self,moc
17991811
18001812 vaccine_type = "COVID19" , "FLU"
18011813 vaccine_type = "," .join (vaccine_type )
1814+ self .mock_redis_client .hkeys .return_value = ["COVID19" , "FLU" ]
18021815 # Construct the application/x-www-form-urlencoded body
18031816 body = {
18041817 self .patient_identifier_key : self .patient_identifier_valid_value ,
@@ -1832,7 +1845,7 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search_400(self
18321845 self .service .search_immunizations .return_value = bundle
18331846
18341847 vaccine_type = "FLUE"
1835-
1848+ self . mock_redis_client . hkeys . return_value = [ "NOT-FLUE" ]
18361849 # Construct the application/x-www-form-urlencoded body
18371850 body = {
18381851 self .patient_identifier_key : self .patient_identifier_valid_value ,
@@ -1864,6 +1877,7 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search_403(self
18641877
18651878 vaccine_type = "COVID19" , "FLU"
18661879 vaccine_type = "," .join (vaccine_type )
1880+ self .mock_redis_client .hkeys .return_value = ["COVID19" , "FLU" ]
18671881
18681882 # Construct the application/x-www-form-urlencoded body
18691883 body = {
@@ -1933,6 +1947,7 @@ def test_search_immunizations_returns_400_on_passing_superseded_nhs_number(self,
19331947 mock_get_supplier_permissions .return_value = ["covid19:search" ]
19341948
19351949 vaccine_type = "COVID19"
1950+ self .mock_redis_client .hkeys .return_value = [vaccine_type ]
19361951 lambda_event = {
19371952 "headers" : {
19381953 "Content-Type" : "application/x-www-form-urlencoded" ,
@@ -1960,6 +1975,7 @@ def test_search_immunizations_returns_200_remove_vaccine_not_done(self, mock_get
19601975 mock_get_supplier_permissions .return_value = ["covid19:search" ]
19611976 self .service .search_immunizations .return_value = bundle
19621977 vaccine_type = "COVID19"
1978+ self .mock_redis_client .hkeys .return_value = [vaccine_type ]
19631979 lambda_event = {
19641980 "headers" : {
19651981 "Content-Type" : "application/x-www-form-urlencoded" ,
@@ -1980,10 +1996,20 @@ def test_search_immunizations_returns_200_remove_vaccine_not_done(self, mock_get
19801996 self .assertNotEqual (entry .get ("resource" , {}).get ("status" ), "not-done" , "entered-in-error" )
19811997
19821998 @patch ("fhir_controller.get_supplier_permissions" )
1983- def test_self_link_excludes_extraneous_params (self , mock_get_supplier_permissions ):
1999+ @patch ("fhir_controller.process_search_params" )
2000+ def test_self_link_excludes_extraneous_params (self , mock_process_search_params , mock_get_supplier_permissions ):
2001+ # patch process_search_params called from search_immunizations
19842002 search_result = Bundle .construct ()
19852003 self .service .search_immunizations .return_value = search_result
19862004 vaccine_type = "COVID19"
2005+
2006+ mock_process_search_params .return_value = SearchParams (
2007+ patient_identifier = self .nhs_number_valid_value ,
2008+ immunization_targets = [vaccine_type ],
2009+ date_from = datetime .date (1900 , 1 , 1 ),
2010+ date_to = datetime .date (9999 , 12 , 31 ),
2011+ include = None
2012+ )
19872013 mock_get_supplier_permissions .return_value = ["covid19:search" ]
19882014 params = f"{ self .immunization_target_key } ={ vaccine_type } &" + urllib .parse .urlencode (
19892015 [(f"{ self .patient_identifier_key } " , f"{ self .patient_identifier_valid_value } " )]
@@ -2003,6 +2029,7 @@ def test_self_link_excludes_extraneous_params(self, mock_get_supplier_permission
20032029 },
20042030 "httpMethod" : "POST" ,
20052031 }
2032+ self .controller ._new_vaccine_request .return_value = False
20062033
20072034 self .controller .search_immunizations (lambda_event )
20082035
0 commit comments