test_emails: Use zerver.lib.send_email for rendering email.

Previously the rendering code in test_emails.py did not match the rendering
code in send_email.py. This commit removes the duplication to reduce the
chance they drift in the future.

This commit also changes test_emails.html to ensure that we always display
both the HTML and text versions of an email.
This commit is contained in:
Rishi Gupta
2017-06-09 21:19:32 -07:00
parent 7fae8fb53d
commit 69e7029663
3 changed files with 21 additions and 31 deletions

View File

@@ -16,8 +16,9 @@ def display_email(user):
# https://github.com/zulip/zulip/issues/4676 is resolved
return user.email
def send_email(template_prefix, to_email, from_email=None, reply_to_email=None, context={}):
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any]) -> bool
# Intended only for test code
def build_email(template_prefix, to_email, from_email=None, reply_to_email=None, context={}):
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any]) -> EmailMultiAlternatives
subject = loader.render_to_string(template_prefix + '.subject',
context=context, using='Jinja2_plaintext').strip()
message = loader.render_to_string(template_prefix + '.txt',
@@ -36,6 +37,12 @@ def send_email(template_prefix, to_email, from_email=None, reply_to_email=None,
mail = EmailMultiAlternatives(subject, message, from_email, [to_email], reply_to=reply_to)
if html_message is not None:
mail.attach_alternative(html_message, 'text/html')
return mail
def send_email(template_prefix, to_email, from_email=None, reply_to_email=None, context={}):
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any]) -> bool
mail = build_email(template_prefix, to_email, from_email=from_email,
reply_to_email=reply_to_email, context=context)
return mail.send() > 0
def send_email_to_user(template_prefix, user, from_email=None, context={}):