Skip to content

Commit 420f464

Browse files
committed
VED-740b: refactor env vars and service url and unittest
1 parent 226596a commit 420f464

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

backend/src/fhir_service.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@
3030
logging.basicConfig(level="INFO")
3131
logger = logging.getLogger()
3232

33-
def get_service_url(
34-
service_env: str = os.getenv("IMMUNIZATION_ENV"),
35-
service_base_path: str = os.getenv("IMMUNIZATION_BASE_PATH"),
36-
):
37-
if not service_base_path:
38-
service_base_path = "immunisation-fhir-api"
39-
33+
def get_service_url(service_env: str = None, service_base_path: str = None
34+
) -> str:
35+
service_env: str = service_env if service_env is not None else os.getenv("IMMUNIZATION_ENV", "")
36+
service_base_path: str = service_base_path if service_base_path else os.getenv("IMMUNIZATION_BASE_PATH")
37+
4038
non_prod = ["internal-dev", "int", "sandbox"]
4139
if service_env in non_prod:
4240
subdomain = f"{service_env}."
@@ -45,8 +43,6 @@ def get_service_url(
4543
else:
4644
subdomain = "internal-dev."
4745

48-
service_base_path = re.sub(r"immunisation-fhir-api(-pr-\d+)?", r"immunisation-fhir-api/FHIR/R4\1", service_base_path)
49-
5046
return f"https://{subdomain}api.service.nhs.uk/{service_base_path}"
5147

5248

backend/tests/test_fhir_service.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import uuid
33
import datetime
44
import unittest
5-
from unittest.mock import MagicMock
5+
import os
6+
from unittest.mock import MagicMock, patch
67
from copy import deepcopy
78
from unittest.mock import create_autospec, patch
89
from decimal import Decimal
@@ -54,22 +55,22 @@ def tearDown(self):
5455
def test_get_service_url(self):
5556
"""it should create service url"""
5657
env = "int"
57-
base_path = "my-base-path"
58+
base_path = "immunisation-fhir-api/FHIR/R4"
5859
url = get_service_url(env, base_path)
5960
self.assertEqual(url, f"https://{env}.api.service.nhs.uk/{base_path}")
6061
# default should be internal-dev
6162
env = "it-does-not-exist"
62-
base_path = "my-base-path"
63+
base_path = "immunisation-fhir-api/FHIR/R4"
6364
url = get_service_url(env, base_path)
6465
self.assertEqual(url, f"https://internal-dev.api.service.nhs.uk/{base_path}")
6566
# prod should not have a subdomain
6667
env = "prod"
67-
base_path = "my-base-path"
68+
base_path = "immunisation-fhir-api/FHIR/R4"
6869
url = get_service_url(env, base_path)
6970
self.assertEqual(url, f"https://api.service.nhs.uk/{base_path}")
7071
# any other env should fall back to internal-dev (like pr-xx or per-user)
7172
env = "pr-42"
72-
base_path = "my-base-path"
73+
base_path = "immunisation-fhir-api/FHIR/R4"
7374
url = get_service_url(env, base_path)
7475
self.assertEqual(url, f"https://internal-dev.api.service.nhs.uk/{base_path}")
7576

@@ -772,6 +773,8 @@ class TestSearchImmunizations(unittest.TestCase):
772773
MOCK_SUPPLIER_SYSTEM_NAME = "Test"
773774

774775
def setUp(self):
776+
os.environ["IMMUNIZATION_ENV"] = "internal-dev"
777+
os.environ["IMMUNIZATION_BASE_PATH"] = "immunisation-fhir-api/FHIR/R4"
775778
self.authoriser = create_autospec(Authoriser)
776779
self.imms_repo = create_autospec(ImmunizationRepository)
777780
self.validator = create_autospec(ImmunizationValidator)
@@ -1083,7 +1086,6 @@ def test_matches_contain_fullUrl(self):
10831086
"""All matches must have a fullUrl consisting of their id.
10841087
See http://hl7.org/fhir/R4B/bundle-definitions.html#Bundle.entry.fullUrl.
10851088
Tested because fhir.resources validation doesn't check this as mandatory."""
1086-
10871089
imms_ids = ["imms-1", "imms-2"]
10881090
imms_list = [create_covid_19_immunization_dict(imms_id) for imms_id in imms_ids]
10891091
self.imms_repo.find_immunizations.return_value = imms_list
@@ -1108,6 +1110,9 @@ def test_patient_contains_fullUrl(self):
11081110
See http://hl7.org/fhir/R4B/bundle-definitions.html#Bundle.entry.fullUrl.
11091111
Tested because fhir.resources validation doesn't check this as mandatory."""
11101112

1113+
print(f"IMMUNIZATION_ENV: {os.environ.get('IMMUNIZATION_ENV')}")
1114+
print(f"IMMUNIZATION_BASE_PATH: {os.environ.get('IMMUNIZATION_BASE_PATH')}")
1115+
11111116
imms_ids = ["imms-1", "imms-2"]
11121117
imms_list = [create_covid_19_immunization_dict(imms_id) for imms_id in imms_ids]
11131118
self.imms_repo.find_immunizations.return_value = imms_list

terraform/endpoints.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ locals {
2424
imms_lambda_env_vars = {
2525
"DYNAMODB_TABLE_NAME" = local.imms_table_name,
2626
"IMMUNIZATION_ENV" = local.resource_scope,
27-
"IMMUNIZATION_BASE_PATH" = strcontains(var.sub_environment, "pr-") ? "immunisation-fhir-api-${var.sub_environment}" : "immunisation-fhir-api"
27+
SERVICE_BASE_PATH=immunisation-fhir-api/FHIR/R4-${{ inputs.sub_environment }}
28+
"IMMUNIZATION_BASE_PATH" = strcontains(var.sub_environment, "pr-") ? "immunisation-fhir-api-/FHIR/R4-${var.sub_environment}" : "immunisation-fhir-api/FHIR/R4"
2829
# except for prod and ref, any other env uses PDS int environment
2930
"PDS_ENV" = var.pds_environment
3031
"PDS_CHECK_ENABLED" = tostring(var.pds_check_enabled)

0 commit comments

Comments
 (0)