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 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 if data is None: | 55 if data is None: |
56 response = urlopen(url) | 56 response = urlopen(url) |
57 else: | 57 else: |
58 response = urlopen(url, urlencode(data)) | 58 response = urlopen(url, urlencode(data)) |
59 return response.code, response.read() | 59 return response.code, response.read() |
60 | 60 |
61 yield response_for | 61 yield response_for |
62 remove_wsgi_intercept() | 62 remove_wsgi_intercept() |
63 | 63 |
64 | 64 |
| 65 def test_form_handler_email_errors(form_config): |
| 66 tmp_config = form_config |
| 67 del tmp_config['url'].value |
| 68 with pytest.raises(Exception) as error: |
| 69 formmail2.make_handler('test', tmp_config)[1] |
| 70 assert error.value.message == 'No URL configured for form handler: test' |
| 71 |
| 72 |
| 73 def test_form_handler_field_errors(form_config): |
| 74 tmp_config = form_config |
| 75 tmp_config['fields'] = {} |
| 76 with pytest.raises(Exception) as error: |
| 77 formmail2.make_handler('test', tmp_config)[1] |
| 78 assert error.value.message == 'No fields configured for form handler: test' |
| 79 |
| 80 del tmp_config['fields'] |
| 81 with pytest.raises(Exception) as error: |
| 82 formmail2.make_handler('test', tmp_config)[1] |
| 83 assert error.value.message == 'No fields configured for form handler: test' |
| 84 |
| 85 |
| 86 def test_form_handler_template_errors(form_config): |
| 87 tmp_config = form_config |
| 88 tmp_config['template'].value = 'no' |
| 89 with pytest.raises(Exception) as error: |
| 90 formmail2.make_handler('test', tmp_config)[1] |
| 91 assert error.typename == 'TemplateNotFound' |
| 92 |
| 93 del tmp_config['template'].value |
| 94 with pytest.raises(Exception) as error: |
| 95 formmail2.make_handler('test', tmp_config)[1] |
| 96 assert error.value.message == ('No template configured for form handler' |
| 97 ': test') |
| 98 del tmp_config['template'] |
| 99 with pytest.raises(Exception) as error: |
| 100 formmail2.make_handler('test', tmp_config)[1] |
| 101 assert error.value.message == ('No template configured for form handler' |
| 102 ': test') |
| 103 |
| 104 |
65 def test_config_parse(form_config): | 105 def test_config_parse(form_config): |
66 assert form_config['url'].value == 'test/apply/submit' | 106 assert form_config['url'].value == 'test/apply/submit' |
67 assert form_config['fields']['email'].value == 'mandatory, email' | 107 assert form_config['fields']['email'].value == 'mandatory, email' |
68 | 108 |
69 | 109 |
70 def test_success(response_for, form_data, mocker): | 110 def test_success(response_for, form_data, mocker): |
71 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | 111 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
72 assert response_for(form_data) == (200, '') | 112 assert response_for(form_data) == (200, '') |
73 assert sm_mock.call_count == 1 | 113 assert sm_mock.call_count == 1 |
74 params = sm_mock.call_args[0][1]['fields'] | 114 params = sm_mock.call_args[0][1]['fields'] |
(...skipping 26 matching lines...) Expand all Loading... |
101 del form_data['non_mandatory_email'] | 141 del form_data['non_mandatory_email'] |
102 assert response_for(form_data) == (200, '') | 142 assert response_for(form_data) == (200, '') |
103 | 143 |
104 | 144 |
105 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): | 145 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): |
106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | 146 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
107 del form_data['mandatory'] | 147 del form_data['mandatory'] |
108 with pytest.raises(HTTPError) as error: | 148 with pytest.raises(HTTPError) as error: |
109 response_for(form_data) | 149 response_for(form_data) |
110 assert error.value.read() == 'No mandatory entered' | 150 assert error.value.read() == 'No mandatory entered' |
OLD | NEW |