Beispiel #1
0
 def __init__(self, **kwargs):
     self._messages = []
     self._conversation_id = kwargs.get("conversation_id", a_string())
     self._requesting_asid = kwargs.get("requesting_asid", a_string())
     self._sending_asid = kwargs.get("sending_asid", a_string())
     self._requesting_system = kwargs.get("requesting_system", a_string())
     self._sending_system = kwargs.get("sending_system", a_string())
Beispiel #2
0
def successful_integration_with_copc_fragments(**kwargs) -> List[Message]:
    """
    A GP2GP transfer where the record was large enough (or had enough attachments)
    to necessitate using COPC messages to transmit the data in multiple chunks.
    In this instance, all transmitted COPC messages have been acknowledged by the requester,
    and the record has been integrated.
    """
    req_complete_time = kwargs.get("request_completed_time", a_datetime())
    ehr_ack_time = kwargs.get("ehr_acknowledge_time",
                              req_complete_time + timedelta(days=1))
    conversation_id = a_string()
    ehr_guid = a_string()
    fragment1_guid = a_string()
    fragment2_guid = a_string()
    fragment3_guid = a_string()

    return (GP2GPTestCase(conversation_id=conversation_id).with_request(
    ).with_sender_acknowledgement(message_ref=conversation_id).with_core_ehr(
        guid=ehr_guid, time=req_complete_time).with_copc_fragment_continue(
        ).with_copc_fragment(guid=fragment1_guid).with_copc_fragment(
            guid=fragment2_guid).with_requester_acknowledgement(
                message_ref=fragment1_guid).with_requester_acknowledgement(
                    message_ref=fragment2_guid).with_copc_fragment(
                        guid=fragment3_guid).with_requester_acknowledgement(
                            message_ref=fragment3_guid).
            with_requester_acknowledgement(message_ref=ehr_guid,
                                           time=ehr_ack_time).build())
Beispiel #3
0
def test_returns_correct_spine_messages_uris():
    spine_bucket = a_string()
    reporting_window = Mock()
    reporting_window.get_dates = Mock(return_value=[
        datetime(year=2020, month=12, day=30),
        datetime(year=2020, month=12, day=31),
        datetime(year=2021, month=1, day=1),
    ])
    reporting_window.get_overflow_dates = Mock(return_value=[
        datetime(year=2021, month=1, day=2),
        datetime(year=2021, month=1, day=3),
    ])

    uri_resolver = TransferClassifierS3UriResolver(
        gp2gp_spine_bucket=spine_bucket,
        transfers_bucket=a_string(),
        ods_metadata_bucket=a_string())

    expected = [
        f"s3://{spine_bucket}/v3/2020/12/30/2020-12-30_spine_messages.csv.gz",
        f"s3://{spine_bucket}/v3/2020/12/31/2020-12-31_spine_messages.csv.gz",
        f"s3://{spine_bucket}/v3/2021/01/01/2021-01-01_spine_messages.csv.gz",
        f"s3://{spine_bucket}/v3/2021/01/02/2021-01-02_spine_messages.csv.gz",
        f"s3://{spine_bucket}/v3/2021/01/03/2021-01-03_spine_messages.csv.gz",
    ]

    actual = uri_resolver.spine_messages(reporting_window)

    assert actual == expected
Beispiel #4
0
def build_mex_headers(**kwargs):
    mocked_timestamp = a_timestamp()

    return {
        "statusevent":
        kwargs.get("status_event", MESH_STATUS_EVENT_TRANSFER),
        "addresstype":
        kwargs.get("address_type", "ALL"),
        "statustimestamp":
        kwargs.get("status_timestamp", mocked_timestamp),
        "fromsmtp":
        kwargs.get("from_smtp", a_string()),
        "content-compressed":
        kwargs.get("content_compressed", "N"),
        "tosmtp":
        kwargs.get("to_smtp", a_string()),
        "statusdescription":
        kwargs.get("status_description", "Transferred to recipient mailbox"),
        "version":
        kwargs.get("version", "1.0"),
        "statussuccess":
        kwargs.get("status_success", MESH_STATUS_SUCCESS),
        "statuscode":
        kwargs.get("status_code", "00"),
        "to":
        kwargs.get("to", a_string()),
        "messagetype":
        kwargs.get("message_type", MESH_MESSAGE_TYPE_DATA),
        "filename":
        kwargs.get("file_name", a_filename(mocked_timestamp)),
        "messageid":
        kwargs.get("message_id", a_string()),
        "from":
        kwargs.get("from", a_string()),
    }
Beispiel #5
0
def ehr_integrated_with_duplicate_having_second_sender_ack_after_integration(
    **kwargs, ) -> List[Message]:
    """
    While normally the "request completed" message (aka "core ehr" message)
    is only transmitted once, sometimes there are duplicate copies sent by
    the sending practice. In this instance, the sender first sent two copies
    and the requester reported via negative ack that the first was a duplicate
    and that the second was integrated. After this the sender sent a third copy,
    which was ignored.
    """
    req_complete_time = kwargs.get("request_completed_time", a_datetime())
    req_complete_time_duplicate = req_complete_time - timedelta(hours=1)
    ehr_ack_time = kwargs.get("ehr_acknowledge_time", req_complete_time)
    sender_ack_time = ehr_ack_time - timedelta(hours=1)
    request_complete_time_after_integration = ehr_ack_time + timedelta(hours=1)
    conversation_id = a_string()
    ehr_guid = a_string()
    duplicate_ehr_guid = a_string()

    return (GP2GPTestCase(conversation_id=conversation_id).with_request(
    ).with_sender_acknowledgement(
        message_ref=conversation_id, time=sender_ack_time).with_core_ehr(
            guid=duplicate_ehr_guid,
            time=req_complete_time_duplicate).with_core_ehr(
                guid=ehr_guid,
                time=req_complete_time).with_requester_acknowledgement(
                    message_ref=duplicate_ehr_guid,
                    error_code=DUPLICATE_EHR_ERROR).
            with_requester_acknowledgement(
                time=ehr_ack_time, message_ref=ehr_guid).with_core_ehr(
                    guid=duplicate_ehr_guid,
                    time=request_complete_time_after_integration).build())
Beispiel #6
0
def test_returns_messages():
    message_ids = [a_string(), a_string(), a_string()]
    client_messages = [mock_client_message(message_id=m_id) for m_id in message_ids]
    mesh_inbox = mock_mesh_inbox(client_messages=client_messages)

    actual_messages_ids = [message.id for message in mesh_inbox.read_messages()]

    assert actual_messages_ids == message_ids
def build_transfer(**kwargs):
    return Transfer(
        conversation_id=kwargs.get("conversation_id", a_string(36)),
        sla_duration=kwargs.get("sla_duration", a_duration()),
        requesting_practice_ods=kwargs.get("requesting_practice_ods",
                                           a_string(6)),
        sending_practice_ods=kwargs.get("sending_practice_ods", a_string(6)),
        error_code=kwargs.get("error_code", None),
        pending=kwargs.get("pending", False),
    )
Beispiel #8
0
def mock_mesh_message(**kwargs):
    message = MagicMock()
    message.id = kwargs.get("message_id", a_string())
    message.file_name = kwargs.get("file_name", a_string())
    message.sender = kwargs.get("sender", a_string())
    message.recipient = kwargs.get("recipient", a_string())
    message.validate.side_effect = kwargs.get("validation_error", None)
    message.acknowledge.side_effect = kwargs.get("acknowledge_error", None)
    message.date_delivered = kwargs.get("date_delivered", a_datetime())
    return message
Beispiel #9
0
def _build_log_record(**kwargs):
    record_fields = {
        "levelname": kwargs.get("levelname", a_string()),
        "module": kwargs.get("module", a_string()),
        "msg": kwargs.get("msg", a_string()),
        "created": kwargs.get("created", an_epoch_timestamp()),
    }
    if "extra" in kwargs:
        record_fields.update(kwargs.get("extra"))
    return makeLogRecord(record_fields)
def build_practice_metrics(**kwargs):
    return PracticeMetrics(
        ods_code=kwargs.get("ods_code", a_string(6)),
        name=kwargs.get("name", a_string()),
        integrated=IntegratedPracticeMetrics(
            transfer_count=kwargs.get("transfer_count", an_integer()),
            within_3_days=kwargs.get("within_3_days", an_integer()),
            within_8_days=kwargs.get("within_8_days", an_integer()),
            beyond_8_days=kwargs.get("beyond_8_days", an_integer()),
        ),
    )
Beispiel #11
0
def ehr_missing_message_for_an_acknowledgement() -> List[Message]:
    """
    In this scenario the sender acknowledged sent an acknowledgement to a non existent message.
    """
    conversation_id = a_string()
    message_ref_acknowledgement_to_non_existent_message = a_string()

    return (GP2GPTestCase(conversation_id=conversation_id).with_request(
    ).with_sender_acknowledgement(
        message_ref=message_ref_acknowledgement_to_non_existent_message).build(
        ))
Beispiel #12
0
def build_message(**kwargs):
    return Message(
        time=kwargs.get("time", a_datetime()),
        conversation_id=kwargs.get("conversation_id", a_string(36)),
        guid=kwargs.get("guid", a_string(36)),
        interaction_id=kwargs.get("interaction_id", a_string(17)),
        from_party_ods=kwargs.get("from_party_ods", a_string(6)),
        to_party_ods=kwargs.get("to_party_ods", a_string(6)),
        message_ref=kwargs.get("message_ref", None),
        error_code=kwargs.get("error_code", None),
    )
Beispiel #13
0
def test_contains_practice_name():
    expected_name = "A Practice"
    practices = [
        PracticeDetails(asids=[a_string()],
                        ods_code=a_string(),
                        name=expected_name)
    ]
    transfers = []

    actual_name = list(calculate_sla_by_practice(practices, transfers))[0].name

    assert actual_name == expected_name
Beispiel #14
0
def test_ccg_ods_code_from_practice_ods_code_returns_matching_ccg_with_multiple_practices(
):
    ccg = build_ccg_details(practices=["B3432",
                                       a_string(),
                                       a_string()],
                            ods_code="3W")
    practice_lookup = OrganisationLookup(practices=[], ccgs=[ccg])

    expected = "3W"

    actual = practice_lookup.ccg_ods_code_from_practice_ods_code("B3432")

    assert actual == expected
def test_resolver_returns_correct_ods_metadata_uri_given_date_anchor():
    ods_metadata_bucket = a_string()
    date_anchor = a_datetime()
    year = date_anchor.year
    month = date_anchor.month

    uri_resolver = OdsDownloaderS3UriResolver(
        asid_lookup_bucket=a_string(), ods_metadata_bucket=ods_metadata_bucket)

    actual = uri_resolver.ods_metadata(date_anchor)

    expected = f"s3://{ods_metadata_bucket}/v4/{year}/{month}/organisationMetadata.json"

    assert actual == expected
Beispiel #16
0
def pending_integration_with_copc_fragments(**kwargs) -> List[Message]:
    """
    A GP2GP transfer where the record was large enough (or had enough attachments)
    to necessitate using COPC messages to transmit the data in multiple chunks.
    In this instance, several COPC messages have been sent by the sender,
    but none have yet been acknowledged by the requester,
    """
    conversation_id = a_string()
    ehr_guid = a_string()

    return (GP2GPTestCase(conversation_id=conversation_id).with_request(
    ).with_sender_acknowledgement(message_ref=conversation_id).with_core_ehr(
        guid=ehr_guid).with_copc_fragment_continue().with_copc_fragment().
            with_copc_fragment().with_copc_fragment().build())
Beispiel #17
0
def request_made(**kwargs) -> List[Message]:
    """
    In this example, the requester has send a GP2GP request,
    but the sender is yet to acknowledge.
    """
    request_time = kwargs.get("request_sent_date", a_datetime())

    return (GP2GPTestCase(
        conversation_id=kwargs.get("conversation_id", a_string()),
        requesting_asid=kwargs.get("requesting_asid", a_string()),
        sending_asid=kwargs.get("sending_asid", a_string()),
        requesting_system=kwargs.get("requesting_system", a_string()),
        sending_system=kwargs.get("sending_system", a_string()),
    ).with_request(time=request_time).build())
Beispiel #18
0
def ehr_integrated_successfully(**kwargs) -> List[Message]:
    """
    In this scenario, the GP2GP transfer was integrated successfully, no COPC messaging was used.
    """
    req_complete_time = kwargs.get("request_completed_time", a_datetime())
    ehr_ack_time = kwargs.get("ehr_acknowledge_time",
                              req_complete_time + timedelta(days=1))
    conversation_id = a_string()
    ehr_guid = a_string()

    return (GP2GPTestCase(conversation_id=conversation_id).with_request(
    ).with_sender_acknowledgement(message_ref=conversation_id).with_core_ehr(
        guid=ehr_guid, time=req_complete_time).with_requester_acknowledgement(
            time=ehr_ack_time, message_ref=ehr_guid).build())
def test_resolver_returns_correct_asid_lookup_uri_given_date_anchor():
    asid_lookup_bucket = a_string()
    date_anchor = a_datetime()
    year = date_anchor.year
    month = date_anchor.month

    uri_resolver = OdsDownloaderS3UriResolver(
        asid_lookup_bucket=asid_lookup_bucket, ods_metadata_bucket=a_string())

    actual = uri_resolver.asid_lookup(date_anchor)

    expected = f"s3://{asid_lookup_bucket}/{year}/{month}/asidLookup.csv.gz"

    assert actual == expected
Beispiel #20
0
def ehr_integration_failed(**kwargs) -> List[Message]:
    """
    This scenario is an example of a GP2GP transfer that failed to integrate.
    """
    ehr_ack_time = kwargs.get("ehr_acknowledge_time", a_datetime())
    req_complete_time = kwargs.get("request_completed_time", a_datetime())
    ehr_ack_error = kwargs.get("error_code", 28)
    conversation_id = a_string()
    ehr_guid = a_string()

    return (GP2GPTestCase(conversation_id=conversation_id).with_request(
    ).with_sender_acknowledgement(message_ref=conversation_id).with_core_ehr(
        guid=ehr_guid, time=req_complete_time).with_requester_acknowledgement(
            time=ehr_ack_time, message_ref=ehr_guid,
            error_code=ehr_ack_error).build())
def build_transfer(**kwargs):
    return Transfer(
        conversation_id=kwargs.get("conversation_id", a_string(36)),
        sla_duration=kwargs.get("sla_duration", a_duration()),
        requesting_practice_asid=kwargs.get("requesting_practice_asid", a_string(12)),
        sending_practice_asid=kwargs.get("sending_practice_asid", a_string(12)),
        requesting_supplier=kwargs.get("requesting_supplier", a_string(12)),
        sending_supplier=kwargs.get("sending_supplier", a_string(12)),
        sender_error_code=kwargs.get("sender_error_code", None),
        final_error_code=kwargs.get("final_error_code", None),
        intermediate_error_codes=kwargs.get("intermediate_error_codes", []),
        status=kwargs.get("status", TransferStatus.PENDING),
        date_requested=kwargs.get("date_requested", a_datetime()),
        date_completed=kwargs.get("date_completed", None),
    )
Beispiel #22
0
def ehr_integrated_late(**kwargs) -> List[Message]:
    """
    This scenario is an example of a GP2GP transfer where the records was integrated,
    but after more that 8 days.
    """
    req_complete_time = kwargs.get("request_completed_time", a_datetime())
    ehr_ack_time = kwargs.get("ehr_acknowledge_time",
                              req_complete_time + timedelta(days=9))
    conversation_id = a_string()
    ehr_guid = a_string()

    return (GP2GPTestCase(conversation_id=conversation_id).with_request(
    ).with_sender_acknowledgement(message_ref=conversation_id).with_core_ehr(
        guid=ehr_guid, time=req_complete_time).with_requester_acknowledgement(
            time=ehr_ack_time, message_ref=ehr_guid).build())
def mock_client_message(**kwargs):
    mex_headers = kwargs.get("mex_headers", build_mex_headers())
    message = MagicMock()
    message.id.return_value = kwargs.get("message_id", a_string())
    message.acknowledge.side_effect = kwargs.get("acknowledge_error", None)
    message.mex_header = lambda key: mex_headers[key]
    return message
def build_practice_sla_metrics(**kwargs):
    return PracticeSlaMetrics(
        ods=kwargs.get("ods", a_string(6)),
        within_3_days=kwargs.get("within_3_days", an_integer()),
        within_8_days=kwargs.get("within_8_days", an_integer()),
        beyond_8_days=kwargs.get("beyond_8_days", an_integer()),
    )
Beispiel #25
0
def build_parsed_conversation(**kwargs):
    return ParsedConversation(
        id=kwargs.get("id", a_string(36)),
        request_started=kwargs.get("request_started", build_message()),
        request_completed=kwargs.get("request_completed", build_message()),
        request_completed_ack=kwargs.get("request_completed_ack", build_message()),
    )
def test_exposes_recipient():
    recipient = a_string()
    client_message = mock_client_message(mex_headers=build_mex_headers(
        to=recipient))
    message = MeshMessage(client_message)

    assert message.recipient == recipient
Beispiel #27
0
def test_mesh_inbox_sns_forwarder(e2e_test_context):
    mesh = e2e_test_context.build_mesh_client(SENDING_MESH_MAILBOX)

    e2e_test_context.set_fake_aws_environment_vars()
    e2e_test_context.populate_ssm_parameters(
        test_name="test-sns", receiving_mailbox_name=RECEIVING_MESH_MAILBOX)
    sns = SnsForTesting(e2e_test_context)
    sqs = SqsForTesting(e2e_test_context)

    queue = sqs.create_queue("test-queue")
    topic = sns.create_topic("test-topic")

    topic.subscribe(queue)

    message_contents = a_string()

    forwarder = _build_sns_forwarder(
        sns_topic_arn=topic.arn,
        config=e2e_test_context.build_forwarder_config(test_name="test-sns"),
    )

    try:
        forwarder.start()
        mesh.send_message(RECEIVING_MESH_MAILBOX,
                          message_contents.encode("utf-8"))

        message = queue.receive_messages()[0]

        assert message["Body"] == message_contents
        assert "meshMessageId" in message["MessageAttributes"]
    finally:
        forwarder.stop()
        e2e_test_context.unset_fake_aws_environment_vars()
Beispiel #28
0
def test_write_transfers_writes_metadata():
    mock_s3 = MockS3()
    s3_data_manager = S3DataManager(mock_s3)

    metadata = {a_string(): a_string()}

    io = TransferClassifierIO(s3_data_manager)

    io.write_transfers(transfers=[build_transfer()],
                       s3_uri="s3://a_bucket/some_data.parquet",
                       metadata=metadata)

    actual_meta_data = mock_s3.object("a_bucket",
                                      "some_data.parquet").get_metadata()

    assert actual_meta_data == metadata
Beispiel #29
0
def test_mesh_inbox_s3_forwarder(e2e_test_context):
    mesh = e2e_test_context.build_mesh_client(SENDING_MESH_MAILBOX)

    e2e_test_context.set_fake_aws_environment_vars()
    e2e_test_context.populate_ssm_parameters(
        test_name="test-s3", receiving_mailbox_name=RECEIVING_MESH_MAILBOX)

    s3 = e2e_test_context.build_aws_resource("s3")

    s3_bucket_name = "test-bucket"
    bucket = s3.Bucket(s3_bucket_name)
    bucket.create()
    s3.meta.client.get_waiter("bucket_exists").wait(Bucket=s3_bucket_name)

    file_contents = bytes(a_string(), "utf-8")
    forwarder = _build_s3_forwarder(
        e2e_test_context.build_forwarder_config("test-s3"), s3_bucket_name)
    try:
        forwarder.start()

        mesh.send_message(RECEIVING_MESH_MAILBOX, file_contents)
        _wait_for_object_count(bucket, expected_count=1)
        actual_object = next(iter(bucket.objects.all()))
        assert re.match(r"\d{4}/\d{2}/\d{2}/\d{20}_\d+\.dat",
                        actual_object.key)
        actual_file_contents = actual_object.get()["Body"].read()
        assert actual_file_contents == file_contents
    finally:
        forwarder.stop()
        e2e_test_context.unset_fake_aws_environment_vars()
def test_exposes_sender():
    sender = a_string()
    client_message = mock_client_message(mex_headers=build_mex_headers(
        **{"from": sender}))
    message = MeshMessage(client_message)

    assert message.sender == sender