OLD | NEW |
(Empty) | |
| 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-2016 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 from urllib import urlencode |
| 16 from urllib2 import urlopen, HTTPError |
| 17 |
| 18 import pytest |
| 19 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, |
| 20 remove_wsgi_intercept) |
| 21 |
| 22 from sitescripts.formmail.web import formmail2 |
| 23 |
| 24 |
| 25 @pytest.fixture() |
| 26 def form_config(): |
| 27 return formmail2.conf_parse(formmail2.get_config_items())['test'] |
| 28 |
| 29 |
| 30 @pytest.fixture() |
| 31 def form_handler(form_config): |
| 32 return formmail2.make_handler('test', form_config)[1] |
| 33 |
| 34 |
| 35 # We make this a fixture instead of a constant so we can modify it in each |
| 36 # test as needed without affecting other tests. |
| 37 @pytest.fixture |
| 38 def form_data(): |
| 39 return { |
| 40 'email': 'john_doe@gmail.com', |
| 41 'mandatory': 'john_doe@gmail.com', |
| 42 'non_mandatory_message': 'Once upon a time\nthere lived a king.', |
| 43 'non_mandatory_email': 'test@test.com' |
| 44 } |
| 45 |
| 46 |
| 47 @pytest.fixture() |
| 48 def response_for(form_handler): |
| 49 host, port = 'test.local', 80 |
| 50 urllib_intercept.install_opener() |
| 51 add_wsgi_intercept(host, port, lambda: form_handler) |
| 52 url = 'http://{}:{}'.format(host, port) |
| 53 |
| 54 def response_for(data): |
| 55 if data is None: |
| 56 response = urlopen(url) |
| 57 else: |
| 58 response = urlopen(url, urlencode(data)) |
| 59 return response.code, response.read() |
| 60 |
| 61 yield response_for |
| 62 remove_wsgi_intercept() |
| 63 |
| 64 |
| 65 def test_config_parse(form_config): |
| 66 assert form_config['url'].value == 'test/apply/submit' |
| 67 assert form_config['fields']['email'].value == 'mandatory, email' |
| 68 |
| 69 |
| 70 def test_success(response_for, form_data, mocker): |
| 71 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
| 72 assert response_for(form_data) == (200, '') |
| 73 assert sm_mock.call_count == 1 |
| 74 params = sm_mock.call_args[0][1]['fields'] |
| 75 assert set(params.keys()) == set(form_data.keys()) |
| 76 for key, value in form_data.items(): |
| 77 assert params[key] == value |
| 78 |
| 79 |
| 80 def test_non_mandatory_no_msg(response_for, form_data, mocker): |
| 81 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
| 82 form_data['non_mandatory'] = '' |
| 83 assert response_for(form_data) == (200, '') |
| 84 |
| 85 |
| 86 def test_invalid_email_cstm_msg(response_for, form_data, mocker, form_config): |
| 87 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
| 88 form_data['email'] = 'bademail' |
| 89 with pytest.raises(HTTPError) as error: |
| 90 response_for(form_data) |
| 91 assert error.value.read() == 'You failed the email validation' |
| 92 |
| 93 |
| 94 def test_valid_nan_mandatory_email(response_for, form_data, mocker): |
| 95 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
| 96 form_data['non_mandatory_email'] = 'asfaf' |
| 97 with pytest.raises(HTTPError) as error: |
| 98 response_for(form_data) |
| 99 assert error.value.read() == 'Invalid email' |
| 100 |
| 101 del form_data['non_mandatory_email'] |
| 102 assert response_for(form_data) == (200, '') |
| 103 |
| 104 |
| 105 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): |
| 106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
| 107 del form_data['mandatory'] |
| 108 with pytest.raises(HTTPError) as error: |
| 109 response_for(form_data) |
| 110 assert error.value.read() == 'No mandatory entered' |
OLD | NEW |