From e3c692ede7d062f86515f7999d0e096c4b595d57 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 4 Mar 2016 10:09:46 +0000 Subject: [PATCH] Validate column heading. Still need to show that it is the heading that is wrong. --- app/main/views/send.py | 6 ++++-- app/utils.py | 16 +++++++++++++++- tests/app/main/test_utils.py | 10 +++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/main/views/send.py b/app/main/views/send.py index 7ff1629fb..12a0078a3 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -27,7 +27,8 @@ from app.main.dao import templates_dao from app.main.dao import services_dao from app import job_api_client from app.utils import ( - validate_recipient, InvalidPhoneError, InvalidEmailError, user_has_permissions) + validate_recipient, validate_header_row, InvalidPhoneError, InvalidEmailError, user_has_permissions, + InvalidHeaderError) from utils.process_csv import first_column_heading @@ -285,6 +286,7 @@ def _get_rows(contents, raw_template): values=row, drop_values={first_column_heading[raw_template['template_type']]} ).replaced - except (InvalidEmailError, InvalidPhoneError, NeededByTemplateError, NoPlaceholderForDataError): + except (InvalidEmailError, InvalidPhoneError, NeededByTemplateError, + NoPlaceholderForDataError, InvalidHeaderError): valid = False return {"valid": valid, "rows": rows} diff --git a/app/utils.py b/app/utils.py index 11eb18876..96bf0a8fd 100644 --- a/app/utils.py +++ b/app/utils.py @@ -3,7 +3,7 @@ import re from functools import wraps from flask import (abort, session) -from utils.process_csv import get_recipient_from_row +from utils.process_csv import get_recipient_from_row, first_column_heading class BrowsableItem(object): @@ -42,6 +42,11 @@ class InvalidPhoneError(Exception): self.message = message +class InvalidHeaderError(Exception): + def __init__(self, message): + self.message = message + + def validate_phone_number(number): sanitised_number = number.replace('(', '') sanitised_number = sanitised_number.replace(')', '') @@ -90,12 +95,21 @@ def validate_email_address(email_address): def validate_recipient(row, template_type): + validate_header_row(row, template_type) return { 'email': validate_email_address, 'sms': validate_phone_number }[template_type](get_recipient_from_row(row, template_type)) +def validate_header_row(row, template_type): + try: + column_heading = first_column_heading[template_type] + row[column_heading] + except KeyError as e: + raise InvalidHeaderError('Invalid header name, should be {}'.format(column_heading)) + + def user_has_permissions(*permissions, or_=False): def wrap(func): @wraps(func) diff --git a/tests/app/main/test_utils.py b/tests/app/main/test_utils.py index 95b435f6e..6ab9b555f 100644 --- a/tests/app/main/test_utils.py +++ b/tests/app/main/test_utils.py @@ -1,7 +1,7 @@ import pytest from flask import url_for -from app.utils import user_has_permissions +from app.utils import user_has_permissions, validate_header_row, validate_recipient, InvalidHeaderError from app.main.views.index import index from werkzeug.exceptions import Forbidden @@ -56,3 +56,11 @@ def test_user_has_permissions_multiple(app_, decorator = user_has_permissions('manage_templates', 'manage_users') decorated_index = decorator(index) response = decorated_index() + + +def test_validate_header_row(): + row = {'bad': '+44 7700 900981'} + try: + validate_header_row(row, 'sms') + except InvalidHeaderError as e: + assert e.message == 'Invalid header name, should be phone number'