コード例 #1
0
def test_process_sns_results_calls_service_callback(sample_template,
                                                    notify_db_session,
                                                    notify_db, mocker):
    with freeze_time('2021-01-01T12:00:00'):
        mocker.patch('app.statsd_client.incr')
        mocker.patch('app.statsd_client.timing_with_dates')
        send_mock = mocker.patch(
            'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
        )
        notification = create_sample_notification(notify_db,
                                                  notify_db_session,
                                                  template=sample_template,
                                                  reference='ref',
                                                  status=NOTIFICATION_SENT,
                                                  sent_by='sns',
                                                  sent_at=datetime.utcnow())
        callback_api = create_service_callback_api(
            service=sample_template.service, url="https://example.com")
        assert get_notification_by_id(
            notification.id).status == NOTIFICATION_SENT

        assert process_sns_results(sns_success_callback(reference='ref'))
        assert get_notification_by_id(
            notification.id).status == NOTIFICATION_DELIVERED
        statsd_client.timing_with_dates.assert_any_call(
            "callback.sns.elapsed-time", datetime.utcnow(),
            notification.sent_at)
        statsd_client.incr.assert_any_call("callback.sns.delivered")
        updated_notification = get_notification_by_id(notification.id)
        encrypted_data = create_delivery_status_callback_data(
            updated_notification, callback_api)
        send_mock.assert_called_once_with(
            [str(notification.id), encrypted_data], queue="service-callbacks")
コード例 #2
0
def timeout_notifications():
    (
        technical_failure_notifications,
        temporary_failure_notifications,
    ) = dao_timeout_notifications(
        current_app.config.get("SENDING_NOTIFICATIONS_TIMEOUT_PERIOD"))

    notifications = technical_failure_notifications + temporary_failure_notifications
    for notification in notifications:
        # queue callback task only if the service_callback_api exists
        service_callback_api = get_service_delivery_status_callback_api_for_service(
            service_id=notification.service_id)
        if service_callback_api:
            encrypted_notification = create_delivery_status_callback_data(
                notification, service_callback_api)
            send_delivery_status_to_service.apply_async(
                [str(notification.id), encrypted_notification],
                queue=QueueNames.CALLBACKS,
            )

    current_app.logger.info(
        "Timeout period reached for {} notifications, status has been updated."
        .format(len(notifications)))
    if technical_failure_notifications:
        message = (
            "{} notifications have been updated to technical-failure because they "
            "have timed out and are still in created.Notification ids: {}".
            format(
                len(technical_failure_notifications),
                [str(x.id) for x in technical_failure_notifications],
            ))
        raise NotificationTechnicalFailureException(message)
コード例 #3
0
def test_ses_callback_should_update_notification_status(
        notify_db, notify_db_session, sample_email_template, mocker):
    with freeze_time("2001-01-01T12:00:00"):
        mocker.patch("app.statsd_client.incr")
        mocker.patch("app.statsd_client.timing_with_dates")
        send_mock = mocker.patch(
            "app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async"
        )
        notification = create_sample_notification(
            notify_db,
            notify_db_session,
            template=sample_email_template,
            reference="ref",
            status="sending",
            sent_at=datetime.utcnow(),
        )
        callback_api = create_service_callback_api(
            service=sample_email_template.service,
            url="https://original_url.com")
        assert get_notification_by_id(notification.id).status == "sending"

        assert process_ses_results(ses_notification_callback(reference="ref"))
        notification = get_notification_by_id(notification.id)
        assert notification.status == "delivered"
        assert notification.provider_response is None
        statsd_client.timing_with_dates.assert_any_call(
            "callback.ses.elapsed-time", datetime.utcnow(),
            notification.sent_at)
        statsd_client.incr.assert_any_call("callback.ses.delivered")
        updated_notification = Notification.query.get(notification.id)
        encrypted_data = create_delivery_status_callback_data(
            updated_notification, callback_api)
        send_mock.assert_called_once_with(
            [str(notification.id), encrypted_data], queue="service-callbacks")
コード例 #4
0
def _process_for_status(notification_status, client_name, provider_reference):
    # record stats
    notification = notifications_dao.update_notification_status_by_id(
        notification_id=provider_reference,
        status=notification_status,
        sent_by=client_name.lower(),
    )
    if not notification:
        return

    statsd_client.incr("callback.{}.{}".format(client_name.lower(),
                                               notification_status))

    if notification.sent_at:
        statsd_client.timing_with_dates(
            "callback.{}.elapsed-time".format(client_name.lower()),
            datetime.utcnow(),
            notification.sent_at,
        )

    if notification.billable_units == 0:
        service = notification.service
        template_model = dao_get_template_by_id(notification.template_id,
                                                notification.template_version)

        template = SMSMessageTemplate(
            template_model.__dict__,
            values=notification.personalisation,
            prefix=service.name,
            show_prefix=service.prefix_sms,
        )
        notification.billable_units = template.fragment_count
        notifications_dao.dao_update_notification(notification)

    if notification_status != NOTIFICATION_PENDING:
        service_callback_api = get_service_delivery_status_callback_api_for_service(
            service_id=notification.service_id)
        # queue callback task only if the service_callback_api exists
        if service_callback_api:
            encrypted_notification = create_delivery_status_callback_data(
                notification, service_callback_api)
            send_delivery_status_to_service.apply_async(
                [str(notification.id), encrypted_notification],
                queue=QueueNames.CALLBACKS,
            )

    success = "{} callback succeeded. reference {} updated".format(
        client_name, provider_reference)
    return success
コード例 #5
0
def test_timeout_notifications_sends_status_update_to_service(
        client, sample_template, mocker):
    callback_api = create_service_callback_api(service=sample_template.service)
    mocked = mocker.patch(
        'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
    )
    notification = create_notification(
        template=sample_template,
        status='sending',
        created_at=datetime.utcnow() -
        timedelta(seconds=current_app.config.get(
            'SENDING_NOTIFICATIONS_TIMEOUT_PERIOD') + 10))
    timeout_notifications()

    encrypted_data = create_delivery_status_callback_data(
        notification, callback_api)
    mocked.assert_called_once_with([str(notification.id), encrypted_data],
                                   queue=QueueNames.CALLBACKS)
コード例 #6
0
def test_create_delivery_status_callback_data(
    notify_db,
    notify_db_session,
    sample_email_template,
):
    notification = create_sample_notification(
        notify_db,
        notify_db_session,
        template=sample_email_template,
        status="sending",
        sent_at=datetime.utcnow(),
    )
    callback_api = create_service_callback_api(
        service=sample_email_template.service, url="https://original_url.com")

    assert encryption.decrypt(
        create_delivery_status_callback_data(notification, callback_api)) == {
            "notification_client_reference":
            notification.client_reference,
            "notification_created_at":
            notification.created_at.strftime(DATETIME_FORMAT),
            "notification_id":
            str(notification.id),
            "notification_provider_response":
            notification.provider_response,
            "notification_sent_at":
            notification.sent_at.strftime(DATETIME_FORMAT),
            "notification_status":
            notification.status,
            "notification_to":
            notification.to,
            "notification_type":
            notification.notification_type,
            "notification_updated_at":
            notification.updated_at,
            "service_callback_api_bearer_token":
            callback_api.bearer_token,
            "service_callback_api_url":
            callback_api.url,
        }