Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/formmail/test/test_formmail2.py

Issue 29352643: Issue 4377 - Add Configurable Form to Email Service (Closed)
Patch Set: Created Sept. 12, 2016, 11 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 'name': 'John Doe',
41 'email': 'john_doe@gmail.com',
42 'subject': 'hey there!',
43 'message': 'Once upon a time\nthere lived a king.',
44 'non_mandatory': 'Once upon a time\nthere lived a king.'
45 }
46
47
48 @pytest.fixture()
49 def response_for(form_handler):
50 host, port = 'test.local', 80
51 urllib_intercept.install_opener()
52 add_wsgi_intercept(host, port, lambda: form_handler)
53 url = 'http://{}:{}'.format(host, port)
54
55 def response_for(data):
56 if data is None:
57 response = urlopen(url)
58 else:
59 response = urlopen(url, urlencode(data))
60 return response.code, response.read()
61
62 yield response_for
63 remove_wsgi_intercept()
64
65
66 def test_config_parse(form_config):
67 assert form_config['url'].value == 'test/apply/submit'
68 assert form_config['fields']['email'].value == 'mandatory, email'
69
70
71 def test_success(response_for, form_data, mocker):
72 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
73 assert response_for(form_data) == (200, '')
74 assert sm_mock.call_count == 1
75 params = sm_mock.call_args[0][1]['fields']
76 assert set(params.keys()) == set(form_data.keys()) | {'time'}
77 for key, value in form_data.items():
78 assert params[key] == value
79
80
81 def test_no_name(response_for, form_data, mocker):
82 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
83 del form_data['name']
84 with pytest.raises(HTTPError) as error:
85 response_for(form_data)
86 assert error.value.read() == 'You failed the name test'
87
88
89 def test_no_email(response_for, form_data, mocker):
90 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
91 del form_data['email']
92 with pytest.raises(HTTPError) as error:
93 response_for(form_data)
94 assert error.value.read() == 'You failed the email test'
95
96
97 def test_no_subject(response_for, form_data, mocker):
98 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
99 del form_data['subject']
100 with pytest.raises(HTTPError) as error:
101 response_for(form_data)
102 assert error.value.read() == 'You failed the subject test'
103
104
105 def test_non_mandatory_msg(response_for, form_data, mocker):
Vasily Kuznetsov 2016/09/12 11:33:20 This test seems redundant. Isn't it the same as th
106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
107 assert response_for(form_data) == (200, '')
108
109
110 def test_non_mandatory_no_msg(response_for, form_data, mocker):
111 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
112 del form_data['non_mandatory']
113 assert response_for(form_data) == (200, '')
OLDNEW

Powered by Google App Engine
This is Rietveld