test_mako_emails.py
196 lines
| 6.8 KiB
| text/x-python
|
PythonLexer
r2310 | ||||
r5088 | # Copyright (C) 2010-2023 RhodeCode GmbH | |||
r2310 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
r1 | ||||
import pytest | ||||
r4038 | import collections | |||
r1 | ||||
r2310 | from rhodecode.lib.partial_renderer import PyramidPartialRenderer | |||
r1728 | from rhodecode.lib.utils2 import AttributeDict | |||
r4500 | from rhodecode.model.db import User, PullRequestReviewers | |||
r1 | from rhodecode.model.notification import EmailNotificationModel | |||
r4631 | @pytest.fixture() | |||
def pr(): | ||||
def factory(ref): | ||||
return collections.namedtuple( | ||||
'PullRequest', | ||||
'pull_request_id, title, title_safe, description, source_ref_parts, source_ref_name, target_ref_parts, target_ref_name')\ | ||||
(200, 'Example Pull Request', 'Example Pull Request', 'Desc of PR', ref, 'bookmark', ref, 'Branch') | ||||
return factory | ||||
r2310 | def test_get_template_obj(app, request_stub): | |||
r1 | template = EmailNotificationModel().get_renderer( | |||
r2310 | EmailNotificationModel.TYPE_TEST, request_stub) | |||
assert isinstance(template, PyramidPartialRenderer) | ||||
r1 | ||||
r1774 | def test_render_email(app, http_host_only_stub): | |||
r1 | kwargs = {} | |||
r4447 | subject, body, body_plaintext = EmailNotificationModel().render_email( | |||
r1 | EmailNotificationModel.TYPE_TEST, **kwargs) | |||
# subject | ||||
assert subject == 'Test "Subject" hello "world"' | ||||
# body plaintext | ||||
assert body_plaintext == 'Email Plaintext Body' | ||||
# body | ||||
r4038 | notification_footer1 = 'This is a notification from RhodeCode.' | |||
notification_footer2 = 'http://{}/'.format(http_host_only_stub) | ||||
assert notification_footer1 in body | ||||
assert notification_footer2 in body | ||||
r512 | assert 'Email Body' in body | |||
r1 | ||||
r4500 | @pytest.mark.parametrize('role', PullRequestReviewers.ROLES) | |||
r4631 | def test_render_pr_email(app, user_admin, role, pr): | |||
r4038 | ref = collections.namedtuple( | |||
'Ref', 'name, type')('fxies123', 'book') | ||||
r4631 | pr = pr(ref) | |||
r4038 | source_repo = target_repo = collections.namedtuple( | |||
'Repo', 'type, repo_name')('hg', 'pull_request_1') | ||||
r1 | ||||
kwargs = { | ||||
r4038 | 'user': User.get_first_super_admin(), | |||
r1 | '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', | ||||
'pull_request_url': 'http://localhost/pr1', | ||||
r4500 | 'user_role': role, | |||
r1 | } | |||
r4447 | subject, body, body_plaintext = EmailNotificationModel().render_email( | |||
r1 | EmailNotificationModel.TYPE_PULL_REQUEST, **kwargs) | |||
# subject | ||||
r4500 | if role == PullRequestReviewers.ROLE_REVIEWER: | |||
assert subject == '@test_admin (RhodeCode Admin) requested a pull request review. !200: "Example Pull Request"' | ||||
elif role == PullRequestReviewers.ROLE_OBSERVER: | ||||
assert subject == '@test_admin (RhodeCode Admin) added you as observer to pull request. !200: "Example Pull Request"' | ||||
r1728 | ||||
r4631 | def test_render_pr_update_email(app, user_admin, pr): | |||
r4120 | ref = collections.namedtuple( | |||
'Ref', 'name, type')('fxies123', 'book') | ||||
r4631 | pr = pr(ref) | |||
r4120 | ||||
source_repo = target_repo = collections.namedtuple( | ||||
'Repo', 'type, repo_name')('hg', 'pull_request_1') | ||||
commit_changes = AttributeDict({ | ||||
'added': ['aaaaaaabbbbb', 'cccccccddddddd'], | ||||
'removed': ['eeeeeeeeeee'], | ||||
}) | ||||
file_changes = AttributeDict({ | ||||
'added': ['a/file1.md', 'file2.py'], | ||||
'modified': ['b/modified_file.rst'], | ||||
'removed': ['.idea'], | ||||
}) | ||||
kwargs = { | ||||
'updating_user': User.get_first_super_admin(), | ||||
'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', | ||||
'pull_request_url': 'http://localhost/pr1', | ||||
'pr_comment_url': 'http://comment-url', | ||||
'pr_comment_reply_url': 'http://comment-url#reply', | ||||
'ancestor_commit_id': 'f39bd443', | ||||
'added_commits': commit_changes.added, | ||||
'removed_commits': commit_changes.removed, | ||||
'changed_files': (file_changes.added + file_changes.modified + file_changes.removed), | ||||
'added_files': file_changes.added, | ||||
'modified_files': file_changes.modified, | ||||
'removed_files': file_changes.removed, | ||||
} | ||||
r4447 | subject, body, body_plaintext = EmailNotificationModel().render_email( | |||
r4120 | EmailNotificationModel.TYPE_PULL_REQUEST_UPDATE, **kwargs) | |||
# subject | ||||
assert subject == '@test_admin (RhodeCode Admin) updated pull request. !200: "Example Pull Request"' | ||||
r1728 | @pytest.mark.parametrize('mention', [ | |||
True, | ||||
False | ||||
]) | ||||
@pytest.mark.parametrize('email_type', [ | ||||
EmailNotificationModel.TYPE_COMMIT_COMMENT, | ||||
EmailNotificationModel.TYPE_PULL_REQUEST_COMMENT | ||||
]) | ||||
r4631 | def test_render_comment_subject_no_newlines(app, mention, email_type, pr): | |||
r4038 | ref = collections.namedtuple( | |||
'Ref', 'name, type')('fxies123', 'book') | ||||
r1728 | ||||
r4631 | pr = pr(ref) | |||
r1728 | ||||
r4038 | source_repo = target_repo = collections.namedtuple( | |||
'Repo', 'type, repo_name')('hg', 'pull_request_1') | ||||
r1728 | ||||
kwargs = { | ||||
r4038 | 'user': User.get_first_super_admin(), | |||
r1728 | 'commit': AttributeDict(raw_id='a'*40, message='Commit message'), | |||
'status_change': 'approved', | ||||
r4038 | 'commit_target_repo_url': 'http://foo.example.com/#comment1', | |||
r1728 | 'repo_name': 'test-repo', | |||
'comment_file': 'test-file.py', | ||||
'comment_line': 'n100', | ||||
'comment_type': 'note', | ||||
r4050 | 'comment_id': 2048, | |||
r1728 | 'commit_comment_url': 'http://comment-url', | |||
r4050 | 'commit_comment_reply_url': 'http://comment-url/#Reply', | |||
r1728 | 'instance_url': 'http://rc-instance', | |||
'comment_body': 'hello world', | ||||
'mention': mention, | ||||
'pr_comment_url': 'http://comment-url', | ||||
r4050 | 'pr_comment_reply_url': 'http://comment-url/#Reply', | |||
r1728 | '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', | ||||
r4038 | ||||
'pull_request_url': 'http://code.rc.com/_pr/123' | ||||
r1728 | } | |||
r4447 | subject, body, body_plaintext = EmailNotificationModel().render_email(email_type, **kwargs) | |||
r1728 | ||||
assert '\n' not in subject | ||||