Passed
Push — master ( 2b1785...27363e )
by Alexander
04:03 queued 01:20
created

tcms/issuetracker/tests/test_kiwitcms.py (1 issue)

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.bugs.tests.factory import BugFactory
16
from tcms.core.contrib.linkreference.models import LinkReference
17
from tcms.core.helpers.comments import get_comments
18
from tcms.issuetracker.types import KiwiTCMS
19
from tcms.rpc.tests.utils import APITestCase
20
from tcms.testcases.models import BugSystem
21
from tcms.tests.factories import ComponentFactory, TestExecutionFactory
22
23
24
@unittest.skipUnless(
25
    os.getenv("TEST_BUGTRACKER_INTEGRATION"),
26
    "Bug tracker integration testing not enabled",
27
)
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(
39
            name="KiwiTCMS integration", product=self.execution_1.run.plan.product
40
        )
41
        self.execution_1.case.add_component(self.component)
42
43
        self.base_url = "https://%s" % Site.objects.get(id=settings.SITE_ID).domain
44
        # note: ^^^ this is https just because .get_full_url() default to that !
45
        bug_system = BugSystem.objects.create(  # nosec:B106:hardcoded_password_funcarg
46
            name="KiwiTCMS internal bug tracker",
47
            tracker_type="tcms.issuetracker.types.KiwiTCMS",
48
            base_url=self.base_url,
49
        )
50
        self.integration = KiwiTCMS(bug_system, None)
51
52
    def test_bug_id_from_url(self):
53
        result = self.integration.bug_id_from_url(self.existing_bug.get_full_url())
54
        self.assertEqual(self.existing_bug.pk, result)
55
56
    def test_details_for_url(self):
57
        result = self.integration.details(self.existing_bug.get_full_url())
58
59
        self.assertEqual(self.existing_bug.summary, result["title"])
60
61
        expected_description = render_to_string(
62
            "include/bug_details.html", {"object": self.existing_bug}
63
        )
64
        self.assertEqual(expected_description, result["description"])
65
66
    def test_auto_update_bugtracker(self):
67
        # make sure bug is not associated with execution
68
        self.assertFalse(
69
            self.existing_bug.executions.filter(pk=self.execution_1.pk).exists()
70
        )
71
72
        # simulate user adding a new bug URL to a TE and clicking
73
        # 'Automatically update bug tracker'
74
        result = self.rpc_client.TestExecution.add_link(
75
            {
76
                "execution_id": self.execution_1.pk,
77
                "is_defect": True,
78
                "url": self.existing_bug.get_full_url(),
79
            },
80
            True,
81
        )
82
83
        # making sure RPC above returned the same URL
84
        self.assertEqual(self.existing_bug.get_full_url(), result["url"])
85
86
        # bug is now associated with execution
87
        self.assertTrue(
88
            self.existing_bug.executions.filter(pk=self.execution_1.pk).exists()
89
        )
90
91 View Code Duplication
    def test_report_issue_from_test_execution_1click_works(self):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
92
        # simulate user clicking the 'Report bug' button in TE widget, TR page
93
        result = self.rpc_client.Bug.report(
94
            self.execution_1.pk, self.integration.bug_system.pk
95
        )
96
        self.assertEqual(result["rc"], 0)
97
        self.assertIn(self.integration.bug_system.base_url, result["response"])
98
        self.assertIn("/bugs/", result["response"])
99
100
        new_bug_id = self.integration.bug_id_from_url(result["response"])
101
        bug = Bug.objects.get(pk=new_bug_id)
102
103
        self.assertEqual("Failed test: %s" % self.execution_1.case.summary, bug.summary)
104
        first_comment = get_comments(bug).first()
105
        for expected_string in [
106
            "Filed from execution %s" % self.execution_1.get_full_url(),
107
            self.execution_1.run.plan.product.name,
108
            self.component.name,
109
            "Steps to reproduce",
110
            self.execution_1.case.text,
111
        ]:
112
            self.assertIn(expected_string, first_comment.comment)
113
114
        # verify that LR has been added to TE
115
        self.assertTrue(
116
            LinkReference.objects.filter(
117
                execution=self.execution_1,
118
                url=result["response"],
119
                is_defect=True,
120
            ).exists()
121
        )
122
123
    def test_empty_details_when_bug_dont_exist(self):
124
        non_existing_bug_id = -1
125
        self.assertFalse(Bug.objects.filter(pk=non_existing_bug_id).exists())
126
127
        result = self.integration.details(
128
            "{}/{}".format(self.base_url, non_existing_bug_id)
129
        )
130
        self.assertEqual(result, {})
131