OLD | NEW |
| (Empty) |
1 # This file is part of the Adblock Plus web scripts, | |
2 # Copyright (C) 2006-present eyeo GmbH | |
3 # | |
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 | |
6 # published by the Free Software Foundation. | |
7 # | |
8 # Adblock Plus is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
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/>. | |
15 | |
16 from urllib import urlencode | |
17 from urllib2 import urlopen, HTTPError | |
18 from csv import DictReader | |
19 | |
20 import pytest | |
21 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, | |
22 remove_wsgi_intercept) | |
23 | |
24 from sitescripts.formmail.web import formmail2 | |
25 | |
26 HOST = 'test.local' | |
27 LOG_PORT = 80 | |
28 NO_LOG_PORT = 81 | |
29 | |
30 | |
31 @pytest.fixture | |
32 def log_path(tmpdir): | |
33 return str(tmpdir.join('test.csv_log')) | |
34 | |
35 | |
36 @pytest.fixture | |
37 def log_form_config(): | |
38 return formmail2.conf_parse(formmail2.get_config_items())['test'] | |
39 | |
40 | |
41 @pytest.fixture | |
42 def form_config(): | |
43 config = formmail2.conf_parse(formmail2.get_config_items())['test'] | |
44 del config['csv_log'] | |
45 return config | |
46 | |
47 | |
48 @pytest.fixture | |
49 def form_handler(log_path, form_config, log_form_config): | |
50 """ Create two handlers, one that logs and another that doesn't """ | |
51 log_form_config['csv_log'].value = log_path | |
52 return (formmail2.make_handler('test', log_form_config)[1], | |
53 formmail2.make_handler('test', form_config)[1]) | |
54 | |
55 | |
56 # We make this a fixture instead of a constant so we can modify it in each | |
57 # test as needed without affecting other tests. | |
58 @pytest.fixture | |
59 def form_data(): | |
60 return { | |
61 'email': 'john_doe@gmail.com', | |
62 'mandatory': 'john_doe@gmail.com', | |
63 'non_mandatory_message': 'Once upon a time\nthere lived a king.', | |
64 'non_mandatory_email': 'test@test.com', | |
65 } | |
66 | |
67 | |
68 @pytest.fixture | |
69 def response_for(form_handler): | |
70 """ Registers two intercepts, returns responses for them based on bool """ | |
71 urllib_intercept.install_opener() | |
72 add_wsgi_intercept(HOST, LOG_PORT, lambda: form_handler[0]) | |
73 add_wsgi_intercept(HOST, NO_LOG_PORT, lambda: form_handler[1]) | |
74 | |
75 def response_for(data, log=False): | |
76 if log: | |
77 url = 'http://{}:{}'.format(HOST, LOG_PORT) | |
78 else: | |
79 url = 'http://{}:{}'.format(HOST, NO_LOG_PORT) | |
80 if data is None: | |
81 response = urlopen(url) | |
82 else: | |
83 response = urlopen(url, urlencode(data)) | |
84 return response.code, response.read() | |
85 | |
86 yield response_for | |
87 remove_wsgi_intercept() | |
88 | |
89 | |
90 @pytest.fixture | |
91 def sm_mock(mocker): | |
92 return mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | |
93 | |
94 | |
95 @pytest.mark.parametrize('key,message', [ | |
96 ('url', 'No URL configured for form handler: test'), | |
97 ('fields', 'No fields configured for form handler: test'), | |
98 ('template', 'No template configured for form handler: test'), | |
99 ]) | |
100 def test_config_errors(key, message, form_config): | |
101 del form_config[key] | |
102 with pytest.raises(Exception) as error: | |
103 formmail2.make_handler('test', form_config)[1] | |
104 assert error.value.message == message | |
105 | |
106 | |
107 @pytest.mark.parametrize('field,message', [ | |
108 (('new_field', 'foo'), 'Unexpected field/fields: new_field'), | |
109 (('mandatory', ''), 'No mandatory entered'), | |
110 (('non_mandatory_email', 'asfaf'), 'Invalid email'), | |
111 (('email', 'asfaf'), 'You failed the email validation'), | |
112 (('email', ''), 'You failed the email test'), | |
113 ]) | |
114 def test_http_errs(field, message, response_for, form_data, sm_mock): | |
115 key, value = field | |
116 form_data[key] = value | |
117 with pytest.raises(HTTPError) as error: | |
118 response_for(form_data) | |
119 assert error.value.read() == message | |
120 | |
121 | |
122 @pytest.mark.parametrize('field,expected', [ | |
123 (('non_mandatory_message', '\xc3\xb6'), (200, '')), | |
124 (('non_mandatory_message', ''), (200, '')), | |
125 ]) | |
126 def test_success(field, expected, log_path, response_for, form_data, sm_mock): | |
127 key, value = field | |
128 form_data[key] = value | |
129 assert response_for(form_data, log=False) == expected | |
130 assert sm_mock.call_count == 1 | |
131 | |
132 params = sm_mock.call_args[0][1]['fields'] | |
133 assert set(params.keys()) == set(form_data.keys()) | |
134 for key, value in form_data.items(): | |
135 assert params[key] == value.decode('utf8') | |
136 | |
137 assert response_for(form_data, log=True) == expected | |
138 assert sm_mock.call_count == 2 | |
139 | |
140 assert response_for(form_data, log=True) == expected | |
141 assert sm_mock.call_count == 3 | |
142 | |
143 with open(log_path) as log_file: | |
144 reader = DictReader(log_file) | |
145 row = reader.next() | |
146 # rows should not be equal because the time field | |
147 # is added by the logging function. | |
148 assert row != reader.next() | |
149 | |
150 | |
151 def test_config_field_errors(form_config): | |
152 form_config['fields'] = {} | |
153 with pytest.raises(Exception) as error: | |
154 formmail2.make_handler('test', form_config)[1] | |
155 assert error.value.message == 'No fields configured for form handler: test' | |
156 | |
157 | |
158 def test_config_template_errors(form_config): | |
159 form_config['template'].value = 'no' | |
160 with pytest.raises(Exception) as error: | |
161 formmail2.make_handler('test', form_config)[1] | |
162 assert error.value.message == 'Template not found at: no' | |
163 | |
164 | |
165 def test_config_parse(form_config): | |
166 assert form_config['url'].value == 'test/apply/submit' | |
167 assert form_config['fields']['email'].value == 'mandatory, email' | |
168 | |
169 | |
170 def test_sendmail_fail(log_path, response_for, form_data, sm_mock): | |
171 sm_mock.side_effect = Exception('Sendmail Fail') | |
172 with pytest.raises(HTTPError): | |
173 response_for(form_data, log=True) | |
174 | |
175 with open(log_path) as log_file: | |
176 row = DictReader(log_file).next() | |
177 assert row != form_data | |
178 | |
179 | |
180 def test_append_field_err(form_config, form_data, log_path): | |
181 """ Checks that error logs are correctly written and appended | |
182 | |
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 | |
185 """ | |
186 formmail2.log_formdata(form_data, log_path) | |
187 del form_data['email'] | |
188 | |
189 # submit two forms with fields that dont match the config | |
190 # this should append the second form to the error log file | |
191 with pytest.raises(Exception): | |
192 formmail2.log_formdata(form_data, log_path) | |
193 with pytest.raises(Exception): | |
194 formmail2.log_formdata(form_data, log_path) | |
195 | |
196 with open(log_path + '_error') as error_log: | |
197 reader = DictReader(error_log) | |
198 assert reader.next() == form_data | |
199 assert reader.next() == form_data | |
OLD | NEW |