def test_set_scheduled_jobs_to_pending_gets_ignores_jobs_not_scheduled(notify_db, notify_db_session):
    one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
    create_job(notify_db, notify_db_session)
    job_scheduled = create_job(notify_db, notify_db_session, scheduled_for=one_minute_ago, job_status='scheduled')
    jobs = dao_set_scheduled_jobs_to_pending()
    assert len(jobs) == 1
    assert jobs[0].id == job_scheduled.id
Esempio n. 2
0
def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
    one_job = create_job(notify_db, notify_db_session, sample_template.service,
                         sample_template)

    other_user = create_user(email="*****@*****.**")
    other_service = create_service(notify_db,
                                   notify_db_session,
                                   user=other_user,
                                   service_name="other service",
                                   email_from='other.service')
    other_template = create_template(notify_db,
                                     notify_db_session,
                                     service=other_service)
    other_job = create_job(notify_db,
                           notify_db_session,
                           service=other_service,
                           template=other_template)

    one_job_from_db = dao_get_jobs_by_service_id(one_job.service_id).items
    other_job_from_db = dao_get_jobs_by_service_id(other_job.service_id).items

    assert len(one_job_from_db) == 1
    assert one_job == one_job_from_db[0]

    assert len(other_job_from_db) == 1
    assert other_job == other_job_from_db[0]

    assert one_job_from_db != other_job_from_db
Esempio n. 3
0
def test_should_return_notifications_only_for_this_service(
        notify_db, notify_db_session):
    service_1 = create_service(notify_db,
                               notify_db_session,
                               service_name="one",
                               email_from="one")
    service_2 = create_service(notify_db,
                               notify_db_session,
                               service_name="two",
                               email_from="two")

    job_1 = create_job(notify_db, notify_db_session, service=service_1)
    job_2 = create_job(notify_db, notify_db_session, service=service_2)

    create_notification(notify_db,
                        notify_db_session,
                        service=service_1,
                        job=job_1,
                        status='created')
    create_notification(notify_db,
                        notify_db_session,
                        service=service_2,
                        job=job_2,
                        status='created')

    assert len(dao_get_notification_outcomes_for_job(service_1.id,
                                                     job_2.id)) == 0
Esempio n. 4
0
def test_get_jobs_with_limit_days(notify_api, notify_db, notify_db_session,
                                  sample_template):
    create_job(
        notify_db,
        notify_db_session,
        service=sample_template.service,
        template=sample_template,
    )
    create_job(notify_db,
               notify_db_session,
               service=sample_template.service,
               template=sample_template,
               created_at=datetime.now() - timedelta(days=7))

    service_id = sample_template.service.id

    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            path = '/service/{}/job'.format(service_id)
            auth_header = create_authorization_header()
            response = client.get(path,
                                  headers=[auth_header],
                                  query_string={'limit_days': 5})
            assert response.status_code == 200
            resp_json = json.loads(response.get_data(as_text=True))
            assert len(resp_json['data']) == 1
Esempio n. 5
0
def test_get_jobs_for_service_with_limit_days_edge_case(
        notify_db, notify_db_session, sample_template):
    one_job = create_job(notify_db, notify_db_session, sample_template.service,
                         sample_template)
    job_two = create_job(notify_db,
                         notify_db_session,
                         sample_template.service,
                         sample_template,
                         created_at=(datetime.now() -
                                     timedelta(days=7)).date())
    one_second_after_midnight = datetime.combine(
        (datetime.now() - timedelta(days=7)).date(),
        datetime.strptime("000001", "%H%M%S").time())
    just_after_midnight_job = create_job(notify_db,
                                         notify_db_session,
                                         sample_template.service,
                                         sample_template,
                                         created_at=one_second_after_midnight)
    job_eight_days_old = create_job(notify_db,
                                    notify_db_session,
                                    sample_template.service,
                                    sample_template,
                                    created_at=datetime.now() -
                                    timedelta(days=8))

    jobs_limit_days = dao_get_jobs_by_service_id(one_job.service_id,
                                                 limit_days=7).items
    assert len(jobs_limit_days) == 3
    assert one_job in jobs_limit_days
    assert job_two in jobs_limit_days
    assert just_after_midnight_job in jobs_limit_days
    assert job_eight_days_old not in jobs_limit_days
Esempio n. 6
0
def test_get_jobs_should_return_statistics(notify_db, notify_db_session, notify_api, sample_service):
    now = datetime.utcnow()
    earlier = datetime.utcnow() - timedelta(days=1)
    job_1 = create_job(notify_db, notify_db_session, service=sample_service, created_at=earlier)
    job_2 = create_job(notify_db, notify_db_session, service=sample_service, created_at=now)

    create_notification(notify_db, notify_db_session, service=sample_service, job=job_1, status='created')
    create_notification(notify_db, notify_db_session, service=sample_service, job=job_1, status='created')
    create_notification(notify_db, notify_db_session, service=sample_service, job=job_1, status='created')
    create_notification(notify_db, notify_db_session, service=sample_service, job=job_2, status='sending')
    create_notification(notify_db, notify_db_session, service=sample_service, job=job_2, status='sending')
    create_notification(notify_db, notify_db_session, service=sample_service, job=job_2, status='sending')

    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            path = '/service/{}/job'.format(sample_service.id)
            auth_header = create_authorization_header(service_id=str(sample_service.id))
            response = client.get(path, headers=[auth_header])
            assert response.status_code == 200
            resp_json = json.loads(response.get_data(as_text=True))
            assert len(resp_json['data']) == 2
            assert resp_json['data'][0]['id'] == str(job_2.id)
            assert {'status': 'sending', 'count': 3} in resp_json['data'][0]['statistics']
            assert resp_json['data'][1]['id'] == str(job_1.id)
            assert {'status': 'created', 'count': 3} in resp_json['data'][1]['statistics']
Esempio n. 7
0
def test_get_jobs_should_return_no_stats_if_no_rows_in_notifications(
        notify_db, notify_db_session, notify_api, sample_service):

    now = datetime.utcnow()
    earlier = datetime.utcnow() - timedelta(days=1)
    job_1 = create_job(notify_db,
                       notify_db_session,
                       service=sample_service,
                       created_at=earlier)
    job_2 = create_job(notify_db,
                       notify_db_session,
                       service=sample_service,
                       created_at=now)

    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            path = '/service/{}/job'.format(sample_service.id)
            auth_header = create_authorization_header()
            response = client.get(path, headers=[auth_header])
            assert response.status_code == 200
            resp_json = json.loads(response.get_data(as_text=True))
            assert len(resp_json['data']) == 2
            assert resp_json['data'][0]['id'] == str(job_2.id)
            assert resp_json['data'][0]['statistics'] == []
            assert resp_json['data'][1]['id'] == str(job_1.id)
            assert resp_json['data'][1]['statistics'] == []
def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):

    from tests.app.conftest import sample_job as create_job
    from tests.app.conftest import sample_service as create_service
    from tests.app.conftest import sample_template as create_template
    from tests.app.conftest import sample_user as create_user

    one_job = create_job(notify_db, notify_db_session, sample_template.service,
                         sample_template)

    other_user = create_user(notify_db, notify_db_session,
                             email="*****@*****.**")
    other_service = create_service(notify_db, notify_db_session,
                                   user=other_user, service_name="other service")
    other_template = create_template(notify_db, notify_db_session,
                                     service=other_service)
    other_job = create_job(notify_db, notify_db_session, service=other_service,
                           template=other_template)

    one_job_from_db = get_jobs_by_service(one_job.service_id)
    other_job_from_db = get_jobs_by_service(other_job.service_id)

    assert len(one_job_from_db) == 1
    assert one_job == one_job_from_db[0]

    assert len(other_job_from_db) == 1
    assert other_job == other_job_from_db[0]

    assert one_job_from_db != other_job_from_db
Esempio n. 9
0
def _setup_jobs(notify_db, notify_db_session, template, number_of_jobs=5):
    for i in range(number_of_jobs):
        create_job(
            notify_db,
            notify_db_session,
            service=template.service,
            template=template)
Esempio n. 10
0
def test_set_scheduled_jobs_to_pending_gets_all_jobs_in_scheduled_state_before_now(notify_db, notify_db_session):
    one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
    one_hour_ago = datetime.utcnow() - timedelta(minutes=60)
    job_new = create_job(notify_db, notify_db_session, scheduled_for=one_minute_ago, job_status='scheduled')
    job_old = create_job(notify_db, notify_db_session, scheduled_for=one_hour_ago, job_status='scheduled')
    jobs = dao_set_scheduled_jobs_to_pending()
    assert len(jobs) == 2
    assert jobs[0].id == job_old.id
    assert jobs[1].id == job_new.id
def test_get_all_jobs(notify_db, notify_db_session, sample_template):
    from tests.app.conftest import sample_job as create_job
    for i in range(5):
        create_job(notify_db,
                   notify_db_session,
                   sample_template.service,
                   sample_template)
    jobs_from_db = _get_jobs()
    assert len(jobs_from_db) == 5
Esempio n. 12
0
def test_set_scheduled_jobs_to_pending_updates_rows(notify_db, notify_db_session):
    one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
    one_hour_ago = datetime.utcnow() - timedelta(minutes=60)
    create_job(notify_db, notify_db_session, scheduled_for=one_minute_ago, job_status='scheduled')
    create_job(notify_db, notify_db_session, scheduled_for=one_hour_ago, job_status='scheduled')
    jobs = dao_set_scheduled_jobs_to_pending()
    assert len(jobs) == 2
    assert jobs[0].job_status == 'pending'
    assert jobs[1].job_status == 'pending'
Esempio n. 13
0
def test_set_scheduled_jobs_to_pending_gets_ignores_jobs_not_scheduled(
        notify_db, notify_db_session):
    one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
    create_job(notify_db, notify_db_session)
    job_scheduled = create_job(notify_db,
                               notify_db_session,
                               scheduled_for=one_minute_ago,
                               job_status='scheduled')
    jobs = dao_set_scheduled_jobs_to_pending()
    assert len(jobs) == 1
    assert jobs[0].id == job_scheduled.id
Esempio n. 14
0
def test_should_return_notifications_only_for_this_service(notify_db, notify_db_session):
    service_1 = create_service(notify_db, notify_db_session, service_name="one", email_from="one")
    service_2 = create_service(notify_db, notify_db_session, service_name="two", email_from="two")

    job_1 = create_job(notify_db, notify_db_session, service=service_1)
    job_2 = create_job(notify_db, notify_db_session, service=service_2)

    create_notification(notify_db, notify_db_session, service=service_1, job=job_1, status='created')
    create_notification(notify_db, notify_db_session, service=service_2, job=job_2, status='created')

    assert len(dao_get_notification_outcomes_for_job(service_1.id, job_2.id)) == 0
Esempio n. 15
0
def test_should_return_notifications_only_for_this_job(notify_db, notify_db_session, sample_service):
    job_1 = create_job(notify_db, notify_db_session, service=sample_service)
    job_2 = create_job(notify_db, notify_db_session, service=sample_service)

    create_notification(notify_db, notify_db_session, service=sample_service, job=job_1, status='created')
    create_notification(notify_db, notify_db_session, service=sample_service, job=job_2, status='created')

    results = dao_get_notification_outcomes_for_job(sample_service.id, job_1.id)
    assert [(row.count, row.status) for row in results] == [
        (1, 'created')
    ]
Esempio n. 16
0
def test_should_get_jobs_older_than_seven_days(notify_db, notify_db_session):
    one_millisecond_before_midnight = datetime(2016, 10, 9, 23, 59, 59, 999)
    midnight = datetime(2016, 10, 10, 0, 0, 0, 0)
    one_millisecond_past_midnight = datetime(2016, 10, 10, 0, 0, 0, 1)

    job_1 = create_job(notify_db, notify_db_session, created_at=one_millisecond_before_midnight)
    create_job(notify_db, notify_db_session, created_at=midnight)
    create_job(notify_db, notify_db_session, created_at=one_millisecond_past_midnight)

    with freeze_time('2016-10-17T00:00:00'):
        jobs = dao_get_jobs_older_than(7)
        assert len(jobs) == 1
        assert jobs[0].id == job_1.id
Esempio n. 17
0
def test_get_all_notifications_for_job_in_order_of_job_number(notify_api,
                                                              notify_db,
                                                              notify_db_session,
                                                              sample_service):
    with notify_api.test_request_context(), notify_api.test_client() as client:
        main_job = create_job(notify_db, notify_db_session, service=sample_service)
        another_job = create_job(notify_db, notify_db_session, service=sample_service)

        notification_1 = create_notification(
            notify_db,
            notify_db_session,
            job=main_job,
            to_field="1",
            created_at=datetime.utcnow(),
            job_row_number=1
        )
        notification_2 = create_notification(
            notify_db,
            notify_db_session,
            job=main_job,
            to_field="2",
            created_at=datetime.utcnow(),
            job_row_number=2
        )
        notification_3 = create_notification(
            notify_db,
            notify_db_session,
            job=main_job,
            to_field="3",
            created_at=datetime.utcnow(),
            job_row_number=3
        )
        create_notification(notify_db, notify_db_session, job=another_job)

        auth_header = create_authorization_header()

        response = client.get(
            path='/service/{}/job/{}/notifications'.format(sample_service.id, main_job.id),
            headers=[auth_header])

        resp = json.loads(response.get_data(as_text=True))
        assert len(resp['notifications']) == 3
        assert resp['notifications'][0]['to'] == notification_1.to
        assert resp['notifications'][0]['job_row_number'] == notification_1.job_row_number
        assert resp['notifications'][1]['to'] == notification_2.to
        assert resp['notifications'][1]['job_row_number'] == notification_2.job_row_number
        assert resp['notifications'][2]['to'] == notification_3.to
        assert resp['notifications'][2]['job_row_number'] == notification_3.job_row_number
        assert response.status_code == 200
Esempio n. 18
0
def test_get_jobs_for_service_with_limit_days_param(notify_db, notify_db_session, sample_template):
    one_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template)
    old_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template,
                         created_at=datetime.now() - timedelta(days=8))

    jobs = dao_get_jobs_by_service_id(one_job.service_id).items

    assert len(jobs) == 2
    assert one_job in jobs
    assert old_job in jobs

    jobs_limit_days = dao_get_jobs_by_service_id(one_job.service_id, limit_days=7).items
    assert len(jobs_limit_days) == 1
    assert one_job in jobs_limit_days
    assert old_job not in jobs_limit_days
Esempio n. 19
0
def test_get_all_notifications_for_job_in_order_of_job_number(
        notify_api, notify_db, notify_db_session, sample_service):
    with notify_api.test_request_context(), notify_api.test_client() as client:
        main_job = create_job(notify_db,
                              notify_db_session,
                              service=sample_service)
        another_job = create_job(notify_db,
                                 notify_db_session,
                                 service=sample_service)

        notification_1 = create_notification(notify_db,
                                             notify_db_session,
                                             job=main_job,
                                             to_field="0412345678",
                                             created_at=datetime.utcnow(),
                                             job_row_number=1)
        notification_2 = create_notification(notify_db,
                                             notify_db_session,
                                             job=main_job,
                                             to_field="0412345677",
                                             created_at=datetime.utcnow(),
                                             job_row_number=2)
        notification_3 = create_notification(notify_db,
                                             notify_db_session,
                                             job=main_job,
                                             to_field="0412345676",
                                             created_at=datetime.utcnow(),
                                             job_row_number=3)
        create_notification(notify_db, notify_db_session, job=another_job)

        auth_header = create_authorization_header()

        response = client.get(path='/service/{}/job/{}/notifications'.format(
            sample_service.id, main_job.id),
                              headers=[auth_header])

        resp = json.loads(response.get_data(as_text=True))
        assert len(resp['notifications']) == 3
        assert resp['notifications'][0]['to'] == notification_1.to
        assert resp['notifications'][0][
            'job_row_number'] == notification_1.job_row_number
        assert resp['notifications'][1]['to'] == notification_2.to
        assert resp['notifications'][1][
            'job_row_number'] == notification_2.job_row_number
        assert resp['notifications'][2]['to'] == notification_3.to
        assert resp['notifications'][2][
            'job_row_number'] == notification_3.job_row_number
        assert response.status_code == 200
Esempio n. 20
0
def test_set_scheduled_jobs_to_pending_updates_rows(notify_db,
                                                    notify_db_session):
    one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
    one_hour_ago = datetime.utcnow() - timedelta(minutes=60)
    create_job(notify_db,
               notify_db_session,
               scheduled_for=one_minute_ago,
               job_status='scheduled')
    create_job(notify_db,
               notify_db_session,
               scheduled_for=one_hour_ago,
               job_status='scheduled')
    jobs = dao_set_scheduled_jobs_to_pending()
    assert len(jobs) == 2
    assert jobs[0].job_status == 'pending'
    assert jobs[1].job_status == 'pending'
Esempio n. 21
0
def test_get_all_notifications_for_job_returns_csv_format(
    client,
    notify_db,
    notify_db_session,
):
    job = create_job(notify_db, notify_db_session)
    notification = create_notification(
        notify_db,
        notify_db_session,
        job=job,
        job_row_number=1,
        created_at=datetime.utcnow(),
    )

    path = '/service/{}/job/{}/notifications'.format(notification.service.id,
                                                     job.id)

    response = client.get(path=path,
                          headers=[create_authorization_header()],
                          query_string={'format_for_csv': True})
    assert response.status_code == 200

    resp = json.loads(response.get_data(as_text=True))
    assert len(resp['notifications']) == 1
    notification = resp['notifications'][0]
    assert set(notification.keys()) == \
        set(['created_at', 'template_type', 'template_name', 'job_name', 'status', 'row_number', 'recipient'])
Esempio n. 22
0
def test_get_all_notifications_for_job_filtered_by_status(
        notify_api,
        notify_db,
        notify_db_session,
        sample_service,
        expected_notification_count,
        status_args
):
    with notify_api.test_request_context(), notify_api.test_client() as client:
        job = create_job(notify_db, notify_db_session, service=sample_service)

        create_notification(
            notify_db,
            notify_db_session,
            job=job,
            to_field="1",
            created_at=datetime.utcnow(),
            status=NOTIFICATION_STATUS_TYPES[0],
            job_row_number=1
        )

        response = client.get(
            path='/service/{}/job/{}/notifications{}'.format(sample_service.id, job.id, status_args),
            headers=[create_authorization_header()]
        )
        resp = json.loads(response.get_data(as_text=True))
        assert len(resp['notifications']) == expected_notification_count
        assert response.status_code == 200
Esempio n. 23
0
def test_set_scheduled_jobs_to_pending_gets_all_jobs_in_scheduled_state_before_now(
        notify_db, notify_db_session):
    one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
    one_hour_ago = datetime.utcnow() - timedelta(minutes=60)
    job_new = create_job(notify_db,
                         notify_db_session,
                         scheduled_for=one_minute_ago,
                         job_status='scheduled')
    job_old = create_job(notify_db,
                         notify_db_session,
                         scheduled_for=one_hour_ago,
                         job_status='scheduled')
    jobs = dao_set_scheduled_jobs_to_pending()
    assert len(jobs) == 2
    assert jobs[0].id == job_old.id
    assert jobs[1].id == job_new.id
Esempio n. 24
0
def test_get_jobs_for_service_with_limit_days_edge_case(notify_db, notify_db_session, sample_template):
    one_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template)
    job_two = create_job(notify_db, notify_db_session, sample_template.service, sample_template,
                         created_at=(datetime.now() - timedelta(days=7)).date())
    one_second_after_midnight = datetime.combine((datetime.now() - timedelta(days=7)).date(),
                                                 datetime.strptime("000001", "%H%M%S").time())
    just_after_midnight_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template,
                                         created_at=one_second_after_midnight)
    job_eight_days_old = create_job(notify_db, notify_db_session, sample_template.service, sample_template,
                                    created_at=datetime.now() - timedelta(days=8))

    jobs_limit_days = dao_get_jobs_by_service_id(one_job.service_id, limit_days=7).items
    assert len(jobs_limit_days) == 3
    assert one_job in jobs_limit_days
    assert job_two in jobs_limit_days
    assert just_after_midnight_job in jobs_limit_days
    assert job_eight_days_old not in jobs_limit_days
Esempio n. 25
0
def test_get_jobs_for_service_doesnt_return_test_messages(
    notify_db,
    notify_db_session,
    sample_template,
    sample_job,
    file_name,
):
    create_job(
        notify_db,
        notify_db_session,
        sample_template.service,
        sample_template,
        original_file_name=file_name,
    )

    jobs = dao_get_jobs_by_service_id(sample_job.service_id).items

    assert jobs == [sample_job]
Esempio n. 26
0
def test_get_jobs_for_service_is_paginated(notify_db, notify_db_session, sample_service, sample_template):
    with freeze_time('2015-01-01T00:00:00') as the_time:
        for _ in range(10):
            the_time.tick(timedelta(hours=1))
            create_job(notify_db, notify_db_session, sample_service, sample_template)

    res = dao_get_jobs_by_service_id(sample_service.id, page=1, page_size=2)

    assert res.per_page == 2
    assert res.total == 10
    assert len(res.items) == 2
    assert res.items[0].created_at == datetime(2015, 1, 1, 10)
    assert res.items[1].created_at == datetime(2015, 1, 1, 9)

    res = dao_get_jobs_by_service_id(sample_service.id, page=2, page_size=2)

    assert len(res.items) == 2
    assert res.items[0].created_at == datetime(2015, 1, 1, 8)
    assert res.items[1].created_at == datetime(2015, 1, 1, 7)
Esempio n. 27
0
def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
    one_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template)

    other_user = create_user(notify_db, notify_db_session, email="*****@*****.**")
    other_service = create_service(notify_db, notify_db_session, user=other_user, service_name="other service",
                                   email_from='other.service')
    other_template = create_template(notify_db, notify_db_session, service=other_service)
    other_job = create_job(notify_db, notify_db_session, service=other_service, template=other_template)

    one_job_from_db = dao_get_jobs_by_service_id(one_job.service_id).items
    other_job_from_db = dao_get_jobs_by_service_id(other_job.service_id).items

    assert len(one_job_from_db) == 1
    assert one_job == one_job_from_db[0]

    assert len(other_job_from_db) == 1
    assert other_job == other_job_from_db[0]

    assert one_job_from_db != other_job_from_db
Esempio n. 28
0
def test_get_jobs_should_return_statistics(client, notify_db,
                                           notify_db_session, notify_api,
                                           sample_service):
    now = datetime.utcnow()
    earlier = datetime.utcnow() - timedelta(days=1)
    job_1 = create_job(notify_db,
                       notify_db_session,
                       service=sample_service,
                       created_at=earlier)
    job_2 = create_job(notify_db,
                       notify_db_session,
                       service=sample_service,
                       created_at=now)
    partial_notification = partial(create_notification,
                                   notify_db,
                                   notify_db_session,
                                   service=sample_service)
    partial_notification(job=job_1, status='created')
    partial_notification(job=job_1, status='created')
    partial_notification(job=job_1, status='created')
    partial_notification(job=job_2, status='sending')
    partial_notification(job=job_2, status='sending')
    partial_notification(job=job_2, status='sending')

    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            path = '/service/{}/job'.format(sample_service.id)
            auth_header = create_authorization_header()
            response = client.get(path, headers=[auth_header])
            assert response.status_code == 200
            resp_json = json.loads(response.get_data(as_text=True))
            assert len(resp_json['data']) == 2
            assert resp_json['data'][0]['id'] == str(job_2.id)
            assert {
                'status': 'sending',
                'count': 3
            } in resp_json['data'][0]['statistics']
            assert resp_json['data'][1]['id'] == str(job_1.id)
            assert {
                'status': 'created',
                'count': 3
            } in resp_json['data'][1]['statistics']
Esempio n. 29
0
def test_should_return_notifications_only_for_this_job(notify_db,
                                                       notify_db_session,
                                                       sample_service):
    job_1 = create_job(notify_db, notify_db_session, service=sample_service)
    job_2 = create_job(notify_db, notify_db_session, service=sample_service)

    create_notification(notify_db,
                        notify_db_session,
                        service=sample_service,
                        job=job_1,
                        status='created')
    create_notification(notify_db,
                        notify_db_session,
                        service=sample_service,
                        job=job_2,
                        status='created')

    results = dao_get_notification_outcomes_for_job(sample_service.id,
                                                    job_1.id)
    assert [(row.count, row.status) for row in results] == [(1, 'created')]
Esempio n. 30
0
def test_get_jobs_for_service_doesnt_return_test_messages(notify_db, notify_db_session, sample_template, sample_job):
    test_job = create_job(
        notify_db,
        notify_db_session,
        sample_template.service,
        sample_template,
        original_file_name='Test message')

    jobs = dao_get_jobs_by_service_id(sample_job.service_id).items

    assert jobs == [sample_job]
Esempio n. 31
0
def test_get_jobs_for_service_is_paginated(notify_db, notify_db_session,
                                           sample_service, sample_template):
    with freeze_time('2015-01-01T00:00:00') as the_time:
        for _ in range(10):
            the_time.tick(timedelta(hours=1))
            create_job(notify_db, notify_db_session, sample_service,
                       sample_template)

    res = dao_get_jobs_by_service_id(sample_service.id, page=1, page_size=2)

    assert res.per_page == 2
    assert res.total == 10
    assert len(res.items) == 2
    assert res.items[0].created_at == datetime(2015, 1, 1, 10)
    assert res.items[1].created_at == datetime(2015, 1, 1, 9)

    res = dao_get_jobs_by_service_id(sample_service.id, page=2, page_size=2)

    assert len(res.items) == 2
    assert res.items[0].created_at == datetime(2015, 1, 1, 8)
    assert res.items[1].created_at == datetime(2015, 1, 1, 7)
Esempio n. 32
0
def test_get_jobs_with_limit_days(admin_request, notify_db, notify_db_session,
                                  sample_template):
    for time in [
            'Sunday 1st July 2018 22:59',
            'Sunday 2nd July 2018 23:00',  # beginning of monday morning
            'Monday 3rd July 2018 12:00'
    ]:
        with freeze_time(time):
            create_job(
                notify_db,
                notify_db_session,
                service=sample_template.service,
                template=sample_template,
            )

    with freeze_time('Monday 9th July 2018 12:00'):
        resp_json = admin_request.get('job.get_jobs_by_service',
                                      service_id=sample_template.service_id,
                                      limit_days=7)

    assert len(resp_json['data']) == 2
Esempio n. 33
0
def test_get_jobs_for_service_with_limit_days_param(notify_db,
                                                    notify_db_session,
                                                    sample_template):
    one_job = create_job(notify_db, notify_db_session, sample_template.service,
                         sample_template)
    old_job = create_job(notify_db,
                         notify_db_session,
                         sample_template.service,
                         sample_template,
                         created_at=datetime.now() - timedelta(days=8))

    jobs = dao_get_jobs_by_service_id(one_job.service_id).items

    assert len(jobs) == 2
    assert one_job in jobs
    assert old_job in jobs

    jobs_limit_days = dao_get_jobs_by_service_id(one_job.service_id,
                                                 limit_days=7).items
    assert len(jobs_limit_days) == 1
    assert one_job in jobs_limit_days
    assert old_job not in jobs_limit_days
Esempio n. 34
0
def test_get_jobs(notify_db, notify_db_session, sample_template):
    one_job = create_job(notify_db, notify_db_session, sample_template.service,
                         sample_template)

    other_user = create_user(email="*****@*****.**")
    other_service = create_service(notify_db,
                                   notify_db_session,
                                   user=other_user,
                                   service_name="other service",
                                   email_from='other.service')
    other_template = create_template(notify_db,
                                     notify_db_session,
                                     service=other_service)
    other_job = create_job(notify_db,
                           notify_db_session,
                           service=other_service,
                           template=other_template)

    jobs = dao_get_jobs().all()

    assert len(jobs) == 2
    assert jobs[0] == other_job
    assert jobs[1] == one_job
Esempio n. 35
0
def test_get_jobs_with_limit_days(notify_api, notify_db, notify_db_session, sample_template):
    create_job(
        notify_db,
        notify_db_session,
        service=sample_template.service,
        template=sample_template,
    )
    create_job(
        notify_db,
        notify_db_session,
        service=sample_template.service,
        template=sample_template,
        created_at=datetime.now() - timedelta(days=7))

    service_id = sample_template.service.id

    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            path = '/service/{}/job'.format(service_id)
            auth_header = create_authorization_header(service_id=service_id)
            response = client.get(path, headers=[auth_header], query_string={'limit_days': 5})
            assert response.status_code == 200
            resp_json = json.loads(response.get_data(as_text=True))
            assert len(resp_json['data']) == 1
Esempio n. 36
0
def test_get_all_notifications_for_job_filtered_by_status(
        client, notify_db, notify_db_session, sample_service,
        expected_notification_count, status_args):
    job = create_job(notify_db, notify_db_session, service=sample_service)

    create_notification(notify_db,
                        notify_db_session,
                        job=job,
                        to_field="1",
                        created_at=datetime.utcnow(),
                        status=NOTIFICATION_STATUS_TYPES[0],
                        job_row_number=1)

    response = client.get(path='/service/{}/job/{}/notifications{}'.format(
        sample_service.id, job.id, status_args),
                          headers=[create_authorization_header()])
    resp = json.loads(response.get_data(as_text=True))
    assert len(resp['notifications']) == expected_notification_count
    assert response.status_code == 200
Esempio n. 37
0
def _setup_jobs(notify_db, notify_db_session, template, number_of_jobs=5):
    for i in range(number_of_jobs):
        create_job(notify_db,
                   notify_db_session,
                   service=template.service,
                   template=template)
Esempio n. 38
0
def create_10_jobs(db, session, service, template):
    with freeze_time('2015-01-01T00:00:00') as the_time:
        for _ in range(10):
            the_time.tick(timedelta(hours=1))
            create_job(db, session, service, template)
Esempio n. 39
0
def create_10_jobs(db, session, service, template):
    with freeze_time('2015-01-01T00:00:00') as the_time:
        for _ in range(10):
            the_time.tick(timedelta(hours=1))
            create_job(db, session, service, template)
Esempio n. 40
0
def test_get_jobs_can_filter_on_statuses(notify_db, notify_db_session, client,
                                         sample_service, statuses_filter,
                                         expected_statuses):
    create_job(notify_db, notify_db_session, job_status='pending')
    create_job(notify_db, notify_db_session, job_status='in progress')
    create_job(notify_db, notify_db_session, job_status='finished')
    create_job(notify_db,
               notify_db_session,
               job_status='sending limits exceeded')
    create_job(notify_db, notify_db_session, job_status='scheduled')
    create_job(notify_db, notify_db_session, job_status='cancelled')
    create_job(notify_db, notify_db_session, job_status='ready to send')
    create_job(notify_db, notify_db_session, job_status='sent to dvla')
    create_job(notify_db, notify_db_session, job_status='error')

    path = '/service/{}/job'.format(sample_service.id)
    response = client.get(path,
                          headers=[create_authorization_header()],
                          query_string={'statuses': statuses_filter})

    assert response.status_code == 200
    resp_json = json.loads(response.get_data(as_text=True))
    assert {x['job_status']
            for x in resp_json['data']} == set(expected_statuses)
Esempio n. 41
0
def test_get_jobs_can_filter_on_statuses(
        notify_db,
        notify_db_session,
        client,
        sample_service,
        statuses_filter,
        expected_statuses
):
    create_job(notify_db, notify_db_session, job_status='pending')
    create_job(notify_db, notify_db_session, job_status='in progress')
    create_job(notify_db, notify_db_session, job_status='finished')
    create_job(notify_db, notify_db_session, job_status='sending limits exceeded')
    create_job(notify_db, notify_db_session, job_status='scheduled')
    create_job(notify_db, notify_db_session, job_status='cancelled')

    path = '/service/{}/job'.format(sample_service.id)
    response = client.get(
        path,
        headers=[create_authorization_header()],
        query_string={'statuses': statuses_filter}
    )

    assert response.status_code == 200
    resp_json = json.loads(response.get_data(as_text=True))
    from pprint import pprint
    pprint(resp_json)
    assert {x['job_status'] for x in resp_json['data']} == set(expected_statuses)