Passed
Push — master ( 32b42a...e604fe )
by Alexander
02:25
created

TestKiwiTCMSIntegration.test_bug_id_from_url()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# pylint: disable=attribute-defined-outside-init, wrong-import-position
2
3
import os
4
import unittest
5
6
from django.conf import settings
7
8
if 'tcms.bugs.apps.AppConfig' not in settings.INSTALLED_APPS:
9
    raise unittest.SkipTest('tcms.bugs is disabled')
10
11
from django.contrib.sites.models import Site
12
from django.template.loader import render_to_string
13
14
from tcms.bugs.models import Bug
15
from tcms.core.contrib.linkreference.models import LinkReference
16
from tcms.core.helpers.comments import get_comments
17
from tcms.issuetracker.types import KiwiTCMS
18
from tcms.rpc.tests.utils import APITestCase
19
from tcms.testcases.models import BugSystem
20
21
from tcms.bugs.tests.factory import BugFactory
22
from tcms.tests.factories import ComponentFactory
23
from tcms.tests.factories import TestExecutionFactory
24
25
26
@unittest.skipUnless(os.getenv('TEST_BUGTRACKER_INTEGRATION'),
27
                     'Bug tracker integration testing not enabled')
28
class TestKiwiTCMSIntegration(APITestCase):
29
    def _fixture_setup(self):
30
        super()._fixture_setup()
31
32
        self.existing_bug = BugFactory()
33
34
        self.execution_1 = TestExecutionFactory()
35
        self.execution_1.case.text = "Given-When-Then"
36
        self.execution_1.case.save()  # will generate history object
37
38
        self.component = ComponentFactory(name='KiwiTCMS integration',
39
                                          product=self.execution_1.run.plan.product)
40
        self.execution_1.case.add_component(self.component)
41
42
        bug_system = BugSystem.objects.create(  # nosec:B106:hardcoded_password_funcarg
43
            name='KiwiTCMS internal bug tracker',
44
            tracker_type='KiwiTCMS',
45
            base_url="https://%s" % Site.objects.get(id=settings.SITE_ID).domain,
46
            # note: ^^^ this is https just because .get_full_url() default to that !
47
        )
48
        self.integration = KiwiTCMS(bug_system)
49
50
    def test_bug_id_from_url(self):
51
        result = self.integration.bug_id_from_url(self.existing_bug.get_full_url())
52
        self.assertEqual(self.existing_bug.pk, result)
53
54
    def test_details_for_url(self):
55
        result = self.integration.details(self.existing_bug.get_full_url())
56
57
        self.assertEqual(self.existing_bug.summary, result['title'])
58
59
        expected_description = render_to_string(
60
            'include/bug_details.html',
61
            {'object': self.existing_bug})
62
        self.assertEqual(expected_description, result['description'])
63
64
    def test_auto_update_bugtracker(self):
65
        # make sure bug is not associated with execution
66
        self.assertFalse(
67
            self.existing_bug.executions.filter(pk=self.execution_1.pk).exists())
68
69
        # simulate user adding a new bug URL to a TE and clicking
70
        # 'Automatically update bug tracker'
71
        result = self.rpc_client.TestExecution.add_link({
72
            'execution_id': self.execution_1.pk,
73
            'is_defect': True,
74
            'url': self.existing_bug.get_full_url(),
75
        }, True)
76
77
        # making sure RPC above returned the same URL
78
        self.assertEqual(self.existing_bug.get_full_url(), result['url'])
79
80
        # bug is now associated with execution
81
        self.assertTrue(
82
            self.existing_bug.executions.filter(pk=self.execution_1.pk).exists())
83
84
    def test_report_issue_from_test_execution_1click_works(self):
85
        # simulate user clicking the 'Report bug' button in TE widget, TR page
86
        result = self.rpc_client.Bug.report(self.execution_1.pk, self.integration.bug_system.pk)
87
        self.assertEqual(result['rc'], 0)
88
        self.assertIn(self.integration.bug_system.base_url, result['response'])
89
        self.assertIn('/bugs/', result['response'])
90
91
        new_bug_id = self.integration.bug_id_from_url(result['response'])
92
        bug = Bug.objects.get(pk=new_bug_id)
93
94
        self.assertEqual("Failed test: %s" % self.execution_1.case.summary, bug.summary)
95
        first_comment = get_comments(bug).first()
96
        for expected_string in [
97
                "Filed from execution %s" % self.execution_1.get_full_url(),
98
                self.execution_1.run.plan.product.name,
99
                self.component.name,
100
                "Steps to reproduce",
101
                self.execution_1.case.text]:
102
            self.assertIn(expected_string, first_comment.comment)
103
104
        # verify that LR has been added to TE
105
        self.assertTrue(LinkReference.objects.filter(
106
            execution=self.execution_1,
107
            url=result['response'],
108
            is_defect=True,
109
        ).exists())
110