def test_check_content_char_count_passes_for_long_email_or_letter(
        sample_service, template_type):
    t = create_template(service=sample_service,
                        content='a' * 1000,
                        template_type=template_type)
    template = templates_dao.dao_get_template_by_id_and_service_id(
        template_id=t.id, service_id=t.service_id)
    template_with_content = get_template_instance(template=template.__dict__,
                                                  values={})
    assert check_content_char_count(template_with_content) is None
Пример #2
0
def test_check_content_char_count_fails(notify_db_session, show_prefix, char_count):
    with pytest.raises(BadRequestError) as e:
        service = create_service(prefix_sms=show_prefix)
        t = create_template(service=service, content='a' * char_count, template_type='sms')
        template = templates_dao.dao_get_template_by_id_and_service_id(template_id=t.id, service_id=service.id)
        template_with_content = get_template_instance(template=template.__dict__, values={})
        check_content_char_count(template_with_content)
    assert e.value.status_code == 400
    assert e.value.message == f'Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. ' \
                              f'Your message is {char_count} characters'
    assert e.value.fields == []
def test_check_content_char_count_passes(notify_db_session, show_prefix,
                                         char_count, template_type):
    service = create_service(prefix_sms=show_prefix)
    t = create_template(service=service,
                        content='a' * char_count,
                        template_type=template_type)
    template = templates_dao.dao_get_template_by_id_and_service_id(
        template_id=t.id, service_id=service.id)
    template_with_content = get_template_instance(template=template.__dict__,
                                                  values={})
    assert check_content_char_count(template_with_content) is None
Пример #4
0
    def handle_template_merge(self, in_data):
        in_data['template'] = in_data.pop('template_history')
        template = get_template_instance(in_data['template'], in_data['personalisation'])
        in_data['body'] = str(template)
        if in_data['template']['template_type'] != models.SMS_TYPE:
            in_data['subject'] = template.subject
            in_data['content_char_count'] = None
        else:
            in_data['content_char_count'] = template.content_count

        in_data.pop('personalisation', None)
        in_data['template'].pop('content', None)
        in_data['template'].pop('subject', None)
        return in_data
Пример #5
0
    def handle_template_merge(self, in_data):
        in_data['template'] = in_data.pop('template_history')
        template = get_template_instance(in_data['template'], in_data['personalisation'])
        in_data['body'] = str(template)
        if in_data['template']['template_type'] == models.EMAIL_TYPE:
            in_data['subject'] = template.subject
            in_data['content_char_count'] = None
        else:
            in_data['content_char_count'] = template.content_count

        in_data.pop('personalisation', None)
        in_data['template'].pop('content', None)
        in_data['template'].pop('subject', None)
        return in_data
Пример #6
0
def preview_template_by_id_and_service_id(service_id, template_id):
    fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
    data = template_schema.dump(fetched_template).data
    template_object = get_template_instance(data, values=request.args.to_dict())

    if template_object.missing_data:
        raise InvalidRequest(
            {'template': [
                'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
            ]}, status_code=400
        )

    data['subject'], data['content'] = template_object.subject, str(template_object)

    return jsonify(data)
Пример #7
0
def create_template_object_for_notification(template, personalisation):
    template_object = get_template_instance(template.__dict__, personalisation)

    if template_object.missing_data:
        message = "Missing personalisation: {}".format(", ".join(
            template_object.missing_data))
        errors = {"template": [message]}
        raise InvalidRequest(errors, status_code=400)

    if template_object.template_type == SMS_TYPE and template_object.content_count > SMS_CHAR_COUNT_LIMIT:
        message = "Content has a character count greater than the limit of {}".format(
            SMS_CHAR_COUNT_LIMIT)
        errors = {"content": [message]}
        raise InvalidRequest(errors, status_code=400)
    return template_object
Пример #8
0
def test_check_is_message_too_long_passes_for_long_email(sample_service):
    email_character_count = 2_000_001
    t = create_template(service=sample_service, content='a' * email_character_count, template_type='email')
    template = templates_dao.dao_get_template_by_id_and_service_id(template_id=t.id,
                                                                   service_id=t.service_id)
    template_with_content = get_template_instance(template=template.__dict__, values={})
    template_with_content.values
    with pytest.raises(BadRequestError) as e:
        check_is_message_too_long(template_with_content)
    assert e.value.status_code == 400
    expected_message = (
        'Your message is too long. ' +
        'Emails cannot be longer than 2000000 bytes. ' +
        'Your message is 2000001 bytes.'
    )
    assert e.value.message == expected_message
    assert e.value.fields == []
Пример #9
0
def post_template_preview(template_id):
    _data = request.get_json()
    if _data is None:
        _data = {}

    _data['id'] = template_id

    data = validate(_data, post_template_preview_request)

    template = templates_dao.dao_get_template_by_id_and_service_id(
        template_id, authenticated_service.id)

    template_object = get_template_instance(template.__dict__,
                                            values=data.get('personalisation'))

    check_placeholders(template_object)

    resp = create_post_template_preview_response(
        template=template, template_object=template_object)

    return jsonify(resp), 200
Пример #10
0
def preview_template_by_id_and_service_id(service_id, template_id):
    fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
    data = template_schema.dump(fetched_template).data

    template_object = get_template_instance(data, values=request.args.to_dict())

    if template_object.missing_data:
        raise InvalidRequest(
            {'template': [
                'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
            ]}, status_code=400
        )

    if template_object.additional_data:
        raise InvalidRequest(
            {'template': [
                'Personalisation not needed for template: {}'.format(", ".join(template_object.additional_data))
            ]}, status_code=400
        )

    data['subject'], data['content'] = template_object.subject, str(template_object)

    return jsonify(data)
Пример #11
0
def post_template_preview(template_id):
    # The payload is empty when there are no place holders in the template.
    _data = request.get_data(as_text=True)
    if not _data:
        _data = {}
    else:
        _data = get_valid_json()

    _data['id'] = template_id

    data = validate(_data, post_template_preview_request)

    template = templates_dao.dao_get_template_by_id_and_service_id(
        template_id, authenticated_service.id)

    template_object = get_template_instance(template.__dict__,
                                            values=data.get('personalisation'))

    check_placeholders(template_object)

    resp = create_post_template_preview_response(
        template=template, template_object=template_object)

    return jsonify(resp), 200
Пример #12
0
def create_content_for_notification(template, personalisation):
    template_object = get_template_instance(template.__dict__, personalisation)
    check_placeholders(template_object)

    return template_object
def create_content_for_notification(template, personalisation):
    template_object = get_template_instance(template.__dict__, personalisation)
    check_placeholders(template_object)

    return template_object
Пример #14
0
 def subject(self):
     from app.utils import get_template_instance
     if self.notification_type == EMAIL_TYPE:
         template_object = get_template_instance(self.template.__dict__, self.personalisation)
         return template_object.subject
Пример #15
0
 def content(self):
     from app.utils import get_template_instance
     template_object = get_template_instance(self.template.__dict__, self.personalisation)
     return str(template_object)