Left: | ||
Right: |
OLD | NEW |
---|---|
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-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 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 |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | |
15 from urllib import urlencode | 16 from urllib import urlencode |
16 from urllib2 import urlopen, HTTPError | 17 from urllib2 import urlopen, HTTPError |
17 | 18 |
18 import pytest | 19 import pytest |
20 import datetime | |
19 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, | 21 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, |
20 remove_wsgi_intercept) | 22 remove_wsgi_intercept) |
21 | 23 from csv import DictReader |
Sebastian Noack
2017/02/14 09:54:14
The imports are grouped incorrectly here. Accordin
Jon Sonesen
2017/02/21 14:19:15
Done.
| |
22 from sitescripts.formmail.web import formmail2 | 24 from sitescripts.formmail.web import formmail2 |
23 | 25 |
24 | 26 |
25 @pytest.fixture() | 27 @pytest.fixture() |
26 def form_config(): | 28 def form_config(): |
27 return formmail2.conf_parse(formmail2.get_config_items())['test'] | 29 return formmail2.conf_parse(formmail2.get_config_items())['test'] |
28 | 30 |
29 | 31 |
30 @pytest.fixture() | 32 @pytest.fixture() |
31 def form_handler(form_config): | 33 def form_handler(form_config, log_path): |
34 # override configured path to log file with tmpdir path | |
35 form_config['csv_log'].value = log_path | |
32 return formmail2.make_handler('test', form_config)[1] | 36 return formmail2.make_handler('test', form_config)[1] |
33 | 37 |
34 | 38 |
39 @pytest.fixture() | |
40 def log_path(form_config, tmpdir): | |
41 log = tmpdir.mkdir('logs').join('test.csv_log') | |
Sebastian Noack
2017/02/14 09:54:14
The temporary variable seems unnecessary. Why not
Vasily Kuznetsov
2017/02/15 10:22:26
Also mkdir() seems unnecessary. tmpdir.join('test.
Jon Sonesen
2017/02/20 09:55:15
Done.
Jon Sonesen
2017/02/20 09:55:16
Done.
| |
42 return str(log) | |
43 | |
44 | |
35 # We make this a fixture instead of a constant so we can modify it in each | 45 # We make this a fixture instead of a constant so we can modify it in each |
36 # test as needed without affecting other tests. | 46 # test as needed without affecting other tests. |
37 @pytest.fixture | 47 @pytest.fixture |
38 def form_data(): | 48 def form_data(): |
39 return { | 49 return { |
40 'email': 'john_doe@gmail.com', | 50 'email': 'john_doe@gmail.com', |
41 'mandatory': 'john_doe@gmail.com', | 51 'mandatory': 'john_doe@gmail.com', |
42 'non_mandatory_message': 'Once upon a time\nthere lived a king.', | 52 'non_mandatory_message': 'Once upon a time\nthere lived a king.', |
Vasily Kuznetsov
2017/02/15 10:22:26
I've just noticed that if we put non-ascii data he
| |
43 'non_mandatory_email': 'test@test.com' | 53 'non_mandatory_email': 'test@test.com', |
44 } | 54 } |
45 | 55 |
46 | 56 |
47 @pytest.fixture() | 57 @pytest.fixture() |
48 def response_for(form_handler): | 58 def response_for(form_handler, log_path): |
49 host, port = 'test.local', 80 | 59 host, port = 'test.local', 80 |
50 urllib_intercept.install_opener() | 60 urllib_intercept.install_opener() |
51 add_wsgi_intercept(host, port, lambda: form_handler) | 61 add_wsgi_intercept(host, port, lambda: form_handler) |
52 url = 'http://{}:{}'.format(host, port) | 62 url = 'http://{}:{}'.format(host, port) |
53 | 63 |
54 def response_for(data): | 64 def response_for(data): |
55 if data is None: | 65 if data is None: |
56 response = urlopen(url) | 66 response = urlopen(url) |
57 else: | 67 else: |
58 response = urlopen(url, urlencode(data)) | 68 response = urlopen(url, urlencode(data)) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 del form_data['non_mandatory_email'] | 111 del form_data['non_mandatory_email'] |
102 assert response_for(form_data) == (200, '') | 112 assert response_for(form_data) == (200, '') |
103 | 113 |
104 | 114 |
105 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): | 115 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): |
106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | 116 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
107 del form_data['mandatory'] | 117 del form_data['mandatory'] |
108 with pytest.raises(HTTPError) as error: | 118 with pytest.raises(HTTPError) as error: |
109 response_for(form_data) | 119 response_for(form_data) |
110 assert error.value.read() == 'No mandatory entered' | 120 assert error.value.read() == 'No mandatory entered' |
121 | |
122 | |
123 def test_collect_with_tmpl(log_path, form_data): | |
124 form_data['time'] = 'test' | |
125 formmail2.collect_formdata(form_data, log_path) | |
126 with open(log_path) as csvfile: | |
127 reader = DictReader(csvfile) | |
Sebastian Noack
2017/02/14 09:54:14
Any reason you didn't just inline those variables?
Jon Sonesen
2017/02/20 09:55:15
Will do.
| |
128 row = reader.next() | |
129 assert row == form_data | |
130 | |
131 | |
132 def test_collect_no_tmpl(log_path, form_data, form_config): | |
133 del(form_config['template']) | |
134 form_data['time'] = 'test' | |
135 formmail2.collect_formdata(form_data, log_path) | |
136 with open(log_path) as csvfile: | |
137 reader = DictReader(csvfile) | |
138 row = reader.next() | |
139 assert row == form_data | |
140 | |
141 | |
142 def test_fieldnames(log_path, form_data): | |
143 form_data['time'] = str(datetime.datetime.now()) | |
144 formmail2.collect_formdata(form_data, log_path) | |
145 with open(log_path) as csvfile: | |
146 reader = DictReader(csvfile) | |
147 for field in reader.fieldnames: | |
148 assert field in tuple(form_data.keys()) | |
Sebastian Noack
2017/02/14 09:54:14
It seems unnecessary to coerce it to a tuple.
Jon Sonesen
2017/02/20 09:55:15
Yeah, you are right.
| |
149 | |
150 | |
151 def test_field_err(form_config, form_data, log_path): | |
152 """ | |
153 Submits a form that does not have the dame fields as previous submissions | |
154 that have the same form name, asserts that proper message is returned and | |
155 the row was properly written | |
156 """ | |
157 formmail2.collect_formdata(form_data, log_path) | |
158 del(form_config['fields']['email']) | |
159 del(form_data['email']) | |
160 try: | |
161 formmail2.collect_formdata(form_data, log_path) | |
162 except Exception as e: | |
163 assert e.message == \ | |
164 'Field names have changed, error log written to {}'\ | |
165 .format(log_path + '_error') | |
166 | |
167 with open(log_path+'_error') as error_log: | |
168 reader = DictReader(error_log) | |
169 assert reader.next() == form_data | |
170 | |
171 | |
172 def test_append_field_err(form_config, form_data, log_path): | |
173 """ | |
174 Submits two identical forms that do not match the previous fields | |
175 found in the log file, triggering two rows to be added to the error | |
176 log and asserting the proper message is returned and that the rows | |
177 were written as expected | |
178 """ | |
179 formmail2.collect_formdata(form_data, log_path) | |
180 del(form_config['fields']['email']) | |
181 del(form_data['email']) | |
182 try: | |
183 formmail2.collect_formdata(form_data, log_path) | |
184 except Exception as e: | |
Sebastian Noack
2017/02/14 09:54:13
Since you ignore the exception, there is no point
Jon Sonesen
2017/02/20 09:55:15
I was playing around with asserting the message he
| |
185 pass | |
186 try: | |
187 formmail2.collect_formdata(form_data, log_path) | |
188 except Exception as e: | |
189 assert e.message == \ | |
Sebastian Noack
2017/02/14 09:54:14
The way the code if wrapped here seems hard to fol
Jon Sonesen
2017/02/20 09:55:15
I agree, it is better how you suggest, thank you
| |
190 'Field names have changed, error log appended to {}'\ | |
191 .format(log_path + '_error') | |
192 | |
193 with open(log_path+'_error') as error_log: | |
194 reader = DictReader(error_log) | |
195 # two identical rows should be in the rror log | |
196 assert reader.next() == form_data | |
197 assert reader.next() == form_data | |
198 | |
199 | |
200 def test_append_log(form_data, log_path): | |
201 """ | |
202 collect data twice, altering a field in the second call | |
203 assert that the 2nd row is equal to the resulting form data | |
204 """ | |
205 form_data['time'] = str(datetime.datetime.now()) | |
206 formmail2.collect_formdata(form_data, log_path) | |
207 form_data['email'] = 'test@foo.com' | |
208 formmail2.collect_formdata(form_data, log_path) | |
209 with open(log_path) as csvfile: | |
210 reader = DictReader(csvfile, fieldnames=form_data.keys()) | |
211 # header | |
212 reader.next() | |
213 row1 = reader.next() | |
214 row2 = reader.next() | |
215 | |
216 assert row2['email'] == form_data['email'] | |
217 assert row2['email'] != row1['email'] | |
OLD | NEW |