diff --git a/redash/handlers/query_results.py b/redash/handlers/query_results.py index c41988af8..f4d258444 100644 --- a/redash/handlers/query_results.py +++ b/redash/handlers/query_results.py @@ -51,10 +51,14 @@ error_messages = { ), "no_permission": error_response("You do not have permission to run queries with this data source.", 403), "select_data_source": error_response("Please select data source to run this query.", 401), + "no_data_source": error_response("Target data source not available.", 401), } def run_query(query, parameters, data_source, query_id, should_apply_auto_limit, max_age=0): + if not data_source: + return error_messages["no_data_source"] + if data_source.paused: if data_source.pause_reason: message = "{} is paused ({}). Please try later.".format(data_source.name, data_source.pause_reason) diff --git a/tests/handlers/test_query_results.py b/tests/handlers/test_query_results.py index 14072d0f9..e678c0eac 100644 --- a/tests/handlers/test_query_results.py +++ b/tests/handlers/test_query_results.py @@ -1,9 +1,16 @@ -from redash.handlers.query_results import error_messages +from redash.handlers.query_results import error_messages, run_query from redash.models import db from redash.utils import json_dumps from tests import BaseTestCase +class TestRunQuery(BaseTestCase): + def test_run_query_with_no_data_source(self): + response, status = run_query(None, None, None, None, None) + self.assertDictEqual(response, error_messages["no_data_source"][0]) + self.assertEqual(status, error_messages["no_data_source"][1]) + + class TestQueryResultsCacheHeaders(BaseTestCase): def test_uses_cache_headers_for_specific_result(self): query_result = self.factory.create_query_result()