Skip to content

Commit

Permalink
[PRMP-1287] add service unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Dec 17, 2024
1 parent 1c46437 commit 4e55604
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
7 changes: 2 additions & 5 deletions lambdas/services/nrl_get_document_reference_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,8 @@ def process_role_code(self, role_codes) -> str:
if role_code.startswith("R"):
return role_code

def patient_is_inactive(self, current_gp_ods_code):
try:
return current_gp_ods_code in PatientOdsInactiveStatus
except TypeError:
return False
def patient_is_inactive(self, current_gp_ods_code: str):
return current_gp_ods_code in PatientOdsInactiveStatus.__members__.values()

def get_document_references(self, document_id: str) -> DocumentReference:
documents = self.document_service.fetch_documents_from_table(
Expand Down
7 changes: 7 additions & 0 deletions lambdas/tests/unit/helpers/mock_response.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from requests.exceptions import HTTPError


class MockResponse:
def __init__(self, status_code, json_data):
self.status_code = status_code
Expand All @@ -8,3 +11,7 @@ def json(self):

def content(self):
return repr(self.json_data)

def raise_for_status(self):
if self.status_code != 200:
raise HTTPError(response=self)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from services.nrl_get_document_reference_service import NRLGetDocumentReferenceService
from tests.unit.conftest import FAKE_URL, TEST_CURRENT_GP_ODS, TEST_UUID
from tests.unit.helpers.data.test_documents import create_test_doc_store_refs
from unit.helpers.mock_response import MockResponse
from utils.constants.ssm import GP_ADMIN_USER_ROLE_CODES, GP_CLINICAL_USER_ROLE_CODE
from utils.lambda_exceptions import NRLGetDocumentReferenceException

MOCK_USER_INFO = {
Expand Down Expand Up @@ -51,7 +53,6 @@ def patched_service(mocker, set_env, context):
mocker.patch.object(service, "s3_service")
mocker.patch.object(service, "pds_service")
mocker.patch.object(service, "document_service")
mocker.patch.object(service, "get_ndr_accepted_role_codes")
yield service


Expand All @@ -62,6 +63,7 @@ def mock_fetch_user_info(patched_service, mocker):
mocker.patch.object(service, "fetch_user_info")
mocker.patch.object(service, "get_patient_current_gp_ods")
mocker.patch.object(service, "patient_is_inactive")
mocker.patch.object(service, "get_ndr_accepted_role_codes")
yield service


Expand Down Expand Up @@ -210,3 +212,74 @@ def test_user_allowed_to_see_file_inactive_patient(
)
is False
)


def test_fetch_user_info(patched_service, mocker, mock_userinfo):
mock_response = MockResponse(status_code=200, json_data=mock_userinfo["user_info"])
mocker.patch("requests.get", return_value=mock_response)
mock_token = "access_token"
actual = patched_service.fetch_user_info(mock_token)

assert actual == mock_userinfo["user_info"]


def test_fetch_user_info_throws_exception_for_non_200_response(patched_service, mocker):
mock_response = MockResponse(status_code=400, json_data="")
mocker.patch("requests.get", return_value=mock_response)

with pytest.raises(NRLGetDocumentReferenceException):
patched_service.fetch_user_info("access_token")


def test_get_ndr_accepted_role_codes(patched_service, mocker):
parameters = [
GP_ADMIN_USER_ROLE_CODES,
GP_CLINICAL_USER_ROLE_CODE,
]
ssm_parameters_expected = {
GP_ADMIN_USER_ROLE_CODES: "R1111,R1112,R1113",
GP_CLINICAL_USER_ROLE_CODE: "R2111,R2112,R2113",
}
patched_service.ssm_service.get_ssm_parameters = mocker.MagicMock(
return_value=ssm_parameters_expected
)

actual = patched_service.get_ndr_accepted_role_codes()

patched_service.ssm_service.get_ssm_parameters.assert_called_with(
parameters_keys=parameters
)
assert actual == ["R1111", "R1112", "R1113", "R2111", "R2112", "R2113"]


@pytest.mark.parametrize(
"input_gp_ods_code, expected",
[
("REST", True),
("Y12345", False),
],
)
def test_patient_is_inactive(patched_service, input_gp_ods_code, expected):
actual = patched_service.patient_is_inactive(input_gp_ods_code)
assert actual is expected


def test_create_document_presigned_url(patched_service, mocker):
expected_url = "https://d12345.cloudfront.net/path/to/resource"

patched_service.s3_service.create_download_presigned_url.return_value = (
"https://example.com/path/to/resource"
)
mocker.patch(
"services.nrl_get_document_reference_service.format_cloudfront_url"
).return_value = "https://d12345.cloudfront.net/path/to/resource"

result = patched_service.create_document_presigned_url(
create_test_doc_store_refs()[0]
)
assert result == expected_url

patched_service.s3_service.create_download_presigned_url.assert_called_once_with(
s3_bucket_name="test-s3-bucket",
file_key="9000000009/test-key-123",
)

0 comments on commit 4e55604

Please sign in to comment.