From d5edb8dbfba87d1f5d912f6d7599f139cb2792a9 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 16 Oct 2017 14:58:08 +0100 Subject: [PATCH] Track form validation errors in Google analytics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We started tracking upload errors in eb264f34b766d92ce79cb116b6093698de593caf This has been useful. This commit adds tracking of other form validation errors, so we can pick up if there’s a form field that’s causing people particular trouble. Also had to rewrite a very old test to look for page content in a smarter way. --- app/templates/components/list-entry.html | 2 +- app/templates/components/radios.html | 6 +++--- app/templates/components/textbox.html | 2 +- tests/app/main/views/test_verify.py | 22 +++++++++++++++------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/templates/components/list-entry.html b/app/templates/components/list-entry.html index 23d54ef55..fc3abc65b 100644 --- a/app/templates/components/list-entry.html +++ b/app/templates/components/list-entry.html @@ -18,7 +18,7 @@ {% endif %} {% if field.errors %} - + {{ field.errors[0][0] }} {% endif %} diff --git a/app/templates/components/radios.html b/app/templates/components/radios.html index fad33d8fe..d5d3682ea 100644 --- a/app/templates/components/radios.html +++ b/app/templates/components/radios.html @@ -9,7 +9,7 @@ {{ field.label.text|safe }} {% if field.errors %} - + {{ field.errors[0] }} {% endif %} @@ -50,7 +50,7 @@ {{ field.label.text }} {% if field.errors %} - + {{ field.errors[0] }} {% endif %} @@ -89,7 +89,7 @@ {{ field.label.text }} {% endif %} {% if field.errors %} - + {{ field.errors[0] }} {% endif %} diff --git a/app/templates/components/textbox.html b/app/templates/components/textbox.html index 937165ac3..f895dfde6 100644 --- a/app/templates/components/textbox.html +++ b/app/templates/components/textbox.html @@ -24,7 +24,7 @@ {% endif %} {% if field.errors %} - + {% if not safe_error_message %}{{ field.errors[0] }}{% else %}{{ field.errors[0]|safe }}{% endif %} {% endif %} diff --git a/tests/app/main/views/test_verify.py b/tests/app/main/views/test_verify.py index 5c4784458..ce27250b3 100644 --- a/tests/app/main/views/test_verify.py +++ b/tests/app/main/views/test_verify.py @@ -4,6 +4,8 @@ import json from flask import url_for from bs4 import BeautifulSoup +from tests.conftest import normalize_spaces + def test_should_return_verify_template( client, @@ -68,17 +70,23 @@ def test_should_activate_user_after_verify( def test_should_return_200_when_sms_code_is_wrong( - client, + client_request, api_user_active, mock_check_verify_code_code_not_found, ): - with client.session_transaction() as session: + with client_request.session_transaction() as session: session['user_details'] = {'email_address': api_user_active.email_address, 'id': api_user_active.id} - response = client.post(url_for('main.verify'), - data={'sms_code': '12345'}) - assert response.status_code == 200 - resp_data = response.get_data(as_text=True) - assert resp_data.count('Code not found') == 1 + + page = client_request.post( + 'main.verify', + _data={'sms_code': '12345'}, + _expected_status=200, + ) + + assert len(page.select('.error-message')) == 1 + assert normalize_spaces(page.select_one('.error-message').text) == ( + 'Code not found' + ) def test_verify_email_redirects_to_verify_if_token_valid(