Revert "Remove deprecated advocate package (#6944)"

This reverts commit bd115e7f5f, as
it turns out to be a useful security feature.

In order to remove this in a better way, we'll need to replace it
with something that provides equivalent functionality.
This commit is contained in:
Justin Clift
2024-05-07 03:20:05 +10:00
parent bd115e7f5f
commit 62890c3ec4
8 changed files with 137 additions and 18 deletions

View File

@@ -1,9 +1,12 @@
from unittest import TestCase
import mock
import requests
from redash.query_runner import BaseHTTPQueryRunner
from redash.utils.requests_session import (
ConfiguredSession,
requests_or_advocate,
)
class RequiresAuthQueryRunner(BaseHTTPQueryRunner):
@@ -34,7 +37,7 @@ class TestBaseHTTPQueryRunner(TestCase):
query_runner = RequiresAuthQueryRunner({})
self.assertRaisesRegex(ValueError, "Username and Password required", query_runner.get_auth)
@mock.patch("requests.request")
@mock.patch.object(ConfiguredSession, "request")
def test_get_response_success(self, mock_get):
mock_response = mock.Mock()
mock_response.status_code = 200
@@ -48,7 +51,7 @@ class TestBaseHTTPQueryRunner(TestCase):
self.assertEqual(response.status_code, 200)
self.assertIsNone(error)
@mock.patch("requests.request")
@mock.patch.object(ConfiguredSession, "request")
def test_get_response_success_custom_auth(self, mock_get):
mock_response = mock.Mock()
mock_response.status_code = 200
@@ -63,7 +66,7 @@ class TestBaseHTTPQueryRunner(TestCase):
self.assertEqual(response.status_code, 200)
self.assertIsNone(error)
@mock.patch("requests.request")
@mock.patch.object(ConfiguredSession, "request")
def test_get_response_failure(self, mock_get):
mock_response = mock.Mock()
mock_response.status_code = 301
@@ -76,12 +79,12 @@ class TestBaseHTTPQueryRunner(TestCase):
mock_get.assert_called_once_with("get", url, auth=None)
self.assertIn(query_runner.response_error, error)
@mock.patch("requests.request")
@mock.patch.object(ConfiguredSession, "request")
def test_get_response_httperror_exception(self, mock_get):
mock_response = mock.Mock()
mock_response.status_code = 500
mock_response.text = "Server Error"
http_error = requests.HTTPError()
http_error = requests_or_advocate.HTTPError()
mock_response.raise_for_status.side_effect = http_error
mock_get.return_value = mock_response
@@ -92,13 +95,13 @@ class TestBaseHTTPQueryRunner(TestCase):
self.assertIsNotNone(error)
self.assertIn("Failed to execute query", error)
@mock.patch("requests.request")
@mock.patch.object(ConfiguredSession, "request")
def test_get_response_requests_exception(self, mock_get):
mock_response = mock.Mock()
mock_response.status_code = 500
mock_response.text = "Server Error"
exception_message = "Some requests exception"
requests_exception = requests.RequestException(exception_message)
requests_exception = requests_or_advocate.RequestException(exception_message)
mock_response.raise_for_status.side_effect = requests_exception
mock_get.return_value = mock_response
@@ -109,7 +112,7 @@ class TestBaseHTTPQueryRunner(TestCase):
self.assertIsNotNone(error)
self.assertEqual(exception_message, error)
@mock.patch("requests.request")
@mock.patch.object(ConfiguredSession, "request")
def test_get_response_generic_exception(self, mock_get):
mock_response = mock.Mock()
mock_response.status_code = 500