From dbe1f45cfc34389baf879b0d17bceb18b6ef7dc1 Mon Sep 17 00:00:00 2001 From: adamzwakk Date: Tue, 4 May 2021 14:57:57 -0400 Subject: [PATCH 1/3] Fixed failed query reporting returning blank email Missing return statement to actually bring the rendered HTML back to `send_failure_report`. This was fine basically everywhere else since it used Flask's version of render_template, but for this more custom case, the string needs to be returned back to be inside the email. --- redash/utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redash/utils/__init__.py b/redash/utils/__init__.py index eff3aec172..d63efc001b 100644 --- a/redash/utils/__init__.py +++ b/redash/utils/__init__.py @@ -211,7 +211,7 @@ def render_template(path, context): Using Flask's `render_template` function requires the entire app context to load, which in turn triggers any function decorated with the `context_processor` decorator, which is not explicitly required for rendering purposes. """ - current_app.jinja_env.get_template(path).render(**context) + return current_app.jinja_env.get_template(path).render(**context) def query_is_select_no_limit(query): From 7e8b63fe6546dd8dc22a3e7dae2f056fd9d2bbc6 Mon Sep 17 00:00:00 2001 From: adamzwakk Date: Fri, 7 May 2021 10:44:02 -0400 Subject: [PATCH 2/3] Added unit test for rendering non-flask templates --- tests/test_utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index a253402ef5..315c608db9 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,12 +1,14 @@ from collections import namedtuple from unittest import TestCase +from redash import create_app from redash.utils import ( build_url, collect_parameters_from_request, filter_none, json_dumps, generate_token, + reder_template, ) @@ -78,3 +80,15 @@ class TestGenerateToken(TestCase): def test_format(self): token = generate_token(40) self.assertRegex(token, r"[a-zA-Z0-9]{40}") + +class TestRenderTemplate(TestCase): + def test_render(self): + app = create_app() + with app.app_context(): + d = {"failures": [{"id": 1, "name": "Failure Unit Test", "failed_at": "May 04, 2021 02:07PM UTC", "failure_reason": "", "failure_count": 1, "comment": None}]} + html, text = [ + render_template("emails/failures.{}".format(f), d) + for f in ["html", "txt"] + ] + self.assertIn('Failure Unit Test',html) + self.assertIn('Failure Unit Test',text) From 4c051b2848137e92e4a014b37a9e193a1f73a8e7 Mon Sep 17 00:00:00 2001 From: adamzwakk Date: Fri, 7 May 2021 10:46:25 -0400 Subject: [PATCH 3/3] Fixed awful typo --- tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 315c608db9..b77c1e78f4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -8,7 +8,7 @@ filter_none, json_dumps, generate_token, - reder_template, + render_template, )