def test_get_all_notifications_filter_by_template_type(client, notify_db, notify_db_session):
    email_template = create_sample_template(notify_db, notify_db_session, template_type="email")
    sms_template = create_sample_template(notify_db, notify_db_session, template_type="sms")

    notification = create_sample_notification(
        notify_db, notify_db_session, template=email_template, to_field="*****@*****.**")
    create_sample_notification(notify_db, notify_db_session, template=sms_template)

    auth_header = create_authorization_header(service_id=notification.service_id)
    response = client.get(
        path='/v2/notifications?template_type=email',
        headers=[('Content-Type', 'application/json'), auth_header])

    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 200
    assert response.headers['Content-type'] == "application/json"
    assert json_response['links']['current'].endswith("/v2/notifications?template_type=email")
    assert 'next' in json_response['links'].keys()
    assert len(json_response['notifications']) == 1

    assert json_response['notifications'][0]['id'] == str(notification.id)
    assert json_response['notifications'][0]['status'] == "created"
    assert json_response['notifications'][0]['template'] == {
        'id': str(email_template.id),
        'uri': email_template.get_link(),
        'version': 1
    }
    assert json_response['notifications'][0]['email_address'] == "*****@*****.**"
    assert json_response['notifications'][0]['type'] == "email"
示例#2
0
def test_get_all_templates_for_service_is_alphabetised(notify_db, notify_db_session, sample_service):
    create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 1',
        template_type="sms",
        content="Template content",
        service=sample_service
    )
    template_2 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 2',
        template_type="sms",
        content="Template content",
        service=sample_service
    )
    create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 3',
        template_type="sms",
        content="Template content",
        service=sample_service
    )

    assert Template.query.count() == 3
    assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template 1'
    assert dao_get_all_templates_for_service(sample_service.id)[1].name == 'Sample Template 2'
    assert dao_get_all_templates_for_service(sample_service.id)[2].name == 'Sample Template 3'

    template_2.name = 'AAAAA Sample Template 2'
    dao_update_template(template_2)
    assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'AAAAA Sample Template 2'
    assert dao_get_all_templates_for_service(sample_service.id)[1].name == 'Sample Template 1'
示例#3
0
def test_get_templates_by_ids_successful(notify_db, notify_db_session):
    template_1 = create_sample_template(notify_db,
                                        notify_db_session,
                                        template_name='Sample Template 1',
                                        template_type="sms",
                                        content="Template content")
    template_2 = create_sample_template(notify_db,
                                        notify_db_session,
                                        template_name='Sample Template 2',
                                        template_type="sms",
                                        content="Template content")
    create_sample_template(notify_db,
                           notify_db_session,
                           template_name='Sample Template 3',
                           template_type="email",
                           content="Template content")
    sample_cache_dict = {
        str.encode(str(template_1.id)): str.encode('2'),
        str.encode(str(template_2.id)): str.encode('3')
    }
    cache = [[k, v] for k, v in sample_cache_dict.items()]
    templates = dao_get_templates_for_cache(cache)
    assert len(templates) == 2
    assert [
        (template_1.id, template_1.template_type, template_1.name, False, 2),
        (template_2.id, template_2.template_type, template_2.name, False, 3)
    ] == templates
def test_get_all_templates_for_service_shows_newest_created_first(notify_db, notify_db_session, sample_service):
    template_1 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 1',
        template_type="sms",
        content="Template content",
        service=sample_service
    )
    template_2 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 2',
        template_type="sms",
        content="Template content",
        service=sample_service
    )
    template_3 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 3',
        template_type="sms",
        content="Template content",
        service=sample_service
    )

    assert Template.query.count() == 3
    assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template 3'
    assert dao_get_all_templates_for_service(sample_service.id)[1].name == 'Sample Template 2'
    assert dao_get_all_templates_for_service(sample_service.id)[2].name == 'Sample Template 1'

    template_2.name = 'Sample Template 2 (updated)'
    dao_update_template(template_2)
    assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template 3'
    assert dao_get_all_templates_for_service(sample_service.id)[1].name == 'Sample Template 2 (updated)'
def test_get_Templates(notify_api, notify_db, notify_db_session, sample_service):
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             service=sample_service)
    assert Template.query.count() == 1
    assert len(get_model_templates()) == 1
    template_name = "Another Template"
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name=template_name,
                                             service=sample_service)
    assert Template.query.count() == 2
    assert len(get_model_templates()) == 2
def test_get_Templates(notify_api, notify_db, notify_db_session,
                       sample_service):
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             service=sample_service)
    assert Template.query.count() == 1
    assert len(get_model_templates()) == 1
    template_name = "Another Template"
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name=template_name,
                                             service=sample_service)
    assert Template.query.count() == 2
    assert len(get_model_templates()) == 2
示例#7
0
def test_get_template_statistics_for_template_returns_empty_if_no_statistics(
        notify_db,
        notify_db_session,
        notify_api,
        sample_service
):
    template = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 1',
        service=sample_service
    )

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

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

            assert response.status_code == 404
            json_resp = json.loads(response.get_data(as_text=True))
            assert json_resp['result'] == 'error'
            assert json_resp['message']['template_id'] == ['No template found for id {}'.format(template.id)]
def test_create_template_raises_invalid_request_when_content_too_large(
        notify_db,
        notify_db_session,
        template_type,
        should_error
):
    sample = create_sample_template(
        notify_db,
        notify_db_session,
        content="((long_text))",
        template_type=template_type
    )
    limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
    template = Template.query.get(sample.id)
    from app.notifications.rest import create_template_object_for_notification
    try:
        create_template_object_for_notification(template,
                                                {'long_text':
                                                    ''.join(
                                                        random.choice(string.ascii_uppercase + string.digits) for _ in
                                                        range(limit + 1))})
        if should_error:
            pytest.fail("expected an InvalidRequest")
    except InvalidRequest as e:
        if not should_error:
            pytest.fail("do not expect an InvalidRequest")
        assert e.message == {'content': ['Content has a character count greater than the limit of {}'.format(limit)]}
示例#9
0
def test_should_preview_a_single_template(
    notify_db,
    client,
    sample_user,
    service_factory,
    subject,
    content,
    path,
    expected_subject,
    expected_content,
    expected_error
):

    template = create_sample_template(
        notify_db, notify_db.session, subject_line=subject, content=content, template_type='email'
    )

    response = client.get(
        path.format(template.service.id, template.id),
        headers=[create_authorization_header()]
    )

    content = json.loads(response.get_data(as_text=True))

    if expected_error:
        assert response.status_code == 400
        assert content['message']['template'] == [expected_error]
    else:
        assert response.status_code == 200
        assert content['content'] == expected_content
        assert content['subject'] == expected_subject
示例#10
0
def test_post_notification_without_document_upload_permission(
        client, notify_db, notify_db_session, mocker):
    service = sample_service(notify_db,
                             notify_db_session,
                             permissions=[EMAIL_TYPE])
    template = create_sample_template(notify_db,
                                      notify_db_session,
                                      service=service,
                                      template_type='email',
                                      content="Document: ((document))")

    mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
    document_download_mock = mocker.patch(
        'app.v2.notifications.post_notifications.document_download_client')
    document_download_mock.upload_document.return_value = 'https://document-url/'

    data = {
        "email_address": service.users[0].email_address,
        "template_id": template.id,
        "personalisation": {
            "document": {
                "file": "abababab"
            }
        }
    }

    auth_header = create_authorization_header(service_id=service.id)
    response = client.post(path="v2/notifications/email",
                           data=json.dumps(data),
                           headers=[('Content-Type', 'application/json'),
                                    auth_header])

    assert response.status_code == 400, response.get_data(as_text=True)
def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
        notify_db,
        notify_db_session,
        notify_api,
        sample_user,
        mocker,
        restricted):
    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
            mocker.patch('app.encryption.encrypt', return_value="something_encrypted")

            service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=restricted)
            email_template = create_sample_email_template(notify_db, notify_db_session, service=service)
            sms_template = create_sample_template(notify_db, notify_db_session, service=service)
            create_sample_notification(notify_db, notify_db_session, template=email_template, service=service)

            data = {
                'to': sample_user.mobile_number,
                'template': str(sms_template.id)
            }

            auth_header = create_authorization_header(service_id=service.id)

            response = client.post(
                path='/notifications/sms',
                data=json.dumps(data),
                headers=[('Content-Type', 'application/json'), auth_header])

            assert response.status_code == 201
示例#12
0
def test_send_notification_uses_priority_queue_when_template_is_marked_as_priority(
        client, notify_db, notify_db_session, mocker, notification_type,
        key_send_to, send_to):
    mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(
        notification_type))

    sample = create_sample_template(notify_db,
                                    notify_db_session,
                                    template_type=notification_type,
                                    process_type='priority')
    mocked = mocker.patch(
        'app.celery.provider_tasks.deliver_{}.apply_async'.format(
            notification_type))

    data = {key_send_to: send_to, 'template_id': str(sample.id)}

    auth_header = create_authorization_header(service_id=sample.service_id)

    response = client.post(
        path='/v2/notifications/{}'.format(notification_type),
        data=json.dumps(data),
        headers=[('Content-Type', 'application/json'), auth_header])

    notification_id = json.loads(response.data)['id']

    assert response.status_code == 201
    mocked.assert_called_once_with([notification_id], queue='priority-tasks')
示例#13
0
def test_should_preview_a_single_template(
    notify_db,
    client,
    sample_user,
    service_factory,
    subject,
    content,
    path,
    expected_subject,
    expected_content,
    expected_error
):

    template = create_sample_template(
        notify_db, notify_db.session, subject_line=subject, content=content, template_type=EMAIL_TYPE
    )

    response = client.get(
        path.format(template.service.id, template.id),
        headers=[create_authorization_header()]
    )

    content = json.loads(response.get_data(as_text=True))

    if expected_error:
        assert response.status_code == 400
        assert content['message']['template'] == [expected_error]
    else:
        assert response.status_code == 200
        assert content['content'] == expected_content
        assert content['subject'] == expected_subject
示例#14
0
def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded(
        client, notify_db, notify_db_session, mocker, notification_type,
        key_send_to, send_to):
    sample = create_sample_template(notify_db,
                                    notify_db_session,
                                    template_type=notification_type)
    persist_mock = mocker.patch(
        'app.v2.notifications.post_notifications.persist_notification')
    deliver_mock = mocker.patch(
        'app.v2.notifications.post_notifications.send_notification_to_queue')
    mocker.patch('app.v2.notifications.post_notifications.check_rate_limiting',
                 side_effect=RateLimitError("LIMIT", "INTERVAL", "TYPE"))

    data = {key_send_to: send_to, 'template_id': str(sample.id)}

    auth_header = create_authorization_header(service_id=sample.service_id)

    response = client.post(
        path='/v2/notifications/{}'.format(notification_type),
        data=json.dumps(data),
        headers=[('Content-Type', 'application/json'), auth_header])

    error = json.loads(response.data)['errors'][0]['error']
    message = json.loads(response.data)['errors'][0]['message']
    status_code = json.loads(response.data)['status_code']
    assert response.status_code == 429
    assert error == 'RateLimitError'
    assert message == 'Exceeded rate limit for key type TYPE of LIMIT requests per INTERVAL seconds'
    assert status_code == 429

    assert not persist_mock.called
    assert not deliver_mock.called
示例#15
0
def test_should_get_a_single_template(
    notify_db,
    client,
    sample_user,
    service_factory,
    subject,
    content,
    template_type
):

    template = create_sample_template(
        notify_db, notify_db.session, subject_line=subject, content=content, template_type=template_type
    )

    response = client.get(
        '/service/{}/template/{}'.format(template.service.id, template.id),
        headers=[create_authorization_header()]
    )

    data = json.loads(response.get_data(as_text=True))['data']

    assert response.status_code == 200
    assert data['content'] == content
    assert data['subject'] == subject
    assert data['process_type'] == 'normal'
    assert not data['redact_personalisation']
示例#16
0
def test_post_sms_notification_returns_400_if_not_allowed_to_send_int_sms(
    client,
    notify_db,
    notify_db_session,
):
    service = sample_service(notify_db,
                             notify_db_session,
                             permissions=[SMS_TYPE])
    template = create_sample_template(notify_db,
                                      notify_db_session,
                                      service=service)

    data = {'phone_number': '20-12-1234-1234', 'template_id': template.id}
    auth_header = create_authorization_header(service_id=service.id)

    response = client.post(path='/v2/notifications/sms',
                           data=json.dumps(data),
                           headers=[('Content-Type', 'application/json'),
                                    auth_header])

    assert response.status_code == 400
    assert response.headers['Content-type'] == 'application/json'

    error_json = json.loads(response.get_data(as_text=True))
    assert error_json['status_code'] == 400
    assert error_json['errors'] == [{
        "error":
        "BadRequestError",
        "message":
        'Cannot send to international mobile numbers'
    }]
def test_get_user_Template(notify_api, notify_db, notify_db_session, sample_service):
    assert Template.query.count() == 0
    template_name = "Random Template"
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name=template_name,
                                             service=sample_service)
    assert get_model_templates(template_id=sample_template.id).name == template_name
    assert Template.query.count() == 1
示例#18
0
def test_get_template_versions_is_empty_for_hidden_templates(
        notify_db, notify_db_session, sample_service):
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name='Test Template',
                                             hidden=True,
                                             service=sample_service)
    versions = dao_get_template_versions(service_id=sample_template.service_id,
                                         template_id=sample_template.id)
    assert len(versions) == 0
def test_get_template_by_id_and_service(notify_db, notify_db_session, sample_service):
    sample_template = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Test Template',
        service=sample_service)
    assert dao_get_template_by_id_and_service_id(
        template_id=sample_template.id,
        service_id=sample_service.id).name == 'Test Template'
    assert Template.query.count() == 1
示例#20
0
def test_get_all_templates_ignores_hidden_templates(notify_db,
                                                    notify_db_session,
                                                    sample_service):
    normal_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name='Normal Template',
                                             service=sample_service,
                                             archived=False)

    create_sample_template(notify_db,
                           notify_db_session,
                           template_name='Hidden Template',
                           hidden=True,
                           service=sample_service)

    templates = dao_get_all_templates_for_service(sample_service.id)

    assert len(templates) == 1
    assert templates[0] == normal_template
def test_get_user_Template(notify_api, notify_db, notify_db_session,
                           sample_service):
    assert Template.query.count() == 0
    template_name = "Random Template"
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name=template_name,
                                             service=sample_service)
    assert get_model_templates(
        template_id=sample_template.id).name == template_name
    assert Template.query.count() == 1
示例#22
0
def test_get_template_version_returns_none_for_hidden_templates(
        notify_db, notify_db_session, sample_service):
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name='Test Template',
                                             hidden=True,
                                             service=sample_service)

    with pytest.raises(NoResultFound):
        dao_get_template_by_id_and_service_id(sample_template.id,
                                              sample_service.id, '1')
示例#23
0
def test_get_template_by_id_and_service(notify_db, notify_db_session,
                                        sample_service):
    sample_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name='Test Template',
                                             service=sample_service)
    template = dao_get_template_by_id_and_service_id(
        template_id=sample_template.id, service_id=sample_service.id)
    assert template.id == sample_template.id
    assert template.name == 'Test Template'
    assert template.version == sample_template.version
    assert not template.redact_personalisation
示例#24
0
def test_get_all_templates_ignores_archived_templates(notify_db,
                                                      notify_db_session,
                                                      sample_service):
    normal_template = create_sample_template(notify_db,
                                             notify_db_session,
                                             template_name='Normal Template',
                                             service=sample_service,
                                             archived=False)
    archived_template = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Archived Template',
        service=sample_service)
    # sample_template fixture uses dao, which forces archived = False at creation.
    archived_template.archived = True
    dao_update_template(archived_template)

    templates = dao_get_all_templates_for_service(sample_service.id)

    assert len(templates) == 1
    assert templates[0] == normal_template
示例#25
0
def test_get_all_templates_for_service(notify_db, notify_db_session,
                                       service_factory):
    service_1 = service_factory.get('service 1', email_from='service.1')
    service_2 = service_factory.get('service 2', email_from='service.2')

    assert Template.query.count() == 2
    assert len(dao_get_all_templates_for_service(service_1.id)) == 1
    assert len(dao_get_all_templates_for_service(service_2.id)) == 1

    create_sample_template(notify_db,
                           notify_db_session,
                           template_name='Sample Template 1',
                           template_type="sms",
                           content="Template content",
                           service=service_1)
    create_sample_template(notify_db,
                           notify_db_session,
                           template_name='Sample Template 2',
                           template_type="sms",
                           content="Template content",
                           service=service_1)
    create_sample_template(notify_db,
                           notify_db_session,
                           template_name='Sample Template 3',
                           template_type="sms",
                           content="Template content",
                           service=service_2)

    assert Template.query.count() == 5
    assert len(dao_get_all_templates_for_service(service_1.id)) == 3
    assert len(dao_get_all_templates_for_service(service_2.id)) == 2
示例#26
0
def test_notification_for_csv_returns_correct_type(notify_db,
                                                   notify_db_session,
                                                   template_type, recipient):
    template = create_sample_template(notify_db,
                                      notify_db_session,
                                      template_type=template_type)
    notification = create_sample_notification_with_job(notify_db,
                                                       notify_db_session,
                                                       template=template,
                                                       to_field=recipient)

    serialized = notification.serialize_for_csv()
    assert serialized['template_type'] == template_type
def test_get_all_templates_ignores_archived_templates(notify_db, notify_db_session, sample_service):
    normal_template = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Normal Template',
        service=sample_service,
        archived=False
    )
    archived_template = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Archived Template',
        service=sample_service
    )
    # sample_template fixture uses dao, which forces archived = False at creation.
    archived_template.archived = True
    dao_update_template(archived_template)

    templates = dao_get_all_templates_for_service(sample_service.id)

    assert len(templates) == 1
    assert templates[0] == normal_template
示例#28
0
def test_notification_for_csv_returns_formatted_status(notify_db,
                                                       notify_db_session,
                                                       template_type, status,
                                                       expected_status):
    template = create_sample_template(notify_db,
                                      notify_db_session,
                                      template_type=template_type)
    notification = create_sample_notification_with_job(notify_db,
                                                       notify_db_session,
                                                       status=status,
                                                       template=template)

    serialized = notification.serialize_for_csv()
    assert serialized['status'] == expected_status
示例#29
0
def test_get_letter_templates_by_ids_successful(notify_db, notify_db_session):
    template_1 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name=PRECOMPILED_TEMPLATE_NAME,
        template_type="letter",
        content="Template content",
        hidden=True
    )
    template_2 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 2',
        template_type="letter",
        content="Template content"
    )
    sample_cache_dict = {str.encode(str(template_1.id)): str.encode('2'),
                         str.encode(str(template_2.id)): str.encode('3')}
    cache = [[k, v] for k, v in sample_cache_dict.items()]
    templates = dao_get_templates_for_cache(cache)
    assert len(templates) == 2
    assert [(template_1.id, template_1.template_type, template_1.name, True, 2),
            (template_2.id, template_2.template_type, template_2.name, False, 3)] == templates
def test_get_all_templates_for_service(notify_db, notify_db_session, service_factory):
    service_1 = service_factory.get('service 1', email_from='service.1')
    service_2 = service_factory.get('service 2', email_from='service.2')

    assert Template.query.count() == 2
    assert len(dao_get_all_templates_for_service(service_1.id)) == 1
    assert len(dao_get_all_templates_for_service(service_2.id)) == 1

    template_1 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 1',
        template_type="sms",
        content="Template content",
        service=service_1
    )
    template_2 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 2',
        template_type="sms",
        content="Template content",
        service=service_1
    )
    template_3 = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 3',
        template_type="sms",
        content="Template content",
        service=service_2
    )

    assert Template.query.count() == 5
    assert len(dao_get_all_templates_for_service(service_1.id)) == 3
    assert len(dao_get_all_templates_for_service(service_2.id)) == 2
示例#31
0
def test_post_notification_with_document_upload(client, notify_db,
                                                notify_db_session, mocker):
    service = sample_service(notify_db,
                             notify_db_session,
                             permissions=[EMAIL_TYPE, UPLOAD_DOCUMENT])
    template = create_sample_template(notify_db,
                                      notify_db_session,
                                      service=service,
                                      template_type='email',
                                      content="Document: ((document))")

    mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
    document_download_mock = mocker.patch(
        'app.v2.notifications.post_notifications.document_download_client')
    document_download_mock.upload_document.return_value = 'https://document-url/'

    data = {
        "email_address": service.users[0].email_address,
        "template_id": template.id,
        "personalisation": {
            "document": {
                "file": "abababab"
            }
        }
    }

    auth_header = create_authorization_header(service_id=service.id)
    response = client.post(path="v2/notifications/email",
                           data=json.dumps(data),
                           headers=[('Content-Type', 'application/json'),
                                    auth_header])

    assert response.status_code == 201, response.get_data(as_text=True)
    resp_json = json.loads(response.get_data(as_text=True))
    assert validate(resp_json, post_email_response) == resp_json

    notification = Notification.query.one()
    assert notification.status == NOTIFICATION_CREATED
    assert notification.personalisation == {
        'document': 'https://document-url/'
    }

    assert resp_json['content']['body'] == 'Document: https://document-url/'
示例#32
0
def test_get_template_statistics_by_id_returns_last_notification(
        notify_db,
        notify_db_session,
        notify_api,
        sample_service):

    template = create_sample_template(
        notify_db,
        notify_db_session,
        template_name='Sample Template 1',
        service=sample_service
    )

    notification_1 = sample_notification(
        notify_db,
        notify_db_session,
        service=sample_service,
        template=template)
    notification_2 = sample_notification(
        notify_db,
        notify_db_session,
        service=sample_service,
        template=template)
    notification_3 = sample_notification(
        notify_db,
        notify_db_session,
        service=sample_service,
        template=template)

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

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

            assert response.status_code == 200
            json_resp = json.loads(response.get_data(as_text=True))['data']
            assert json_resp['id'] == str(notification_3.id)
def test_get_all_notifications_filter_multiple_query_parameters(client, notify_db, notify_db_session):
    email_template = create_sample_template(notify_db, notify_db_session, template_type="email")

    # this is the notification we are looking for
    older_notification = create_sample_notification(
        notify_db, notify_db_session, template=email_template, status="pending")

    # wrong status
    create_sample_notification(notify_db, notify_db_session, template=email_template)
    # wrong template
    create_sample_notification(notify_db, notify_db_session, status="pending")

    # we only want notifications created before this one
    newer_notification = create_sample_notification(notify_db, notify_db_session)

    # this notification was created too recently
    create_sample_notification(notify_db, notify_db_session, template=email_template, status="pending")

    auth_header = create_authorization_header(service_id=newer_notification.service_id)
    response = client.get(
        path='/v2/notifications?status=pending&template_type=email&older_than={}'.format(newer_notification.id),
        headers=[('Content-Type', 'application/json'), auth_header])

    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 200
    assert response.headers['Content-type'] == "application/json"
    # query parameters aren't returned in order
    for url_part in [
        "/v2/notifications?",
        "template_type=email",
        "status=pending",
        "older_than={}".format(newer_notification.id)
    ]:
        assert url_part in json_response['links']['current']

    assert 'next' in json_response['links'].keys()
    assert len(json_response['notifications']) == 1

    assert json_response['notifications'][0]['id'] == str(older_notification.id)
示例#34
0
def set_up_notifications(notify_db, notify_db_session):
    sms = create_sample_template(notify_db, notify_db_session)
    email = sample_email_template(notify_db, notify_db_session)
    today = datetime.now()
    a_week_ago = datetime.now() - timedelta(days=7)
    a_month_ago = datetime.now() - timedelta(days=30)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=a_month_ago,
                        template=sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=a_month_ago,
                        template=email)
    email.name = 'Updated Email Template Name'
    dao_update_template(email)
    sms.name = 'Updated SMS Template Name'
    dao_update_template(sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=a_week_ago,
                        template=sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=a_week_ago,
                        template=email)
    email.name = 'New Email Template Name'
    dao_update_template(email)
    sms.name = 'New SMS Template Name'
    dao_update_template(sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=today,
                        template=sms)
    sample_notification(notify_db,
                        notify_db_session,
                        created_at=today,
                        template=email)
    return email, sms
def test_should_error_if_notification_type_does_not_match_template_type(
        client,
        notify_db,
        notify_db_session,
        template_type,
        notification_type,
        to
):
    template = create_sample_template(notify_db, notify_db_session, template_type=template_type)
    data = {
        'to': to,
        'template': template.id
    }
    auth_header = create_authorization_header(service_id=template.service_id)
    response = client.post("/notifications/{}".format(notification_type),
                           data=json.dumps(data),
                           headers=[('Content-Type', 'application/json'), auth_header])

    assert response.status_code == 400
    json_resp = json.loads(response.get_data(as_text=True))
    assert json_resp['result'] == 'error'
    assert '{0} template is not suitable for {1} notification'.format(template_type, notification_type) \
           in json_resp['message']
示例#36
0
def test_should_get_a_single_template(
    notify_db,
    client,
    sample_user,
    service_factory,
    subject,
    content,
    template_type
):

    template = create_sample_template(
        notify_db, notify_db.session, subject_line=subject, content=content, template_type=template_type
    )

    response = client.get(
        '/service/{}/template/{}'.format(template.service.id, template.id),
        headers=[create_authorization_header()]
    )

    data = json.loads(response.get_data(as_text=True))['data']

    assert response.status_code == 200
    assert data['content'] == content
    assert data['subject'] == subject