-
Notifications
You must be signed in to change notification settings - Fork 1
[PRMP-1475] Implement GetUserInformation Lambda #1137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
steph-torres-nhs
wants to merge
37
commits into
main
Choose a base branch
from
PRMP-1475
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
70084e5
[PRMP-1464] introduce model, mock data, start implementing hcw api se…
steph-torres-nhs 52fc5cc
[PRMP-1464] implement get practitioner using hcw api
steph-torres-nhs fc05a4d
[PRMP-1464] add logging to service
steph-torres-nhs c7c583a
[PRMP-1464] remove comment
steph-torres-nhs 680929c
[PRMP-1464] return default message from api error
steph-torres-nhs 8473080
Merge branch 'main' into PRMP-1464
steph-torres-nhs f2e5bf0
[PRMP-1464] add headers
steph-torres-nhs 7985705
[PRMP-1464] remove comment
steph-torres-nhs dc94136
[PRMP-1464] format
steph-torres-nhs b9c80f9
Merge branch 'main' into PRMP-1464
steph-torres-nhs c46ecd7
[PRMP-1486] refactor, start implementation of mock api service
steph-torres-nhs d56e57b
[PRMP-1464] rename var to not shadow builtin
steph-torres-nhs 79497bd
[PRMP-1485] start mock implementation
steph-torres-nhs 65fe060
[PRMP-1486] format
steph-torres-nhs f18cf5c
[PRMP-1484] rename function, update mocks returned
steph-torres-nhs 32568ac
[PRMP-1484] add test coverage
steph-torres-nhs 6475e73
[PRMP-1464] remove unused fixture
steph-torres-nhs 3799cfb
Merge branch 'main' into PRMP-1464
steph-torres-nhs 0489f56
[PRMP-1484] move conftest
steph-torres-nhs 9e287b0
Merge branch 'PRMP-1464' into PRMP-1484
steph-torres-nhs b3982d0
[PRMP-1484] merge in main, resolve conflicts
steph-torres-nhs b3fd866
[PRMP-1484] refactor, remove magic strings
steph-torres-nhs 855d67d
[PRMP-1484] format
steph-torres-nhs 05674e4
[PRMP-1475] update lambda deployment action, create files
steph-torres-nhs d5aef1b
[PRMP-1475] implement get_user_information_handler
steph-torres-nhs 924cc41
[PRMP-1475] add env var decorator
steph-torres-nhs ad87931
[PRMP-1475] adjust workflow
steph-torres-nhs 9505f63
[PRMP-1475] amend api resposne
steph-torres-nhs 8d11076
[PRMP-1475] add route to authoriser
steph-torres-nhs 08ef729
[PRMP-1475] add logging to handler
steph-torres-nhs d360e3a
Merge branch 'main' into PRMP-1475
steph-torres-nhs 62b020b
[PRMP-1475] handle empty identifier or missing querystring param
steph-torres-nhs 4c5fb9b
[PRMP-1475] amend invalid event handling
steph-torres-nhs 3dac2b3
Merge branch 'main' into PRMP-1475
steph-torres-nhs ec50fef
Merge branch 'main' into PRMP-1475
steph-torres-nhs 9134ba0
Merge branch 'main' into PRMP-1475
steph-torres-nhs b5c4f2c
[PRMP-1475] merge main, resolve conflicts
steph-torres-nhs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
55 changes: 55 additions & 0 deletions
55
lambdas/handlers/user_restrictions/get_user_information_handler.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import json | ||
|
|
||
| from enums.lambda_error import LambdaError | ||
| from services.user_restrictions.utilites import get_healthcare_worker_api_service | ||
| from utils.audit_logging_setup import LoggingService | ||
| from utils.decorators.ensure_env_var import ensure_environment_variables | ||
| from utils.decorators.handle_lambda_exceptions import handle_lambda_exceptions | ||
| from utils.exceptions import ( | ||
| HealthcareWorkerAPIException, | ||
| HealthcareWorkerPractitionerModelException, | ||
| ) | ||
| from utils.lambda_response import ApiGatewayResponse | ||
|
|
||
| logger = LoggingService(__name__) | ||
|
|
||
|
|
||
| @ensure_environment_variables( | ||
| names=[ | ||
| "HEALTHCARE_WORKER_API_URL", | ||
| "USE_MOCK_HEALTHCARE_SERVICE", | ||
| ], | ||
| ) | ||
| @handle_lambda_exceptions | ||
| def lambda_handler(event, context): | ||
| try: | ||
| logger.info("Processing request to retrieve user information.") | ||
| identifier = event.get("querystringParameters", {}).get("identifier") | ||
|
|
||
| if not identifier: | ||
| logger.error("No identifier provided.") | ||
| return ApiGatewayResponse( | ||
| 400, | ||
| LambdaError.UserRestrictionInvalidEvent.create_error_body(), | ||
| "GET", | ||
| ).create_api_gateway_response() | ||
|
|
||
| healthcare_worker_api_service = get_healthcare_worker_api_service() | ||
|
|
||
| practitioner_information = healthcare_worker_api_service.get_practitioner( | ||
| identifier=identifier, | ||
| ) | ||
| logger.info("Returning user information.") | ||
| return ApiGatewayResponse( | ||
| 200, | ||
| json.dumps(practitioner_information.model_dump_camel_case()), | ||
| "GET", | ||
| ).create_api_gateway_response() | ||
| except HealthcareWorkerAPIException as e: | ||
| return ApiGatewayResponse(502, e.message, "GET").create_api_gateway_response() | ||
| except HealthcareWorkerPractitionerModelException: | ||
| return ApiGatewayResponse( | ||
| 500, | ||
| LambdaError.UserRestrictionModelValidationError.create_error_body(), | ||
| "GET", | ||
| ).create_api_gateway_response() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| from pydantic import BaseModel | ||
| from pydantic.alias_generators import to_camel | ||
|
|
||
|
|
||
| class Practitioner(BaseModel): | ||
| first_name: str | ||
| last_name: str | ||
| smartcard_id: str | ||
|
|
||
| def model_dump_camel_case(self, *args, **kwargs): | ||
| model_dump_results = self.model_dump(*args, **kwargs) | ||
| camel_case_model_dump_results = {} | ||
| for key in model_dump_results: | ||
| camel_case_model_dump_results[to_camel(key)] = model_dump_results[key] | ||
| return camel_case_model_dump_results |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
lambdas/tests/unit/handlers/user_restrictions/test_get_user_information_handler.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import json | ||
|
|
||
| import pytest | ||
|
|
||
| from enums.lambda_error import LambdaError | ||
| from handlers.user_restrictions.get_user_information_handler import lambda_handler | ||
| from services.mock_data.user_restrictions.build_mock_data import ( | ||
| build_mock_response_and_practitioner, | ||
| ) | ||
| from tests.unit.services.user_restriction.conftest import MOCK_IDENTIFIER | ||
| from utils.exceptions import ( | ||
| HealthcareWorkerAPIException, | ||
| HealthcareWorkerPractitionerModelException, | ||
| ) | ||
| from utils.lambda_response import ApiGatewayResponse | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_healthcare_worker_api_service(mocker): | ||
| mocked_class = mocker.patch( | ||
| "handlers.user_restrictions.get_user_information_handler.get_healthcare_worker_api_service", | ||
| ) | ||
| mocked_instance = mocked_class.return_value | ||
| yield mocked_instance | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_valid_event(event): | ||
| event["querystringParameters"] = { | ||
| "identifier": MOCK_IDENTIFIER, | ||
| } | ||
| yield event | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_invalid_event_missing_identifier(event): | ||
| event["querystringParameters"] = { | ||
| "no_identifier": "abcdef", | ||
| } | ||
| yield event | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_invalid_event_empty_querystring(event): | ||
| event["querystringParameters"] = {} | ||
| yield event | ||
|
|
||
|
|
||
| def test_lambda_handler_happy_path( | ||
| mock_healthcare_worker_api_service, | ||
| monkeypatch, | ||
| context, | ||
| mock_valid_event, | ||
| set_env, | ||
| ): | ||
| _, _, mock_practitioner = build_mock_response_and_practitioner(MOCK_IDENTIFIER) | ||
| mock_healthcare_worker_api_service.get_practitioner.return_value = mock_practitioner | ||
|
|
||
| expected = ApiGatewayResponse( | ||
| 200, | ||
| json.dumps(mock_practitioner.model_dump_camel_case()), | ||
| "GET", | ||
| ).create_api_gateway_response() | ||
|
|
||
| actual = lambda_handler(mock_valid_event, context) | ||
|
|
||
| mock_healthcare_worker_api_service.get_practitioner.assert_called_with( | ||
| identifier=MOCK_IDENTIFIER, | ||
| ) | ||
| assert actual == expected | ||
|
|
||
|
|
||
| def test_lambda_handler_returns_400_invalid_event( | ||
| mock_healthcare_worker_api_service, | ||
| context, | ||
| event, | ||
| mock_invalid_event_missing_identifier, | ||
| mock_invalid_event_empty_querystring, | ||
| set_env, | ||
| ): | ||
|
|
||
| invalid_events = [ | ||
| event, | ||
| mock_invalid_event_missing_identifier, | ||
| mock_invalid_event_empty_querystring, | ||
| ] | ||
| expected_body = LambdaError.UserRestrictionInvalidEvent.create_error_body() | ||
|
|
||
| expected = ApiGatewayResponse( | ||
| 400, | ||
| expected_body, | ||
| "GET", | ||
| ).create_api_gateway_response() | ||
|
|
||
| for invalid_event in invalid_events: | ||
| actual = lambda_handler(invalid_event, context) | ||
|
|
||
| assert expected == actual | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("status_code", [400, 404, 500, 403, 401]) | ||
| def test_lambda_handler_handles_non_200_response_from_get_practitioner( | ||
| mock_healthcare_worker_api_service, | ||
| mock_valid_event, | ||
| context, | ||
| status_code, | ||
| set_env, | ||
| ): | ||
| api_error = HealthcareWorkerAPIException(status_code=status_code) | ||
|
|
||
| mock_healthcare_worker_api_service.get_practitioner.side_effect = api_error | ||
| expected_body = api_error.message | ||
|
|
||
| expected = ApiGatewayResponse( | ||
| 502, | ||
| expected_body, | ||
| "GET", | ||
| ).create_api_gateway_response() | ||
|
|
||
| actual = lambda_handler(mock_valid_event, context) | ||
|
|
||
| assert actual == expected | ||
|
|
||
|
|
||
| def test_lambda_handler_handles_validation_error_from_get_practitioner( | ||
| mock_healthcare_worker_api_service, | ||
| mock_valid_event, | ||
| context, | ||
| set_env, | ||
| ): | ||
| mock_healthcare_worker_api_service.get_practitioner.side_effect = ( | ||
| HealthcareWorkerPractitionerModelException | ||
| ) | ||
|
|
||
| expected = ApiGatewayResponse( | ||
| 500, | ||
| LambdaError.UserRestrictionModelValidationError.create_error_body(), | ||
| "GET", | ||
| ).create_api_gateway_response() | ||
|
|
||
| actual = lambda_handler(mock_valid_event, context) | ||
|
|
||
| assert actual == expected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feature flag check?