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 import os | |
16 | |
15 from urllib import urlencode | 17 from urllib import urlencode |
16 from urllib2 import urlopen, HTTPError | 18 from urllib2 import urlopen, HTTPError |
17 | 19 |
18 import pytest | 20 import pytest |
21 import datetime | |
19 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, | 22 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, |
20 remove_wsgi_intercept) | 23 remove_wsgi_intercept) |
21 | 24 from csv import DictReader |
22 from sitescripts.formmail.web import formmail2 | 25 from sitescripts.formmail.web import formmail2 |
23 | 26 |
24 | 27 |
25 @pytest.fixture() | 28 @pytest.fixture() |
26 def form_config(): | 29 def form_config(): |
27 return formmail2.conf_parse(formmail2.get_config_items())['test'] | 30 return formmail2.conf_parse(formmail2.get_config_items())['test'] |
Vasily Kuznetsov
2017/02/08 18:16:17
As discussed over TS, let's override the csv_log w
Jon Sonesen
2017/02/10 13:57:00
Done.
| |
28 | 31 |
29 | 32 |
30 @pytest.fixture() | 33 @pytest.fixture() |
31 def form_handler(form_config): | 34 def form_handler(form_config): |
32 return formmail2.make_handler('test', form_config)[1] | 35 return formmail2.make_handler('test', form_config)[1] |
33 | 36 |
34 | 37 |
38 @pytest.fixture() | |
39 def log_path(form_config, tmpdir_factory): | |
40 log_file = form_config['csv_log'].value | |
41 log = tmpdir_factory.mktemp(log_file).join(log_file) | |
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.', |
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, monkeypatch): |
59 # override the configured test.csv path | |
60 monkeypatch.chdir(os.path.dirname(log_path)) | |
Vasily Kuznetsov
2017/02/08 18:16:17
As discussed over TS, we won't need this if overri
Jon Sonesen
2017/02/10 13:56:59
Done.
| |
49 host, port = 'test.local', 80 | 61 host, port = 'test.local', 80 |
50 urllib_intercept.install_opener() | 62 urllib_intercept.install_opener() |
51 add_wsgi_intercept(host, port, lambda: form_handler) | 63 add_wsgi_intercept(host, port, lambda: form_handler) |
52 url = 'http://{}:{}'.format(host, port) | 64 url = 'http://{}:{}'.format(host, port) |
53 | 65 |
54 def response_for(data): | 66 def response_for(data): |
55 if data is None: | 67 if data is None: |
56 response = urlopen(url) | 68 response = urlopen(url) |
57 else: | 69 else: |
58 response = urlopen(url, urlencode(data)) | 70 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'] | 113 del form_data['non_mandatory_email'] |
102 assert response_for(form_data) == (200, '') | 114 assert response_for(form_data) == (200, '') |
103 | 115 |
104 | 116 |
105 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): | 117 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): |
106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | 118 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
107 del form_data['mandatory'] | 119 del form_data['mandatory'] |
108 with pytest.raises(HTTPError) as error: | 120 with pytest.raises(HTTPError) as error: |
109 response_for(form_data) | 121 response_for(form_data) |
110 assert error.value.read() == 'No mandatory entered' | 122 assert error.value.read() == 'No mandatory entered' |
123 | |
124 | |
125 def test_collect_with_tmpl(log_path, form_data): | |
126 form_data['time'] = 'test' | |
127 formmail2.collect_formdata('test', form_data, log_path) | |
128 with open(log_path) as csvfile: | |
129 reader = DictReader(csvfile) | |
130 row = reader.next() | |
131 assert row == form_data | |
132 | |
133 | |
134 def test_collect_no_tmpl(log_path, form_data, form_config): | |
135 del(form_config['template']) | |
136 form_data['time'] = 'test' | |
137 formmail2.collect_formdata('test', form_data, log_path) | |
138 with open(log_path) as csvfile: | |
139 reader = DictReader(csvfile) | |
140 row = reader.next() | |
141 assert row == form_data | |
142 | |
143 | |
144 def test_fieldnames(log_path, form_data): | |
145 form_data['time'] = str(datetime.datetime.now()) | |
146 formmail2.collect_formdata('test', form_data, log_path) | |
147 with open(log_path) as csvfile: | |
148 reader = DictReader(csvfile) | |
149 for field in reader.fieldnames: | |
150 assert field in tuple(form_data.keys()) | |
151 | |
152 | |
Vasily Kuznetsov
2017/02/08 18:16:16
We should also have a test for the situation when
Jon Sonesen
2017/02/10 13:57:00
Great
| |
153 def test_append_log(form_data, log_path): | |
154 """ | |
155 collect data twice, altering a field in the second call | |
156 assert that the 2nd row is equal to the resulting form data | |
157 """ | |
158 form_data['time'] = str(datetime.datetime.now()) | |
159 formmail2.collect_formdata('test', form_data, log_path) | |
160 form_data['email'] = 'test@foo.com' | |
161 formmail2.collect_formdata('test', form_data, log_path) | |
162 with open(log_path) as csvfile: | |
163 reader = DictReader(csvfile, fieldnames=form_data.keys()) | |
164 # header | |
165 reader.next() | |
166 row1 = reader.next() | |
167 row2 = reader.next() | |
168 | |
169 assert row2['email'] == form_data['email'] | |
170 assert row2['email'] != row1['email'] | |
OLD | NEW |