def test_create_template_raises_invalid_request_when_content_too_large(
        sample_service, template_type, should_error):
    sample = create_template(sample_service,
                             template_type=template_type,
                             content="((long_text))")
    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(SMS_CHAR_COUNT_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(SMS_CHAR_COUNT_LIMIT)
            ]
        }
def test_create_template_raises_invalid_request_exception_with_missing_personalisation(
        sample_template_with_placeholders):
    template = Template.query.get(sample_template_with_placeholders.id)
    from app.notifications.rest import create_template_object_for_notification
    with pytest.raises(InvalidRequest) as e:
        create_template_object_for_notification(template, {})
    assert {'template': ['Missing personalisation:  Name']} == e.value.message
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)]}
def test_create_template_raises_invalid_request_exception_with_missing_personalisation(
        sample_template_with_placeholders):
    template = Template.query.get(sample_template_with_placeholders.id)
    from app.notifications.rest import create_template_object_for_notification
    with pytest.raises(InvalidRequest) as e:
        create_template_object_for_notification(template, {})
    assert {'template': ['Missing personalisation:  Name']} == e.value.message
def test_create_template_doesnt_raise_with_too_much_personalisation(
        sample_template_with_placeholders):
    from app.notifications.rest import create_template_object_for_notification
    template = Template.query.get(sample_template_with_placeholders.id)
    create_template_object_for_notification(template, {
        'name': 'Jo',
        'extra': 'stuff'
    })
def test_create_template_raises_invalid_request_exception_with_too_much_personalisation_data(
        sample_template_with_placeholders
):
    from app.notifications.rest import create_template_object_for_notification
    template = Template.query.get(sample_template_with_placeholders.id)
    with pytest.raises(InvalidRequest) as e:
        create_template_object_for_notification(template, {'name': 'Jo', 'extra': 'stuff'})
        assert {'template': ['Personalisation not needed for template: foo']} in e.value.message