Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid depending on app context when templating failure reports #4231

Merged
merged 5 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions redash/tasks/failure_report.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import re
from collections import Counter
from flask import render_template
from flask import current_app
from redash.tasks.general import send_mail
from redash.worker import celery
from redash import redis_connection, settings, models
Expand Down Expand Up @@ -50,9 +50,10 @@ def send_failure_report(user_id):
'base_url': base_url(user.org)
}

html = render_template('emails/failures.html', **context)
text = render_template('emails/failures.txt', **context)
subject = "Redash failed to execute {} of your scheduled queries".format(len(unique_errors.keys()))
html, text = [
current_app.jinja_env.get_template('emails/failures.{}'.format(f)).render(**context) for f in ['html', 'txt']]

send_mail.delay([user.email], subject, html, text)

redis_connection.delete(key(user_id))
Expand Down
8 changes: 5 additions & 3 deletions tests/tasks/test_failure_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ def notify(self, message="Oh no, I failed!", query=None, **kwargs):
notify_of_failure(message, query)
return key(query.user.id)

@mock.patch('redash.tasks.failure_report.render_template')
def send_email(self, user, render_template):
@mock.patch('redash.tasks.failure_report.current_app')
def send_email(self, user, current_app):
current_app.jinja_env.get_template().render = mock.Mock(return_value='')

send_failure_report(user.id)

_, context = render_template.call_args
_, context = current_app.jinja_env.get_template().render.call_args
return context['failures']

def test_schedules_email_if_failure_count_is_beneath_limit(self):
Expand Down