Skip to content

Commit

Permalink
prmdr-327 unit tests for ssm functions
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Oct 17, 2023
1 parent 74a0449 commit 77027df
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lambdas/services/pds_api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def handle_response(self, response: Response, nhs_number: str) -> PatientDetails

def pds_request(self, nshNumber: str, retry_on_expired: bool):
endpoint, access_token_response = self.get_parameters_for_pds_api_request()
access_token_response = json.loads(access_token_response)
access_token = access_token_response["access_token"]
access_token_expiration = int(access_token_response["expires_in"]) + int(
access_token_response["issued_at"]
Expand Down Expand Up @@ -107,7 +108,7 @@ def get_parameters_for_new_access_token(self):
return self.ssm_service.get_ssm_parameters(parameters, with_decryption=True)

def update_access_token_ssm(self, parameter_value: str):
parameter_key = "/prs/dev-ndr/pds-fhir-access-token"
parameter_key = SSMParameter.PDS_API_ACCESS_TOKEN
self.ssm_service.update_ssm_parameter(
parameter_key=parameter_key,
parameter_value=parameter_value,
Expand All @@ -120,9 +121,9 @@ def get_parameters_for_pds_api_request(self):
SSMParameter.PDS_API_ACCESS_TOKEN,
]
ssm_response = self.ssm_service.get_ssm_parameters(
parameters, with_decryption=True
parameters_keys=parameters, with_decryption=True
)
return ssm_response[parameters[0]], json.loads(ssm_response[parameters[1]])
return ssm_response[parameters[0]], ssm_response[parameters[1]]

def create_jwt_token_for_new_access_token_request(
self, access_token_ssm_parameters
Expand Down
35 changes: 32 additions & 3 deletions lambdas/tests/unit/services/test_pds_api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class FakeSSMService:
def __init__(self, *arg, **kwargs):
pass

def get_ssm_parameters(self, *arg, **kwargs):
pass
def get_ssm_parameters(self, parameters_keys, *arg, **kwargs):
return {parameter : f"test_value_{parameter}" for parameter in parameters_keys}

def update_ssm_parameter(self, *arg, **kwargs):
pass

pds_service = PdsApiService(FakeSSMService)
fake_ssm_service = FakeSSMService()
pds_service = PdsApiService(fake_ssm_service)


def test_handle_response_200_returns_PatientDetails(mocker):
Expand Down Expand Up @@ -116,3 +119,29 @@ def test_create_jwt_for_new_access_token(mocker):
algorithm="RS512",
headers={"kid": "test_string_pds_kid"},
)

def test_get_parameters_for_pds_api_request():
ssm_parameters_expected = (f"test_value_{SSMParameter.PDS_API_ENDPOINT}", f"test_value_{SSMParameter.PDS_API_ACCESS_TOKEN}")
actual = pds_service.get_parameters_for_pds_api_request()
assert ssm_parameters_expected == actual

def test_update_access_token_ssm(mocker):
fake_ssm_service.update_ssm_parameter = mocker.MagicMock()

pds_service.update_access_token_ssm("test_string")

fake_ssm_service.update_ssm_parameter.assert_called_with(parameter_key=SSMParameter.PDS_API_ACCESS_TOKEN, parameter_value="test_string", parameter_type="SecureString")

def test_get_parameters_for_new_access_token(mocker):
parameters = [
SSMParameter.NHS_OAUTH_ENDPOINT,
SSMParameter.PDS_KID,
SSMParameter.NHS_OAUTH_KEY,
SSMParameter.PDS_API_KEY,
]
fake_ssm_service.get_ssm_parameters = mocker.MagicMock()
pds_service.get_parameters_for_new_access_token()
fake_ssm_service.get_ssm_parameters.assert_called_with(parameters, with_decryption=True)

def test_get_new_access_token(mocker):
pass

0 comments on commit 77027df

Please sign in to comment.