Index: sitescripts/reports/tests/test_updateReport.py |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/sitescripts/reports/tests/test_updateReport.py |
@@ -0,0 +1,83 @@ |
+# This file is part of the Adblock Plus web scripts, |
+# Copyright (C) 2017-present eyeo GmbH |
+# |
+# Adblock Plus is free software: you can redistribute it and/or modify |
+# it under the terms of the GNU General Public License version 3 as |
+# published by the Free Software Foundation. |
+# |
+# Adblock Plus is distributed in the hope that it will be useful, |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+# GNU General Public License for more details. |
+# |
+# You should have received a copy of the GNU General Public License |
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ |
+from urllib import urlencode |
+from urllib2 import urlopen, HTTPError |
+ |
+import pytest |
+from wsgi_intercept import (urllib_intercept, add_wsgi_intercept, |
+ remove_wsgi_intercept) |
+ |
+from sitescripts.reports.web.updateReport import handleRequest |
+ |
+LOCAL_HOST = 'test.local' |
+REMOTE_HOST = 'reports.adblockplus.org' |
+PORT = 80 |
+PLAINTEXT_GUID = '12345678-1234-1234-1234-123456789abc' |
+ |
+ |
+def intercept_fn(environ, start_response): |
+ assert environ['SERVER_NAME'] == REMOTE_HOST |
+ assert PLAINTEXT_GUID in environ['PATH_INFO'] |
+ return 'Intercepted!' |
+ |
+ |
+@pytest.fixture |
+def response_for(): |
+ """Register two intercepts, and returns responses for them.""" |
+ urllib_intercept.install_opener() |
+ add_wsgi_intercept(LOCAL_HOST, PORT, lambda: handleRequest) |
+ add_wsgi_intercept(REMOTE_HOST, 443, lambda: intercept_fn) |
+ |
+ def response_for(data): |
+ url = 'http://{}:{}'.format(LOCAL_HOST, PORT) |
+ if data is None: |
+ response = urlopen(url) |
+ else: |
+ response = urlopen(url, urlencode(data)) |
+ return response.code, response.read() |
+ |
+ yield response_for |
+ remove_wsgi_intercept() |
+ |
+ |
+@pytest.fixture |
+def form_data(): |
+ return { |
+ 'email': 'john_doe@gmail.com', |
+ 'secret': '92b3e705f2abe74c20c1c5ea9abd9ba2', |
+ 'guid': PLAINTEXT_GUID, |
+ 'status': 'test STATUS', |
+ 'usefulness': 'test USEFULNESS', |
+ 'notify': 'test NOTIFY', |
+ 'test_mode': True, |
+ } |
+ |
+ |
+@pytest.mark.parametrize('field,message', [ |
+ (('guid', ''), 'Invalid or missing report GUID'), |
+ (('secret', ''), 'Wrong secret value'), |
+]) |
+def test_http_errs(field, message, response_for, form_data): |
+ key, value = field |
+ form_data[key] = value |
+ with pytest.raises(HTTPError) as error: |
+ response_for(form_data) |
+ |
+ assert message in error.value.read() |
+ |
+ |
+def test_success(response_for, form_data): |
+ assert response_for(form_data) == (200, '\nIntercepted!') |