# HG changeset patch # User Marcin Kuzminski # Date 2017-05-03 16:44:10 # Node ID 49fb0ceca7f15acace81ea710b32457cce0e7f38 # Parent 25849370f14bec3e6d0a6a176b4b0b40b1a05f3e emails: fixed newlines in email templates that can break email sending code. diff --git a/rhodecode/model/notification.py b/rhodecode/model/notification.py --- a/rhodecode/model/notification.py +++ b/rhodecode/model/notification.py @@ -332,14 +332,19 @@ class EmailNotificationModel(BaseModel): :param kwargs: """ + kwargs['rhodecode_instance_name'] = self.rhodecode_instance_name _kwargs = { 'instance_url': h.url('home', qualified=True), + 'whitespace_filter': self.whitespace_filter } _kwargs.update(kwargs) return _kwargs + def whitespace_filter(self, text): + return text.replace('\n', '').replace('\t', '') + def get_renderer(self, type_): template_name = self.email_types[type_] return PartialRenderer(template_name) diff --git a/rhodecode/templates/email_templates/commit_comment.mako b/rhodecode/templates/email_templates/commit_comment.mako --- a/rhodecode/templates/email_templates/commit_comment.mako +++ b/rhodecode/templates/email_templates/commit_comment.mako @@ -3,7 +3,7 @@ <%namespace name="base" file="base.mako"/> ## EMAIL SUBJECT -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> <% data = { 'user': h.person(user), diff --git a/rhodecode/templates/email_templates/email_test.mako b/rhodecode/templates/email_templates/email_test.mako --- a/rhodecode/templates/email_templates/email_test.mako +++ b/rhodecode/templates/email_templates/email_test.mako @@ -1,7 +1,7 @@ ## -*- coding: utf-8 -*- <%inherit file="base.mako"/> -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> RhodeCode test email: ${h.format_date(date)} diff --git a/rhodecode/templates/email_templates/main.mako b/rhodecode/templates/email_templates/main.mako --- a/rhodecode/templates/email_templates/main.mako +++ b/rhodecode/templates/email_templates/main.mako @@ -1,7 +1,7 @@ ## -*- coding: utf-8 -*- <%inherit file="base.mako"/> -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> diff --git a/rhodecode/templates/email_templates/password_reset.mako b/rhodecode/templates/email_templates/password_reset.mako --- a/rhodecode/templates/email_templates/password_reset.mako +++ b/rhodecode/templates/email_templates/password_reset.mako @@ -1,7 +1,7 @@ ## -*- coding: utf-8 -*- <%inherit file="base.mako"/> -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> RhodeCode Password reset diff --git a/rhodecode/templates/email_templates/password_reset_confirmation.mako b/rhodecode/templates/email_templates/password_reset_confirmation.mako --- a/rhodecode/templates/email_templates/password_reset_confirmation.mako +++ b/rhodecode/templates/email_templates/password_reset_confirmation.mako @@ -1,7 +1,7 @@ ## -*- coding: utf-8 -*- <%inherit file="base.mako"/> -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> Your new RhodeCode password diff --git a/rhodecode/templates/email_templates/pull_request_comment.mako b/rhodecode/templates/email_templates/pull_request_comment.mako --- a/rhodecode/templates/email_templates/pull_request_comment.mako +++ b/rhodecode/templates/email_templates/pull_request_comment.mako @@ -3,7 +3,7 @@ <%namespace name="base" file="base.mako"/> ## EMAIL SUBJECT -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> <% data = { 'user': h.person(user), diff --git a/rhodecode/templates/email_templates/pull_request_review.mako b/rhodecode/templates/email_templates/pull_request_review.mako --- a/rhodecode/templates/email_templates/pull_request_review.mako +++ b/rhodecode/templates/email_templates/pull_request_review.mako @@ -2,7 +2,7 @@ <%inherit file="base.mako"/> <%namespace name="base" file="base.mako"/> -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> <% data = { 'user': h.person(user), diff --git a/rhodecode/templates/email_templates/test.mako b/rhodecode/templates/email_templates/test.mako --- a/rhodecode/templates/email_templates/test.mako +++ b/rhodecode/templates/email_templates/test.mako @@ -1,7 +1,7 @@ ## -*- coding: utf-8 -*- <%inherit file="base.mako"/> -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> Test "Subject" ${_('hello "world"')|n} diff --git a/rhodecode/templates/email_templates/user_registration.mako b/rhodecode/templates/email_templates/user_registration.mako --- a/rhodecode/templates/email_templates/user_registration.mako +++ b/rhodecode/templates/email_templates/user_registration.mako @@ -1,7 +1,7 @@ ## -*- coding: utf-8 -*- <%inherit file="base.mako"/> -<%def name="subject()" filter="n,trim"> +<%def name="subject()" filter="n,trim,whitespace_filter"> RhodeCode new user registration: ${user.username} diff --git a/rhodecode/tests/lib/test_mako_emails.py b/rhodecode/tests/lib/test_mako_emails.py --- a/rhodecode/tests/lib/test_mako_emails.py +++ b/rhodecode/tests/lib/test_mako_emails.py @@ -3,6 +3,7 @@ import collections import pytest from rhodecode.lib.utils import PartialRenderer +from rhodecode.lib.utils2 import AttributeDict from rhodecode.model.notification import EmailNotificationModel @@ -66,3 +67,57 @@ def test_render_pr_email(pylonsapp, user # subject assert subject == 'Marcin Kuzminski wants you to review pull request #200: "Example Pull Request"' + + +@pytest.mark.parametrize('mention', [ + True, + False +]) +@pytest.mark.parametrize('email_type', [ + EmailNotificationModel.TYPE_COMMIT_COMMENT, + EmailNotificationModel.TYPE_PULL_REQUEST_COMMENT +]) +def test_render_comment_subject_no_newlines(pylonsapp, mention, email_type): + ref = collections.namedtuple('Ref', + 'name, type')( + 'fxies123', 'book' + ) + + pr = collections.namedtuple('PullRequest', + 'pull_request_id, title, description, source_ref_parts, source_ref_name, target_ref_parts, target_ref_name')( + 200, 'Example Pull Request', 'Desc of PR', ref, 'bookmark', ref, 'Branch') + + source_repo = target_repo = collections.namedtuple('Repo', + 'type, repo_name')( + 'hg', 'pull_request_1') + + kwargs = { + 'user': ' Marcin Kuzminski', + 'commit': AttributeDict(raw_id='a'*40, message='Commit message'), + 'status_change': 'approved', + 'commit_target_repo': AttributeDict(), + 'repo_name': 'test-repo', + 'comment_file': 'test-file.py', + 'comment_line': 'n100', + 'comment_type': 'note', + 'commit_comment_url': 'http://comment-url', + 'instance_url': 'http://rc-instance', + 'comment_body': 'hello world', + 'mention': mention, + + 'pr_comment_url': 'http://comment-url', + 'pr_source_repo': AttributeDict(repo_name='foobar'), + 'pr_source_repo_url': 'http://soirce-repo/url', + 'pull_request': pr, + 'pull_request_commits': [], + + 'pull_request_target_repo': target_repo, + 'pull_request_target_repo_url': 'x', + + 'pull_request_source_repo': source_repo, + 'pull_request_source_repo_url': 'x', + } + subject, headers, body, body_plaintext = EmailNotificationModel().render_email( + email_type, **kwargs) + + assert '\n' not in subject