Skip to content

Commit

Permalink
Merge pull request #660 from NHSDigital/feature/eema1-NRL-759-removeA…
Browse files Browse the repository at this point in the history
…ppIDDependency

NRL-759 switch to using new app ID header
  • Loading branch information
axelkrastek1-nhs authored Jul 1, 2024
2 parents 4192db9 + 58c171f commit 58eca28
Show file tree
Hide file tree
Showing 33 changed files with 256 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from moto import mock_aws

from api.consumer.readDocumentReference.read_document_reference import handler
from nrlf.core.constants import PointerTypes
from nrlf.core.dynamodb.repository import DocumentPointer, DocumentPointerRepository
from nrlf.tests.data import load_document_reference
from nrlf.tests.dynamodb import mock_repository
Expand Down Expand Up @@ -105,14 +104,12 @@ def test_read_document_reference_missing_id():
def test_read_document_reference_unauthorised_for_type(
repository: DocumentPointerRepository,
):
doc_ref = load_document_reference("Y05868-736253002-Valid")
doc_ref = load_document_reference("RQI-736253002-Valid")
doc_pointer = DocumentPointer.from_document_reference(doc_ref)
repository.create(doc_pointer)

event = create_test_api_gateway_event(
headers=create_headers(
app_id="12356", pointer_types=[PointerTypes.EMERGENCY_HEALTHCARE_PLAN.value]
),
headers=create_headers(app_id="12356", ods_code="RQI"),
path_parameters={"id": doc_pointer.id},
)

Expand Down Expand Up @@ -157,7 +154,7 @@ def test_document_reference_invalid_json(repository: DocumentPointerRepository):
repository.create(doc_pointer)

event = create_test_api_gateway_event(
headers=create_headers(pointer_types=[PointerTypes.MENTAL_HEALTH_PLAN.value]),
headers=create_headers(),
path_parameters={"id": doc_pointer.id},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
_set_create_time_fields,
handler,
)
from nrlf.core.constants import PointerTypes
from nrlf.core.dynamodb.repository import DocumentPointer, DocumentPointerRepository
from nrlf.producer.fhir.r4.model import (
DocumentReferenceRelatesTo,
Expand Down Expand Up @@ -659,10 +658,6 @@ def test_create_document_reference_invalid_relatesto_type(
event = create_test_api_gateway_event(
headers=create_headers(
app_id="123456",
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
],
),
body=doc_ref.json(exclude_none=True),
)
Expand Down Expand Up @@ -710,12 +705,7 @@ def test_create_document_reference_with_no_context_related_for_ssp_url(
del doc_ref.context.related

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -769,12 +759,7 @@ def test_create_document_reference_with_no_asid_in_for_ssp_url(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -828,12 +813,7 @@ def test_create_document_reference_with_invalid_asid_for_ssp_url(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -892,12 +872,7 @@ def test_create_document_reference_supersede_deletes_old_pointers_replace(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -957,13 +932,7 @@ def test_create_document_reference_supersede_succeeds_with_toggle(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
],
nrl_permissions=["supersede-ignore-delete-fail"],
),
headers=create_headers(nrl_permissions=["supersede-ignore-delete-fail"]),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -1022,12 +991,7 @@ def test_create_document_reference_supersede_fails_without_toggle(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -1086,12 +1050,7 @@ def test_create_document_reference_create_relatesto_not_replaces(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_read_document_reference_missing_id():

def test_read_document_reference_incorrect_ods_code():
event = create_test_api_gateway_event(
headers=create_headers(ods_code="X26"),
headers=create_headers(ods_code="RQI"),
path_parameters={"id": "Y05868-99999-99999-999999"},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ def test_search_document_reference_filters_by_pointer_types(
repository.create(doc_pointer)

event = create_test_api_gateway_event(
headers=create_headers(
app_id="123445", pointer_types=[PointerTypes.EOL_COORDINATION_SUMMARY.value]
),
headers=create_headers(app_id="123445", ods_code="X26"),
query_string_parameters={
"subject:identifier": "https://fhir.nhs.uk/Id/nhs-number|6700028191",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ def test_search_document_reference_filters_by_pointer_types(
repository.create(doc_pointer)

event = create_test_api_gateway_event(
headers=create_headers(
app_id="12356", pointer_types=[PointerTypes.EOL_COORDINATION_SUMMARY.value]
),
headers=create_headers(app_id="12356", ods_code="X26"),
body=json.dumps(
{
"subject:identifier": "https://fhir.nhs.uk/Id/nhs-number|6700028191",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def test_update_document_reference_invalid_resource():
def test_update_document_reference_invalid_producer_id():
doc_ref = load_document_reference("Y05868-736253002-Valid")
event = create_test_api_gateway_event(
headers=create_headers(ods_code="X26"),
headers=create_headers(ods_code="RQI"),
path_parameters={"id": "Y05868-99999-99999-999999"},
body=doc_ref.json(exclude_none=True),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
_set_upsert_time_fields,
handler,
)
from nrlf.core.constants import PERMISSION_SUPERSEDE_IGNORE_DELETE_FAIL, PointerTypes
from nrlf.core.constants import PERMISSION_SUPERSEDE_IGNORE_DELETE_FAIL
from nrlf.core.dynamodb.repository import DocumentPointer, DocumentPointerRepository
from nrlf.producer.fhir.r4.model import (
DocumentReferenceRelatesTo,
Expand Down Expand Up @@ -748,10 +748,6 @@ def test_create_document_reference_invalid_relatesto_type(
event = create_test_api_gateway_event(
headers=create_headers(
app_id="12356",
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
],
),
body=doc_ref.json(exclude_none=True),
)
Expand Down Expand Up @@ -799,12 +795,7 @@ def test_create_document_reference_with_no_context_related_for_ssp_url(
del doc_ref.context.related

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -858,12 +849,7 @@ def test_create_document_reference_with_no_asid_in_for_ssp_url(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -917,12 +903,7 @@ def test_create_document_reference_with_invalid_asid_for_ssp_url(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -980,12 +961,7 @@ def test_create_document_reference_supersede_deletes_old_pointers_replace(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -1044,13 +1020,7 @@ def test_create_document_reference_supersede_succeeds_with_toggle(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
],
nrl_permissions=["supersede-ignore-delete-fail"],
),
headers=create_headers(nrl_permissions=["supersede-ignore-delete-fail"]),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -1109,12 +1079,7 @@ def test_create_document_reference_supersede_fails_without_toggle(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down Expand Up @@ -1172,12 +1137,7 @@ def test_create_document_reference_create_relatesto_not_replaces(
]

event = create_test_api_gateway_event(
headers=create_headers(
pointer_types=[
PointerTypes.EOL_COORDINATION_SUMMARY.value,
PointerTypes.MENTAL_HEALTH_PLAN.value,
]
),
headers=create_headers(),
body=doc_ref.json(exclude_none=True),
)

Expand Down
12 changes: 8 additions & 4 deletions layer/nrlf/core/authoriser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import sys
from os import path

from botocore.exceptions import ClientError

Expand All @@ -14,7 +15,7 @@ def get_pointer_types(
) -> list[str]:
ods_code = ".".join(connection_metadata.ods_code)

app_id = connection_metadata.client_rp_details.developer_app_id
app_id = connection_metadata.nrl_app_id
ods_code = connection_metadata.ods_code
ods_code_extension = connection_metadata.ods_code_extension

Expand Down Expand Up @@ -57,11 +58,10 @@ def get_pointer_types(

def parse_permissions_file(
connection_metadata: ConnectionMetadata,
path_to_search: str = "/opt/python/nrlf_permissions",
) -> list[str]:
ods_code = ".".join(connection_metadata.ods_code)

app_id = connection_metadata.client_rp_details.developer_app_id
app_id = connection_metadata.nrl_app_id
ods_code = connection_metadata.ods_code
ods_code_extension = connection_metadata.ods_code_extension

Expand All @@ -70,7 +70,11 @@ def parse_permissions_file(
else:
key = f"{app_id}/{ods_code}.json"

file_path = f"{path_to_search}/{key}"
file_path = f"/opt/python/nrlf_permissions/{key}"

if connection_metadata.is_test_event:
file_path = path.abspath(f"layer/test_permissions/{key}")

pointer_types = []
try:
with open(file_path) as file:
Expand Down
7 changes: 3 additions & 4 deletions layer/nrlf/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ def load_connection_metadata(headers: Dict[str, str], config: Config):

logger.log(LogReference.HANDLER004b)
pointer_types = parse_permissions_file(metadata)
if not pointer_types and metadata.enable_authorization_lookup:
if not pointer_types and not metadata.is_test_event:
logger.log(LogReference.HANDLER004)
pointer_types = get_pointer_types(metadata, config)
metadata.pointer_types = pointer_types
elif pointer_types:
metadata.pointer_types = pointer_types

metadata.pointer_types = pointer_types

return metadata

Expand Down
5 changes: 2 additions & 3 deletions layer/nrlf/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ class ConnectionMetadata(BaseModel):
ods_code: str = Field(alias="nrl.ods-code")
ods_code_extension: str | None = Field(alias="nrl.ods-code-extension", default=None)
nrl_permissions: list[str] = Field(alias="nrl.permissions", default_factory=list)
enable_authorization_lookup: bool = Field(
alias="nrl.enable-authorization-lookup", default=False
)
nrl_app_id: str = Field(alias="nrl.app-id")
is_test_event: bool = Field(alias="nrl.test-event", default=False)
client_rp_details: ClientRpDetails

@property
Expand Down
Loading

0 comments on commit 58eca28

Please sign in to comment.