Esempio n. 1
0
def test_should_put_send_email_task_in_research_mode_queue_if_research_mode_service(
        notify_db, notify_db_session, mocker
):
    service = sample_service(notify_db, notify_db_session)
    service.research_mode = True
    services_dao.dao_update_service(service)

    template = sample_email_template(notify_db, notify_db_session, service=service)

    notification = _notification_json(template, to="*****@*****.**")

    mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')

    notification_id = uuid.uuid4()

    send_email(
        template.service_id,
        notification_id,
        encryption.encrypt(notification),
        datetime.utcnow().strftime(DATETIME_FORMAT)
    )

    persisted_notification = Notification.query.one()
    provider_tasks.deliver_email.apply_async.assert_called_once_with(
        [str(persisted_notification.id)],
        queue="research-mode"
    )
Esempio n. 2
0
def test_should_process_email_job_into_research_mode_queue_if_research_mode_service(
        notify_db, notify_db_session, mocker
):
    mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('sms'))
    mocker.patch('app.celery.tasks.send_email.apply_async')
    mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
    mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")

    service = sample_service(notify_db, notify_db_session)
    service.research_mode = True
    services_dao.dao_update_service(service)
    template = sample_email_template(notify_db, notify_db_session, service=service)
    job = sample_job(notify_db, notify_db_session, template=template, service=service)

    process_job(job.id)
    s3.get_job_from_s3.assert_called_once_with(
        str(job.service.id),
        str(job.id)
    )
    tasks.send_email.apply_async.assert_called_once_with(
        (str(job.service_id),
         "uuid",
         "something_encrypted",
         "2016-01-01T11:09:00.061258Z"),
        queue="research-mode"
    )
Esempio n. 3
0
def test_should_process_email_job_if_exactly_on_send_limits(notify_db,
                                                            notify_db_session,
                                                            mocker):
    service = sample_service(notify_db, notify_db_session, limit=10)
    template = sample_email_template(notify_db, notify_db_session, service=service)
    job = sample_job(notify_db, notify_db_session, service=service, template=template, notification_count=10)

    mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('multiple_email'))
    mocker.patch('app.celery.tasks.send_email.apply_async')
    mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
    mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")

    process_job(job.id)

    s3.get_job_from_s3.assert_called_once_with(
        str(job.service.id),
        str(job.id)
    )
    job = jobs_dao.dao_get_job_by_id(job.id)
    assert job.job_status == 'finished'
    tasks.send_email.apply_async.assert_called_with(
        (
            str(job.service_id),
            "uuid",
            "something_encrypted",
            "2016-01-01T11:09:00.061258Z"
        ),
        queue="db-email"
    )
def test_should_by_able_to_get_template_count_from_notifications_history_across_days(
        notify_db,
        notify_db_session,
        sample_service):
    sms = sample_template(notify_db, notify_db_session)
    email = sample_email_template(notify_db, notify_db_session)

    today = datetime.now()
    yesterday = datetime.now() - timedelta(days=1)
    one_month_ago = datetime.now() - timedelta(days=30)

    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)

    sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=sms)

    sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)

    results = dao_get_template_usage(sample_service.id)

    assert len(results) == 2

    assert [(row.name, row.template_type, row.count) for row in results] == [
        ('Email Template Name', 'email', 5),
        ('Template Name', 'sms', 5)
    ]
Esempio n. 5
0
def test_get_template_statistics_for_service(notify_db, notify_db_session, notify_api, sample_service):
    sms = sample_template(notify_db, notify_db_session, service=sample_service)
    email = sample_email_template(notify_db, notify_db_session, service=sample_service)
    today = datetime.now()
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)

    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            auth_header = create_authorization_header()

            response = client.get(
                '/service/{}/template-statistics'.format(sample_service.id),
                headers=[('Content-Type', 'application/json'), auth_header]
            )

            assert response.status_code == 200
            json_resp = json.loads(response.get_data(as_text=True))
            assert len(json_resp['data']) == 2
            assert json_resp['data'][0]['count'] == 2
            assert json_resp['data'][0]['template_id'] == str(email.id)
            assert json_resp['data'][0]['template_name'] == email.name
            assert json_resp['data'][0]['template_type'] == email.template_type
            assert json_resp['data'][1]['count'] == 2
            assert json_resp['data'][1]['template_id'] == str(sms.id)
            assert json_resp['data'][1]['template_name'] == sms.name
            assert json_resp['data'][1]['template_type'] == sms.template_type
Esempio n. 6
0
def test_should_not_process_email_job_if_would_exceed_send_limits(notify_db, notify_db_session, mocker):
    service = sample_service(notify_db, notify_db_session, limit=0)
    template = sample_email_template(notify_db, notify_db_session, service=service)
    job = sample_job(notify_db, notify_db_session, service=service, template=template)

    mocker.patch('app.celery.tasks.s3.get_job_from_s3')
    mocker.patch('app.celery.tasks.send_email.apply_async')

    process_job(job.id)

    job = jobs_dao.dao_get_job_by_id(job.id)
    assert job.job_status == 'sending limits exceeded'
    assert s3.get_job_from_s3.called is False
    assert tasks.send_email.apply_async.called is False
Esempio n. 7
0
def test_should_not_process_email_job_if_would_exceed_send_limits_inc_today(notify_db, notify_db_session, mocker):
    service = sample_service(notify_db, notify_db_session, limit=1)
    template = sample_email_template(notify_db, notify_db_session, service=service)
    job = sample_job(notify_db, notify_db_session, service=service, template=template)

    sample_notification(notify_db, notify_db_session, service=service, job=job)

    mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('email'))
    mocker.patch('app.celery.tasks.send_email.apply_async')

    process_job(job.id)

    job = jobs_dao.dao_get_job_by_id(job.id)
    assert job.job_status == 'sending limits exceeded'
    assert s3.get_job_from_s3.called is False
    assert tasks.send_email.apply_async.called is False
def test_should_by_able_to_get_template_count_from_notifications_history(notify_db, notify_db_session, sample_service):
    sms = sample_template(notify_db, notify_db_session)
    email = sample_email_template(notify_db, notify_db_session)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, service=sample_service, template=email)

    results = dao_get_template_usage(sample_service.id)
    assert results[0].name == 'Email Template Name'
    assert results[0].template_type == 'email'
    assert results[0].count == 2

    assert results[1].name == 'Template Name'
    assert results[1].template_type == 'sms'
    assert results[1].count == 3
Esempio n. 9
0
def set_up_notifications(notify_db, notify_db_session):
    sms = create_sample_template(notify_db, notify_db_session)
    email = sample_email_template(notify_db, notify_db_session)
    today = datetime.now()
    a_week_ago = datetime.now() - timedelta(days=7)
    a_month_ago = datetime.now() - timedelta(days=30)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=a_month_ago,
                        template=sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=a_month_ago,
                        template=email)
    email.name = 'Updated Email Template Name'
    dao_update_template(email)
    sms.name = 'Updated SMS Template Name'
    dao_update_template(sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=a_week_ago,
                        template=sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=a_week_ago,
                        template=email)
    email.name = 'New Email Template Name'
    dao_update_template(email)
    sms.name = 'New SMS Template Name'
    dao_update_template(sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=today,
                        template=sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=today,
                        template=email)
    return email, sms
def test_notification_document_with_pdf_attachment(
    mocker,
    notify_db,
    notify_db_session,
    filename_attribute_present,
    filename,
    expected_filename,
):
    template = sample_email_template(notify_db, notify_db_session, content="Here is your ((file))")
    personalisation = {
        "file": document_download_response(
            {
                "direct_file_url": "http://foo.bar/direct_file_url",
                "url": "http://foo.bar/url",
                "mime_type": "application/pdf",
                "mlwr_sid": "false",
            }
        )
    }
    if filename_attribute_present:
        personalisation["file"]["document"]["filename"] = filename
        personalisation["file"]["document"]["sending_method"] = "attach"
    else:
        personalisation["file"]["document"]["sending_method"] = "link"

    db_notification = create_notification(template=template, personalisation=personalisation)

    statsd_mock = mocker.patch("app.delivery.send_to_providers.statsd_client")
    send_mock = mocker.patch("app.aws_ses_client.send_email", return_value="reference")
    request_mock = mocker.patch(
        "app.delivery.send_to_providers.urllib.request.Request",
        return_value="request_mock",
    )
    # See https://stackoverflow.com/a/34929900
    cm = MagicMock()
    cm.read.return_value = "request_content"
    cm.__enter__.return_value = cm
    urlopen_mock = mocker.patch("app.delivery.send_to_providers.urllib.request.urlopen")
    urlopen_mock.return_value = cm

    send_to_providers.send_email_to_provider(db_notification)

    attachments = []
    if filename_attribute_present:
        request_mock.assert_called_once_with("http://foo.bar/direct_file_url")
        urlopen_mock.assert_called_once_with("request_mock")
        attachments = [
            {
                "data": "request_content",
                "name": expected_filename,
                "mime_type": "application/pdf",
            }
        ]
    send_mock.assert_called_once_with(
        ANY,
        ANY,
        ANY,
        body=ANY,
        html_body=ANY,
        reply_to_address=ANY,
        attachments=attachments,
    )
    if not filename_attribute_present:
        assert "http://foo.bar/url" in send_mock.call_args[1]["html_body"]

    notification = Notification.query.get(db_notification.id)
    assert notification.status == "sending"

    if attachments:
        statsd_calls = statsd_mock.timing_with_dates.call_args_list
        statsd_key = "email.with-attachments.process_type-normal"
        assert call(statsd_key, notification.sent_at, notification.created_at) in statsd_calls
        assert call(statsd_key) in statsd_mock.incr.call_args_list
Esempio n. 11
0
def test_get_template_statistics_for_service_limited_by_day(notify_db, notify_db_session, notify_api, sample_service):
    sms = sample_template(notify_db, notify_db_session, service=sample_service)
    email = sample_email_template(notify_db, notify_db_session, service=sample_service)
    today = datetime.now()
    a_week_ago = datetime.now() - timedelta(days=7)
    a_month_ago = datetime.now() - timedelta(days=30)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=a_week_ago, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=a_week_ago, service=sample_service, template=email)
    sample_notification(notify_db, notify_db_session, created_at=a_month_ago, service=sample_service, template=sms)
    sample_notification(notify_db, notify_db_session, created_at=a_month_ago, service=sample_service, template=email)

    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            auth_header = create_authorization_header()

            response = client.get(
                '/service/{}/template-statistics'.format(sample_service.id),
                headers=[('Content-Type', 'application/json'), auth_header],
                query_string={'limit_days': 1}
            )

            assert response.status_code == 200
            json_resp = json.loads(response.get_data(as_text=True))
            assert len(json_resp['data']) == 2
            assert json_resp['data'][0]['count'] == 1
            assert json_resp['data'][0]['template_id'] == str(email.id)
            assert json_resp['data'][0]['template_name'] == email.name
            assert json_resp['data'][0]['template_type'] == email.template_type
            assert json_resp['data'][1]['count'] == 1
            assert json_resp['data'][1]['template_id'] == str(sms.id)
            assert json_resp['data'][1]['template_name'] == sms.name
            assert json_resp['data'][1]['template_type'] == sms.template_type

            response_for_a_week = client.get(
                '/service/{}/template-statistics'.format(sample_service.id),
                headers=[('Content-Type', 'application/json'), auth_header],
                query_string={'limit_days': 7}
            )

            assert response.status_code == 200
            json_resp = json.loads(response_for_a_week.get_data(as_text=True))
            assert len(json_resp['data']) == 2
            assert json_resp['data'][0]['count'] == 2
            assert json_resp['data'][0]['template_name'] == 'Email Template Name'
            assert json_resp['data'][1]['count'] == 2
            assert json_resp['data'][1]['template_name'] == 'Template Name'

            response_for_a_month = client.get(
                '/service/{}/template-statistics'.format(sample_service.id),
                headers=[('Content-Type', 'application/json'), auth_header],
                query_string={'limit_days': 30}
            )

            assert response_for_a_month.status_code == 200
            json_resp = json.loads(response_for_a_month.get_data(as_text=True))
            assert len(json_resp['data']) == 2
            assert json_resp['data'][0]['count'] == 3
            assert json_resp['data'][0]['template_name'] == 'Email Template Name'
            assert json_resp['data'][1]['count'] == 3
            assert json_resp['data'][1]['template_name'] == 'Template Name'

            response_for_all = client.get(
                '/service/{}/template-statistics'.format(sample_service.id),
                headers=[('Content-Type', 'application/json'), auth_header]
            )

            assert response_for_all.status_code == 200
            json_resp = json.loads(response_for_all.get_data(as_text=True))
            assert len(json_resp['data']) == 2
            assert json_resp['data'][0]['count'] == 3
            assert json_resp['data'][0]['template_name'] == 'Email Template Name'
            assert json_resp['data'][1]['count'] == 3
            assert json_resp['data'][1]['template_name'] == 'Template Name'