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

Delta Between Two Patch Sets: sitescripts/formmail/test/test_formmail2.py

Issue 29352643: Issue 4377 - Add Configurable Form to Email Service (Closed)
Left Patch Set: addressed comments Created Sept. 12, 2016, 12:16 p.m.
Right Patch Set: fixed make_error to properly build message and fix test to fail if message not built Created Sept. 12, 2016, 3:54 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2016 Eyeo GmbH 2 # Copyright (C) 2006-2016 Eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 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 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 21 matching lines...) Expand all
32 return formmail2.make_handler('test', form_config)[1] 32 return formmail2.make_handler('test', form_config)[1]
33 33
34 34
35 # We make this a fixture instead of a constant so we can modify it in each 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. 36 # test as needed without affecting other tests.
37 @pytest.fixture 37 @pytest.fixture
38 def form_data(): 38 def form_data():
39 return { 39 return {
40 'email': 'john_doe@gmail.com', 40 'email': 'john_doe@gmail.com',
41 'mandatory': 'john_doe@gmail.com', 41 'mandatory': 'john_doe@gmail.com',
42 'non_mandatory': 'Once upon a time\nthere lived a king.' 42 'non_mandatory_message': 'Once upon a time\nthere lived a king.',
43 'non_mandatory_email': 'test@test.com'
43 } 44 }
44 45
45 46
46 @pytest.fixture() 47 @pytest.fixture()
47 def response_for(form_handler): 48 def response_for(form_handler):
48 host, port = 'test.local', 80 49 host, port = 'test.local', 80
49 urllib_intercept.install_opener() 50 urllib_intercept.install_opener()
50 add_wsgi_intercept(host, port, lambda: form_handler) 51 add_wsgi_intercept(host, port, lambda: form_handler)
51 url = 'http://{}:{}'.format(host, port) 52 url = 'http://{}:{}'.format(host, port)
52 53
(...skipping 11 matching lines...) Expand all
64 def test_config_parse(form_config): 65 def test_config_parse(form_config):
65 assert form_config['url'].value == 'test/apply/submit' 66 assert form_config['url'].value == 'test/apply/submit'
66 assert form_config['fields']['email'].value == 'mandatory, email' 67 assert form_config['fields']['email'].value == 'mandatory, email'
67 68
68 69
69 def test_success(response_for, form_data, mocker): 70 def test_success(response_for, form_data, mocker):
70 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail') 71 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
71 assert response_for(form_data) == (200, '') 72 assert response_for(form_data) == (200, '')
72 assert sm_mock.call_count == 1 73 assert sm_mock.call_count == 1
73 params = sm_mock.call_args[0][1]['fields'] 74 params = sm_mock.call_args[0][1]['fields']
74 assert set(params.keys()) == set(form_data.keys()) | {'time'} 75 assert set(params.keys()) == set(form_data.keys())
75 for key, value in form_data.items(): 76 for key, value in form_data.items():
76 assert params[key] == value 77 assert params[key] == value
77 78
78 79
79 def test_non_mandatory_no_msg(response_for, form_data, mocker): 80 def test_non_mandatory_no_msg(response_for, form_data, mocker):
80 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') 81 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
81 del form_data['non_mandatory'] 82 form_data['non_mandatory'] = ''
82 assert response_for(form_data) == (200, '') 83 assert response_for(form_data) == (200, '')
83 84
84 85
85 def test_invalid_email_cstm_msg(response_for, form_data, mocker, form_config): 86 def test_invalid_email_cstm_msg(response_for, form_data, mocker, form_config):
86 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') 87 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
87 form_data['email'] = 'bademail' 88 form_data['email'] = 'bademail'
88 with pytest.raises(HTTPError) as error: 89 with pytest.raises(HTTPError) as error:
89 response_for(form_data) 90 response_for(form_data)
90 assert error.value.read() == 'You failed the email validation' 91 assert error.value.read() == 'You failed the email validation'
91 92
92 93
93 def test_mandatory_fail_cstm_msg(response_for, form_data, mocker): 94 def test_valid_nan_mandatory_email(response_for, form_data, mocker):
94 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') 95 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
95 del form_data['email'] 96 form_data['non_mandatory_email'] = 'asfaf'
96 with pytest.raises(HTTPError) as error: 97 with pytest.raises(HTTPError) as error:
97 response_for(form_data) 98 response_for(form_data)
98 assert error.value.read() == 'You failed the email test' 99 assert error.value.read() == 'Invalid email'
100
101 del form_data['non_mandatory_email']
102 assert response_for(form_data) == (200, '')
99 103
100 104
101 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): 105 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker):
102 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') 106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
103 del form_data['mandatory'] 107 del form_data['mandatory']
104 with pytest.raises(HTTPError) as error: 108 with pytest.raises(HTTPError) as error:
105 response_for(form_data) 109 response_for(form_data)
106 assert error.value.read() == 'No mandatory entered' 110 assert error.value.read() == 'No mandatory entered'
LEFTRIGHT

Powered by Google App Engine
This is Rietveld