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: addressed comments Created Sept. 12, 2016, 12:16 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/formmail/test/template/test.mail ('k') | sitescripts/formmail/web/formmail2.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 'email': 'john_doe@gmail.com',
41 'mandatory': 'john_doe@gmail.com',
42 'non_mandatory': 'Once upon a time\nthere lived a king.'
43 }
44
45
46 @pytest.fixture()
47 def response_for(form_handler):
48 host, port = 'test.local', 80
49 urllib_intercept.install_opener()
50 add_wsgi_intercept(host, port, lambda: form_handler)
51 url = 'http://{}:{}'.format(host, port)
52
53 def response_for(data):
54 if data is None:
55 response = urlopen(url)
56 else:
57 response = urlopen(url, urlencode(data))
58 return response.code, response.read()
59
60 yield response_for
61 remove_wsgi_intercept()
62
63
64 def test_config_parse(form_config):
65 assert form_config['url'].value == 'test/apply/submit'
66 assert form_config['fields']['email'].value == 'mandatory, email'
67
68
69 def test_success(response_for, form_data, mocker):
70 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
71 assert response_for(form_data) == (200, '')
72 assert sm_mock.call_count == 1
73 params = sm_mock.call_args[0][1]['fields']
74 assert set(params.keys()) == set(form_data.keys()) | {'time'}
75 for key, value in form_data.items():
76 assert params[key] == value
77
78
79 def test_non_mandatory_no_msg(response_for, form_data, mocker):
80 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
81 del form_data['non_mandatory']
82 assert response_for(form_data) == (200, '')
83
84
85 def test_invalid_email_cstm_msg(response_for, form_data, mocker, form_config):
86 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
87 form_data['email'] = 'bademail'
88 with pytest.raises(HTTPError) as error:
89 response_for(form_data)
90 assert error.value.read() == 'You failed the email validation'
91
92
93 def test_mandatory_fail_cstm_msg(response_for, form_data, mocker):
94 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
95 del form_data['email']
96 with pytest.raises(HTTPError) as error:
97 response_for(form_data)
98 assert error.value.read() == 'You failed the email test'
99
100
101 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker):
102 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
103 del form_data['mandatory']
104 with pytest.raises(HTTPError) as error:
105 response_for(form_data)
106 assert error.value.read() == 'No mandatory entered'
OLDNEW
« no previous file with comments | « sitescripts/formmail/test/template/test.mail ('k') | sitescripts/formmail/web/formmail2.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld