diff --git a/app/template/rest.py b/app/template/rest.py index db744fdda..d51111317 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -31,7 +31,7 @@ from app.errors import ( InvalidRequest ) from app.letters.utils import get_letter_pdf -from app.models import SMS_TYPE, Template, CHOOSE_POSTAGE +from app.models import SMS_TYPE, Template, SECOND_CLASS, LETTER_TYPE from app.notifications.validators import service_has_permission, check_reply_to from app.schema_validation import validate from app.schemas import (template_schema, template_history_schema) @@ -78,11 +78,8 @@ def create_template(service_id): errors = {'template_type': [message]} raise InvalidRequest(errors, 403) - if new_template.postage: - if not service_has_permission(CHOOSE_POSTAGE, fetched_service.permissions): - message = "Setting postage on templates is not enabled for this service." - errors = {'template_postage': [message]} - raise InvalidRequest(errors, 403) + if not new_template.postage and new_template.template_type == LETTER_TYPE: + new_template.postage = SECOND_CLASS new_template.service = fetched_service @@ -116,12 +113,6 @@ def update_template(service_id, template_id): if data.get('redact_personalisation') is True: return redact_template(fetched_template, data) - if data.get('postage'): - if not service_has_permission(CHOOSE_POSTAGE, fetched_template.service.permissions): - message = "Setting postage on templates is not enabled for this service." - errors = {'template_postage': [message]} - raise InvalidRequest(errors, 403) - if "reply_to" in data: check_reply_to(service_id, data.get("reply_to"), fetched_template.template_type) updated = dao_update_template_reply_to(template_id=template_id, reply_to=data.get("reply_to")) diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 64c51becc..b84d989c1 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -89,7 +89,7 @@ def test_should_create_a_new_template_for_a_service( assert sorted(json_resp['data']) == sorted(template_schema.dump(template).data) -def test_should_create_a_new_template_for_a_service_adds_folder_relationship( +def test_create_a_new_template_for_a_service_adds_folder_relationship( client, sample_service ): parent_folder = create_template_folder(service=sample_service, name='parent folder') @@ -115,6 +115,35 @@ def test_should_create_a_new_template_for_a_service_adds_folder_relationship( assert template.folder == parent_folder +@pytest.mark.parametrize("template_type, expected_postage", [ + (SMS_TYPE, None), (EMAIL_TYPE, None), (LETTER_TYPE, "second") +]) +def test_create_a_new_template_for_a_service_adds_postage_for_letters_only( + client, sample_service, template_type, expected_postage +): + data = { + 'name': 'my template', + 'template_type': template_type, + 'content': 'template content', + 'service': str(sample_service.id), + 'created_by': str(sample_service.users[0].id) + } + if template_type in [EMAIL_TYPE, LETTER_TYPE]: + data["subject"] = "Hi, I have good news" + + data = json.dumps(data) + auth_header = create_authorization_header() + + response = client.post( + '/service/{}/template'.format(sample_service.id), + headers=[('Content-Type', 'application/json'), auth_header], + data=data + ) + assert response.status_code == 201 + template = Template.query.filter(Template.name == 'my template').first() + assert template.postage == expected_postage + + def test_create_template_should_return_400_if_folder_is_for_a_different_service( client, sample_service ): @@ -218,34 +247,6 @@ def test_should_raise_error_on_create_if_no_permission( assert json_resp['message'] == expected_error -def test_should_raise_error_on_create_if_no_choose_postage_permission(client, sample_user): - service = create_service(service_permissions=[LETTER_TYPE]) - data = { - 'name': 'my template', - 'template_type': LETTER_TYPE, - 'content': 'template content', - 'service': str(service.id), - 'created_by': str(sample_user.id), - 'subject': "Some letter", - 'postage': 'first', - } - - data = json.dumps(data) - auth_header = create_authorization_header() - - response = client.post( - '/service/{}/template'.format(service.id), - headers=[('Content-Type', 'application/json'), auth_header], - data=data - ) - json_resp = json.loads(response.get_data(as_text=True)) - assert response.status_code == 403 - assert json_resp['result'] == 'error' - assert json_resp['message'] == { - "template_postage": ["Setting postage on templates is not enabled for this service."] - } - - @pytest.mark.parametrize('template_factory, expected_error', [ (sample_template_without_sms_permission, {'template_type': ['Updating text message templates is not allowed']}), (sample_template_without_email_permission, {'template_type': ['Updating email templates is not allowed']}), @@ -275,33 +276,6 @@ def test_should_be_error_on_update_if_no_permission( assert json_resp['message'] == expected_error -def test_should_be_error_on_update_if_no_choose_postage_permission(client, sample_user): - service = create_service(service_name='some_service', service_permissions=[LETTER_TYPE]) - template = create_template(service, template_type=LETTER_TYPE) - data = { - 'content': 'new template content', - 'created_by': str(sample_user.id), - 'postage': 'first' - } - - data = json.dumps(data) - auth_header = create_authorization_header() - - update_response = client.post( - '/service/{}/template/{}'.format( - template.service_id, template.id), - headers=[('Content-Type', 'application/json'), auth_header], - data=data - ) - - json_resp = json.loads(update_response.get_data(as_text=True)) - assert update_response.status_code == 403 - assert json_resp['result'] == 'error' - assert json_resp['message'] == { - "template_postage": ["Setting postage on templates is not enabled for this service."] - } - - def test_should_error_if_created_by_missing(client, sample_user, sample_service): service_id = str(sample_service.id) data = {