LEFT | RIGHT |
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 19 matching lines...) Expand all Loading... |
30 @pytest.fixture() | 30 @pytest.fixture() |
31 def form_handler(form_config): | 31 def form_handler(form_config): |
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 'name': 'John Doe', | |
41 'email': 'john_doe@gmail.com', | 40 'email': 'john_doe@gmail.com', |
42 'subject': 'hey there!', | 41 'mandatory': 'john_doe@gmail.com', |
43 'message': 'Once upon a time\nthere lived a king.', | 42 'non_mandatory_message': 'Once upon a time\nthere lived a king.', |
44 'non_mandatory': 'Once upon a time\nthere lived a king.' | 43 'non_mandatory_email': 'test@test.com' |
45 } | 44 } |
46 | 45 |
47 | 46 |
48 @pytest.fixture() | 47 @pytest.fixture() |
49 def response_for(form_handler): | 48 def response_for(form_handler): |
50 host, port = 'test.local', 80 | 49 host, port = 'test.local', 80 |
51 urllib_intercept.install_opener() | 50 urllib_intercept.install_opener() |
52 add_wsgi_intercept(host, port, lambda: form_handler) | 51 add_wsgi_intercept(host, port, lambda: form_handler) |
53 url = 'http://{}:{}'.format(host, port) | 52 url = 'http://{}:{}'.format(host, port) |
54 | 53 |
(...skipping 11 matching lines...) Expand all Loading... |
66 def test_config_parse(form_config): | 65 def test_config_parse(form_config): |
67 assert form_config['url'].value == 'test/apply/submit' | 66 assert form_config['url'].value == 'test/apply/submit' |
68 assert form_config['fields']['email'].value == 'mandatory, email' | 67 assert form_config['fields']['email'].value == 'mandatory, email' |
69 | 68 |
70 | 69 |
71 def test_success(response_for, form_data, mocker): | 70 def test_success(response_for, form_data, mocker): |
72 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | 71 sm_mock = mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
73 assert response_for(form_data) == (200, '') | 72 assert response_for(form_data) == (200, '') |
74 assert sm_mock.call_count == 1 | 73 assert sm_mock.call_count == 1 |
75 params = sm_mock.call_args[0][1]['fields'] | 74 params = sm_mock.call_args[0][1]['fields'] |
76 assert set(params.keys()) == set(form_data.keys()) | {'time'} | 75 assert set(params.keys()) == set(form_data.keys()) |
77 for key, value in form_data.items(): | 76 for key, value in form_data.items(): |
78 assert params[key] == value | 77 assert params[key] == value |
79 | 78 |
80 | 79 |
81 def test_no_name(response_for, form_data, mocker): | 80 def test_non_mandatory_no_msg(response_for, form_data, mocker): |
82 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | 81 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
83 del form_data['name'] | 82 form_data['non_mandatory'] = '' |
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): | |
106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | |
107 assert response_for(form_data) == (200, '') | 83 assert response_for(form_data) == (200, '') |
108 | 84 |
109 | 85 |
110 def test_non_mandatory_no_msg(response_for, form_data, mocker): | 86 def test_invalid_email_cstm_msg(response_for, form_data, mocker, form_config): |
111 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') | 87 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
112 del form_data['non_mandatory'] | 88 form_data['email'] = 'bademail' |
| 89 with pytest.raises(HTTPError) as error: |
| 90 response_for(form_data) |
| 91 assert error.value.read() == 'You failed the email validation' |
| 92 |
| 93 |
| 94 def test_valid_nan_mandatory_email(response_for, form_data, mocker): |
| 95 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
| 96 form_data['non_mandatory_email'] = 'asfaf' |
| 97 with pytest.raises(HTTPError) as error: |
| 98 response_for(form_data) |
| 99 assert error.value.read() == 'Invalid email' |
| 100 |
| 101 del form_data['non_mandatory_email'] |
113 assert response_for(form_data) == (200, '') | 102 assert response_for(form_data) == (200, '') |
| 103 |
| 104 |
| 105 def test_mandatory_fail_dflt_msg(response_for, form_data, mocker): |
| 106 mocker.patch('sitescripts.formmail.web.formmail2.sendMail') |
| 107 del form_data['mandatory'] |
| 108 with pytest.raises(HTTPError) as error: |
| 109 response_for(form_data) |
| 110 assert error.value.read() == 'No mandatory entered' |
LEFT | RIGHT |