def test_multiple_auth_failures(self): """Ensure that we don't recurse indefinitely if auth fails persistently.""" http = FakeHttp([(FakeResponse(401), {}), (FakeResponse(401), {})]) self.mock.Http.return_value = http response, _ = http_utils.request('https://url/', configuration=self.config) # Ensure that all expected requests were made. self.assertEqual(http.replies, []) self.assertEqual(response.status, 401)
def test_authentication(self): """Ensure that we can authenticate properly if needed.""" http = FakeHttp([(FakeResponse(401), {}), (FakeResponse( 200, include_auth_header=True), {})]) self.mock.Http.return_value = http response, _ = http_utils.request('https://url/', configuration=self.config) # Ensure that all expected requests were made. self.assertEqual(http.replies, []) self.mock.write_data_to_file.assert_called_once_with( 'fake auth token', http_utils.AUTHORIZATION_CACHE_FILE) self.assertEqual(response.status, 200)
def test_archived_testcase(self): """Ensure that we properly unpack archived test cases.""" self.mock.request.return_value = (FakeResponse(200, filename='test.zip'), 'zip data') self.mock.get_file_list.return_value = ['test.html', 'resource.js'] testcase = reproduce.SerializedTestcase({ 'archive_state': data_types.ArchiveStatus.ALL, 'absolute_path': '/path/to/test.html', 'minimized_keys': 'key', }) current_testcase_directory = os.path.join(reproduce.CONFIG_DIRECTORY, 'current-testcase') zip_path = os.path.join(current_testcase_directory, 'test.zip') reproduce._download_testcase(1, testcase, self.config) self.mock.write_data_to_file.assert_called_once_with( 'zip data', zip_path) self.mock.unpack.assert_called_once_with(zip_path, current_testcase_directory)
def test_archived_testcase(self): """Ensure that we properly unpack archived test cases.""" self.mock.request.return_value = ( FakeResponse(200, filename="test.zip"), "zip data", ) self.mock.get_file_list.return_value = ["test.html", "resource.js"] testcase = reproduce.SerializedTestcase({ "archive_state": data_types.ArchiveStatus.ALL, "absolute_path": "/path/to/test.html", "minimized_keys": "key", }) current_testcase_directory = os.path.join(reproduce.CONFIG_DIRECTORY, "current-testcase") zip_path = os.path.join(current_testcase_directory, "test.zip") reproduce._download_testcase(1, testcase, self.config) self.mock.write_data_to_file.assert_called_once_with( "zip data", zip_path) self.mock.unpack.assert_called_once_with(zip_path, current_testcase_directory)
def test_https_server(self): """Ensure that we can get the configuration for an HTTPS server.""" self.mock.request.return_value = (FakeResponse(200), '{"x": 1}') configuration = config.ReproduceToolConfiguration( 'https://clusterfuzz/testcase-detail/1') self.assertEqual(configuration.get('x'), 1) self.mock.request.assert_called_once_with( 'https://clusterfuzz/reproduce-tool/get-config', body={})
def test_unauthenticated_request(self): """Ensure that we can make an unauthenticated request.""" http = FakeHttp([(FakeResponse(200), {})]) self.mock.Http.return_value = http response, _ = http_utils.request('https://url/', body='test body') # Ensure that all expected requests were made. self.assertEqual(http.replies, []) self.assertEqual(http.last_body, '"test body"') self.assertEqual(http.last_headers, {}) self.assertEqual(response.status, 200)
def test_authenticated_request(self): """Ensure that we reuse credentials if we've previously authenticated.""" http = FakeHttp([(FakeResponse(200), {})]) self.mock.Http.return_value = http self.mock.read_data_from_file.return_value = 'cached auth token' response, _ = http_utils.request('https://url/', configuration=self.config) # Ensure that all expected requests were made. self.assertEqual(http.replies, []) self.assertEqual(http.last_headers, { 'Authorization': 'cached auth token', 'User-Agent': 'clusterfuzz-reproduce' }) self.assertEqual(response.status, 200)
def test_archive_missing_file(self): """Ensure that we raise if the archive is missing an expected file.""" self.mock.request.return_value = (FakeResponse(200, filename='test.zip'), 'zip data') self.mock.get_file_list.return_value = [] testcase = reproduce.SerializedTestcase({ 'archive_state': data_types.ArchiveStatus.ALL, 'absolute_path': '/path/to/test.html', 'minimized_keys': 'key', }) with self.assertRaises(errors.ReproduceToolUnrecoverableError): reproduce._download_testcase(1, testcase, self.config)
def test_non_archived_testcase(self): """Ensure that we properly unpack non-archived test cases.""" self.mock.request.return_value = (FakeResponse(200, filename='test.html'), 'html data') testcase = reproduce.SerializedTestcase({ 'archive_state': data_types.ArchiveStatus.NONE, 'minimized_keys': 'key', }) reproduce._download_testcase(1, testcase, self.config) self.mock.write_data_to_file.assert_called_once_with( 'html data', os.path.join(reproduce.CONFIG_DIRECTORY, 'current-testcase', 'test.html'))
def test_non_archived_testcase(self): """Ensure that we properly unpack non-archived test cases.""" self.mock.request.return_value = ( FakeResponse(200, filename="test.html"), "html data", ) testcase = reproduce.SerializedTestcase({ "archive_state": data_types.ArchiveStatus.NONE, "minimized_keys": "key" }) reproduce._download_testcase(1, testcase, self.config) self.mock.write_data_to_file.assert_called_once_with( "html data", os.path.join(reproduce.CONFIG_DIRECTORY, "current-testcase", "test.html"), )
def test_initial_request_failed(self): """Ensure that we bail out if the initial request fails.""" self.mock.request.return_value = (FakeResponse(500), '') testcase = reproduce.SerializedTestcase({}) with self.assertRaises(errors.ReproduceToolUnrecoverableError): reproduce._download_testcase(1, testcase, self.config)
def test_failure(self): """Ensure that we raise an exception if the server encounters an error.""" self.mock.request.return_value = (FakeResponse(403), '{"x": 1}') with self.assertRaises(errors.ReproduceToolUnrecoverableError): config.ReproduceToolConfiguration('https://clusterfuzz/testcase-detail/1')