From 047a7d548820e650330dfff3b50d6fc3dbca6b15 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Wed, 1 Jun 2016 12:19:59 +0100 Subject: [PATCH] Only test the items of the template that can change on an update request. Return 200 if no change is made. --- app/template/rest.py | 25 +++++++++++++++++-------- tests/app/template/test_rest.py | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/template/rest.py b/app/template/rest.py index e010fbb03..94a51a749 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -5,7 +5,6 @@ from flask import ( current_app ) import bleach -from sqlalchemy.exc import IntegrityError from app.dao.templates_dao import ( dao_update_template, @@ -59,18 +58,18 @@ def update_template(service_id, template_id): fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id) current_data = dict(template_schema.dump(fetched_template).data.items()) - update_template = dict(template_schema.dump(fetched_template).data.items()) - update_template.update(request.get_json()) - update_template['content'] = _strip_html(update_template['content']) + updated_template = dict(template_schema.dump(fetched_template).data.items()) + updated_template.update(request.get_json()) + updated_template['content'] = _strip_html(updated_template['content']) # Check if there is a change to make. - if current_data == update_template: - return jsonify(data=update_template), 304 + if _template_has_not_changed(current_data, updated_template): + return jsonify(data=updated_template), 200 - update_dict, errors = template_schema.load(update_template) + update_dict, errors = template_schema.load(updated_template) if errors: return jsonify(result="error", message=errors), 400 over_limit, json_resp = _content_count_greater_than_limit( - update_template['content'], + updated_template['content'], fetched_template.template_type) if over_limit: return json_resp, 400 @@ -119,3 +118,13 @@ def get_template_versions(service_id, template_id): def _strip_html(content): return bleach.clean(content, tags=[], strip=True) + + +def _template_has_not_changed(current_data, updated_template): + if (current_data['name'] == updated_template['name'] and + current_data['content'] == updated_template['content'] and + current_data['subject'] == updated_template['subject']and + current_data['archived'] == updated_template['archived']): + return True + else: + return False diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 6adef6af2..45e7300e6 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -440,6 +440,6 @@ def test_update_does_not_create_new_version_when_there_is_no_change(notify_api, resp = client.post('/service/{}/template/{}'.format(sample_template.service_id, sample_template.id), data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) - assert resp.status_code == 304 + assert resp.status_code == 200 template = dao_get_template_by_id(sample_template.id) assert template.version == 1