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

Unified Diff: sitescripts/reports/tests/test_resolveReport.py

Issue 29584613: #4431 - Introduce sitescripts.reports.web.resolveReport handler (Closed)
Patch Set: Address comments on PS1 Created Oct. 24, 2017, 3:35 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sitescripts/reports/README.md ('k') | sitescripts/reports/web/resolveReport.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/reports/tests/test_resolveReport.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/sitescripts/reports/tests/test_resolveReport.py
@@ -0,0 +1,76 @@
+# 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/>.
+
+import base64
+import ConfigParser
+
+import pytest
+
+from sitescripts.reports.web.resolveReport import (
+ resolve_report,
+ CONF_SECTION,
+ CONF_KEY_KEY,
+ CONF_URL_KEY,
+)
+
+ENCRYPTION_KEY = '12345678901234567890123456789012'
+URL_TEMPLATE = 'https://example.com/{report_id}'
+
+PLAINTEXT_GUID = '12345678-1234-1234-1234-123456789abc'
+ENCRYPTED_GUID = ('MTIzNDU2Nzg5YWJj,9RuQq5zPNVw2fnjk7zT/jfS+YkDRjWFrly'
+ 'YIRRrhdeiuy86yaBq0eqA7iTLwIfjzs1yefw==')
+
+BAD_GUIDS = [
+ '', # Nothing.
+ 'foobar', # No comma.
+ # Wrong base64 encoding.
+ 'TIzNDU2Nzg5YWJj,9RuQq5zPNVw2fnjk7zT/jfS+YkDRjWFrly'
+ 'YIRRrhdeiuy86yaBq0eqA7iTLwIfjzs1yefw==',
+ # Wrong nonce.
+ 'MNIzNDU2Nzg5YWJj,9RuQq5zPNVw2fnjk7zT/jfS+YkDRjWFrly'
+ 'YIRRrhdeiuy86yaBq0eqA7iTLwIfjzs1yefw==',
+]
+
+
+@pytest.fixture()
+def reports_config(mocker):
+ """Mock config to override encryption key and redirect URL."""
+ mock_config = ConfigParser.ConfigParser()
+ mock_config.add_section(CONF_SECTION)
+ mock_config.set(CONF_SECTION, CONF_KEY_KEY,
+ base64.b64encode(ENCRYPTION_KEY))
+ mock_config.set(CONF_SECTION, CONF_URL_KEY, URL_TEMPLATE)
+ mocker.patch('sitescripts.reports.web.resolveReport.get_config',
+ lambda: mock_config)
+
+
+def test_success(reports_config, mocker):
+ start_response_mock = mocker.Mock()
+ result = resolve_report({'QUERY_STRING': ENCRYPTED_GUID},
+ start_response_mock)
+ assert result == ['Found']
+ start_response_mock.assert_called_once_with(
+ '302 Found',
+ [('Location', URL_TEMPLATE.format(report_id=PLAINTEXT_GUID))],
+ )
+
+
+@pytest.mark.parametrize('guid', BAD_GUIDS)
+def test_bad_wrong_guid(reports_config, mocker, guid):
+ start_response_mock = mocker.Mock()
+ result = resolve_report({'QUERY_STRING': guid},
+ start_response_mock)
+ assert result == ['Not Found']
+ start_response_mock.assert_called_once_with('404 Not Found', [])
« no previous file with comments | « sitescripts/reports/README.md ('k') | sitescripts/reports/web/resolveReport.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld