Skip to content

Commit 8e709c1

Browse files
committed
Addition for the update journey - missed during refactor
1 parent d6e297f commit 8e709c1

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

backend/src/fhir_controller.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def update_immunization(self, aws_event):
209209
)
210210
return self.create_response(404, json.dumps(exp_error))
211211

212-
if "diagnostics" in existing_record and existing_record is not None:
212+
if "diagnostics" in existing_record:
213213
exp_error = create_operation_outcome(
214214
resource_id=str(uuid.uuid4()),
215215
severity=Severity.error,
@@ -222,14 +222,16 @@ def update_immunization(self, aws_event):
222222
# Validate if the imms resource does not exist - end
223223

224224
existing_resource_version = int(existing_record["Version"])
225+
existing_resource_vacc_type = existing_record["VaccineType"]
225226

226227
try:
227228
# Validate if the imms resource to be updated is a logically deleted resource - start
228-
if existing_record["DeletedAt"] == True:
229+
if existing_record["DeletedAt"]:
229230
outcome, resource, updated_version = self.fhir_service.reinstate_immunization(
230231
imms_id,
231232
imms,
232233
existing_resource_version,
234+
existing_resource_vacc_type,
233235
supplier_system
234236
)
235237
# Validate if the imms resource to be updated is a logically deleted resource-end
@@ -284,13 +286,15 @@ def update_immunization(self, aws_event):
284286
imms_id,
285287
imms,
286288
existing_resource_version,
289+
existing_resource_vacc_type,
287290
supplier_system
288291
)
289292
else:
290293
outcome, resource, updated_version = self.fhir_service.update_immunization(
291294
imms_id,
292295
imms,
293296
existing_resource_version,
297+
existing_resource_vacc_type,
294298
supplier_system
295299
)
296300

backend/src/fhir_service.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def update_immunization(
139139
imms_id: str,
140140
immunization: dict,
141141
existing_resource_version: int,
142+
existing_resource_vacc_type: str,
142143
supplier_system: str,
143144
) -> tuple[Optional[UpdateOutcome], Immunization | dict, Optional[int]]:
144145
# TODO - raise ticket to refactor this and below 3. Should have one update method and call repo based on type
@@ -150,7 +151,9 @@ def update_immunization(
150151

151152
vaccination_type = get_vaccine_type(immunization)
152153

153-
if not self.authoriser.authorise(supplier_system, ApiOperationCode.UPDATE, {vaccination_type}):
154+
# If the user is updating the vaccination_type, they must have permissions for both the new and old type
155+
if not self.authoriser.authorise(supplier_system, ApiOperationCode.UPDATE,
156+
{vaccination_type, existing_resource_vacc_type}):
154157
raise UnauthorizedVaxError()
155158

156159
imms, updated_version = self.immunization_repo.update_immunization(
@@ -168,6 +171,7 @@ def reinstate_immunization(
168171
imms_id: str,
169172
immunization: dict,
170173
existing_resource_version: int,
174+
existing_resource_vacc_type: str,
171175
supplier_system: str,
172176
) -> tuple[Optional[UpdateOutcome], Immunization | dict, Optional[int]]:
173177
immunization["id"] = imms_id
@@ -177,7 +181,8 @@ def reinstate_immunization(
177181

178182
vaccination_type = get_vaccine_type(immunization)
179183

180-
if not self.authoriser.authorise(supplier_system, ApiOperationCode.UPDATE, {vaccination_type}):
184+
if not self.authoriser.authorise(supplier_system, ApiOperationCode.UPDATE,
185+
{vaccination_type, existing_resource_vacc_type}):
181186
raise UnauthorizedVaxError()
182187

183188
imms, updated_version = self.immunization_repo.reinstate_immunization(
@@ -195,6 +200,7 @@ def update_reinstated_immunization(
195200
imms_id: str,
196201
immunization: dict,
197202
existing_resource_version: int,
203+
existing_resource_vacc_type: str,
198204
supplier_system: str,
199205
) -> tuple[Optional[UpdateOutcome], Immunization | dict, Optional[int]]:
200206
immunization["id"] = imms_id
@@ -204,7 +210,8 @@ def update_reinstated_immunization(
204210

205211
vaccination_type = get_vaccine_type(immunization)
206212

207-
if not self.authoriser.authorise(supplier_system, ApiOperationCode.UPDATE, {vaccination_type}):
213+
if not self.authoriser.authorise(supplier_system, ApiOperationCode.UPDATE,
214+
{vaccination_type, existing_resource_vacc_type}):
208215
raise UnauthorizedVaxError()
209216

210217
imms, updated_version = self.immunization_repo.update_reinstated_immunization(

backend/tests/test_fhir_controller.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ def test_update_immunization(self):
889889
response = self.controller.update_immunization(aws_event)
890890

891891
self.service.update_immunization.assert_called_once_with(
892-
imms_id, json.loads(imms), 1, "Test"
892+
imms_id, json.loads(imms), 1, "COVID19", "Test"
893893
)
894894
self.assertEqual(response["statusCode"], 200)
895895
self.assertEqual(response["headers"]["E-Tag"], 2)
@@ -1041,7 +1041,7 @@ def test_update_deletedat_immunization_with_version(self):
10411041
response = self.controller.update_immunization(aws_event)
10421042

10431043
self.service.reinstate_immunization.assert_called_once_with(
1044-
imms_id, json.loads(imms), 1, "Test"
1044+
imms_id, json.loads(imms), 1, "COVID19", "Test"
10451045
)
10461046
self.assertEqual(response["statusCode"], 200)
10471047
self.assertEqual(response["headers"]["E-Tag"], 2)
@@ -1067,7 +1067,7 @@ def test_update_deletedat_immunization_without_version(self):
10671067
response = self.controller.update_immunization(aws_event)
10681068

10691069
self.service.reinstate_immunization.assert_called_once_with(
1070-
imms_id, json.loads(imms), 1, "Test"
1070+
imms_id, json.loads(imms), 1, "COVID19", "Test"
10711071
)
10721072
self.assertEqual(response["statusCode"], 200)
10731073
self.assertEqual(response["headers"]["E-Tag"], 2)
@@ -1269,7 +1269,7 @@ def test_update_immunization_when_reinstated_true(self):
12691269
response = self.controller.update_immunization(aws_event)
12701270

12711271
self.service.update_reinstated_immunization.assert_called_once_with(
1272-
imms_id, json.loads(imms), 1, "Test"
1272+
imms_id, json.loads(imms), 1, "COVID19", "Test"
12731273
)
12741274
self.assertEqual(response["statusCode"], 200)
12751275
self.assertEqual(response["headers"]["E-Tag"], int("3"))

backend/tests/test_fhir_service.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -564,18 +564,20 @@ def test_update_immunization(self):
564564
self.imms_repo.update_immunization.return_value = (
565565
create_covid_19_immunization_dict(imms_id), 2
566566
)
567+
self.mock_redis_client.hget.return_value = "COVID19"
567568
self.authoriser.authorise.return_value = True
568569

569570
nhs_number = VALID_NHS_NUMBER
570571
req_imms = create_covid_19_immunization_dict(imms_id, nhs_number)
571572
req_patient = get_contained_patient(req_imms)
572573

573574
# When
574-
outcome, _, _ = self.fhir_service.update_immunization(imms_id, req_imms, 1, "Test")
575+
outcome, _, _ = self.fhir_service.update_immunization(imms_id, req_imms, 1, "COVID19", "Test")
575576

576577
# Then
577578
self.assertEqual(outcome, UpdateOutcome.UPDATE)
578579
self.imms_repo.update_immunization.assert_called_once_with(imms_id, req_imms, req_patient, 1, "Test")
580+
self.authoriser.authorise.assert_called_once_with("Test", ApiOperationCode.UPDATE, {"COVID19"})
579581

580582
def test_id_not_present(self):
581583
"""it should populate id in the message if it is not present"""
@@ -587,7 +589,7 @@ def test_id_not_present(self):
587589
del req_imms["id"]
588590

589591
# When
590-
self.fhir_service.update_immunization(req_imms_id, req_imms, 1, "Test")
592+
self.fhir_service.update_immunization(req_imms_id, req_imms, 1, "COVID19", "Test")
591593

592594
# Then
593595
passed_imms = self.imms_repo.update_immunization.call_args.args[1]
@@ -601,7 +603,7 @@ def test_patient_error(self):
601603

602604
with self.assertRaises(InvalidPatientId) as e:
603605
# When
604-
self.fhir_service.update_immunization(imms_id, bad_patient_imms, 1, "Test")
606+
self.fhir_service.update_immunization(imms_id, bad_patient_imms, 1, "COVID19", "Test")
605607

606608
# Then
607609
self.assertEqual(e.exception.patient_identifier, invalid_nhs_number)
@@ -615,7 +617,7 @@ def test_patient_error_invalid_nhs_number(self):
615617

616618
with self.assertRaises(InvalidPatientId) as e:
617619
# When
618-
self.fhir_service.update_immunization(imms_id, bad_patient_imms, 1, "Test")
620+
self.fhir_service.update_immunization(imms_id, bad_patient_imms, 1, "COVID19", "Test")
619621

620622
# Then
621623
self.assertEqual(e.exception.patient_identifier, invalid_nhs_number)
@@ -631,7 +633,7 @@ def test_reinstate_immunization_returns_updated_version(self):
631633
self.imms_repo.reinstate_immunization.return_value = (req_imms, 5)
632634

633635
outcome, resource, version = self.fhir_service.reinstate_immunization(
634-
imms_id, req_imms, 1, "Test"
636+
imms_id, req_imms, 1, "COVID19", "Test"
635637
)
636638

637639
self.assertEqual(outcome, UpdateOutcome.UPDATE)
@@ -646,7 +648,7 @@ def test_reinstate_immunization_raises_exception_when_missing_authz(self):
646648
self.mock_redis_client.hget.return_value = "FLU"
647649

648650
with self.assertRaises(UnauthorizedVaxError):
649-
self.fhir_service.reinstate_immunization(imms_id, req_imms, 1, "Test")
651+
self.fhir_service.reinstate_immunization(imms_id, req_imms, 1, "FLU", "Test")
650652

651653
self.authoriser.authorise.assert_called_once_with("Test", ApiOperationCode.UPDATE, {"FLU"})
652654

@@ -659,7 +661,7 @@ def test_update_reinstated_immunization_returns_updated_version(self):
659661
self.imms_repo.update_reinstated_immunization.return_value = (req_imms, 9)
660662

661663
outcome, resource, version = self.fhir_service.update_reinstated_immunization(
662-
imms_id, req_imms, 1, "Test"
664+
imms_id, req_imms, 1, "COVID19", "Test"
663665
)
664666

665667
self.assertEqual(outcome, UpdateOutcome.UPDATE)
@@ -672,7 +674,7 @@ def test_reinstate_immunization_with_diagnostics(self):
672674
self.fhir_service._validate_patient = MagicMock(return_value={"diagnostics": "invalid patient"})
673675

674676
outcome, resource, version = self.fhir_service.reinstate_immunization(
675-
imms_id, req_imms, 1, "Test"
677+
imms_id, req_imms, 1, "COVID19", "Test"
676678
)
677679

678680
self.assertIsNone(outcome)
@@ -687,7 +689,7 @@ def test_update_reinstated_immunization_with_diagnostics(self):
687689
self.fhir_service._validate_patient = MagicMock(return_value={"diagnostics": "invalid patient"})
688690

689691
outcome, resource, version = self.fhir_service.update_reinstated_immunization(
690-
imms_id, req_imms, 1, "Test"
692+
imms_id, req_imms, 1, "COVID19", "Test"
691693
)
692694

693695
self.assertIsNone(outcome)

0 commit comments

Comments
 (0)