Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/formmail/test/test_formmail.py

Issue 29984580: Issue 4413 - Merge formmail.py and formmail2.py (Closed) Base URL: https://hg.adblockplus.org/sitescripts/
Patch Set: Start the README Created Jan. 18, 2019, 12:44 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sitescripts/formmail/test/test_formmail.py
===================================================================
--- a/sitescripts/formmail/test/test_formmail.py
+++ b/sitescripts/formmail/test/test_formmail.py
@@ -14,13 +14,43 @@
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
from urllib import urlencode
-from urllib2 import urlopen
+from urllib2 import urlopen, HTTPError
+from csv import DictReader
import pytest
from wsgi_intercept import (urllib_intercept, add_wsgi_intercept,
remove_wsgi_intercept)
-from sitescripts.formmail.web.formmail import handleRequest
+from sitescripts.formmail.web import formmail
+
+HOST = 'test.local'
+LOG_PORT = 80
+NO_LOG_PORT = 81
+
+
+@pytest.fixture
+def log_path(tmpdir):
+ return str(tmpdir.join('test.csv_log'))
+
+
+@pytest.fixture
+def log_form_config():
+ return formmail.conf_parse(formmail.get_config_items())['test']
+
+
+@pytest.fixture
+def form_config():
+ config = formmail.conf_parse(formmail.get_config_items())['test']
+ del config['csv_log']
+ return config
+
+
+@pytest.fixture
+def form_handler(log_path, form_config, log_form_config):
+ """ Create two handlers, one that logs and another that doesn't """
+ log_form_config['csv_log'].value = log_path
+ return (formmail.make_handler('test', log_form_config)[1],
+ formmail.make_handler('test', form_config)[1])
# We make this a fixture instead of a constant so we can modify it in each
@@ -28,66 +58,142 @@
@pytest.fixture
def form_data():
return {
- 'name': 'John Doe',
'email': 'john_doe@gmail.com',
- 'subject': 'Hello there!',
- 'message': 'Once upon a time\nthere lived a king.',
+ 'mandatory': 'john_doe@gmail.com',
+ 'non_mandatory_message': 'Once upon a time\nthere lived a king.',
+ 'non_mandatory_email': 'test@test.com',
}
-@pytest.fixture()
-def response_for():
- host, port = 'test.local', 80
+@pytest.fixture
+def response_for(form_handler):
+ """ Registers two intercepts, returns responses for them based on bool """
urllib_intercept.install_opener()
- add_wsgi_intercept(host, port, lambda: handleRequest)
- url = 'http://{}:{}'.format(host, port)
+ add_wsgi_intercept(HOST, LOG_PORT, lambda: form_handler[0])
+ add_wsgi_intercept(HOST, NO_LOG_PORT, lambda: form_handler[1])
- def response_for(data):
+ def response_for(data, log=False):
+ if log:
+ url = 'http://{}:{}'.format(HOST, LOG_PORT)
+ else:
+ url = 'http://{}:{}'.format(HOST, NO_LOG_PORT)
if data is None:
response = urlopen(url)
else:
response = urlopen(url, urlencode(data))
- assert response.getcode() == 200
- return response.read()
+ return response.code, response.read()
yield response_for
remove_wsgi_intercept()
-def test_get_error(response_for):
- assert response_for(None) == 'Unsupported request method'
+@pytest.fixture
+def sm_mock(mocker):
+ return mocker.patch('sitescripts.formmail.web.formmail.sendMail')
-def test_no_name(response_for, form_data):
- del form_data['name']
- assert response_for(form_data) == 'No name entered'
+@pytest.mark.parametrize('key,message', [
+ ('url', 'No URL configured for form handler: test'),
+ ('fields', 'No fields configured for form handler: test'),
+ ('template', 'No template configured for form handler: test'),
+])
+def test_config_errors(key, message, form_config):
+ del form_config[key]
+ with pytest.raises(Exception) as error:
+ formmail.make_handler('test', form_config)[1]
+ assert error.value.message == message
-def test_no_email(response_for, form_data):
+@pytest.mark.parametrize('field,message', [
+ (('new_field', 'foo'), 'Unexpected field/fields: new_field'),
+ (('mandatory', ''), 'No mandatory entered'),
+ (('non_mandatory_email', 'asfaf'), 'Invalid email'),
+ (('email', 'asfaf'), 'You failed the email validation'),
+ (('email', ''), 'You failed the email test'),
+])
+def test_http_errs(field, message, response_for, form_data, sm_mock):
+ key, value = field
+ form_data[key] = value
+ with pytest.raises(HTTPError) as error:
+ response_for(form_data)
+ assert error.value.read() == message
+
+
+@pytest.mark.parametrize('field,expected', [
+ (('non_mandatory_message', '\xc3\xb6'), (200, '')),
+ (('non_mandatory_message', ''), (200, '')),
+])
+def test_success(field, expected, log_path, response_for, form_data, sm_mock):
+ key, value = field
+ form_data[key] = value
+ assert response_for(form_data, log=False) == expected
+ assert sm_mock.call_count == 1
+
+ params = sm_mock.call_args[0][1]['fields']
+ assert set(params.keys()) == set(form_data.keys())
+ for key, value in form_data.items():
+ assert params[key] == value.decode('utf8')
+
+ assert response_for(form_data, log=True) == expected
+ assert sm_mock.call_count == 2
+
+ assert response_for(form_data, log=True) == expected
+ assert sm_mock.call_count == 3
+
+ with open(log_path) as log_file:
+ reader = DictReader(log_file)
+ row = reader.next()
+ # rows should not be equal because the time field
+ # is added by the logging function.
+ assert row != reader.next()
+
+
+def test_config_field_errors(form_config):
+ form_config['fields'] = {}
+ with pytest.raises(Exception) as error:
+ formmail.make_handler('test', form_config)[1]
+ assert error.value.message == 'No fields configured for form handler: test'
+
+
+def test_config_template_errors(form_config):
+ form_config['template'].value = 'no'
+ with pytest.raises(Exception) as error:
+ formmail.make_handler('test', form_config)[1]
+ assert error.value.message == 'Template not found at: no'
+
+
+def test_config_parse(form_config):
+ assert form_config['url'].value == 'test/apply/submit'
+ assert form_config['fields']['email'].value == 'mandatory, email'
+
+
+def test_sendmail_fail(log_path, response_for, form_data, sm_mock):
+ sm_mock.side_effect = Exception('Sendmail Fail')
+ with pytest.raises(HTTPError):
+ response_for(form_data, log=True)
+
+ with open(log_path) as log_file:
+ row = DictReader(log_file).next()
+ assert row != form_data
+
+
+def test_append_field_err(form_config, form_data, log_path):
+ """ Checks that error logs are correctly written and appended
+
+ Submits three forms, the second two have different fields to the first
+ and should be added to the same log file as each other, and be identical
+ """
+ formmail.log_formdata(form_data, log_path)
del form_data['email']
- assert response_for(form_data) == 'No email address entered'
+ # submit two forms with fields that dont match the config
+ # this should append the second form to the error log file
+ with pytest.raises(Exception):
+ formmail.log_formdata(form_data, log_path)
+ with pytest.raises(Exception):
+ formmail.log_formdata(form_data, log_path)
-def test_no_subject(response_for, form_data):
- del form_data['subject']
- assert response_for(form_data) == 'No subject entered'
-
-
-def test_no_message(response_for, form_data):
- del form_data['message']
- assert response_for(form_data) == 'No message entered'
-
-
-def test_bad_email(response_for, form_data):
- form_data['email'] = 'bad_email'
- assert response_for(form_data) == 'Invalid email address'
-
-
-def test_success(response_for, form_data, mocker):
- sm_mock = mocker.patch('sitescripts.formmail.web.formmail.sendMail')
- assert response_for(form_data) == 'Message sent'
- assert sm_mock.call_count == 1
- params = sm_mock.call_args[0][1]
- assert set(params.keys()) == set(form_data.keys()) | {'time'}
- for key, value in form_data.items():
- assert params[key] == value
+ with open(log_path + '_error') as error_log:
+ reader = DictReader(error_log)
+ assert reader.next() == form_data
+ assert reader.next() == form_data

Powered by Google App Engine
This is Rietveld