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 |
| 16 from urllib import urlencode |
| 17 from urllib2 import urlopen |
| 18 |
| 19 import pytest |
| 20 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, |
| 21 remove_wsgi_intercept) |
| 22 |
| 23 from sitescripts.formmail.web.formmail import handleRequest |
| 24 |
| 25 |
| 26 # We make this a fixture instead of a constant so we can modify it in each |
| 27 # test as needed without affecting other tests. |
| 28 @pytest.fixture |
| 29 def form_data(): |
| 30 return { |
| 31 'name': 'John Doe', |
| 32 'email': 'john_doe@gmail.com', |
| 33 'subject': 'Hello there!', |
| 34 'message': 'Once upon a time\nthere lived a king.' |
| 35 } |
| 36 |
| 37 |
| 38 @pytest.fixture() |
| 39 def response_for(): |
| 40 host, port = 'test.local', 80 |
| 41 urllib_intercept.install_opener() |
| 42 add_wsgi_intercept(host, port, lambda: handleRequest) |
| 43 url = 'http://{}:{}'.format(host, port) |
| 44 |
| 45 def response_for(data): |
| 46 if data is None: |
| 47 response = urlopen(url) |
| 48 else: |
| 49 response = urlopen(url, urlencode(data)) |
| 50 assert response.getcode() == 200 |
| 51 return response.read() |
| 52 |
| 53 yield response_for |
| 54 remove_wsgi_intercept() |
| 55 |
| 56 |
| 57 def test_get_error(response_for): |
| 58 assert response_for(None) == 'Unsupported request method' |
| 59 |
| 60 |
| 61 def test_no_name(response_for, form_data): |
| 62 del form_data['name'] |
| 63 assert response_for(form_data) == 'No name entered' |
| 64 |
| 65 |
| 66 def test_no_email(response_for, form_data): |
| 67 del form_data['email'] |
| 68 assert response_for(form_data) == 'No email address entered' |
| 69 |
| 70 |
| 71 def test_no_subject(response_for, form_data): |
| 72 del form_data['subject'] |
| 73 assert response_for(form_data) == 'No subject entered' |
| 74 |
| 75 |
| 76 def test_no_message(response_for, form_data): |
| 77 del form_data['message'] |
| 78 assert response_for(form_data) == 'No message entered' |
| 79 |
| 80 |
| 81 def test_bad_email(response_for, form_data): |
| 82 form_data['email'] = 'bad_email' |
| 83 assert response_for(form_data) == 'Invalid email address' |
| 84 |
| 85 |
| 86 def test_success(response_for, form_data, mocker): |
| 87 sm_mock = mocker.patch('sitescripts.formmail.web.formmail.sendMail') |
| 88 assert response_for(form_data) == 'Message sent' |
| 89 assert sm_mock.call_count == 1 |
| 90 params = sm_mock.call_args[0][1] |
| 91 assert set(params.keys()) == set(form_data.keys()) | {'time'} |
| 92 for key, value in form_data.items(): |
| 93 assert params[key] == value |
OLD | NEW |