Issues (87)

tcms/issuetracker/tests/test_bitbucket.py (3 issues)

1
# pylint: disable=attribute-defined-outside-init
2
import os
3
import unittest
4
5
from django.utils import timezone
6
7
from tcms.core.contrib.linkreference.models import LinkReference
8
from tcms.issuetracker.bitbucket import BitBucket, BitBucketAPI
9
from tcms.rpc.tests.utils import APITestCase
10
from tcms.testcases.models import BugSystem
11
from tcms.tests.factories import ComponentFactory, TestExecutionFactory
12
13
14
@unittest.skipUnless(
15
    os.getenv("TEST_BUGTRACKER_INTEGRATION"),
16
    "Bug tracker integration testing not enabled",
17
)
18
class TestBitBucketIntegration(APITestCase):
19
    existing_bug_id = 1
20
    existing_bug_url = "https://bitbucket.org/kiwitcms/integration/issues/1"
21
22 View Code Duplication
    def _fixture_setup(self):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
23
        super()._fixture_setup()
24
25
        self.execution_1 = TestExecutionFactory()
26
        self.execution_1.case.summary = "Tested at " + timezone.now().isoformat()
27
        self.execution_1.case.text = "Given-When-Then"
28
        self.execution_1.case.save()  # will generate history object
29
        self.execution_1.run.summary = (
30
            "Automated TR for BitBucket integration on " + timezone.now().isoformat()
31
        )
32
        self.execution_1.run.save()
33
34
        self.component = ComponentFactory(
35
            name="BitBucket integration", product=self.execution_1.run.plan.product
36
        )
37
        self.execution_1.case.add_component(self.component)
38
39
        bug_system = BugSystem.objects.create(  # nosec:B106:hardcoded_password_funcarg
40
            name="BitBucket for kiwitcms/test-bitbucket-integration",
41
            tracker_type="tcms.issuetracker.bitbucket.BitBucket",
42
            base_url="https://bitbucket.org/kiwitcms/integration",
43
            api_username=os.getenv("BITBUCKET_INTEGRATION_API_USERNAME"),
44
            api_password=os.getenv("BITBUCKET_INTEGRATION_API_PASSWORD"),
45
        )
46
        self.integration = BitBucket(bug_system, None)
47
48
    def test_bug_id_from_url(self):
49
        result = self.integration.bug_id_from_url(self.existing_bug_url)
50
        self.assertEqual(self.existing_bug_id, result)
51
52
    def test_details(self):
53
        result = self.integration.details(self.existing_bug_url)
54
55
        self.assertEqual("Hello World", result["title"])
56
        self.assertIn("First public bug here", result["description"])
57
58
    def test_auto_update_bugtracker(self):
59
        initial_comments = self.integration.rpc.get_comments(self.existing_bug_id)
60
61
        # simulate user adding a new bug URL to a TE and clicking
62
        # 'Automatically update bug tracker'
63
        result = self.rpc_client.TestExecution.add_link(
64
            {
65
                "execution_id": self.execution_1.pk,
66
                "is_defect": True,
67
                "url": self.existing_bug_url,
68
            },
69
            True,
70
        )
71
72
        # making sure RPC above returned the same URL
73
        self.assertEqual(self.existing_bug_url, result["url"])
74
75
        # wait until comments have been refreshed b/c this seem to happen async
76
        initial_comments_length = initial_comments["size"]
77
        current_comments_length = initial_comments_length
78
        while current_comments_length != initial_comments_length + 1:
79
            comments = self.integration.rpc.get_comments(self.existing_bug_id)
80
            current_comments_length = comments["size"]
81
82
        last_comment = comments["values"][0]
0 ignored issues
show
The variable comments does not seem to be defined in case the while loop on line 78 is not entered. Are you sure this can never be the case?
Loading history...
83
84
        # assert that a comment has been added as the last one
85
        # and also verify its text
86
        for expected_string in [
87
            "Confirmed via test execution",
88
            f"TR-{self.execution_1.run_id}: {self.execution_1.run.summary}",
89
            self.execution_1.run.get_full_url(),
90
            f"TE-{self.execution_1.pk}: {self.execution_1.case.summary}",
91
        ]:
92
            self.assertIn(expected_string, last_comment["content"]["raw"])
93
94
        self.integration.rpc.delete_comment(self.existing_bug_id, last_comment["id"])
95
96 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...
97
        # simulate user clicking the 'Report bug' button in TE widget, TR page
98
        result = self.rpc_client.Bug.report(
99
            self.execution_1.pk, self.integration.bug_system.pk
100
        )
101
        self.assertEqual(result["rc"], 0)
102
        self.assertIn(self.integration.bug_system.base_url, result["response"])
103
        self.assertIn("/issues/", result["response"])
104
105
        new_issue_id = self.integration.bug_id_from_url(result["response"])
106
        issue = self.integration.rpc.get_issue(new_issue_id)
107
108
        self.assertEqual(
109
            f"Failed test: {self.execution_1.case.summary}", issue["title"]
110
        )
111
        for expected_string in [
112
            f"Filed from execution {self.execution_1.get_full_url()}",
113
            self.execution_1.run.plan.product.name,
114
            self.component.name,
115
            "Steps to reproduce",
116
            self.execution_1.case.text,
117
        ]:
118
            self.assertIn(expected_string, issue["content"]["raw"])
119
120
        # verify that LR has been added to TE
121
        self.assertTrue(
122
            LinkReference.objects.filter(
123
                execution=self.execution_1,
124
                url=result["response"],
125
                is_defect=True,
126
            ).exists()
127
        )
128
129
        # Close issue after test is finised.
130
        close_issue(self.integration.rpc, new_issue_id)
131
132
    def test_report_issue_from_test_execution_empty_baseurl_exception(self):
133
        # simulate user clicking the 'Report bug' button in TE widget, TR page
134
        bug_system = BugSystem.objects.create(  # nosec:B106:hardcoded_password_funcarg
135
            name="BitBucket for kiwitcms/test-bitbucket-integration Exception",
136
            tracker_type="tcms.issuetracker.bitbucket.BitBucket",
137
            base_url="incorrect_url",
138
            api_username=os.getenv("BITBUCKET_INTEGRATION_API_USERNAME"),
139
            api_password=os.getenv("BITBUCKET_INTEGRATION_API_PASSWORD"),
140
        )
141
        integration = BitBucket(bug_system, None)
142
        result = self.rpc_client.Bug.report(
143
            self.execution_1.pk, integration.bug_system.pk
144
        )
145
        self.assertIn("issues/new", result["response"])
146
147
148
@unittest.skipUnless(
149
    os.getenv("TEST_BUGTRACKER_INTEGRATION"),
150
    "Bug tracker integration testing not enabled",
151
)
152
class TestBitBucketAPI(unittest.TestCase):
153
    @classmethod
154
    def setUpClass(cls):
155
        super().setUpClass()
156
        client_id = os.getenv("BITBUCKET_INTEGRATION_API_USERNAME")
157
        client_secret = os.getenv("BITBUCKET_INTEGRATION_API_PASSWORD")
158
        base_url = "https://bitbucket.org/kiwitcms/integration"
159
        cls.api_instance = BitBucketAPI(base_url, client_id, client_secret)
160
161
        test_issue_data = {
162
            "title": "Sample Issue",
163
            "kind": "bug",
164
            "priority": "major",
165
            "content": {"raw": "Sample Issue Description"},
166
        }
167
        result = cls.api_instance.create_issue(test_issue_data)
168
        cls.issue_id = result["id"]
169
170
    @classmethod
171
    def tearDownClass(cls):
172
        close_issue(cls.api_instance, cls.issue_id)
173
        return super().tearDownClass()
174
175
    def test_create_issue(self):
176
        test_issue_data = {
177
            "title": "Sample Issue",
178
            "kind": "bug",
179
            "priority": "major",
180
            "content": {"raw": "Sample Issue Description"},
181
        }
182
        result = self.api_instance.create_issue(test_issue_data)
183
        self.assertEqual(result["title"], "Sample Issue")
184
185
        # Close work item after test is finished.
186
        close_issue(self.api_instance, result["id"])
187
188
    def test_add_comment(self):
189
        test_comment_body = {"content": {"raw": "Test Comment"}}
190
        result = self.api_instance.add_comment(self.issue_id, test_comment_body)
191
        self.assertEqual(result["content"]["raw"], "Test Comment")
192
        self.api_instance.delete_comment(self.issue_id, result["id"])
193
194
195
def close_issue(instance, issue_id):
196
    close_issue_body = {"changes": {"state": {"new": "resolved"}}}
197
    instance.update_issue(issue_id, close_issue_body)
198