Only test the items of the template that can change on an update request.

Return 200 if no change is made.
This commit is contained in:
Rebecca Law
2016-06-01 12:19:59 +01:00
parent 05e72b07ae
commit 047a7d5488
2 changed files with 18 additions and 9 deletions

View File

@@ -5,7 +5,6 @@ from flask import (
current_app current_app
) )
import bleach import bleach
from sqlalchemy.exc import IntegrityError
from app.dao.templates_dao import ( from app.dao.templates_dao import (
dao_update_template, 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) 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()) current_data = dict(template_schema.dump(fetched_template).data.items())
update_template = dict(template_schema.dump(fetched_template).data.items()) updated_template = dict(template_schema.dump(fetched_template).data.items())
update_template.update(request.get_json()) updated_template.update(request.get_json())
update_template['content'] = _strip_html(update_template['content']) updated_template['content'] = _strip_html(updated_template['content'])
# Check if there is a change to make. # Check if there is a change to make.
if current_data == update_template: if _template_has_not_changed(current_data, updated_template):
return jsonify(data=update_template), 304 return jsonify(data=updated_template), 200
update_dict, errors = template_schema.load(update_template) update_dict, errors = template_schema.load(updated_template)
if errors: if errors:
return jsonify(result="error", message=errors), 400 return jsonify(result="error", message=errors), 400
over_limit, json_resp = _content_count_greater_than_limit( over_limit, json_resp = _content_count_greater_than_limit(
update_template['content'], updated_template['content'],
fetched_template.template_type) fetched_template.template_type)
if over_limit: if over_limit:
return json_resp, 400 return json_resp, 400
@@ -119,3 +118,13 @@ def get_template_versions(service_id, template_id):
def _strip_html(content): def _strip_html(content):
return bleach.clean(content, tags=[], strip=True) 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

View File

@@ -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), resp = client.post('/service/{}/template/{}'.format(sample_template.service_id, sample_template.id),
data=json.dumps(data), data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]) 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) template = dao_get_template_by_id(sample_template.id)
assert template.version == 1 assert template.version == 1