Skip to content

Commit

Permalink
PRMP-1242: Fix missing auth service bug (#466)
Browse files Browse the repository at this point in the history
* add missing auth service to lg validator function

* [PRMP-1242] un-singletoned SSM service, made get_pds_service return an instance of SSM Service

* [PRMP-1242] refactored boolean logic for PDS API stubbing

* [PRMP-1242] converted environment variable mocking to monkeypatch

* [PRMP-1242] variable naming and parametrizing unit test

---------

Co-authored-by: AndyFlintNHS <[email protected]>
Co-authored-by: AndyFlintNHS <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2024
1 parent 1427b0c commit bc9efaa
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
7 changes: 1 addition & 6 deletions lambdas/scripts/batch_update_ods_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from pydantic import BaseModel, TypeAdapter, ValidationError
from requests import HTTPError
from services.base.dynamo_service import DynamoDBService
from services.base.nhs_oauth_service import NhsOauthService
from services.base.ssm_service import SSMService
from utils.exceptions import PdsErrorException, PdsResponseValidationException
from utils.utilities import get_pds_service

Expand All @@ -33,10 +31,7 @@ def __init__(
):
self.progress_store = progress_store_file_path
self.table_name = os.getenv("table_name")
pds_service_class = get_pds_service()
ssm_service = SSMService()
auth_service = NhsOauthService(ssm_service)
self.pds_service = pds_service_class(ssm_service, auth_service)
self.pds_service = get_pds_service()
self.dynamo_service = DynamoDBService()
self.progress: Dict[str, ProgressForPatient] = {}

Expand Down
6 changes: 2 additions & 4 deletions lambdas/services/search_patient_details_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from enums.repository_role import RepositoryRole
from pydantic import ValidationError
from pydantic_core import PydanticSerializationError
from services.base.nhs_oauth_service import NhsOauthService
from services.base.ssm_service import SSMService
from utils.audit_logging_setup import LoggingService
from utils.exceptions import (
Expand All @@ -26,9 +25,8 @@ def __init__(self, user_role, user_ods_code):

def handle_search_patient_request(self, nhs_number):
try:
auth_service = NhsOauthService(self.ssm_service)
pds_api_service = get_pds_service()(self.ssm_service, auth_service)
patient_details = pds_api_service.fetch_patient_details(nhs_number)
pds_service = get_pds_service()
patient_details = pds_service.fetch_patient_details(nhs_number)

self.check_if_user_authorise(
gp_ods_for_patient=patient_details.general_practice_ods
Expand Down
39 changes: 39 additions & 0 deletions lambdas/tests/unit/utils/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import pytest
from services.mock_pds_service import MockPdsApiService
from services.pds_api_service import PdsApiService
from utils.exceptions import InvalidResourceIdException
from utils.utilities import (
camelize_dict,
flatten,
get_file_key_from_s3_url,
get_pds_service,
redact_id_to_last_4_chars,
validate_nhs_number,
)
Expand Down Expand Up @@ -34,6 +37,42 @@ def test_decapitalise_keys():
assert actual == expected


@pytest.mark.parametrize(
"stub_value",
[
("True"),
("true"),
],
)
def test_get_pds_service_returns_stubbed_pds_when_true(monkeypatch, stub_value):
monkeypatch.setenv("PDS_FHIR_IS_STUBBED", stub_value)

response = get_pds_service()

assert isinstance(response, MockPdsApiService)


def test_get_pds_service_returns_stubbed_pds_when_unset():
response = get_pds_service()

assert isinstance(response, MockPdsApiService)


@pytest.mark.parametrize(
"stub_value",
[
("False"),
("false"),
],
)
def test_get_pds_service_returns_real_pds(monkeypatch, stub_value):
monkeypatch.setenv("PDS_FHIR_IS_STUBBED", stub_value)

response = get_pds_service()

assert isinstance(response, PdsApiService)


def test_redact_id():
mock_session_id = "1234532532432"
expected = "2432"
Expand Down
3 changes: 1 addition & 2 deletions lambdas/utils/lloyd_george_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ def validate_patient_date_of_birth(file_date_of_birth, pds_patient_details):


def getting_patient_info_from_pds(nhs_number: str) -> Patient:
pds_service_class = get_pds_service()
pds_service = pds_service_class(SSMService())
pds_service = get_pds_service()
pds_response = pds_service.pds_request(nhs_number=nhs_number, retry_on_expired=True)
check_pds_response_status(pds_response)
patient = parse_pds_response(pds_response)
Expand Down
16 changes: 10 additions & 6 deletions lambdas/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from urllib.parse import urlparse

from inflection import camelize
from services.base.nhs_oauth_service import NhsOauthService
from services.base.ssm_service import SSMService
from services.mock_pds_service import MockPdsApiService
from services.patient_search_service import PatientSearch
from services.pds_api_service import PdsApiService
from utils.exceptions import InvalidResourceIdException

Expand Down Expand Up @@ -42,12 +45,13 @@ def create_reference_id() -> str:
return str(uuid.uuid4())


def get_pds_service():
return (
PdsApiService
if (os.getenv("PDS_FHIR_IS_STUBBED") == "false")
else MockPdsApiService
)
def get_pds_service() -> PatientSearch:
if os.getenv("PDS_FHIR_IS_STUBBED") in ["False", "false"]:
ssm_service = SSMService()
auth_service = NhsOauthService(ssm_service)
return PdsApiService(ssm_service, auth_service)
else:
return MockPdsApiService()


def redact_id_to_last_4_chars(str_id: str) -> str:
Expand Down

0 comments on commit bc9efaa

Please sign in to comment.