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

Delta Between Two Patch Sets: sitescripts/formmail/test/test_formmail2.py

Issue 29374647: Issue 4814 - Adds csv log to formmail2 (Closed) Base URL: https://hg.adblockplus.org/sitescripts
Left Patch Set: Created March 23, 2017, 2:33 p.m.
Right Patch Set: Created March 23, 2017, 6:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « .sitescripts.example ('k') | sitescripts/formmail/web/formmail2.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2017 eyeo GmbH 2 # Copyright (C) 2006-2017 eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 yield response_for 86 yield response_for
87 remove_wsgi_intercept() 87 remove_wsgi_intercept()
88 88
89 89
90 @pytest.fixture 90 @pytest.fixture
91 def sm_mock(mocker): 91 def sm_mock(mocker):
92 return mocker.patch('sitescripts.formmail.web.formmail2.sendMail') 92 return mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
93 93
94 94
95 @pytest.mark.parametrize('key,message', [ 95 @pytest.mark.parametrize('key,message', [
96 ('url', 'No URL configured for form handler: test'), 96 ('url', 'No URL configured for form handler: test'),
97 ('fields', 'No fields configured for form handler: test'), 97 ('fields', 'No fields configured for form handler: test'),
98 ('template', 'No template configured for form handler: test'), 98 ('template', 'No template configured for form handler: test'),
99 ]) 99 ])
100 def test_config_errors(key, message, form_config): 100 def test_config_errors(key, message, form_config):
101 del form_config[key] 101 del form_config[key]
102 with pytest.raises(Exception) as error: 102 with pytest.raises(Exception) as error:
103 formmail2.make_handler('test', form_config)[1] 103 formmail2.make_handler('test', form_config)[1]
104 assert error.value.message == message 104 assert error.value.message == message
105 105
106 106
107 @pytest.mark.parametrize('field,message', [ 107 @pytest.mark.parametrize('field,message', [
108 (('new_field', 'foo'), 'Unexpected field/fields: new_field'), 108 (('new_field', 'foo'), 'Unexpected field/fields: new_field'),
Sebastian Noack 2017/03/23 15:27:58 Nit: The indentation seems inconsistent here. I su
Jon Sonesen 2017/03/23 18:42:16 Done.
109 (('mandatory', ''), 'No mandatory entered'), 109 (('mandatory', ''), 'No mandatory entered'),
110 (('non_mandatory_email', 'asfaf'), 'Invalid email'), 110 (('non_mandatory_email', 'asfaf'), 'Invalid email'),
111 (('email', 'asfaf'), 'You failed the email validation'), 111 (('email', 'asfaf'), 'You failed the email validation'),
112 (('email', ''), 'You failed the email test'), 112 (('email', ''), 'You failed the email test'),
113 ]) 113 ])
114 def test_http_errs(field, message, response_for, form_data, sm_mock): 114 def test_http_errs(field, message, response_for, form_data, sm_mock):
115 key, value = field 115 key, value = field
116 form_data[key] = value 116 form_data[key] = value
117 with pytest.raises(HTTPError) as error: 117 with pytest.raises(HTTPError) as error:
118 response_for(form_data) 118 response_for(form_data)
119 assert error.value.read() == message 119 assert error.value.read() == message
120 120
121 121
122 @pytest.mark.parametrize('field,expected', [ 122 @pytest.mark.parametrize('field,expected', [
123 (('non_mandatory_message', '\xc3\xb6'), (200, '')), 123 (('non_mandatory_message', '\xc3\xb6'), (200, '')),
124 (('non_mandatory_message', ''), (200, '')), 124 (('non_mandatory_message', ''), (200, '')),
125 ]) 125 ])
Sebastian Noack 2017/03/23 15:27:58 Nit: Same here, the closing bracket should be on t
Jon Sonesen 2017/03/23 18:42:16 Done.
126 def test_success(field, expected, log_path, response_for, form_data, sm_mock): 126 def test_success(field, expected, log_path, response_for, form_data, sm_mock):
127 key, value = field 127 key, value = field
128 form_data[key] = value 128 form_data[key] = value
129 assert response_for(form_data, log=False) == expected 129 assert response_for(form_data, log=False) == expected
130 assert sm_mock.call_count == 1 130 assert sm_mock.call_count == 1
131 131
132 params = sm_mock.call_args[0][1]['fields'] 132 params = sm_mock.call_args[0][1]['fields']
133 assert set(params.keys()) == set(form_data.keys()) 133 assert set(params.keys()) == set(form_data.keys())
134 for key, value in form_data.items(): 134 for key, value in form_data.items():
135 assert params[key] == value.decode('utf8') 135 assert params[key] == value.decode('utf8')
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 assert row != form_data 177 assert row != form_data
178 178
179 179
180 def test_append_field_err(form_config, form_data, log_path): 180 def test_append_field_err(form_config, form_data, log_path):
181 """ Checks that error logs are correctly written and appended 181 """ Checks that error logs are correctly written and appended
182 182
183 Submits three forms, the second two have different fields to the first 183 Submits three forms, the second two have different fields to the first
184 and should be added to the same log file as each other, and be identical 184 and should be added to the same log file as each other, and be identical
185 """ 185 """
186 formmail2.log_formdata(form_data, log_path) 186 formmail2.log_formdata(form_data, log_path)
187 del(form_data['email']) 187 del form_data['email']
Sebastian Noack 2017/03/23 15:27:58 Nit: del isn't a function, but a statement. So the
Jon Sonesen 2017/03/23 18:42:16 Done.
188 188
189 # submit two forms with fields that dont match the config 189 # submit two forms with fields that dont match the config
190 # this should append the second form to the error log file 190 # this should append the second form to the error log file
191 with pytest.raises(Exception): 191 with pytest.raises(Exception):
192 formmail2.log_formdata(form_data, log_path) 192 formmail2.log_formdata(form_data, log_path)
193 with pytest.raises(Exception): 193 with pytest.raises(Exception):
194 formmail2.log_formdata(form_data, log_path) 194 formmail2.log_formdata(form_data, log_path)
195 195
196 with open(log_path + '_error') as error_log: 196 with open(log_path + '_error') as error_log:
197 reader = DictReader(error_log) 197 reader = DictReader(error_log)
198 assert reader.next() == form_data 198 assert reader.next() == form_data
199 assert reader.next() == form_data 199 assert reader.next() == form_data
LEFTRIGHT

Powered by Google App Engine
This is Rietveld