diff --git a/app/template/rest.py b/app/template/rest.py index 56416dc30..8cbb4125f 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -12,6 +12,7 @@ from notifications_utils import SMS_CHAR_COUNT_LIMIT from notifications_utils.pdf import extract_page_from_pdf from notifications_utils.template import SMSMessageTemplate from requests import post as requests_post +from sqlalchemy.orm.exc import NoResultFound from app.dao.notifications_dao import get_notification_by_id from app.dao.services_dao import dao_fetch_service_by_id @@ -51,8 +52,13 @@ def _content_count_greater_than_limit(content, template_type): def validate_parent_folder(template_json): if template_json.get("parent_folder_id"): - return dao_get_template_folder_by_id_and_service_id(template_folder_id=template_json.pop("parent_folder_id"), - service_id=template_json['service']) + try: + return dao_get_template_folder_by_id_and_service_id( + template_folder_id=template_json.pop("parent_folder_id"), + service_id=template_json['service'] + ) + except NoResultFound: + raise InvalidRequest("parent_folder_id not found", status_code=400) else: return None diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index d3de8e3d5..7cf46aa80 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -107,7 +107,7 @@ def test_should_create_a_new_template_for_a_service_adds_folder_relationship( assert template.folder == parent_folder -def test_create_template_should_return_404_if_folder_is_for_a_different_service( +def test_create_template_should_return_400_if_folder_is_for_a_different_service( client, sample_service ): service2 = create_service(service_name='second service') @@ -129,10 +129,11 @@ def test_create_template_should_return_404_if_folder_is_for_a_different_service( headers=[('Content-Type', 'application/json'), auth_header], data=data ) - assert response.status_code == 404 + assert response.status_code == 400 + assert json.loads(response.get_data(as_text=True))['message'] == 'parent_folder_id not found' -def test_create_template_should_return_404_if_folder_does_not_exist( +def test_create_template_should_return_400_if_folder_does_not_exist( client, sample_service ): data = { @@ -151,7 +152,8 @@ def test_create_template_should_return_404_if_folder_does_not_exist( headers=[('Content-Type', 'application/json'), auth_header], data=data ) - assert response.status_code == 404 + assert response.status_code == 400 + assert json.loads(response.get_data(as_text=True))['message'] == 'parent_folder_id not found' def test_should_raise_error_if_service_does_not_exist_on_create(client, sample_user, fake_uuid):