Passed
Push — master ( f810bc...b4273f )
by Alexander
02:58
created

TestSendMailOnCaseIsDeleted.setUpTestData()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
# pylint: disable=invalid-name, no-member
3
4
from mock import patch
5
6
from django.conf import settings
7
from django.template.loader import render_to_string
8
from django.utils.translation import ugettext_lazy as _
9
10
from tcms.core.history import history_email_for
11
from tcms.testcases.models import BugSystem
12
from tcms.testcases.helpers.email import get_case_notification_recipients
13
from tcms.tests import BasePlanCase
14
from tcms.tests.factories import ComponentFactory
15
from tcms.tests.factories import BuildFactory
16
from tcms.tests.factories import TestCaseComponentFactory
17
from tcms.tests.factories import TestExecutionFactory
18
from tcms.tests.factories import TestCaseTagFactory
19
from tcms.tests.factories import TestRunFactory
20
from tcms.tests.factories import TagFactory
21
22
23
class TestCaseRemoveBug(BasePlanCase):
24
    """Test TestCase.remove_bug"""
25
26
    @classmethod
27
    def setUpTestData(cls):
28
        super(TestCaseRemoveBug, cls).setUpTestData()
29
        cls.build = BuildFactory(product=cls.product)
30
        cls.test_run = TestRunFactory(product_version=cls.version, plan=cls.plan,
31
                                      manager=cls.tester, default_tester=cls.tester)
32
        cls.case_run = TestExecutionFactory(assignee=cls.tester, tested_by=cls.tester,
33
                                            case=cls.case, run=cls.test_run, build=cls.build)
34
        cls.bug_system = BugSystem.objects.get(name='Bugzilla')
35
36
    def setUp(self):
37
        self.bug_id_1 = '12345678'
38
        self.case.add_bug(self.bug_id_1, self.bug_system.pk,
39
                          summary='error when add a bug to a case')
40
        self.bug_id_2 = '10000'
41
        self.case.add_bug(self.bug_id_2, self.bug_system.pk, case_run=self.case_run)
42
43
    def tearDown(self):
44
        self.case.case_bug.all().delete()
45
46
    def test_remove_case_bug(self):
47
        self.case.remove_bug(self.bug_id_1)
48
49
        bug_found = self.case.case_bug.filter(bug_id=self.bug_id_1).exists()
50
        self.assertFalse(bug_found)
51
52
        bug_found = self.case.case_bug.filter(bug_id=self.bug_id_2).exists()
53
        self.assertTrue(bug_found,
54
                        'Bug {0} does not exist. It should not be deleted.'.format(self.bug_id_2))
55
56
    def test_case_bug_not_removed_by_passing_case_run(self):
57
        self.case.remove_bug(self.bug_id_1, run_id=self.case_run.pk)
58
59
        bug_found = self.case.case_bug.filter(bug_id=self.bug_id_1).exists()
60
        self.assertTrue(bug_found,
61
                        'Bug {0} does not exist. It should not be deleted.'.format(self.bug_id_1))
62
63
        bug_found = self.case.case_bug.filter(bug_id=self.bug_id_2).exists()
64
        self.assertTrue(bug_found,
65
                        'Bug {0} does not exist. It should not be deleted.'.format(self.bug_id_2))
66
67
    def test_remove_case_run_bug(self):
68
        self.case.remove_bug(self.bug_id_2, run_id=self.case_run.pk)
69
70
        bug_found = self.case.case_bug.filter(bug_id=self.bug_id_2).exists()
71
        self.assertFalse(bug_found)
72
73
        bug_found = self.case.case_bug.filter(bug_id=self.bug_id_1).exists()
74
        self.assertTrue(bug_found,
75
                        'Bug {0} does not exist. It should not be deleted.'.format(self.bug_id_1))
76
77
    def test_case_run_bug_not_removed_by_missing_case_run(self):
78
        self.case.remove_bug(self.bug_id_2)
79
80
        bug_found = self.case.case_bug.filter(bug_id=self.bug_id_1).exists()
81
        self.assertTrue(bug_found,
82
                        'Bug {0} does not exist. It should not be deleted.'.format(self.bug_id_1))
83
84
        bug_found = self.case.case_bug.filter(bug_id=self.bug_id_2).exists()
85
        self.assertTrue(bug_found,
86
                        'Bug {0} does not exist. It should not be deleted.'.format(self.bug_id_2))
87
88
89
class TestCaseRemoveComponent(BasePlanCase):
90
    """Test TestCase.remove_component"""
91
92
    @classmethod
93
    def setUpTestData(cls):
94
        super(TestCaseRemoveComponent, cls).setUpTestData()
95
96
        cls.component_1 = ComponentFactory(name='Application',
97
                                           product=cls.product,
98
                                           initial_owner=cls.tester,
99
                                           initial_qa_contact=cls.tester)
100
        cls.component_2 = ComponentFactory(name='Database',
101
                                           product=cls.product,
102
                                           initial_owner=cls.tester,
103
                                           initial_qa_contact=cls.tester)
104
105
        cls.cc_rel_1 = TestCaseComponentFactory(case=cls.case,
106
                                                component=cls.component_1)
107
        cls.cc_rel_2 = TestCaseComponentFactory(case=cls.case,
108
                                                component=cls.component_2)
109
110
    def test_remove_a_component(self):
111
        self.case.remove_component(self.component_1)
112
113
        found = self.case.component.filter(pk=self.component_1.pk).exists()
114
        self.assertFalse(
115
            found,
116
            'Component {0} exists. But, it should be removed.'.format(
117
                self.component_1.pk))
118
        found = self.case.component.filter(pk=self.component_2.pk).exists()
119
        self.assertTrue(
120
            found,
121
            'Component {0} does not exist. It should not be removed.'.format(
122
                self.component_2.pk))
123
124
125
class TestCaseRemoveTag(BasePlanCase):
126
    """Test TestCase.remove_tag"""
127
128
    @classmethod
129
    def setUpTestData(cls):
130
        super(TestCaseRemoveTag, cls).setUpTestData()
131
132
        cls.tag_rhel = TagFactory(name='rhel')
133
        cls.tag_fedora = TagFactory(name='fedora')
134
        TestCaseTagFactory(case=cls.case, tag=cls.tag_rhel)
135
        TestCaseTagFactory(case=cls.case, tag=cls.tag_fedora)
136
137
    def test_remove_tag(self):
138
        self.case.remove_tag(self.tag_rhel)
139
140
        tag_pks = list(self.case.tag.all().values_list('pk', flat=True))
141
        self.assertEqual([self.tag_fedora.pk], tag_pks)
142
143
144
class TestSendMailOnCaseIsUpdated(BasePlanCase):
145
    """Test send mail on case post_save signal is triggered"""
146
    @classmethod
147
    def setUpTestData(cls):
148
        super().setUpTestData()
149
150
        cls.case.emailing.notify_on_case_update = True
151
        cls.case.emailing.auto_to_case_author = True
152
        cls.case.emailing.save()
153
154
    @patch('tcms.core.utils.mailto.send_mail')
155
    def test_send_mail_to_case_author(self, send_mail):
156
        self.case.summary = 'New summary for running test'
157
        self.case.save()
158
159
        expected_subject, expected_body = history_email_for(self.case, self.case.summary)
160
        recipients = get_case_notification_recipients(self.case)
161
162
        # Verify notification mail
163
        send_mail.assert_called_once_with(settings.EMAIL_SUBJECT_PREFIX + expected_subject,
164
                                          expected_body,
165
                                          settings.DEFAULT_FROM_EMAIL,
166
                                          recipients,
167
                                          fail_silently=False)
168
169
170
class TestSendMailOnCaseIsDeleted(BasePlanCase):
171
    """Test send mail on case post_delete signal is triggered"""
172
    @classmethod
173
    def setUpTestData(cls):
174
        super().setUpTestData()
175
176
        cls.case.emailing.notify_on_case_delete = True
177
        cls.case.emailing.auto_to_case_author = True
178
        cls.case.emailing.save()
179
180
    @patch('tcms.core.utils.mailto.send_mail')
181
    def test_send_mail_to_case_author(self, send_mail):
182
        expected_subject = _('DELETED: TestCase #%(pk)d - %(summary)s') % {
183
                'pk': self.case.pk,
184
                'summary': self.case.summary
185
            }
186
        expected_body = render_to_string('email/post_case_delete/email.txt', {'case': self.case})
187
        recipients = get_case_notification_recipients(self.case)
188
189
        self.case.delete()
190
191
        # Verify notification mail
192
        send_mail.assert_called_once_with(settings.EMAIL_SUBJECT_PREFIX + expected_subject,
193
                                          expected_body,
194
                                          settings.DEFAULT_FROM_EMAIL,
195
                                          recipients,
196
                                          fail_silently=False)
197