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 from urllib import urlencode |
| 17 from urllib2 import urlopen, HTTPError |
| 18 |
| 19 import pytest |
| 20 from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, |
| 21 remove_wsgi_intercept) |
| 22 |
| 23 from sitescripts.reports.web.updateReport import handleRequest |
| 24 |
| 25 LOCAL_HOST = 'test.local' |
| 26 REMOTE_HOST = 'reports.adblockplus.org' |
| 27 PORT = 80 |
| 28 PLAINTEXT_GUID = '12345678-1234-1234-1234-123456789abc' |
| 29 |
| 30 |
| 31 def intercept_fn(environ, start_response): |
| 32 assert environ['SERVER_NAME'] == REMOTE_HOST |
| 33 assert PLAINTEXT_GUID in environ['PATH_INFO'] |
| 34 return 'Intercepted!' |
| 35 |
| 36 |
| 37 @pytest.fixture |
| 38 def response_for(): |
| 39 """Register two intercepts, and returns responses for them.""" |
| 40 urllib_intercept.install_opener() |
| 41 add_wsgi_intercept(LOCAL_HOST, PORT, lambda: handleRequest) |
| 42 add_wsgi_intercept(REMOTE_HOST, 443, lambda: intercept_fn) |
| 43 |
| 44 def response_for(data): |
| 45 url = 'http://{}:{}'.format(LOCAL_HOST, PORT) |
| 46 if data is None: |
| 47 response = urlopen(url) |
| 48 else: |
| 49 response = urlopen(url, urlencode(data)) |
| 50 return response.code, response.read() |
| 51 |
| 52 yield response_for |
| 53 remove_wsgi_intercept() |
| 54 |
| 55 |
| 56 @pytest.fixture |
| 57 def form_data(): |
| 58 return { |
| 59 'email': 'john_doe@gmail.com', |
| 60 'secret': '92b3e705f2abe74c20c1c5ea9abd9ba2', |
| 61 'guid': PLAINTEXT_GUID, |
| 62 'status': 'test STATUS', |
| 63 'usefulness': 'test USEFULNESS', |
| 64 'notify': 'test NOTIFY', |
| 65 'test_mode': True, |
| 66 } |
| 67 |
| 68 |
| 69 @pytest.mark.parametrize('field,message', [ |
| 70 (('guid', ''), 'Invalid or missing report GUID'), |
| 71 (('secret', ''), 'Wrong secret value'), |
| 72 ]) |
| 73 def test_http_errs(field, message, response_for, form_data): |
| 74 key, value = field |
| 75 form_data[key] = value |
| 76 with pytest.raises(HTTPError) as error: |
| 77 response_for(form_data) |
| 78 |
| 79 assert message in error.value.read() |
| 80 |
| 81 |
| 82 def test_success(response_for, form_data): |
| 83 assert response_for(form_data) == (200, '\nIntercepted!') |
OLD | NEW |