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)
    ]
Example #2
0
def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notify_db_session, mocker):
    user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900890")
    service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
    template = sample_template(notify_db, notify_db_session, service=service)
    notification = _notification_json(template, "+447700900890")  # The user’s own number, but in a different format

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

    notification_id = uuid.uuid4()
    encrypt_notification = encryption.encrypt(notification)
    send_sms(
        service.id,
        notification_id,
        encrypt_notification,
        datetime.utcnow().strftime(DATETIME_FORMAT)
    )

    persisted_notification = Notification.query.one()
    assert persisted_notification.to == '+447700900890'
    assert persisted_notification.template_id == template.id
    assert persisted_notification.template_version == template.version
    assert persisted_notification.status == 'created'
    assert persisted_notification.created_at <= datetime.utcnow()
    assert not persisted_notification.sent_at
    assert not persisted_notification.sent_by
    assert not persisted_notification.job_id
    assert not persisted_notification.personalisation
    assert persisted_notification.notification_type == 'sms'
    provider_tasks.deliver_sms.apply_async.assert_called_once_with(
        [str(persisted_notification.id)],
        queue="send-sms"
    )
Example #3
0
def test_should_put_send_sms_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_template(notify_db, notify_db_session, service=service)

    notification = _notification_json(template, to="+447234123123")

    mocked_deliver_sms = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')

    notification_id = uuid.uuid4()

    send_sms(
        template.service_id,
        notification_id,
        encryption.encrypt(notification),
        datetime.utcnow().strftime(DATETIME_FORMAT)
    )
    persisted_notification = Notification.query.one()
    provider_tasks.deliver_sms.apply_async.assert_called_once_with(
        [str(persisted_notification.id)],
        queue="research-mode"
    )
    assert mocked_deliver_sms.called
Example #4
0
def test_should_send_email_if_restricted_service_and_non_team_email_address_with_test_key(notify_db,
                                                                                          notify_db_session,
                                                                                          mocker):
    user = sample_user(notify_db, notify_db_session)
    service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
    template = sample_template(
        notify_db, notify_db_session, service=service, template_type='email', subject_line='Hello'
    )

    notification = _notification_json(template, to="*****@*****.**")
    mocked_deliver_email = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')

    notification_id = uuid.uuid4()
    send_email(
        service.id,
        notification_id,
        encryption.encrypt(notification),
        datetime.utcnow().strftime(DATETIME_FORMAT),
        key_type=KEY_TYPE_TEST
    )

    persisted_notification = Notification.query.one()
    mocked_deliver_email.assert_called_once_with(
        [str(persisted_notification.id)],
        queue="send-email"
    )
def test_should_be_able_to_get_no_template_usage_history_if_no_notifications_using_template(
        notify_db,
        notify_db_session):
    sms = sample_template(notify_db, notify_db_session)

    results = dao_get_last_template_usage(sms.id)
    assert not results
def test_get_total_notifications_counts_ignores_research_mode(notify_db, notify_db_session):
    created_at = datetime.utcnow()
    service = sample_service(notify_db, notify_db_session, research_mode=True)
    template = sample_template(notify_db, notify_db_session, service=service)

    create_notification(template, status='created', sent_at=None)

    sample_notification_history(
        notify_db,
        notify_db_session,
        template,
        notification_type='email',
        sent_at=created_at + timedelta(seconds=5)
    )
    sample_notification_history(
        notify_db,
        notify_db_session,
        template,
        notification_type='sms',
        sent_at=created_at + timedelta(seconds=5)
    )

    result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)

    assert result.messages_total == 2
    assert result.messages_within_10_secs == 2
def test_template_usage_should_ignore_test_keys(
        notify_db,
        notify_db_session,
        sample_team_api_key,
        sample_test_api_key
):
    sms = sample_template(notify_db, notify_db_session)

    one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
    two_minutes_ago = datetime.utcnow() - timedelta(minutes=2)

    team_key = sample_notification(
        notify_db,
        notify_db_session,
        created_at=two_minutes_ago,
        template=sms,
        api_key_id=sample_team_api_key.id,
        key_type=KEY_TYPE_TEAM)
    sample_notification(
        notify_db,
        notify_db_session,
        created_at=one_minute_ago,
        template=sms,
        api_key_id=sample_test_api_key.id,
        key_type=KEY_TYPE_TEST)

    results = dao_get_last_template_usage(sms.id)
    assert results.id == team_key.id
Example #8
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
def test_should_be_able_to_get_template_usage_history(notify_db, notify_db_session, sample_service):
    with freeze_time('2000-01-01 12:00:00'):
        sms = sample_template(notify_db, notify_db_session)
        notification = sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
        results = dao_get_last_template_usage(sms.id)
        assert results.template.name == 'Template Name'
        assert results.template.template_type == 'sms'
        assert results.created_at == datetime(year=2000, month=1, day=1, hour=12, minute=0, second=0)
        assert results.template_id == sms.id
        assert results.id == notification.id
def test_should_be_able_to_get_all_template_usage_history_order_by_notification_created_at(
        notify_db,
        notify_db_session,
        sample_service):
    sms = sample_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)
    most_recent = sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)

    results = dao_get_last_template_usage(sms.id)
    assert results.id == most_recent.id
def test_should_by_able_to_get_template_count_from_notifications_history_for_service(
        notify_db,
        notify_db_session):
    service_1 = sample_service(notify_db, notify_db_session, service_name="test1", email_from="test1")
    service_2 = sample_service(notify_db, notify_db_session, service_name="test2", email_from="test2")
    service_3 = sample_service(notify_db, notify_db_session, service_name="test3", email_from="test3")

    sms = sample_template(notify_db, notify_db_session)

    sample_notification(notify_db, notify_db_session, service=service_1, template=sms)
    sample_notification(notify_db, notify_db_session, service=service_1, template=sms)
    sample_notification(notify_db, notify_db_session, service=service_2, template=sms)

    assert dao_get_template_usage(service_1.id)[0].count == 2
    assert dao_get_template_usage(service_2.id)[0].count == 1
    assert len(dao_get_template_usage(service_3.id)) == 0
Example #12
0
def test_should_not_send_email_if_restricted_service_and_invalid_email_address(notify_db, notify_db_session, mocker):
    user = sample_user(notify_db, notify_db_session)
    service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
    template = sample_template(
        notify_db, notify_db_session, service=service, template_type='email', subject_line='Hello'
    )
    notification = _notification_json(template, to="*****@*****.**")

    notification_id = uuid.uuid4()
    send_email(
        service.id,
        notification_id,
        encryption.encrypt(notification),
        datetime.utcnow().strftime(DATETIME_FORMAT)
    )

    assert Notification.query.count() == 0
Example #13
0
def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, notify_db_session, mocker):
    user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900205")
    service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
    template = sample_template(notify_db, notify_db_session, service=service)

    notification = _notification_json(template, "07700 900849")
    mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')

    notification_id = uuid.uuid4()
    send_sms(
        service.id,
        notification_id,
        encryption.encrypt(notification),
        datetime.utcnow().strftime(DATETIME_FORMAT)
    )
    assert provider_tasks.deliver_sms.apply_async.called is False
    assert Notification.query.count() == 0
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
def test_template_history_should_ignore_test_keys(
    notify_db,
    notify_db_session,
    sample_team_api_key,
    sample_test_api_key,
    sample_api_key
):
    sms = sample_template(notify_db, notify_db_session)

    sample_notification(
        notify_db, notify_db_session, template=sms, api_key_id=sample_api_key.id, key_type=KEY_TYPE_NORMAL)
    sample_notification(
        notify_db, notify_db_session, template=sms, api_key_id=sample_team_api_key.id, key_type=KEY_TYPE_TEAM)
    sample_notification(
        notify_db, notify_db_session, template=sms, api_key_id=sample_test_api_key.id, key_type=KEY_TYPE_TEST)
    sample_notification(
        notify_db, notify_db_session, template=sms)

    results = dao_get_template_usage(sms.service_id)
    assert results[0].name == 'Template Name'
    assert results[0].template_type == 'sms'
    assert results[0].count == 3
Example #16
0
def test_should_send_sms_if_restricted_service_and_non_team_number_with_test_key(notify_db,
                                                                                 notify_db_session,
                                                                                 mocker):
    user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900205")
    service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
    template = sample_template(notify_db, notify_db_session, service=service)

    notification = _notification_json(template, "07700 900849")
    mocked_deliver_sms = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')

    notification_id = uuid.uuid4()
    send_sms(
        service.id,
        notification_id,
        encryption.encrypt(notification),
        datetime.utcnow().strftime(DATETIME_FORMAT),
        key_type=KEY_TYPE_TEST
    )

    persisted_notification = Notification.query.one()
    mocked_deliver_sms.assert_called_once_with(
        [str(persisted_notification.id)],
        queue="send-sms"
    )
Example #17
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'