Beispiel #1
0
def test_should_delete_inbound_sms_keyword_according_to_data_retention(
        notify_db_session):
    no_retention_service = create_service(service_name='no retention')
    short_retention_service = create_service(service_name='three days')
    long_retention_service = create_service(service_name='thirty days')

    services = [
        short_retention_service, no_retention_service, long_retention_service
    ]

    create_service_data_retention(long_retention_service,
                                  notification_type='sms',
                                  days_of_retention=30)
    create_service_data_retention(short_retention_service,
                                  notification_type='sms',
                                  days_of_retention=3)
    # email retention doesn't affect anything
    create_service_data_retention(short_retention_service,
                                  notification_type='email',
                                  days_of_retention=4)

    dates = [
        datetime(2017, 6, 5, 3, 59),  # older than three days
        datetime(2017, 6, 1, 3, 59),  # older than seven days
        datetime(2017, 5, 1, 0, 0),  # older than thirty days
    ]

    for date, service in product(dates, services):
        create_inbound_sms_keyword(service, created_at=date)

    deleted_count = delete_inbound_sms_keyword_older_than_retention()

    assert deleted_count == 6
    assert {
        x.created_at
        for x in dao_get_inbound_sms_keyword_for_service(
            short_retention_service.id)
    } == set(dates[:1])
    assert {
        x.created_at
        for x in dao_get_inbound_sms_keyword_for_service(
            no_retention_service.id)
    } == set(dates[:1])
    assert {
        x.created_at
        for x in dao_get_inbound_sms_keyword_for_service(
            long_retention_service.id)
    } == set(dates[:1])
Beispiel #2
0
def test_update_fact_notification_status(notify_db_session):
    first_service = create_service(service_name='First Service')
    first_template = create_template(service=first_service)
    second_service = create_service(service_name='second Service')
    second_template = create_template(service=second_service,
                                      template_type='email')
    third_service = create_service(service_name='third Service')
    third_template = create_template(service=third_service,
                                     template_type='letter')

    create_service_data_retention(second_service, 'email', days_of_retention=3)

    process_day = date.today() - timedelta(days=5)
    with freeze_time(datetime.combine(process_day, time.min)):
        create_notification(template=first_template, status='delivered')

        # 2nd service email has 3 day data retention - data has been moved to history and doesn't exist in notifications
        create_notification_history(template=second_template,
                                    status='temporary-failure')

        create_notification(template=third_template, status='sending')

    # these created notifications from a different day get ignored
    with freeze_time(
            datetime.combine(date.today() - timedelta(days=4), time.min)):
        create_notification(template=first_template)
        create_notification_history(template=second_template)
        create_notification(template=third_template)

    for notification_type in ('letter', 'sms', 'email'):
        data = fetch_notification_status_for_day(
            process_day=process_day, notification_type=notification_type)
        update_fact_notification_status(data=data,
                                        process_day=process_day,
                                        notification_type=notification_type)

    new_fact_data = FactNotificationStatus.query.order_by(
        FactNotificationStatus.bst_date,
        FactNotificationStatus.notification_type).all()

    assert len(new_fact_data) == 3
    assert new_fact_data[0].bst_date == process_day
    assert new_fact_data[0].template_id == second_template.id
    assert new_fact_data[0].service_id == second_service.id
    assert new_fact_data[0].job_id == UUID(
        '00000000-0000-0000-0000-000000000000')
    assert new_fact_data[0].notification_type == 'email'
    assert new_fact_data[0].notification_status == 'temporary-failure'
    assert new_fact_data[0].notification_count == 1

    assert new_fact_data[1].bst_date == process_day
    assert new_fact_data[1].template_id == third_template.id
    assert new_fact_data[1].service_id == third_service.id
    assert new_fact_data[1].job_id == UUID(
        '00000000-0000-0000-0000-000000000000')
    assert new_fact_data[1].notification_type == 'letter'
    assert new_fact_data[1].notification_status == 'sending'
    assert new_fact_data[1].notification_count == 1

    assert new_fact_data[2].bst_date == process_day
    assert new_fact_data[2].template_id == first_template.id
    assert new_fact_data[2].service_id == first_service.id
    assert new_fact_data[2].job_id == UUID(
        '00000000-0000-0000-0000-000000000000')
    assert new_fact_data[2].notification_type == 'sms'
    assert new_fact_data[2].notification_status == 'delivered'
    assert new_fact_data[2].notification_count == 1
Beispiel #3
0
def test_get_uploads_for_service(sample_template):
    create_service_data_retention(sample_template.service,
                                  'sms',
                                  days_of_retention=9)
    job = create_job(sample_template, processing_started=datetime.utcnow())
    letter_template = create_uploaded_template(sample_template.service)
    letter = create_uploaded_letter(letter_template, sample_template.service)

    other_service = create_service(service_name="other service")
    other_template = create_template(service=other_service)
    other_job = create_job(other_template,
                           processing_started=datetime.utcnow())
    other_letter_template = create_uploaded_template(other_service)
    create_uploaded_letter(other_letter_template, other_service)

    uploads_from_db = dao_get_uploads_by_service_id(job.service_id).items
    other_uploads_from_db = dao_get_uploads_by_service_id(
        other_job.service_id).items

    assert len(uploads_from_db) == 2

    assert uploads_from_db[0] == (
        None,
        'Uploaded letters',
        1,
        'letter',
        None,
        letter.created_at.replace(hour=17, minute=30, second=0, microsecond=0),
        None,
        letter.created_at.replace(hour=17, minute=30, second=0, microsecond=0),
        None,
        'letter_day',
        None,
    )
    assert uploads_from_db[1] == (
        job.id,
        job.original_file_name,
        job.notification_count,
        'sms',
        9,
        job.created_at,
        job.scheduled_for,
        job.processing_started,
        job.job_status,
        "job",
        None,
    )

    assert len(other_uploads_from_db) == 2
    assert other_uploads_from_db[0] == (
        None,
        'Uploaded letters',
        1,
        'letter',
        None,
        letter.created_at.replace(hour=17, minute=30, second=0, microsecond=0),
        None,
        letter.created_at.replace(hour=17, minute=30, second=0, microsecond=0),
        None,
        "letter_day",
        None,
    )
    assert other_uploads_from_db[1] == (other_job.id,
                                        other_job.original_file_name,
                                        other_job.notification_count,
                                        other_job.template.template_type, 7,
                                        other_job.created_at,
                                        other_job.scheduled_for,
                                        other_job.processing_started,
                                        other_job.job_status, "job", None)

    assert uploads_from_db[1] != other_uploads_from_db[1]
Beispiel #4
0
def test_get_uploads_only_gets_uploads_within_service_retention_period(
        sample_template):
    letter_template = create_uploaded_template(sample_template.service)
    create_service_data_retention(sample_template.service,
                                  'sms',
                                  days_of_retention=3)

    days_ago = datetime.utcnow() - timedelta(days=4)
    upload_1 = create_uploaded_letter(letter_template,
                                      service=letter_template.service)
    upload_2 = create_job(sample_template,
                          processing_started=datetime.utcnow() -
                          timedelta(days=1),
                          created_at=days_ago,
                          job_status=JOB_STATUS_IN_PROGRESS)
    # older than custom retention for sms:
    create_job(sample_template,
               processing_started=datetime.utcnow() - timedelta(days=5),
               created_at=days_ago,
               job_status=JOB_STATUS_IN_PROGRESS)
    upload_3 = create_uploaded_letter(letter_template,
                                      service=letter_template.service,
                                      created_at=datetime.utcnow() -
                                      timedelta(days=3))

    # older than retention for sms but within letter retention:
    upload_4 = create_uploaded_letter(letter_template,
                                      service=letter_template.service,
                                      created_at=datetime.utcnow() -
                                      timedelta(days=6))

    # older than default retention for letters:
    create_uploaded_letter(letter_template,
                           service=letter_template.service,
                           created_at=datetime.utcnow() - timedelta(days=8))

    results = dao_get_uploads_by_service_id(
        service_id=sample_template.service_id).items

    assert len(results) == 4

    # Uploaded letters get their `created_at` shifted time of printing
    # 17:30 BST == 16:30 UTC
    assert results[0].created_at == upload_1.created_at.replace(hour=16,
                                                                minute=30,
                                                                second=0,
                                                                microsecond=0)

    # Jobs keep their original `created_at`
    assert results[1].created_at == upload_2.created_at.replace(hour=14,
                                                                minute=00,
                                                                second=0,
                                                                microsecond=0)

    # Still in BST here…
    assert results[2].created_at == upload_3.created_at.replace(hour=16,
                                                                minute=30,
                                                                second=0,
                                                                microsecond=0)

    # Now we’ve gone far enough back to be in GMT
    # 17:30 GMT == 17:30 UTC
    assert results[3].created_at == upload_4.created_at.replace(hour=17,
                                                                minute=30,
                                                                second=0,
                                                                microsecond=0)
Beispiel #5
0
def test_get_uploads_for_service(sample_template):
    create_service_data_retention(sample_template.service,
                                  'sms',
                                  days_of_retention=9)
    job = create_job(sample_template, processing_started=datetime.utcnow())
    letter_template = create_uploaded_template(sample_template.service)
    letter = create_uploaded_letter(letter_template, sample_template.service)

    other_service = create_service(service_name="other service")
    other_template = create_template(service=other_service)
    other_job = create_job(other_template,
                           processing_started=datetime.utcnow())
    other_letter_template = create_uploaded_template(other_service)
    other_letter = create_uploaded_letter(other_letter_template, other_service)

    uploads_from_db = dao_get_uploads_by_service_id(job.service_id).items
    other_uploads_from_db = dao_get_uploads_by_service_id(
        other_job.service_id).items

    assert len(uploads_from_db) == 2

    assert uploads_from_db[0] == (
        letter.id,
        letter.client_reference,
        1,
        None,
        7,
        letter.created_at,
        None,
        letter.created_at,
        letter.status,
        "letter",
        "file-name",
    )
    assert uploads_from_db[1] == (
        job.id,
        job.original_file_name,
        job.notification_count,
        'sms',
        9,
        job.created_at,
        job.scheduled_for,
        job.processing_started,
        job.job_status,
        "job",
        None,
    )

    assert len(other_uploads_from_db) == 2
    assert other_uploads_from_db[0] == (other_letter.id,
                                        other_letter.client_reference, 1, None,
                                        7, other_letter.created_at, None,
                                        other_letter.created_at,
                                        other_letter.status, "letter",
                                        "file-name")
    assert other_uploads_from_db[1] == (other_job.id,
                                        other_job.original_file_name,
                                        other_job.notification_count,
                                        other_job.template.template_type, 7,
                                        other_job.created_at,
                                        other_job.scheduled_for,
                                        other_job.processing_started,
                                        other_job.job_status, "job", None)

    assert uploads_from_db[0] != other_uploads_from_db[0]
    assert uploads_from_db[1] != other_uploads_from_db[1]
def test_fetch_service_data_retention_by_notification_type(sample_service, notification_type, alternate):
    data_retention = create_service_data_retention(service=sample_service, notification_type=notification_type)
    create_service_data_retention(service=sample_service, notification_type=alternate)
    result = fetch_service_data_retention_by_notification_type(sample_service.id, notification_type)
    assert result == data_retention