Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 # This file is part of the Adblock Plus web scripts, | |
2 # Copyright (C) 2017-present 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 sys | |
17 from urllib import urlencode | |
18 from urllib2 import urlopen, HTTPError | |
19 | |
20 import pytest | |
21 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, | |
22 remove_wsgi_intercept) | |
23 | |
24 sys.modules['MySQLdb'] = sys | |
Vasily Kuznetsov
2019/02/05 17:12:08
We should probably add a comment here explaining w
rhowell
2019/02/07 03:54:33
Done.
| |
25 from sitescripts.reports.web.updateReport import handleRequest | |
26 | |
27 LOCAL_HOST = 'test.local' | |
28 REMOTE_HOST = 'reports.adblockplus.org' | |
29 PORT = 80 | |
30 PLAINTEXT_GUID = '12345678-1234-1234-1234-123456789abc' | |
31 | |
32 | |
33 def intercept_fn(environ, start_response): | |
34 assert environ['SERVER_NAME'] == REMOTE_HOST | |
35 assert PLAINTEXT_GUID in environ['PATH_INFO'] | |
36 return 'Intercepted!' | |
37 | |
38 | |
39 @pytest.fixture | |
40 def response_for(): | |
41 """Register two intercepts, and return responses for them.""" | |
42 urllib_intercept.install_opener() | |
43 add_wsgi_intercept(LOCAL_HOST, PORT, lambda: handleRequest) | |
44 add_wsgi_intercept(REMOTE_HOST, 443, lambda: intercept_fn) | |
45 | |
46 def response_for(data): | |
47 url = 'http://{}:{}'.format(LOCAL_HOST, PORT) | |
48 response = urlopen(url, urlencode(data)) | |
49 return response.code, response.read() | |
50 | |
51 yield response_for | |
52 remove_wsgi_intercept() | |
53 | |
54 | |
55 @pytest.fixture | |
56 def form_data(): | |
57 return { | |
58 'email': 'john_doe@gmail.com', | |
59 'secret': '92b3e705f2abe74c20c1c5ea9abd9ba2', | |
60 'guid': PLAINTEXT_GUID, | |
61 'status': 'x' * 1025, | |
62 'usefulness': 'test USEFULNESS', | |
63 'notify': 'test NOTIFY', | |
64 'test_mode': True, | |
65 } | |
66 | |
67 | |
68 @pytest.mark.parametrize('field,message', [ | |
69 (('guid', ''), 'Invalid or missing report GUID'), | |
70 (('secret', ''), 'Wrong secret value'), | |
71 ]) | |
72 def test_http_errs(field, message, response_for, form_data, mocker): | |
73 mocker.patch('sitescripts.reports.web.updateReport.getReport', | |
Vasily Kuznetsov
2019/02/05 17:12:08
This patch could be made into a fixture, since we
rhowell
2019/02/07 03:54:32
Each of the three calls to getReport return someth
Vasily Kuznetsov
2019/02/07 20:19:15
Yeah, you're right. Not worth it.
rhowell
2019/02/08 01:34:51
Acknowledged.
| |
74 new=lambda *args: {'usefulness': 1}) | |
75 key, value = field | |
76 form_data[key] = value | |
77 with pytest.raises(HTTPError) as error: | |
78 response_for(form_data) | |
79 | |
80 assert message in error.value.read() | |
81 | |
82 | |
83 def test_success(response_for, form_data, mocker): | |
84 # These methods are patched to avoid the need for a MySQL database | |
85 mocker.patch('sitescripts.reports.web.updateReport.getReport', | |
86 new=lambda *args: {'usefulness': 1, 'email': 'test@test.com'}) | |
87 mocker.patch('sitescripts.reports.web.updateReport.saveReport', | |
Vasily Kuznetsov
2019/02/05 17:12:08
If you want a noop function, you actually don't ev
rhowell
2019/02/07 03:54:33
Ah, nice. Good idea.
| |
88 new=lambda *args: None) | |
89 mocker.patch('sitescripts.reports.web.updateReport.updateUserUsefulness', | |
90 new=lambda *args: None) | |
91 mocker.patch('sitescripts.reports.web.updateReport.sendUpdateNotification', | |
92 new=lambda *args: None) | |
93 assert response_for(form_data) == (200, '\nIntercepted!') | |
94 | |
95 | |
96 def test_get_report_error(response_for, form_data, mocker): | |
97 mocker.patch('sitescripts.reports.web.updateReport.getReport', | |
98 new=lambda *args: None) | |
rhowell
2019/02/07 03:54:33
For some reason, I get an error if I don't provide
Vasily Kuznetsov
2019/02/07 20:19:15
I tested it a bit and it seems that when we provid
rhowell
2019/02/08 01:34:51
Done.
| |
99 with pytest.raises(HTTPError) as error: | |
100 response_for(form_data) | |
101 | |
102 assert 'Report does not exist' in error.value.read() | |
OLD | NEW |