1
|
|
|
# pylint: disable=wrong-import-position |
2
|
|
|
import unittest |
3
|
|
|
|
4
|
|
|
from django.conf import settings |
5
|
|
|
|
6
|
|
|
if "tcms.bugs.apps.AppConfig" not in settings.INSTALLED_APPS: |
7
|
|
|
raise unittest.SkipTest("tcms.bugs is disabled") |
8
|
|
|
|
9
|
|
|
from django.template.loader import render_to_string # noqa: E402 |
10
|
|
|
from django.test import TestCase # noqa: E402 |
11
|
|
|
from django.urls import reverse # noqa: E402 |
12
|
|
|
from django.utils.translation import gettext_lazy as _ # noqa: E402 |
13
|
|
|
from mock import patch # noqa: E402 |
14
|
|
|
|
15
|
|
|
from tcms.bugs.tests.factory import BugFactory # noqa: E402 |
16
|
|
|
from tcms.core.helpers.comments import add_comment, get_comments # noqa: E402 |
17
|
|
|
from tcms.tests import BaseCaseRun, user_should_have_perm # noqa: E402 |
18
|
|
|
from tcms.tests.factories import UserFactory # noqa: E402 |
19
|
|
|
from tcms.utils.permissions import initiate_user_with_default_setups # noqa: E402 |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class TestSendMailOnAssigneeChange(TestCase): |
23
|
|
|
"""Test that assignee is notified by mail each time they are assigned a bug. |
24
|
|
|
|
25
|
|
|
Ideally, notifications are sent out when: |
26
|
|
|
* Assignee is assigned bug on bug creation |
27
|
|
|
* Assignee is assigned bug which was previously assigned to someone other assignee |
28
|
|
|
""" |
29
|
|
|
|
30
|
|
|
@patch("tcms.core.utils.mailto.send_mail") |
31
|
|
|
def test_notify_assignee_on_bug_creation( |
32
|
|
|
self, send_mail |
33
|
|
|
): # pylint: disable=no-self-use |
34
|
|
|
assignee = UserFactory() |
35
|
|
|
bug = BugFactory(assignee=assignee) |
36
|
|
|
|
37
|
|
|
expected_subject = _("Bug #%(pk)d - %(summary)s") % { |
38
|
|
|
"pk": bug.pk, |
39
|
|
|
"summary": bug.summary, |
40
|
|
|
} |
41
|
|
|
expected_body = render_to_string( |
42
|
|
|
"email/post_bug_save/email.txt", |
43
|
|
|
{"bug": bug, "comment": get_comments(bug).last()}, |
44
|
|
|
) |
45
|
|
|
expected_recipients = [assignee.email, bug.reporter.email] |
46
|
|
|
expected_recipients.sort() |
47
|
|
|
|
48
|
|
|
send_mail.assert_called_once_with( |
49
|
|
|
settings.EMAIL_SUBJECT_PREFIX + expected_subject, |
50
|
|
|
expected_body, |
51
|
|
|
settings.DEFAULT_FROM_EMAIL, |
52
|
|
|
expected_recipients, |
53
|
|
|
fail_silently=False, |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
@patch("tcms.core.utils.mailto.send_mail") |
57
|
|
|
def test_notification_even_there_is_no_assignee(self, send_mail): |
58
|
|
|
BugFactory(assignee=None) |
59
|
|
|
|
60
|
|
|
self.assertTrue(send_mail.called) |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
class TestSendMailOnNewComment(BaseCaseRun): |
64
|
|
|
"""Test that assignee and reporter are notified by mail each time a comment is added. |
65
|
|
|
|
66
|
|
|
Ideally, notifications are sent out when: |
67
|
|
|
* A new comment is added to the bug. |
68
|
|
|
* Bug is closed. |
69
|
|
|
* Bug is reopened. |
70
|
|
|
""" |
71
|
|
|
|
72
|
|
|
@classmethod |
73
|
|
|
def setUpTestData(cls): |
74
|
|
|
super().setUpTestData() |
75
|
|
|
|
76
|
|
|
initiate_user_with_default_setups(cls.tester) |
77
|
|
|
cls.comment_bug_url = reverse("bugs-comment") |
78
|
|
|
user_should_have_perm(cls.tester, "bugs.change_bug") |
79
|
|
|
|
80
|
|
|
cls.assignee = UserFactory() |
81
|
|
|
cls.url = reverse("bugs-comment") |
82
|
|
|
|
83
|
|
View Code Duplication |
@patch("tcms.core.utils.mailto.send_mail") |
|
|
|
|
84
|
|
|
def test_email_sent_when_bug_closed(self, send_mail): |
85
|
|
|
bug = BugFactory(assignee=self.assignee, reporter=self.tester) |
86
|
|
|
self.client.post( |
87
|
|
|
self.url, {"bug": bug.pk, "text": "", "action": "close"}, follow=True |
88
|
|
|
) |
89
|
|
|
|
90
|
|
|
expected_body = render_to_string( |
91
|
|
|
"email/post_bug_save/email.txt", |
92
|
|
|
{"bug": bug, "comment": get_comments(bug).last()}, |
93
|
|
|
) |
94
|
|
|
expected_recipients = [self.assignee.email, self.tester.email] |
95
|
|
|
expected_recipients.sort() |
96
|
|
|
|
97
|
|
|
expected_subject = _("Bug #%(pk)d - %(summary)s") % { |
98
|
|
|
"pk": bug.pk, |
99
|
|
|
"summary": bug.summary, |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
self.assertTrue(send_mail.called) |
103
|
|
|
self.assertEqual( |
104
|
|
|
settings.EMAIL_SUBJECT_PREFIX + expected_subject, |
105
|
|
|
send_mail.call_args.args[0], |
106
|
|
|
) |
107
|
|
|
self.assertEqual(expected_body, send_mail.call_args.args[1]) |
108
|
|
|
self.assertEqual(settings.DEFAULT_FROM_EMAIL, send_mail.call_args.args[2]) |
109
|
|
|
self.assertTrue(len(send_mail.call_args.args[3]) == len(expected_recipients)) |
110
|
|
|
self.assertIn(expected_recipients[0], send_mail.call_args.args[3]) |
111
|
|
|
self.assertIn(expected_recipients[1], send_mail.call_args.args[3]) |
112
|
|
|
|
113
|
|
View Code Duplication |
@patch("tcms.core.utils.mailto.send_mail") |
|
|
|
|
114
|
|
|
def test_email_sent_when_bug_reopened(self, send_mail): |
115
|
|
|
bug = BugFactory(assignee=self.assignee, reporter=self.tester) |
116
|
|
|
bug.status = False |
117
|
|
|
bug.save() |
118
|
|
|
self.client.post( |
119
|
|
|
self.url, {"bug": bug.pk, "text": "", "action": "reopen"}, follow=True |
120
|
|
|
) |
121
|
|
|
|
122
|
|
|
expected_body = render_to_string( |
123
|
|
|
"email/post_bug_save/email.txt", |
124
|
|
|
{"bug": bug, "comment": get_comments(bug).last()}, |
125
|
|
|
) |
126
|
|
|
expected_recipients = [self.assignee.email, self.tester.email] |
127
|
|
|
expected_recipients.sort() |
128
|
|
|
|
129
|
|
|
expected_subject = _("Bug #%(pk)d - %(summary)s") % { |
130
|
|
|
"pk": bug.pk, |
131
|
|
|
"summary": bug.summary, |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
self.assertTrue(send_mail.called) |
135
|
|
|
self.assertEqual( |
136
|
|
|
settings.EMAIL_SUBJECT_PREFIX + expected_subject, |
137
|
|
|
send_mail.call_args.args[0], |
138
|
|
|
) |
139
|
|
|
self.assertEqual(expected_body, send_mail.call_args.args[1]) |
140
|
|
|
self.assertEqual(settings.DEFAULT_FROM_EMAIL, send_mail.call_args.args[2]) |
141
|
|
|
self.assertTrue(len(send_mail.call_args.args[3]) == len(expected_recipients)) |
142
|
|
|
self.assertIn(expected_recipients[0], send_mail.call_args.args[3]) |
143
|
|
|
self.assertIn(expected_recipients[1], send_mail.call_args.args[3]) |
144
|
|
|
|
145
|
|
|
@patch("tcms.core.utils.mailto.send_mail") |
146
|
|
|
def test_email_sent_to_all_commenters(self, send_mail): |
147
|
|
|
bug = BugFactory(assignee=self.assignee, reporter=self.tester) |
148
|
|
|
commenter = UserFactory() |
149
|
|
|
tracker = UserFactory() |
150
|
|
|
add_comment([bug], _("*bug created*"), tracker) |
151
|
|
|
add_comment([bug], _("*bug created*"), commenter) |
152
|
|
|
|
153
|
|
|
expected_body = render_to_string( |
154
|
|
|
"email/post_bug_save/email.txt", |
155
|
|
|
{"bug": bug, "comment": get_comments(bug).last()}, |
156
|
|
|
) |
157
|
|
|
expected_recipients = [ |
158
|
|
|
self.assignee.email, |
159
|
|
|
self.tester.email, |
160
|
|
|
commenter.email, |
161
|
|
|
tracker.email, |
162
|
|
|
] |
163
|
|
|
expected_recipients.sort() |
164
|
|
|
|
165
|
|
|
expected_subject = _("Bug #%(pk)d - %(summary)s") % { |
166
|
|
|
"pk": bug.pk, |
167
|
|
|
"summary": bug.summary, |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
self.assertTrue(send_mail.called) |
171
|
|
|
self.assertEqual( |
172
|
|
|
settings.EMAIL_SUBJECT_PREFIX + expected_subject, |
173
|
|
|
send_mail.call_args.args[0], |
174
|
|
|
) |
175
|
|
|
self.assertEqual(expected_body, send_mail.call_args.args[1]) |
176
|
|
|
self.assertEqual(settings.DEFAULT_FROM_EMAIL, send_mail.call_args.args[2]) |
177
|
|
|
self.assertTrue(len(send_mail.call_args.args[3]) == len(expected_recipients)) |
178
|
|
|
self.assertIn(expected_recipients[0], send_mail.call_args.args[3]) |
179
|
|
|
self.assertIn(expected_recipients[1], send_mail.call_args.args[3]) |
180
|
|
|
|