55import unittest
66import uuid
77
8+ from unittest .mock import patch
89from fhir .resources .R4B .bundle import Bundle
910from fhir .resources .R4B .immunization import Immunization
1011from unittest .mock import create_autospec , ANY , patch , Mock
@@ -70,12 +71,14 @@ def setUp(self):
7071 self .authorizer = create_autospec (Authorization )
7172 self .controller = FhirController (self .authorizer , self .service )
7273
73- def test_get_imms_by_identifer (self ):
74+ @patch ("fhir_controller.get_supplier_permissions" )
75+ def test_get_imms_by_identifer (self , mock_get_permissions ):
7476 """it should return Immunization Id if it exists"""
7577 # Given
78+ mock_get_permissions .return_value = ["COVID19:search" ]
7679 self .service .get_immunization_by_identifier .return_value = {"id" : "test" , "Version" : 1 }
7780 lambda_event = {
78- "headers" : {"VaccineTypePermissions" : "COVID19:search" , " SupplierSystem" : "test" },
81+ "headers" : {"SupplierSystem" : "test" },
7982 "queryStringParameters" : {
8083 "immunization.identifier" : "https://supplierABC/identifiers/vacc|f10b59b3-fc73-4616-99c9-9e882ab31184" ,
8184 "_element" : "id,meta" ,
@@ -89,19 +92,22 @@ def test_get_imms_by_identifer(self):
8992 # When
9093 response = self .controller .get_immunization_by_identifier (lambda_event )
9194 # Then
95+ mock_get_permissions .assert_called_once_with ("test" )
9296 self .service .get_immunization_by_identifier .assert_called_once_with (
93- identifiers , "COVID19:search" , identifier , _element , False
97+ identifiers , [ "COVID19:search" ] , identifier , _element
9498 )
9599
96100 self .assertEqual (response ["statusCode" ], 200 )
97101 body = json .loads (response ["body" ])
98102 self .assertEqual (body ["id" ], "test" )
99103
100- def test_get_imms_by_identifer_no_vax_permission (self ):
104+ @patch ("fhir_controller.get_supplier_permissions" )
105+ def test_get_imms_by_identifer_no_vax_permission (self , mock_get_permissions ):
101106 """it should return Immunization Id if it exists"""
102107 # Given
108+ mock_get_permissions .return_value = []
103109 lambda_event = {
104- "headers" : {"VaccineTypePermissions" : "" , " SupplierSystem" : "test" },
110+ "headers" : {"SupplierSystem" : "test" },
105111 "queryStringParameters" : {
106112 "immunization.identifier" : "https://supplierABC/identifiers/vacc|f10b59b3-fc73-4616-99c9-9e882ab31184" ,
107113 "_element" : "id,meta" ,
@@ -111,6 +117,7 @@ def test_get_imms_by_identifer_no_vax_permission(self):
111117 # When
112118 response = self .controller .get_immunization_by_identifier (lambda_event )
113119 # Then
120+ mock_get_permissions .assert_called_once_with ("test" )
114121 self .assertEqual (response ["statusCode" ], 403 )
115122
116123 def test_get_imms_by_identifer_header_missing (self ):
@@ -126,37 +133,12 @@ def test_get_imms_by_identifer_header_missing(self):
126133 response = self .controller .get_immunization_by_identifier (lambda_event )
127134
128135 self .assertEqual (response ["statusCode" ], 403 )
129-
130- def test_get_imms_by_identifer_for_batch (self ):
131- """it should return Immunization Id if it exists"""
132- # Given
133- self .service .get_immunization_by_identifier .return_value = {"id" : "test" , "Version" : 1 }
134- lambda_event = {
135- "headers" : {"VaccineTypePermissions" : "COVID19:search" , "SupplierSystem" : "Imms-Batch-App" },
136- "queryStringParameters" : {
137- "immunization.identifier" : "https://supplierABC/identifiers/vacc|f10b59b3-fc73-4616-99c9-9e882ab31184" ,
138- "_element" : "id,meta" ,
139- },
140- "body" : None ,
141- }
142- identifier = lambda_event .get ("queryStringParameters" , {}).get ("immunization.identifier" )
143- _element = lambda_event .get ("queryStringParameters" , {}).get ("_element" )
144-
145- identifiers = identifier .replace ("|" , "#" )
146- # When
147- response = self .controller .get_immunization_by_identifier (lambda_event )
148- # Then
149- self .service .get_immunization_by_identifier .assert_called_once_with (
150- identifiers , None , identifier , _element , True
151- )
152-
153- self .assertEqual (response ["statusCode" ], 200 )
154- body = json .loads (response ["body" ])
155- self .assertEqual (body ["id" ], "test" )
156-
157- def test_not_found_for_identifier (self ):
136+
137+ @patch ("fhir_controller.get_supplier_permissions" )
138+ def test_not_found_for_identifier (self , mock_get_permissions ):
158139 """it should return not-found OperationOutcome if it doesn't exist"""
159140 # Given
141+ mock_get_permissions .return_value = ["COVID19:search" ]
160142 self .service .get_immunization_by_identifier .return_value = {
161143 "resourceType" : "Bundle" ,
162144 "type" : "searchset" ,
@@ -170,7 +152,7 @@ def test_not_found_for_identifier(self):
170152 "total" : 0 ,
171153 }
172154 lambda_event = {
173- "headers" : {"VaccineTypePermissions" : "COVID19:search" , " SupplierSystem" : "test" },
155+ "headers" : {"SupplierSystem" : "test" },
174156 "queryStringParameters" : {
175157 "immunization.identifier" : "https://supplierABC/identifiers/vacc|f10b59b3-fc73-4616-99c9-9e882ab31184" ,
176158 "_element" : "id,meta" ,
@@ -183,11 +165,13 @@ def test_not_found_for_identifier(self):
183165
184166 imms = identifier .replace ("|" , "#" )
185167 # When
168+
186169 response = self .controller .get_immunization_by_identifier (lambda_event )
187170
188171 # Then
172+ mock_get_permissions .assert_called_once_with ("test" )
189173 self .service .get_immunization_by_identifier .assert_called_once_with (
190- imms , "COVID19:search" , identifier , _element , False
174+ imms , [ "COVID19:search" ] , identifier , _element
191175 )
192176
193177 self .assertEqual (response ["statusCode" ], 200 )
@@ -419,13 +403,15 @@ def test_validate_immunization_identifier_having_whitespace(self):
419403 self .assertEqual (response ["statusCode" ], 400 )
420404 outcome = json .loads (response ["body" ])
421405 self .assertEqual (outcome ["resourceType" ], "OperationOutcome" )
422-
423- def test_validate_imms_id_invalid_vaccinetype (self ):
406+
407+ @patch ("fhir_controller.get_supplier_permissions" )
408+ def test_validate_imms_id_invalid_vaccinetype (self , mock_get_permissions ):
424409 """it should validate lambda's Immunization id"""
425410 # Given
411+ mock_get_permissions .return_value = ["COVID19:search" ]
426412 self .service .get_immunization_by_identifier .side_effect = UnauthorizedVaxError ()
427413 lambda_event = {
428- "headers" : {"VaccineTypePermissions" : "COVID19:search" , " SupplierSystem" : "test" },
414+ "headers" : {"SupplierSystem" : "test" },
429415 "queryStringParameters" : {
430416 "immunization.identifier" : "https://supplierABC/identifiers/vacc|f10b59b3-fc73-4616-99c9-9e882ab31184" ,
431417 "_element" : "id" ,
@@ -439,8 +425,9 @@ def test_validate_imms_id_invalid_vaccinetype(self):
439425 response = self .controller .get_immunization_by_identifier (lambda_event )
440426
441427 # Then
428+ mock_get_permissions .assert_called_once_with ("test" )
442429 self .service .get_immunization_by_identifier .assert_called_once_with (
443- identifiers , "COVID19:search" , identifier , _element , False
430+ identifiers , [ "COVID19:search" ] , identifier , _element
444431 )
445432
446433 self .assertEqual (response ["statusCode" ], 403 )
0 commit comments