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