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 | |
16 import os | |
17 | |
15 from urllib import urlencode | 18 from urllib import urlencode |
16 from urllib2 import urlopen, HTTPError | 19 from urllib2 import urlopen, HTTPError |
17 | 20 |
18 import pytest | 21 import pytest |
22 import datetime | |
19 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, | 23 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, |
20 remove_wsgi_intercept) | 24 remove_wsgi_intercept) |
21 | 25 from csv import DictReader |
22 from sitescripts.formmail.web import formmail2 | 26 from sitescripts.formmail.web import formmail2 |
23 | 27 |
24 | 28 |
25 @pytest.fixture() | 29 @pytest.fixture() |
26 def form_config(): | 30 def form_config(): |
27 return formmail2.conf_parse(formmail2.get_config_items())['test'] | 31 return formmail2.conf_parse(formmail2.get_config_items())['test'] |
28 | 32 |
29 | 33 |
30 @pytest.fixture() | 34 @pytest.fixture() |
31 def form_handler(form_config): | 35 def form_handler(form_config, log_path): |
36 # override configured path to log file with tmpdir path | |
37 form_config['csv_log'].value = log_path | |
32 return formmail2.make_handler('test', form_config)[1] | 38 return formmail2.make_handler('test', form_config)[1] |
33 | 39 |
34 | 40 |
41 @pytest.fixture() | |
42 def log_path(form_config, tmpdir_factory): | |
43 log_file = os.path.basename(form_config['csv_log'].value) | |
Vasily Kuznetsov
2017/02/13 11:11:52
That's clever, but on the other hand perhaps we do
Jon Sonesen
2017/02/13 12:21:16
Done.
| |
44 log = tmpdir_factory.mktemp(log_file).join(log_file) | |
45 return str(log) | |
46 | |
47 | |
35 # We make this a fixture instead of a constant so we can modify it in each | 48 # We make this a fixture instead of a constant so we can modify it in each |
36 # test as needed without affecting other tests. | 49 # test as needed without affecting other tests. |
37 @pytest.fixture | 50 @pytest.fixture |
38 def form_data(): | 51 def form_data(): |
39 return { | 52 return { |
40 'email': 'john_doe@gmail.com', | 53 'email': 'john_doe@gmail.com', |
41 'mandatory': 'john_doe@gmail.com', | 54 'mandatory': 'john_doe@gmail.com', |
42 'non_mandatory_message': 'Once upon a time\nthere lived a king.', | 55 'non_mandatory_message': 'Once upon a time\nthere lived a king.', |
43 'non_mandatory_email': 'test@test.com' | 56 'non_mandatory_email': 'test@test.com', |
44 } | 57 } |
45 | 58 |
46 | 59 |
47 @pytest.fixture() | 60 @pytest.fixture() |
48 def response_for(form_handler): | 61 def response_for(form_handler, log_path): |
49 host, port = 'test.local', 80 | 62 host, port = 'test.local', 80 |
50 urllib_intercept.install_opener() | 63 urllib_intercept.install_opener() |
51 add_wsgi_intercept(host, port, lambda: form_handler) | 64 add_wsgi_intercept(host, port, lambda: form_handler) |
52 url = 'http://{}:{}'.format(host, port) | 65 url = 'http://{}:{}'.format(host, port) |
53 | 66 |
54 def response_for(data): | 67 def response_for(data): |
55 if data is None: | 68 if data is None: |
56 response = urlopen(url) | 69 response = urlopen(url) |
57 else: | 70 else: |
58 response = urlopen(url, urlencode(data)) | 71 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'] | 114 del form_data['non_mandatory_email'] |
102 assert response_for(form_data) == (200, '') | 115 assert response_for(form_data) == (200, '') |
103 | 116 |
104 | 117 |
105 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): | 118 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): |
106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | 119 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
107 del form_data['mandatory'] | 120 del form_data['mandatory'] |
108 with pytest.raises(HTTPError) as error: | 121 with pytest.raises(HTTPError) as error: |
109 response_for(form_data) | 122 response_for(form_data) |
110 assert error.value.read() == 'No mandatory entered' | 123 assert error.value.read() == 'No mandatory entered' |
124 | |
125 | |
126 def test_collect_with_tmpl(log_path, form_data): | |
127 form_data['time'] = 'test' | |
128 formmail2.collect_formdata(form_data, log_path) | |
129 with open(log_path) as csvfile: | |
130 reader = DictReader(csvfile) | |
131 row = reader.next() | |
132 assert row == form_data | |
133 | |
134 | |
135 def test_collect_no_tmpl(log_path, form_data, form_config): | |
136 del(form_config['template']) | |
137 form_data['time'] = 'test' | |
138 formmail2.collect_formdata(form_data, log_path) | |
139 with open(log_path) as csvfile: | |
140 reader = DictReader(csvfile) | |
141 row = reader.next() | |
142 assert row == form_data | |
143 | |
144 | |
145 def test_fieldnames(log_path, form_data): | |
146 form_data['time'] = str(datetime.datetime.now()) | |
147 formmail2.collect_formdata(form_data, log_path) | |
148 with open(log_path) as csvfile: | |
149 reader = DictReader(csvfile) | |
150 for field in reader.fieldnames: | |
151 assert field in tuple(form_data.keys()) | |
152 | |
153 | |
154 def test_field_err_log(form_config, form_data, log_path): | |
155 formmail2.collect_formdata(form_data, log_path) | |
156 del(form_config['fields']['email']) | |
157 del(form_data['email']) | |
158 try: | |
159 formmail2.collect_formdata(form_data, log_path) | |
160 except Exception as e: | |
161 assert e.message == \ | |
162 'Field names have changed, error log written to {}'\ | |
163 .format(log_path + '_error') | |
164 | |
165 | |
166 def test_append_field_err_log(form_config, form_data, log_path): | |
167 formmail2.collect_formdata(form_data, log_path) | |
168 del(form_config['fields']['email']) | |
169 del(form_data['email']) | |
170 try: | |
171 formmail2.collect_formdata(form_data, log_path) | |
172 except Exception as e: | |
173 pass | |
174 try: | |
175 formmail2.collect_formdata(form_data, log_path) | |
176 except Exception as e: | |
177 assert e.message == \ | |
178 'Field names have changed, error log appended to {}'\ | |
179 .format(log_path + '_error') | |
180 | |
181 | |
182 def test_append_log(form_data, log_path): | |
183 """ | |
184 collect data twice, altering a field in the second call | |
185 assert that the 2nd row is equal to the resulting form data | |
186 """ | |
187 form_data['time'] = str(datetime.datetime.now()) | |
188 formmail2.collect_formdata(form_data, log_path) | |
189 form_data['email'] = 'test@foo.com' | |
190 formmail2.collect_formdata(form_data, log_path) | |
191 with open(log_path) as csvfile: | |
192 reader = DictReader(csvfile, fieldnames=form_data.keys()) | |
193 # header | |
194 reader.next() | |
195 row1 = reader.next() | |
196 row2 = reader.next() | |
197 | |
198 assert row2['email'] == form_data['email'] | |
199 assert row2['email'] != row1['email'] | |
OLD | NEW |