def test_get_monthly_notification_stats_combines_todays_data_and_historic_stats(admin_request, sample_template):
    create_ft_notification_status(datetime(2016, 5, 1), template=sample_template, count=1)
    create_ft_notification_status(datetime(2016, 6, 1), template=sample_template, notification_status='created', count=2)  # noqa

    create_notification(sample_template, created_at=datetime(2016, 6, 5), status='created')
    create_notification(sample_template, created_at=datetime(2016, 6, 5), status='delivered')

    # this doesn't get returned in the stats because it is old - it should be in ft_notification_status by now
    create_notification(sample_template, created_at=datetime(2016, 6, 4), status='sending')

    response = admin_request.get(
        'service.get_monthly_notification_stats',
        service_id=sample_template.service_id,
        year=2016
    )

    assert len(response['data']) == 3  # apr, may, jun
    assert response['data']['2016-05'] == {
        'sms': {
            'delivered': 1
        },
        'email': {},
        'letter': {}
    }
    assert response['data']['2016-06'] == {
        'sms': {
            # combines the stats from the historic ft_notification_status and the current notifications
            'created': 3,
            'delivered': 1,
        },
        'email': {},
        'letter': {}
    }
def test_get_template_usage_by_month_returns_correct_data(
        admin_request, sample_template):
    create_ft_notification_status(utc_date=date(2017, 4, 2),
                                  template=sample_template,
                                  count=3)
    create_notification(sample_template, created_at=datetime.utcnow())

    resp_json = admin_request.get('service.get_monthly_template_usage',
                                  service_id=sample_template.service_id,
                                  year=2017)
    resp_json = resp_json['stats']

    assert len(resp_json) == 2

    assert resp_json[0]["template_id"] == str(sample_template.id)
    assert resp_json[0]["name"] == sample_template.name
    assert resp_json[0]["type"] == sample_template.template_type
    assert resp_json[0]["month"] == 4
    assert resp_json[0]["year"] == 2017
    assert resp_json[0]["count"] == 3

    assert resp_json[1]["template_id"] == str(sample_template.id)
    assert resp_json[1]["name"] == sample_template.name
    assert resp_json[1]["type"] == sample_template.template_type
    assert resp_json[1]["month"] == 11
    assert resp_json[1]["year"] == 2017
    assert resp_json[1]["count"] == 1
Ejemplo n.º 3
0
def test_get_platform_stats_with_real_query(admin_request, notify_db_session):
    service_1 = create_service(service_name='service_1')
    sms_template = create_template(service=service_1, template_type=SMS_TYPE)
    email_template = create_template(service=service_1,
                                     template_type=EMAIL_TYPE)
    create_ft_notification_status(date(2018, 10, 29),
                                  'sms',
                                  service_1,
                                  count=10)
    create_ft_notification_status(date(2018, 10, 29),
                                  'email',
                                  service_1,
                                  count=3)

    create_notification(sms_template,
                        created_at=datetime(2018, 10, 31, 11, 0, 0),
                        key_type='test')
    create_notification(sms_template,
                        created_at=datetime(2018, 10, 31, 12, 0, 0),
                        status='delivered')
    create_notification(email_template,
                        created_at=datetime(2018, 10, 31, 13, 0, 0),
                        status='delivered')

    response = admin_request.get(
        'platform_stats.get_platform_stats',
        start_date=date(2018, 10, 29),
    )
    assert response == {
        'email': {
            'failures': {
                'virus-scan-failed': 0,
                'temporary-failure': 0,
                'permanent-failure': 0,
                'technical-failure': 0
            },
            'total': 4,
            'test-key': 0
        },
        'letter': {
            'failures': {
                'virus-scan-failed': 0,
                'temporary-failure': 0,
                'permanent-failure': 0,
                'technical-failure': 0
            },
            'total': 0,
            'test-key': 0
        },
        'sms': {
            'failures': {
                'virus-scan-failed': 0,
                'temporary-failure': 0,
                'permanent-failure': 0,
                'technical-failure': 0
            },
            'total': 11,
            'test-key': 1
        }
    }
Ejemplo n.º 4
0
def test_get_platform_stats_with_real_query(admin_request, notify_db_session):
    service_1 = create_service(service_name="service_1")
    sms_template = create_template(service=service_1, template_type=SMS_TYPE)
    email_template = create_template(service=service_1,
                                     template_type=EMAIL_TYPE)
    create_ft_notification_status(date(2018, 10, 29),
                                  "sms",
                                  service_1,
                                  count=10)
    create_ft_notification_status(date(2018, 10, 29),
                                  "email",
                                  service_1,
                                  count=3)

    create_notification(sms_template,
                        created_at=datetime(2018, 10, 31, 11, 0, 0),
                        key_type="test")
    create_notification(sms_template,
                        created_at=datetime(2018, 10, 31, 12, 0, 0),
                        status="delivered")
    create_notification(email_template,
                        created_at=datetime(2018, 10, 31, 13, 0, 0),
                        status="delivered")

    response = admin_request.get(
        "platform_stats.get_platform_stats",
        start_date=date(2018, 10, 29),
    )
    assert response == {
        "email": {
            "failures": {
                "virus-scan-failed": 0,
                "temporary-failure": 0,
                "permanent-failure": 0,
                "technical-failure": 0,
            },
            "total": 4,
            "test-key": 0,
        },
        "letter": {
            "failures": {
                "virus-scan-failed": 0,
                "temporary-failure": 0,
                "permanent-failure": 0,
                "technical-failure": 0,
            },
            "total": 0,
            "test-key": 0,
        },
        "sms": {
            "failures": {
                "virus-scan-failed": 0,
                "temporary-failure": 0,
                "permanent-failure": 0,
                "technical-failure": 0,
            },
            "total": 11,
            "test-key": 1,
        },
    }
Ejemplo n.º 5
0
 def __create_ft_status(job, status, count):
     create_ft_notification_status(bst_date=job.created_at.date(),
                                   notification_type='sms',
                                   service=job.service,
                                   job=job,
                                   template=job.template,
                                   key_type='normal',
                                   notification_status=status,
                                   count=count)
Ejemplo n.º 6
0
def test_dao_get_last_date_template_was_used_returns_bst_date_from_stats_table(
        sample_template):
    last_status_date = (datetime.utcnow() - timedelta(days=2)).date()
    create_ft_notification_status(bst_date=last_status_date,
                                  template=sample_template)

    last_used_date = dao_get_last_date_template_was_used(
        template_id=sample_template.id, service_id=sample_template.service_id)
    assert last_used_date == last_status_date
Ejemplo n.º 7
0
def test_get_last_used_datetime_for_template(admin_request, sample_template):
    date_from_notification = datetime.utcnow() - timedelta(hours=2)
    create_notification(template=sample_template,
                        created_at=date_from_notification)
    date_from_ft_status = (datetime.utcnow() - timedelta(days=2)).date()
    create_ft_notification_status(bst_date=date_from_ft_status,
                                  template=sample_template)

    json_resp = admin_request.get(
        'template_statistics.get_last_used_datetime_for_template',
        service_id=str(sample_template.service_id),
        template_id=sample_template.id)
    assert json_resp['last_date_used'] == date_from_notification.strftime(
        DATETIME_FORMAT)
Ejemplo n.º 8
0
def test_get_jobs_should_retrieve_from_ft_notification_status_for_old_jobs(admin_request, sample_template):
    # it's the 10th today, so 3 days should include all of 7th, 8th, 9th, and some of 10th.
    just_three_days_ago = datetime(2017, 6, 6, 22, 59, 59)
    not_quite_three_days_ago = just_three_days_ago + timedelta(seconds=1)

    job_1 = create_job(sample_template, created_at=just_three_days_ago, processing_started=just_three_days_ago)
    job_2 = create_job(sample_template, created_at=just_three_days_ago, processing_started=not_quite_three_days_ago)
    # is old but hasn't started yet (probably a scheduled job). We don't have any stats for this job yet.
    job_3 = create_job(sample_template, created_at=just_three_days_ago, processing_started=None)

    # some notifications created more than three days ago, some created after the midnight cutoff
    create_ft_notification_status(date(2017, 6, 6), job=job_1, notification_status='delivered', count=2)
    create_ft_notification_status(date(2017, 6, 7), job=job_1, notification_status='delivered', count=4)
    # job2's new enough
    create_notification(job=job_2, status='created', created_at=not_quite_three_days_ago)

    # this isn't picked up because the job is too new
    create_ft_notification_status(date(2017, 6, 7), job=job_2, notification_status='delivered', count=8)
    # this isn't picked up - while the job is old, it started in last 3 days so we look at notification table instead
    create_ft_notification_status(date(2017, 6, 7), job=job_3, notification_status='delivered', count=16)

    # this isn't picked up because we're using the ft status table for job_1 as it's old
    create_notification(job=job_1, status='created', created_at=not_quite_three_days_ago)

    resp_json = admin_request.get('job.get_jobs_by_service', service_id=sample_template.service_id)

    assert resp_json['data'][0]['id'] == str(job_3.id)
    assert resp_json['data'][0]['statistics'] == []
    assert resp_json['data'][1]['id'] == str(job_2.id)
    assert resp_json['data'][1]['statistics'] == [{'status': 'created', 'count': 1}]
    assert resp_json['data'][2]['id'] == str(job_1.id)
    assert resp_json['data'][2]['statistics'] == [{'status': 'delivered', 'count': 6}]
Ejemplo n.º 9
0
def test_fetch_notification_statuses_for_job(sample_template):
    j1 = create_job(sample_template)
    j2 = create_job(sample_template)

    create_ft_notification_status(date(2018, 10, 1),
                                  job=j1,
                                  notification_status='created',
                                  count=1)
    create_ft_notification_status(date(2018, 10, 1),
                                  job=j1,
                                  notification_status='delivered',
                                  count=2)
    create_ft_notification_status(date(2018, 10, 2),
                                  job=j1,
                                  notification_status='created',
                                  count=4)
    create_ft_notification_status(date(2018, 10, 1),
                                  job=j2,
                                  notification_status='created',
                                  count=8)

    assert {
        x.status: x.count
        for x in fetch_notification_statuses_for_job(j1.id)
    } == {
        'created': 5,
        'delivered': 2
    }
def test_get_monthly_notification_stats_checks_dates(admin_request,
                                                     sample_service):
    t = create_template(sample_service)
    create_ft_notification_status(datetime(2016, 3, 31),
                                  template=t,
                                  notification_status="created")
    create_ft_notification_status(datetime(2016, 4, 1),
                                  template=t,
                                  notification_status="sending")
    create_ft_notification_status(datetime(2017, 3, 31),
                                  template=t,
                                  notification_status="delivered")
    create_ft_notification_status(datetime(2017, 4, 11),
                                  template=t,
                                  notification_status="permanent-failure")

    response = admin_request.get(
        "service.get_monthly_notification_stats",
        service_id=sample_service.id,
        year=2016,
    )

    assert "2016-03" not in response["data"]
    assert "2017-04" not in response["data"]
    assert response["data"]["2016-04"]["sms"] == {"sending": 1}
    assert response["data"]["2017-03"]["sms"] == {"delivered": 1}
def test_fetch_monthly_template_usage_for_service_does_not_include_cancelled_status(
        sample_template
):
    create_ft_notification_status(utc_date=date(2018, 3, 1),
                                  service=sample_template.service,
                                  template=sample_template,
                                  notification_status='cancelled',
                                  count=15)
    create_notification(template=sample_template, created_at=datetime.utcnow(), status='cancelled')
    results = fetch_monthly_template_usage_for_service(
        datetime(2018, 1, 1), datetime(2018, 3, 31), sample_template.service_id
    )

    assert len(results) == 0
Ejemplo n.º 12
0
def test_send_total_sent_notifications_to_performance_platform_calls_with_correct_totals(
        notify_db_session, sample_template, sample_email_template, mocker):
    perf_mock = mocker.patch(
        'app.celery.nightly_tasks.total_sent_notifications.send_total_notifications_sent_for_day_stats'
    )  # noqa

    today = date(2016, 6, 11)
    create_ft_notification_status(utc_date=today, template=sample_template)
    create_ft_notification_status(utc_date=today,
                                  template=sample_email_template)

    # Create some notifications for the day before
    yesterday = date(2016, 6, 10)
    create_ft_notification_status(utc_date=yesterday,
                                  template=sample_template,
                                  count=2)
    create_ft_notification_status(utc_date=yesterday,
                                  template=sample_email_template,
                                  count=3)

    with patch.object(PerformancePlatformClient,
                      'active',
                      new_callable=PropertyMock) as mock_active:
        mock_active.return_value = True
        send_total_sent_notifications_to_performance_platform(yesterday)

        perf_mock.assert_has_calls([
            call("2016-06-10", 'sms', 2),
            call("2016-06-10", 'email', 3),
            call("2016-06-10", 'letter', 0)
        ])
def test_get_template_usage_by_month_returns_two_templates(
        admin_request, sample_template, sample_service):
    template_one = create_template(
        sample_service,
        template_type=LETTER_TYPE,
        template_name=PRECOMPILED_TEMPLATE_NAME,
        hidden=True,
    )
    create_ft_notification_status(utc_date=datetime(2017, 4, 1),
                                  template=template_one,
                                  count=1)
    create_ft_notification_status(utc_date=datetime(2017, 4, 1),
                                  template=sample_template,
                                  count=3)
    create_notification(sample_template, created_at=datetime.utcnow())

    resp_json = admin_request.get(
        "service.get_monthly_template_usage",
        service_id=sample_template.service_id,
        year=2017,
    )

    resp_json = sorted(resp_json["stats"],
                       key=lambda k: (k["year"], k["month"], k["count"]))
    assert len(resp_json) == 3

    assert resp_json[0]["template_id"] == str(template_one.id)
    assert resp_json[0]["name"] == template_one.name
    assert resp_json[0]["type"] == template_one.template_type
    assert resp_json[0]["month"] == 4
    assert resp_json[0]["year"] == 2017
    assert resp_json[0]["count"] == 1
    assert resp_json[0]["is_precompiled_letter"] is True

    assert resp_json[1]["template_id"] == str(sample_template.id)
    assert resp_json[1]["name"] == sample_template.name
    assert resp_json[1]["type"] == sample_template.template_type
    assert resp_json[1]["month"] == 4
    assert resp_json[1]["year"] == 2017
    assert resp_json[1]["count"] == 3
    assert resp_json[1]["is_precompiled_letter"] is False

    assert resp_json[2]["template_id"] == str(sample_template.id)
    assert resp_json[2]["name"] == sample_template.name
    assert resp_json[2]["type"] == sample_template.template_type
    assert resp_json[2]["month"] == 11
    assert resp_json[2]["year"] == 2017
    assert resp_json[2]["count"] == 1
    assert resp_json[2]["is_precompiled_letter"] is False
def test_get_service_notification_statistics(admin_request, sample_service,
                                             sample_template, today_only,
                                             stats):
    create_ft_notification_status(date(2000, 1, 1),
                                  'sms',
                                  sample_service,
                                  count=1)
    with freeze_time('2000-01-02T12:00:00'):
        create_notification(sample_template, status='created')
        resp = admin_request.get('service.get_service_notification_statistics',
                                 service_id=sample_template.service_id,
                                 today_only=today_only)

    assert set(resp['data'].keys()) == {SMS_TYPE, EMAIL_TYPE, LETTER_TYPE}
    assert resp['data'][SMS_TYPE] == stats
def test_get_monthly_notification_stats_only_gets_for_one_service(admin_request, notify_db_session):
    services = [create_service(), create_service(service_name="2")]

    templates = [create_template(services[0]), create_template(services[1])]

    create_ft_notification_status(datetime(2016, 6, 1), template=templates[0], notification_status='created')
    create_ft_notification_status(datetime(2016, 6, 1), template=templates[1], notification_status='delivered')

    response = admin_request.get('service.get_monthly_notification_stats', service_id=services[0].id, year=2016)

    assert response['data']['2016-06'] == {
        'sms': {'created': 1},
        'email': {},
        'letter': {}
    }
def test_get_monthly_notification_stats_combines_todays_data_and_historic_stats(
        admin_request, sample_template):
    create_ft_notification_status(datetime(2016, 5, 1),
                                  template=sample_template,
                                  count=1)
    create_ft_notification_status(
        datetime(2016, 6, 1),
        template=sample_template,
        notification_status="created",
        count=2,
    )  # noqa

    create_notification(sample_template,
                        created_at=datetime(2016, 6, 5),
                        status="created")
    create_notification(sample_template,
                        created_at=datetime(2016, 6, 5),
                        status="delivered")

    # this doesn't get returned in the stats because it is old - it should be in ft_notification_status by now
    create_notification(sample_template,
                        created_at=datetime(2016, 6, 4),
                        status="sending")

    response = admin_request.get(
        "service.get_monthly_notification_stats",
        service_id=sample_template.service_id,
        year=2016,
    )

    assert len(response["data"]) == 3  # apr, may, jun
    assert response["data"]["2016-05"] == {
        "sms": {
            "delivered": 1
        },
        "email": {},
        "letter": {},
    }
    assert response["data"]["2016-06"] == {
        "sms": {
            # combines the stats from the historic ft_notification_status and the current notifications
            "created": 3,
            "delivered": 1,
        },
        "email": {},
        "letter": {},
    }
def test_get_service_notification_statistics(admin_request, sample_service,
                                             sample_template, today_only,
                                             stats):
    create_ft_notification_status(date(2000, 1, 1),
                                  "sms",
                                  sample_service,
                                  count=1)
    with freeze_time("2000-01-02T12:00:00"):
        create_notification(sample_template, status="created")
        resp = admin_request.get(
            "service.get_service_notification_statistics",
            service_id=sample_template.service_id,
            today_only=today_only,
        )

    assert set(resp["data"].keys()) == {SMS_TYPE, EMAIL_TYPE, LETTER_TYPE}
    assert resp["data"][SMS_TYPE] == stats
def test_fetch_notification_status_for_service_for_today_and_7_previous_days(notify_db_session):
    service_1 = create_service(service_name='service_1')
    sms_template = create_template(service=service_1, template_type=SMS_TYPE)
    sms_template_2 = create_template(service=service_1, template_type=SMS_TYPE)
    email_template = create_template(service=service_1, template_type=EMAIL_TYPE)

    create_ft_notification_status(date(2018, 10, 29), 'sms', service_1, count=10)
    create_ft_notification_status(date(2018, 10, 24), 'sms', service_1, count=8)
    create_ft_notification_status(date(2018, 10, 29), 'sms', service_1, notification_status='created')
    create_ft_notification_status(date(2018, 10, 29), 'email', service_1, count=3)
    create_ft_notification_status(date(2018, 10, 26), 'letter', service_1, count=5)

    create_notification(sms_template, created_at=datetime(2018, 10, 31, 11, 0, 0))
    create_notification(sms_template_2, created_at=datetime(2018, 10, 31, 11, 0, 0))
    create_notification(sms_template, created_at=datetime(2018, 10, 31, 12, 0, 0), status='delivered')
    create_notification(email_template, created_at=datetime(2018, 10, 31, 13, 0, 0), status='delivered')

    # too early, shouldn't be included
    create_notification(service_1.templates[0], created_at=datetime(2018, 10, 30, 12, 0, 0), status='delivered')

    results = sorted(
        fetch_notification_status_for_service_for_today_and_7_previous_days(service_1.id),
        key=lambda x: (x.notification_type, x.status)
    )

    assert len(results) == 4

    assert results[0].notification_type == 'email'
    assert results[0].status == 'delivered'
    assert results[0].count == 4

    assert results[1].notification_type == 'letter'
    assert results[1].status == 'delivered'
    assert results[1].count == 5

    assert results[2].notification_type == 'sms'
    assert results[2].status == 'created'
    assert results[2].count == 3

    assert results[3].notification_type == 'sms'
    assert results[3].status == 'delivered'
    assert results[3].count == 11
def test_get_monthly_notification_stats_checks_dates(admin_request, sample_service):
    t = create_template(sample_service)
    create_ft_notification_status(datetime(2016, 3, 31), template=t, notification_status='created')
    create_ft_notification_status(datetime(2016, 4, 1), template=t, notification_status='sending')
    create_ft_notification_status(datetime(2017, 3, 31), template=t, notification_status='delivered')
    create_ft_notification_status(datetime(2017, 4, 11), template=t, notification_status='permanent-failure')

    response = admin_request.get('service.get_monthly_notification_stats', service_id=sample_service.id, year=2016)

    assert '2016-03' not in response['data']
    assert '2017-04' not in response['data']
    assert response['data']['2016-04']['sms'] == {'sending': 1}
    assert response['data']['2017-03']['sms'] == {'delivered': 1}
Ejemplo n.º 20
0
def test_get_total_sent_notifications_for_day_and_type_returns_total_for_right_day(
        day, sample_template):
    date = datetime.strptime(day, "%Y-%m-%d")
    create_ft_notification_status(
        utc_date=date - timedelta(days=1),
        notification_type=sample_template.template_type,
        service=sample_template.service,
        template=sample_template,
        count=1)
    create_ft_notification_status(
        utc_date=date,
        notification_type=sample_template.template_type,
        service=sample_template.service,
        template=sample_template,
        count=2)
    create_ft_notification_status(
        utc_date=date + timedelta(days=1),
        notification_type=sample_template.template_type,
        service=sample_template.service,
        template=sample_template,
        count=3)

    total = get_total_sent_notifications_for_day_and_type(
        day, sample_template.template_type)

    assert total == 2
def test_get_monthly_notification_stats_ignores_test_keys(admin_request, sample_service):
    create_ft_notification_status(datetime(2016, 6, 1), service=sample_service, key_type=KEY_TYPE_NORMAL, count=1)
    create_ft_notification_status(datetime(2016, 6, 1), service=sample_service, key_type=KEY_TYPE_TEAM, count=2)
    create_ft_notification_status(datetime(2016, 6, 1), service=sample_service, key_type=KEY_TYPE_TEST, count=4)

    response = admin_request.get('service.get_monthly_notification_stats', service_id=sample_service.id, year=2016)

    assert response['data']['2016-06']['sms'] == {'delivered': 3}
def test_get_monthly_notification_stats_only_gets_for_one_service(
        admin_request, notify_db_session):
    services = [create_service(), create_service(service_name="2")]

    templates = [create_template(services[0]), create_template(services[1])]

    create_ft_notification_status(datetime(2016, 6, 1),
                                  template=templates[0],
                                  notification_status="created")
    create_ft_notification_status(datetime(2016, 6, 1),
                                  template=templates[1],
                                  notification_status="delivered")

    response = admin_request.get("service.get_monthly_notification_stats",
                                 service_id=services[0].id,
                                 year=2016)

    assert response["data"]["2016-06"] == {
        "sms": {
            "created": 1
        },
        "email": {},
        "letter": {},
    }
def test_get_total_sent_notifications_for_day_and_type_returns_right_notification_type(
        notification_type, count, sample_template, sample_email_template, sample_letter_template
):
    create_ft_notification_status(utc_date="2019-03-27", service=sample_template.service, template=sample_template,
                                  count=3)
    create_ft_notification_status(utc_date="2019-03-27", service=sample_email_template.service,
                                  template=sample_email_template, count=5)
    create_ft_notification_status(utc_date="2019-03-27", service=sample_letter_template.service,
                                  template=sample_letter_template, count=7)

    result = get_total_sent_notifications_for_day_and_type(day='2019-03-27', notification_type=notification_type)

    assert result == count
Ejemplo n.º 24
0
def test_fetch_monthly_template_usage_for_service_does_join_to_notifications_if_today_is_not_in_date_range(
        sample_service):
    template_one = create_template(service=sample_service,
                                   template_type='sms',
                                   template_name='a')
    template_two = create_template(service=sample_service,
                                   template_type='email',
                                   template_name='b')
    create_ft_notification_status(utc_date=date(2018, 2, 1),
                                  service=template_two.service,
                                  template=template_two,
                                  count=15)
    create_ft_notification_status(utc_date=date(2018, 2, 2),
                                  service=template_one.service,
                                  template=template_one,
                                  count=20)
    create_ft_notification_status(utc_date=date(2018, 3, 1),
                                  service=template_one.service,
                                  template=template_one,
                                  count=3)
    create_notification(template=template_one, created_at=datetime.utcnow())
    results = fetch_monthly_template_usage_for_service(datetime(2018, 1, 1),
                                                       datetime(2018, 2, 20),
                                                       template_one.service_id)

    assert len(results) == 2

    assert results[0].template_id == template_one.id
    assert results[0].name == template_one.name
    assert results[
        0].is_precompiled_letter == template_one.is_precompiled_letter
    assert results[0].template_type == template_one.template_type
    assert results[0].month == 2
    assert results[0].year == 2018
    assert results[0].count == 20
    assert results[1].template_id == template_two.id
    assert results[1].name == template_two.name
    assert results[
        1].is_precompiled_letter == template_two.is_precompiled_letter
    assert results[1].template_type == template_two.template_type
    assert results[1].month == 2
    assert results[1].year == 2018
    assert results[1].count == 15
def test_get_monthly_notification_stats_ignores_test_keys(
        admin_request, sample_service):
    create_ft_notification_status(datetime(2016, 6, 1),
                                  service=sample_service,
                                  key_type=KEY_TYPE_NORMAL,
                                  count=1)
    create_ft_notification_status(datetime(2016, 6, 1),
                                  service=sample_service,
                                  key_type=KEY_TYPE_TEAM,
                                  count=2)
    create_ft_notification_status(datetime(2016, 6, 1),
                                  service=sample_service,
                                  key_type=KEY_TYPE_TEST,
                                  count=4)

    response = admin_request.get(
        "service.get_monthly_notification_stats",
        service_id=sample_service.id,
        year=2016,
    )

    assert response["data"]["2016-06"]["sms"] == {"delivered": 3}
Ejemplo n.º 26
0
def test_fetch_monthly_notification_statuses_per_service_for_rows_that_should_be_excluded(
        notify_db_session):
    valid_service = create_service(service_name='valid service')
    inactive_service = create_service(service_name='inactive', active=False)
    research_mode_service = create_service(service_name='research_mode',
                                           research_mode=True)
    restricted_service = create_service(service_name='restricted',
                                        restricted=True)

    # notification in 'created' state
    create_ft_notification_status(date(2019, 3, 15),
                                  service=valid_service,
                                  notification_status=NOTIFICATION_CREATED)
    # notification created by inactive service
    create_ft_notification_status(date(2019, 3, 15), service=inactive_service)
    # notification created with test key
    create_ft_notification_status(date(2019, 3, 12),
                                  service=valid_service,
                                  key_type=KEY_TYPE_TEST)
    # notification created by research mode service
    create_ft_notification_status(date(2019, 3, 2),
                                  service=research_mode_service)
    # notification created by trial mode service
    create_ft_notification_status(date(2019, 3, 19),
                                  service=restricted_service)
    # notifications outside date range
    create_ft_notification_status(date(2019, 2, 28), service=valid_service)
    create_ft_notification_status(date(2019, 4, 1), service=valid_service)

    results = fetch_monthly_notification_statuses_per_service(
        date(2019, 3, 1), date(2019, 3, 31))
    assert len(results) == 0
Ejemplo n.º 27
0
def test_fetch_monthly_notification_statuses_per_service(notify_db_session):
    service_one = create_service(
        service_name='service one',
        service_id=UUID('e4e34c4e-73c1-4802-811c-3dd273f21da4'))
    service_two = create_service(
        service_name='service two',
        service_id=UUID('b19d7aad-6f09-4198-8b62-f6cf126b87e5'))

    create_ft_notification_status(date(2019, 4, 30),
                                  notification_type='letter',
                                  service=service_one,
                                  notification_status=NOTIFICATION_DELIVERED)
    create_ft_notification_status(date(2019, 3, 1),
                                  notification_type='email',
                                  service=service_one,
                                  notification_status=NOTIFICATION_SENDING,
                                  count=4)
    create_ft_notification_status(
        date(2019, 3, 2),
        notification_type='email',
        service=service_one,
        notification_status=NOTIFICATION_TECHNICAL_FAILURE,
        count=2)
    create_ft_notification_status(date(2019, 3, 7),
                                  notification_type='email',
                                  service=service_one,
                                  notification_status=NOTIFICATION_FAILED,
                                  count=1)
    create_ft_notification_status(
        date(2019, 3, 10),
        notification_type='letter',
        service=service_two,
        notification_status=NOTIFICATION_PERMANENT_FAILURE,
        count=1)
    create_ft_notification_status(
        date(2019, 3, 10),
        notification_type='letter',
        service=service_two,
        notification_status=NOTIFICATION_PERMANENT_FAILURE,
        count=1)
    create_ft_notification_status(date(2019, 3, 13),
                                  notification_type='sms',
                                  service=service_one,
                                  notification_status=NOTIFICATION_SENT,
                                  count=1)
    create_ft_notification_status(
        date(2019, 4, 1),
        notification_type='letter',
        service=service_two,
        notification_status=NOTIFICATION_TEMPORARY_FAILURE,
        count=10)
    create_ft_notification_status(date(2019, 3, 31),
                                  notification_type='letter',
                                  service=service_one,
                                  notification_status=NOTIFICATION_DELIVERED)

    results = fetch_monthly_notification_statuses_per_service(
        date(2019, 3, 1), date(2019, 4, 30))

    assert len(results) == 6
    # column order: date, service_id, service_name, notifaction_type, count_sending, count_delivered,
    # count_technical_failure, count_temporary_failure, count_permanent_failure, count_sent
    assert [x for x in results[0]] == [
        date(2019, 3, 1), service_two.id, 'service two', 'letter', 0, 0, 0, 0,
        2, 0
    ]
    assert [x for x in results[1]] == [
        date(2019, 3, 1), service_one.id, 'service one', 'email', 4, 0, 3, 0,
        0, 0
    ]
    assert [x for x in results[2]] == [
        date(2019, 3, 1), service_one.id, 'service one', 'letter', 0, 1, 0, 0,
        0, 0
    ]
    assert [x for x in results[3]] == [
        date(2019, 3, 1), service_one.id, 'service one', 'sms', 0, 0, 0, 0, 0,
        1
    ]
    assert [x for x in results[4]] == [
        date(2019, 4, 1), service_two.id, 'service two', 'letter', 0, 0, 0, 10,
        0, 0
    ]
    assert [x for x in results[5]] == [
        date(2019, 4, 1), service_one.id, 'service one', 'letter', 0, 1, 0, 0,
        0, 0
    ]
Ejemplo n.º 28
0
def set_up_data():
    service_2 = create_service(service_name='service_2')
    create_template(service=service_2, template_type=LETTER_TYPE)
    service_1 = create_service(service_name='service_1')
    sms_template = create_template(service=service_1, template_type=SMS_TYPE)
    email_template = create_template(service=service_1,
                                     template_type=EMAIL_TYPE)
    create_ft_notification_status(date(2018, 10, 24),
                                  'sms',
                                  service_1,
                                  count=8)
    create_ft_notification_status(date(2018, 10, 26),
                                  'letter',
                                  service_1,
                                  count=5)
    create_ft_notification_status(date(2018, 10, 29),
                                  'sms',
                                  service_1,
                                  count=10)
    create_ft_notification_status(date(2018, 10, 29),
                                  'sms',
                                  service_1,
                                  notification_status='created')
    create_ft_notification_status(date(2018, 10, 29),
                                  'email',
                                  service_1,
                                  count=3)
    create_ft_notification_status(date(2018, 10, 29),
                                  'letter',
                                  service_2,
                                  count=10)

    create_notification(service_1.templates[0],
                        created_at=datetime(2018, 10, 30, 12, 0, 0),
                        status='delivered')
    create_notification(sms_template,
                        created_at=datetime(2018, 10, 31, 11, 0, 0))
    create_notification(sms_template,
                        created_at=datetime(2018, 10, 31, 12, 0, 0),
                        status='delivered')
    create_notification(email_template,
                        created_at=datetime(2018, 10, 31, 13, 0, 0),
                        status='delivered')
    return service_1, service_2
Ejemplo n.º 29
0
def test_fetch_notification_status_by_template_for_service_for_today_and_7_previous_days(
        notify_db_session):
    service_1 = create_service(service_name='service_1')
    sms_template = create_template(template_name='sms Template 1',
                                   service=service_1,
                                   template_type=SMS_TYPE)
    sms_template_2 = create_template(template_name='sms Template 2',
                                     service=service_1,
                                     template_type=SMS_TYPE)
    email_template = create_template(service=service_1,
                                     template_type=EMAIL_TYPE)

    # create unused email template
    create_template(service=service_1, template_type=EMAIL_TYPE)

    create_ft_notification_status(date(2018, 10, 29),
                                  'sms',
                                  service_1,
                                  count=10)
    create_ft_notification_status(date(2018, 10, 29),
                                  'sms',
                                  service_1,
                                  count=11)
    create_ft_notification_status(date(2018, 10, 24),
                                  'sms',
                                  service_1,
                                  count=8)
    create_ft_notification_status(date(2018, 10, 29),
                                  'sms',
                                  service_1,
                                  notification_status='created')
    create_ft_notification_status(date(2018, 10, 29),
                                  'email',
                                  service_1,
                                  count=3)
    create_ft_notification_status(date(2018, 10, 26),
                                  'letter',
                                  service_1,
                                  count=5)

    create_notification(sms_template,
                        created_at=datetime(2018, 10, 31, 11, 0, 0))
    create_notification(sms_template,
                        created_at=datetime(2018, 10, 31, 12, 0, 0),
                        status='delivered')
    create_notification(sms_template_2,
                        created_at=datetime(2018, 10, 31, 12, 0, 0),
                        status='delivered')
    create_notification(email_template,
                        created_at=datetime(2018, 10, 31, 13, 0, 0),
                        status='delivered')

    # too early, shouldn't be included
    create_notification(service_1.templates[0],
                        created_at=datetime(2018, 10, 30, 12, 0, 0),
                        status='delivered')

    results = fetch_notification_status_for_service_for_today_and_7_previous_days(
        service_1.id, by_template=True)

    assert [
        ('email Template Name', False, mock.ANY, 'email', 'delivered', 1),
        ('email Template Name', False, mock.ANY, 'email', 'delivered', 3),
        ('letter Template Name', False, mock.ANY, 'letter', 'delivered', 5),
        ('sms Template 1', False, mock.ANY, 'sms', 'created', 1),
        ('sms Template Name', False, mock.ANY, 'sms', 'created', 1),
        ('sms Template 1', False, mock.ANY, 'sms', 'delivered', 1),
        ('sms Template 2', False, mock.ANY, 'sms', 'delivered', 1),
        ('sms Template Name', False, mock.ANY, 'sms', 'delivered', 10),
        ('sms Template Name', False, mock.ANY, 'sms', 'delivered', 11),
    ] == sorted(results,
                key=lambda x:
                (x.notification_type, x.status, x.template_name, x.count))
Ejemplo n.º 30
0
def test_fetch_monthly_template_usage_for_service(sample_service):
    template_one = create_template(service=sample_service,
                                   template_type='sms',
                                   template_name='a')
    template_two = create_template(service=sample_service,
                                   template_type='email',
                                   template_name='b')
    template_three = create_template(service=sample_service,
                                     template_type='letter',
                                     template_name='c')

    create_ft_notification_status(utc_date=date(2017, 12, 10),
                                  service=sample_service,
                                  template=template_two,
                                  count=3)
    create_ft_notification_status(utc_date=date(2017, 12, 10),
                                  service=sample_service,
                                  template=template_one,
                                  count=6)

    create_ft_notification_status(utc_date=date(2018, 1, 1),
                                  service=sample_service,
                                  template=template_one,
                                  count=4)

    create_ft_notification_status(utc_date=date(2018, 3, 1),
                                  service=sample_service,
                                  template=template_three,
                                  count=5)
    create_notification(template=template_three,
                        created_at=datetime.utcnow() - timedelta(days=1))
    create_notification(template=template_three, created_at=datetime.utcnow())
    results = fetch_monthly_template_usage_for_service(datetime(2017, 4, 1),
                                                       datetime(2018, 3, 31),
                                                       sample_service.id)

    assert len(results) == 4

    assert results[0].template_id == template_one.id
    assert results[0].name == template_one.name
    assert results[0].is_precompiled_letter is False
    assert results[0].template_type == template_one.template_type
    assert results[0].month == 12
    assert results[0].year == 2017
    assert results[0].count == 6
    assert results[1].template_id == template_two.id
    assert results[1].name == template_two.name
    assert results[1].is_precompiled_letter is False
    assert results[1].template_type == template_two.template_type
    assert results[1].month == 12
    assert results[1].year == 2017
    assert results[1].count == 3

    assert results[2].template_id == template_one.id
    assert results[2].name == template_one.name
    assert results[2].is_precompiled_letter is False
    assert results[2].template_type == template_one.template_type
    assert results[2].month == 1
    assert results[2].year == 2018
    assert results[2].count == 4

    assert results[3].template_id == template_three.id
    assert results[3].name == template_three.name
    assert results[3].is_precompiled_letter is False
    assert results[3].template_type == template_three.template_type
    assert results[3].month == 3
    assert results[3].year == 2018
    assert results[3].count == 6