Skip to content

Commit 5969962

Browse files
committed
test_for_search
1 parent b4eb8d4 commit 5969962

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

backend/src/fhir_controller.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ def delete_immunization(self, aws_event):
403403
return self.create_response(500, unhandled_error.to_operation_outcome())
404404
except UnauthorizedVaxError as unauthorized:
405405
return self.create_response(403, unauthorized.to_operation_outcome())
406+
406407
def search_immunizations(self, aws_event: APIGatewayProxyEventV1) -> dict:
407408
if response := self.authorize_request(EndpointOperation.SEARCH, aws_event):
408409
return response

backend/src/fhir_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def create_immunization(
116116
self.validator.validate(immunization)
117117
except (ValidationError, ValueError, MandatoryError) as error:
118118
raise CustomValidationError(message=str(error)) from error
119-
patient = None
119+
patient = self._validate_patient(immunization)
120120
if "diagnostics" in patient:
121121
return patient
122122

backend/tests/test_fhir_controller.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,18 +1028,15 @@ def test_update_immunization(self,mock_get_permissions):
10281028
self.assertTrue("body" not in response)
10291029

10301030
@patch("fhir_controller.sqs_client.send_message")
1031-
def test_update_immunization_etag_missing(self, mock_sqs_message, mock):
1031+
@patch("fhir_controller.get_supplier_permissions")
1032+
def test_update_immunization_etag_missing(self, mock_sqs_message, mock_permissions):
10321033
"""it should update Immunization"""
1034+
mock_permissions.return_value = ["COVID19:update"]
10331035
imms_id = "valid-id"
10341036
imms = {"id": "valid-id"}
10351037
aws_event = {
10361038
"headers": {
1037-
"VaccineTypePermissions": "COVID19:update",
1038-
"SupplierSystem": "Imms-Batch-App",
1039-
"BatchSupplierSystem": "Test",
1040-
"file_key": "test",
1041-
"row_id": "123",
1042-
"created_at_formatted_string": "2020-01-01",
1039+
"SupplierSystem": "Test",
10431040
"local_id": ValidValues.test_local_id,
10441041
"operation_requested": "update"
10451042
},
@@ -1048,6 +1045,7 @@ def test_update_immunization_etag_missing(self, mock_sqs_message, mock):
10481045
}
10491046
response = self.controller.update_immunization(aws_event)
10501047
mock_sqs_message.assert_called_once()
1048+
mock_permissions.assert_called_once_with("Test")
10511049
self.assertEqual(response["statusCode"], 400)
10521050
self.assertIn(
10531051
"Validation errors: Immunization resource version not specified in the request headers",
@@ -1180,6 +1178,7 @@ def test_unauthorised_update_immunization(self):
11801178
self.assertEqual(response["statusCode"], 403)
11811179

11821180
@patch("fhir_controller.sqs_client.send_message")
1181+
@patch("fhir_controller.get_supplier_permissions")
11831182
def test_update_immunization_for_batch(self, mock_send_message):
11841183
"""it should update Immunization"""
11851184
imms_id = "valid-id"
@@ -1777,7 +1776,8 @@ def setUp(self):
17771776
@patch("fhir_controller.get_supplier_permissions")
17781777
def test_get_search_immunizations(self, mock_get_supplier_permissions):
17791778
"""it should search based on patient_identifier and immunization_target"""
1780-
mock_get_supplier_permissions.return_value = {"vaccine_type_permissions": "flu:search"}
1779+
1780+
mock_get_supplier_permissions.return_value = ["covid19:search"]
17811781
search_result = Bundle.construct()
17821782
self.service.search_immunizations.return_value = search_result
17831783

@@ -1827,17 +1827,19 @@ def test_get_search_immunizations_vax_permission_check(self):
18271827
# Then
18281828
self.assertEqual(response["statusCode"], 403)
18291829

1830-
def test_get_search_immunizations_for_unauthorized_vaccine_type_search(self):
1830+
@patch("fhir_controller.get_supplier_permissions")
1831+
def test_get_search_immunizations_for_unauthorized_vaccine_type_search(self, mock_get_supplier_permissions):
18311832
"""it should return 200 and contains warning operation outcome as the user is not having authorization for one of the vaccine type"""
18321833
search_result = load_json_data("sample_immunization_response _for _not_done_event.json")
1834+
mock_get_supplier_permissions.return_value = ["covid19:search"]
18331835
bundle = Bundle.parse_obj(search_result)
18341836
self.service.search_immunizations.return_value = bundle
18351837

18361838
vaccine_type = VaccineTypes().all[0], VaccineTypes().all[1]
18371839
vaccine_type = ",".join(vaccine_type)
18381840

18391841
lambda_event = {
1840-
"headers": {"Content-Type": "application/x-www-form-urlencoded", "VaccineTypePermissions": "flu:search"},
1842+
"headers": {"Content-Type": "application/x-www-form-urlencoded", "SupplierSystem": "test",},
18411843
"multiValueQueryStringParameters": {
18421844
self.immunization_target_key: [vaccine_type],
18431845
self.patient_identifier_key: [self.patient_identifier_valid_value],
@@ -1855,16 +1857,18 @@ def test_get_search_immunizations_for_unauthorized_vaccine_type_search(self):
18551857
)
18561858
self.assertTrue(operation_outcome_present, "OperationOutcome resource is not present in the response")
18571859

1858-
def test_get_search_immunizations_for_unauthorized_vaccine_type_search_400(self):
1860+
@patch("fhir_controller.get_supplier_permissions")
1861+
def test_get_search_immunizations_for_unauthorized_vaccine_type_search_400(self,mock_get_supplier_permissions):
18591862
"""it should return 400 as the the request is having invalid vaccine type"""
18601863
search_result = load_json_data("sample_immunization_response _for _not_done_event.json")
1864+
mock_get_supplier_permissions.return_value = ["covid19:search"]
18611865
bundle = Bundle.parse_obj(search_result)
18621866
self.service.search_immunizations.return_value = bundle
18631867

18641868
vaccine_type = "FLUE"
18651869

18661870
lambda_event = {
1867-
"headers": {"Content-Type": "application/x-www-form-urlencoded", "VaccineTypePermissions": "flu:search"},
1871+
"headers": {"Content-Type": "application/x-www-form-urlencoded", "SupplierSystem": "test"},
18681872
"multiValueQueryStringParameters": {
18691873
self.immunization_target_key: [vaccine_type],
18701874
self.patient_identifier_key: [self.patient_identifier_valid_value],
@@ -1877,17 +1881,19 @@ def test_get_search_immunizations_for_unauthorized_vaccine_type_search_400(self)
18771881
body = json.loads(response["body"])
18781882
self.assertEqual(body["resourceType"], "OperationOutcome")
18791883

1880-
def test_get_search_immunizations_for_unauthorized_vaccine_type_search_403(self):
1884+
@patch("fhir_controller.get_supplier_permissions")
1885+
def test_get_search_immunizations_for_unauthorized_vaccine_type_search_403(self, mock_get_supplier_permissions):
18811886
"""it should return 403 as the user doesnt have vaccinetype permission"""
18821887
search_result = load_json_data("sample_immunization_response _for _not_done_event.json")
18831888
bundle = Bundle.parse_obj(search_result)
1889+
mock_get_supplier_permissions.return_value = []
18841890
self.service.search_immunizations.return_value = bundle
18851891

18861892
vaccine_type = VaccineTypes().all[0], VaccineTypes().all[1]
18871893
vaccine_type = ",".join(vaccine_type)
18881894

18891895
lambda_event = {
1890-
"headers": {"Content-Type": "application/x-www-form-urlencoded", "VaccineTypePermissions": ""},
1896+
"headers": {"Content-Type": "application/x-www-form-urlencoded", "SupplierSystem": "test",},
18911897
"multiValueQueryStringParameters": {
18921898
self.immunization_target_key: [vaccine_type],
18931899
self.patient_identifier_key: [self.patient_identifier_valid_value],
@@ -1896,21 +1902,24 @@ def test_get_search_immunizations_for_unauthorized_vaccine_type_search_403(self)
18961902

18971903
# When
18981904
response = self.controller.search_immunizations(lambda_event)
1905+
mock_get_supplier_permissions.assert_called_once_with("test")
18991906
self.assertEqual(response["statusCode"], 403)
19001907
body = json.loads(response["body"])
19011908
self.assertEqual(body["resourceType"], "OperationOutcome")
19021909

1903-
def test_get_search_immunizations_unauthorized(self):
1910+
@patch("fhir_controller.get_supplier_permissions")
1911+
def test_get_search_immunizations_unauthorized(self, mock_get_supplier_permissions):
19041912
"""it should search based on patient_identifier and immunization_target"""
19051913
search_result = Bundle.construct()
1914+
mock_get_supplier_permissions.return_value = []
19061915
self.service.search_immunizations.return_value = search_result
19071916

19081917
vaccine_type = VaccineTypes().all[0]
19091918
params = f"{self.immunization_target_key}={vaccine_type}&" + urllib.parse.urlencode(
19101919
[(f"{self.patient_identifier_key}", f"{self.patient_identifier_valid_value}")]
19111920
)
19121921
lambda_event = {
1913-
"headers": {"Content-Type": "application/x-www-form-urlencoded", "VaccineTypePermissions": "FLU:search"},
1922+
"headers": {"Content-Type": "application/x-www-form-urlencoded", "SupplierSystem": "test"},
19141923
"multiValueQueryStringParameters": {
19151924
self.immunization_target_key: [vaccine_type],
19161925
self.patient_identifier_key: [self.patient_identifier_valid_value],
@@ -1919,11 +1928,13 @@ def test_get_search_immunizations_unauthorized(self):
19191928

19201929
# When
19211930
response = self.controller.search_immunizations(lambda_event)
1922-
1931+
mock_get_supplier_permissions.assert_called_once_with("test")
19231932
self.assertEqual(response["statusCode"], 403)
19241933

1925-
def test_post_search_immunizations(self):
1934+
@patch("fhir_controller.get_supplier_permissions")
1935+
def test_post_search_immunizations(self,mock_get_supplier_permissions):
19261936
"""it should search based on patient_identifier and immunization_target"""
1937+
mock_get_supplier_permissions.return_value = ["covid19:search"]
19271938
search_result = Bundle.construct()
19281939
self.service.search_immunizations.return_value = search_result
19291940

@@ -1943,27 +1954,28 @@ def test_post_search_immunizations(self):
19431954
# Construct the lambda event
19441955
lambda_event = {
19451956
"httpMethod": "POST",
1946-
"headers": {
1947-
"Content-Type": "application/x-www-form-urlencoded",
1948-
"VaccineTypePermissions": "COVID19:search",
1949-
},
1957+
"headers": {"Content-Type": "application/x-www-form-urlencoded", "SupplierSystem": "Test"},
19501958
"body": base64_encoded_body,
19511959
}
1960+
19521961
# When
19531962
response = self.controller.search_immunizations(lambda_event)
19541963
# Then
19551964
self.service.search_immunizations.assert_called_once_with(
19561965
self.nhs_number_valid_value, [vaccine_type], params, ANY, ANY
19571966
)
19581967
self.assertEqual(response["statusCode"], 200)
1968+
mock_get_supplier_permissions.assert_called_once_with("Test")
19591969
body = json.loads(response["body"])
19601970
self.assertEqual(body["resourceType"], "Bundle")
19611971

1962-
def test_post_search_immunizations_for_unauthorized_vaccine_type_search(self):
1972+
@patch("fhir_controller.get_supplier_permissions")
1973+
def test_post_search_immunizations_for_unauthorized_vaccine_type_search(self,mock_get_supplier_permissions):
19631974
"""it should return 200 and contains warning operation outcome as the user is not having authorization for one of the vaccine type"""
19641975
search_result = load_json_data("sample_immunization_response _for _not_done_event.json")
19651976
bundle = Bundle.parse_obj(search_result)
19661977
self.service.search_immunizations.return_value = bundle
1978+
mock_get_supplier_permissions.return_value = ["covid19:search"]
19671979

19681980
vaccine_type = VaccineTypes().all[0], VaccineTypes().all[1]
19691981
vaccine_type = ",".join(vaccine_type)
@@ -1979,7 +1991,7 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search(self):
19791991
# Construct the lambda event
19801992
lambda_event = {
19811993
"httpMethod": "POST",
1982-
"headers": {"Content-Type": "application/x-www-form-urlencoded", "VaccineTypePermissions": "flu:search"},
1994+
"headers": {"Content-Type": "application/x-www-form-urlencoded", "SupplierSystem": "Test"},
19831995
"body": base64_encoded_body,
19841996
}
19851997
# When
@@ -2022,10 +2034,12 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search_400(self
20222034
body = json.loads(response["body"])
20232035
self.assertEqual(body["resourceType"], "OperationOutcome")
20242036

2025-
def test_post_search_immunizations_for_unauthorized_vaccine_type_search_403(self):
2037+
@patch("fhir_controller.get_supplier_permissions")
2038+
def test_post_search_immunizations_for_unauthorized_vaccine_type_search_403(self, mock_get_supplier_permissions):
20262039
"""it should return 403 as the user doesnt have vaccinetype permission"""
20272040
search_result = load_json_data("sample_immunization_response _for _not_done_event.json")
20282041
bundle = Bundle.parse_obj(search_result)
2042+
mock_get_supplier_permissions.return_value = []
20292043
self.service.search_immunizations.return_value = bundle
20302044

20312045
vaccine_type = VaccineTypes().all[0], VaccineTypes().all[1]
@@ -2043,7 +2057,7 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search_403(self
20432057
# Construct the lambda event
20442058
lambda_event = {
20452059
"httpMethod": "POST",
2046-
"headers": {"Content-Type": "application/x-www-form-urlencoded", "VaccineTypePermissions": ""},
2060+
"headers": {"Content-Type": "application/x-www-form-urlencoded", "SupplierSystem": "Test"},
20472061
"body": base64_encoded_body,
20482062
}
20492063
# When
@@ -2089,18 +2103,20 @@ def test_search_immunizations_returns_400_on_ParameterException_from_parameter_p
20892103
outcome = json.loads(response["body"])
20902104
self.assertEqual(outcome["resourceType"], "OperationOutcome")
20912105

2092-
def test_search_immunizations_returns_400_on_passing_superseded_nhs_number(self):
2106+
@patch("fhir_controller.get_supplier_permissions")
2107+
def test_search_immunizations_returns_400_on_passing_superseded_nhs_number(self, mock_get_supplier_permissions):
20932108
"This method should return 400 as input paramter has superseded nhs number."
20942109
search_result = {
20952110
"diagnostics": "Validation errors: contained[?(@.resourceType=='Patient')].identifier[0].value does not exists"
20962111
}
20972112
self.service.search_immunizations.return_value = search_result
2113+
mock_get_supplier_permissions.return_value = ["covid19:search"]
20982114

20992115
vaccine_type = VaccineTypes().all[0]
21002116
lambda_event = {
21012117
"headers": {
21022118
"Content-Type": "application/x-www-form-urlencoded",
2103-
"VaccineTypePermissions": "COVID19:search",
2119+
"SupplierSystem": "Test",
21042120
},
21052121
"multiValueQueryStringParameters": {
21062122
self.immunization_target_key: [vaccine_type],
@@ -2112,19 +2128,22 @@ def test_search_immunizations_returns_400_on_passing_superseded_nhs_number(self)
21122128
response = self.controller.search_immunizations(lambda_event)
21132129

21142130
self.assertEqual(response["statusCode"], 400)
2131+
mock_get_supplier_permissions.assert_called_once_with("Test")
21152132
body = json.loads(response["body"])
21162133
self.assertEqual(body["resourceType"], "OperationOutcome")
21172134

2118-
def test_search_immunizations_returns_200_remove_vaccine_not_done(self):
2135+
@patch("fhir_controller.get_supplier_permissions")
2136+
def test_search_immunizations_returns_200_remove_vaccine_not_done(self, mock_get_supplier_permissions):
21192137
"This method should return 200 but remove the data which has status as not done."
21202138
search_result = load_json_data("sample_immunization_response _for _not_done_event.json")
21212139
bundle = Bundle.parse_obj(search_result)
2140+
mock_get_supplier_permissions.return_value = ["covid19:search"]
21222141
self.service.search_immunizations.return_value = bundle
21232142
vaccine_type = VaccineTypes().all[0]
21242143
lambda_event = {
21252144
"headers": {
21262145
"Content-Type": "application/x-www-form-urlencoded",
2127-
"VaccineTypePermissions": "COVID19:search",
2146+
"SupplierSystem": "Test",
21282147
},
21292148
"multiValueQueryStringParameters": {
21302149
self.immunization_target_key: [vaccine_type],
@@ -2140,10 +2159,12 @@ def test_search_immunizations_returns_200_remove_vaccine_not_done(self):
21402159
for entry in body.get("entry", []):
21412160
self.assertNotEqual(entry.get("resource", {}).get("status"), "not-done", "entered-in-error")
21422161

2143-
def test_self_link_excludes_extraneous_params(self):
2162+
@patch("fhir_controller.get_supplier_permissions")
2163+
def test_self_link_excludes_extraneous_params(self, mock_get_supplier_permissions):
21442164
search_result = Bundle.construct()
21452165
self.service.search_immunizations.return_value = search_result
21462166
vaccine_type = VaccineTypes().all[0]
2167+
mock_get_supplier_permissions.return_value = ["covid19:search"]
21472168
params = f"{self.immunization_target_key}={vaccine_type}&" + urllib.parse.urlencode(
21482169
[(f"{self.patient_identifier_key}", f"{self.patient_identifier_valid_value}")]
21492170
)
@@ -2158,7 +2179,7 @@ def test_self_link_excludes_extraneous_params(self):
21582179
"body": None,
21592180
"headers": {
21602181
"Content-Type": "application/x-www-form-urlencoded",
2161-
"VaccineTypePermissions": "COVID19:search",
2182+
"SupplierSystem": "Test",
21622183
},
21632184
"httpMethod": "POST",
21642185
}

0 commit comments

Comments
 (0)