Issues (87)

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

1
# pylint: disable=attribute-defined-outside-init
2
3
import os
4
import time
5
import unittest
6
7
from tcms.core.contrib.linkreference.models import LinkReference
8
from tcms.issuetracker.types import Gitlab
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 View Code Duplication
@unittest.skipUnless(
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
15
    os.getenv("TEST_BUGTRACKER_INTEGRATION"),
16
    "Bug tracker integration testing not enabled",
17
)
18
class TestGitlabIntegration(APITestCase):
19
    existing_bug_id = 1
20
    # NOTE: repository has been made private to workaround problems with automatically
21
    # created issues marked as SPAM, see:
22
    # https://gitlab.com/gitlab-org/gitlab/-/issues/337246
23
    existing_bug_url = "https://gitlab.com/kiwitcms/integration-testing/-/issues/1"
24
25
    def _fixture_setup(self):
26
        super()._fixture_setup()
27
28
        self.execution_1 = TestExecutionFactory()
29
        self.execution_1.case.text = "Given-When-Then"
30
        self.execution_1.case.save()  # will generate history object
31
32
        self.component = ComponentFactory(
33
            name="Gitlab integration", product=self.execution_1.run.plan.product
34
        )
35
        self.execution_1.case.add_component(self.component)
36
37
        bug_system = BugSystem.objects.create(  # nosec:B106:hardcoded_password_funcarg
38
            name="GitLab-EE for root/kiwitcms",
39
            tracker_type="tcms.issuetracker.types.Gitlab",
40
            base_url="https://gitlab.com/kiwitcms/integration-testing/",
41
            api_url="https://gitlab.com",
42
            api_password=os.getenv("GITLAB_INTEGRATION_API_TOKEN"),
43
        )
44
        self.integration = Gitlab(bug_system, None)
45
46
    def test_bug_id_from_url(self):
47
        result = self.integration.bug_id_from_url(self.existing_bug_url)
48
        self.assertEqual(self.existing_bug_id, result)
49
50
    def test_details_for_public_url(self):
51
        result = self.integration.details(self.existing_bug_url)
52
53
        self.assertEqual("Hello GitLab Hosted", result["title"])
54
        self.assertEqual("Here we start testing.", result["description"])
55
56
    def test_details_for_private_url(self):
57
        bug_system = BugSystem.objects.create(  # nosec:B106:hardcoded_password_funcarg
58
            name="Private GitLab for kiwitcms/katinar",
59
            tracker_type="tcms.issuetracker.types.Gitlab",
60
            base_url="https://gitlab.com/kiwitcms/katinar",
61
            api_url="https://gitlab.com",
62
            api_password=os.getenv("GITLAB_INTEGRATION_API_TOKEN"),
63
        )
64
        integration = Gitlab(bug_system, None)
65
66
        result = integration.details("https://gitlab.com/kiwitcms/katinar/-/issues/1")
67
68
        self.assertEqual("Hello Private GitLab", result["title"])
69
        self.assertEqual("Hello World", result["description"])
70
71
    def test_auto_update_bugtracker(self):
72
        repo_id = self.integration.it_class.repo_id(self.integration.bug_system)
73
        gl_project = self.integration.rpc.projects.get(repo_id)
74
        gl_issue = gl_project.issues.get(self.existing_bug_id)
75
76
        # make sure there are no comments to confuse the test
77
        initial_comment_count = 0
78
        for comment in gl_issue.notes.list():
79
            initial_comment_count += 1
80
            self.assertNotIn("Confirmed via test execution", comment.body)
81
82
        # simulate user adding a new bug URL to a TE and clicking
83
        # 'Automatically update bug tracker'
84
        result = self.rpc_client.TestExecution.add_link(
85
            {
86
                "execution_id": self.execution_1.pk,
87
                "is_defect": True,
88
                "url": self.existing_bug_url,
89
            },
90
            True,
91
        )
92
93
        # making sure RPC above returned the same URL
94
        self.assertEqual(self.existing_bug_url, result["url"])
95
96
        # wait until comments have been refreshed b/c this seem to happen async
97
        retries = 0
98
        while len(gl_issue.notes.list()) <= initial_comment_count:
99
            time.sleep(1)
100
            retries += 1
101
            self.assertLess(retries, 20)
102
103
        # sort by id b/c the gitlab library returns newest comments first but
104
        # that may be depending on configuration !
105
        last_comment = sorted(gl_issue.notes.list(), key=lambda x: x.id)[-1]
106
107
        # assert that a comment has been added as the last one
108
        # and also verify its text
109
        for expected_string in [
110
            "Confirmed via test execution",
111
            f"TR-{self.execution_1.run_id}: {self.execution_1.run.summary}",
112
            self.execution_1.run.get_full_url(),
113
            f"TE-{self.execution_1.pk}: {self.execution_1.case.summary}",
114
        ]:
115
            self.assertIn(expected_string, last_comment.body)
116
117
        # remove comment at the end to satisfy AssertNotIn above
118
        last_comment.delete()
119
120
    def test_report_issue_from_test_execution_1click_works(self):
121
        # simulate user clicking the 'Report bug' button in TE widget, TR page
122
        result = self.rpc_client.Bug.report(
123
            self.execution_1.pk, self.integration.bug_system.pk
124
        )
125
        self.assertEqual(result["rc"], 0)
126
        self.assertIn(self.integration.bug_system.base_url, result["response"])
127
        self.assertIn("/-/issues/", result["response"])
128
129
        # assert that the result looks like valid URL parameters
130
        new_issue_id = self.integration.bug_id_from_url(result["response"])
131
        repo_id = self.integration.it_class.repo_id(self.integration.bug_system)
132
        gl_project = self.integration.rpc.projects.get(repo_id)
133
        issue = gl_project.issues.get(new_issue_id)
134
135
        self.assertEqual(f"Failed test: {self.execution_1.case.summary}", issue.title)
136
        for expected_string in [
137
            f"Filed from execution {self.execution_1.get_full_url()}",
138
            self.execution_1.run.plan.product.name,
139
            self.component.name,
140
            "Steps to reproduce",
141
            self.execution_1.case.text,
142
        ]:
143
            self.assertIn(expected_string, issue.description)
144
145
        # verify that LR has been added to TE
146
        self.assertTrue(
147
            LinkReference.objects.filter(
148
                execution=self.execution_1,
149
                url=result["response"],
150
                is_defect=True,
151
            ).exists()
152
        )
153
154
        # close issue
155
        issue.state_event = "close"
156
        issue.save()
157