| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 Loading... | |
| 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'), |
| 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 ]) |
| 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') |
| 136 | 136 |
| 137 assert response_for(form_data, log=True) == expected | 137 assert response_for(form_data, log=True) == expected |
| 138 assert sm_mock.call_count == 2 | 138 assert sm_mock.call_count == 2 |
| 139 | 139 |
| 140 assert response_for(form_data, log=True) == expected | 140 assert response_for(form_data, log=True) == expected |
| 141 assert sm_mock.call_count == 3 | 141 assert sm_mock.call_count == 3 |
| 142 | 142 |
| 143 with open(log_path) as log_file: | 143 with open(log_path) as log_file: |
| 144 reader = DictReader(log_file) | 144 reader = DictReader(log_file) |
| 145 row = reader.next() | 145 row = reader.next() |
| 146 # rows should not be equal because the time field | |
| 147 # is added by the logging function. | |
| 146 assert row != reader.next() | 148 assert row != reader.next() |
|
Vasily Kuznetsov
2017/03/23 13:19:16
Are they not equal because of time field? What do
Jon Sonesen
2017/03/23 13:31:01
Done.
| |
| 147 | 149 |
| 148 | 150 |
| 149 def test_config_field_errors(form_config): | 151 def test_config_field_errors(form_config): |
| 150 form_config['fields'] = {} | 152 form_config['fields'] = {} |
| 151 with pytest.raises(Exception) as error: | 153 with pytest.raises(Exception) as error: |
| 152 formmail2.make_handler('test', form_config)[1] | 154 formmail2.make_handler('test', form_config)[1] |
| 153 assert error.value.message == 'No fields configured for form handler: test' | 155 assert error.value.message == 'No fields configured for form handler: test' |
| 154 | 156 |
| 155 | 157 |
| 156 def test_config_template_errors(form_config): | 158 def test_config_template_errors(form_config): |
| 157 form_config['template'].value = 'no' | 159 form_config['template'].value = 'no' |
| 158 with pytest.raises(Exception) as error: | 160 with pytest.raises(Exception) as error: |
| 159 formmail2.make_handler('test', form_config)[1] | 161 formmail2.make_handler('test', form_config)[1] |
| 160 assert error.value.message == 'Template not found at: no' | 162 assert error.value.message == 'Template not found at: no' |
| 161 | 163 |
| 162 | 164 |
| 163 def test_config_parse(form_config): | 165 def test_config_parse(form_config): |
| 164 assert form_config['url'].value == 'test/apply/submit' | 166 assert form_config['url'].value == 'test/apply/submit' |
| 165 assert form_config['fields']['email'].value == 'mandatory, email' | 167 assert form_config['fields']['email'].value == 'mandatory, email' |
| 166 | 168 |
| 167 | 169 |
| 168 def test_sendmail_fail(log_path, response_for, form_data, sm_mock): | 170 def test_sendmail_fail(log_path, response_for, form_data, sm_mock): |
| 169 sm_mock.side_effect = Exception('Sendmail Fail') | 171 sm_mock.side_effect = Exception('Sendmail Fail') |
| 170 with pytest.raises(HTTPError) as error: | 172 with pytest.raises(HTTPError): |
| 171 response_for(form_data, log=True) | 173 response_for(form_data, log=True) |
| 172 assert error.typename == 'HTTPError' | |
|
Vasily Kuznetsov
2017/03/23 13:19:16
You probably don't need to assert this since you'v
Jon Sonesen
2017/03/23 13:31:01
Acknowledged.
| |
| 173 | 174 |
| 174 with open(log_path) as log_file: | 175 with open(log_path) as log_file: |
| 175 row = DictReader(log_file).next() | 176 row = DictReader(log_file).next() |
| 176 assert 'time' in row | 177 assert row != form_data |
| 177 | 178 |
| 178 | 179 |
| 179 def test_append_field_err(form_config, form_data, log_path): | 180 def test_append_field_err(form_config, form_data, log_path): |
| 180 """ Checks that error logs are correctly written and appended | 181 """ Checks that error logs are correctly written and appended |
| 181 | 182 |
| 182 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 |
| 183 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 |
| 184 """ | 185 """ |
| 185 formmail2.log_formdata(form_data, log_path) | 186 formmail2.log_formdata(form_data, log_path) |
| 186 del(form_data['email']) | 187 del form_data['email'] |
| 187 try: | 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): | |
| 188 formmail2.log_formdata(form_data, log_path) | 192 formmail2.log_formdata(form_data, log_path) |
| 189 except Exception as e: | 193 with pytest.raises(Exception): |
|
Vasily Kuznetsov
2017/03/23 13:19:16
Perhaps it's better to use with pytest.raises(...)
Jon Sonesen
2017/03/23 13:31:01
Done. Odd that I chose to use try's in the first p
| |
| 190 assert e.message == ('Field names have changed, error log ' | |
| 191 'written to {}_error').format(log_path) | |
| 192 try: | |
|
Vasily Kuznetsov
2017/03/23 13:19:16
I suppose there's some reason for trying this twic
Jon Sonesen
2017/03/23 13:31:01
Done.
| |
| 193 formmail2.log_formdata(form_data, log_path) | 194 formmail2.log_formdata(form_data, log_path) |
| 194 except Exception as e: | |
| 195 assert e.message == ('Field names have changed, error log' | |
|
Vasily Kuznetsov
2017/03/23 13:19:16
Do we need to assert the whole message here again
Jon Sonesen
2017/03/23 13:31:01
Agreed, I will remove the assertions
| |
| 196 ' appended to {}_error').format(log_path) | |
| 197 | 195 |
| 198 with open(log_path + '_error') as error_log: | 196 with open(log_path + '_error') as error_log: |
| 199 reader = DictReader(error_log) | 197 reader = DictReader(error_log) |
| 200 assert reader.next() == form_data | 198 assert reader.next() == form_data |
| 201 assert reader.next() == form_data | 199 assert reader.next() == form_data |
| LEFT | RIGHT |