1
|
|
|
# pylint: disable=attribute-defined-outside-init |
2
|
|
|
|
3
|
|
|
import os |
4
|
|
|
import time |
5
|
|
|
import unittest |
6
|
|
|
|
7
|
|
|
from django.utils import timezone |
8
|
|
|
|
9
|
|
|
from tcms.core.contrib.linkreference.models import LinkReference |
10
|
|
|
from tcms.issuetracker.types import GitHub |
11
|
|
|
from tcms.rpc.tests.utils import APITestCase |
12
|
|
|
from tcms.testcases.models import BugSystem |
13
|
|
|
from tcms.tests.factories import ComponentFactory, TestExecutionFactory |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@unittest.skipUnless( |
17
|
|
|
os.getenv("TEST_BUGTRACKER_INTEGRATION"), |
18
|
|
|
"Bug tracker integration testing not enabled", |
19
|
|
|
) |
20
|
|
|
class TestGitHubIntegration(APITestCase): |
21
|
|
|
existing_bug_id = 1 |
22
|
|
|
existing_bug_url = "https://github.com/kiwitcms/test-github-integration/issues/1" |
23
|
|
|
|
24
|
|
|
def _fixture_setup(self): |
25
|
|
|
super()._fixture_setup() |
26
|
|
|
|
27
|
|
|
self.execution_1 = TestExecutionFactory() |
28
|
|
|
self.execution_1.case.summary = "Tested at " + timezone.now().isoformat() |
29
|
|
|
self.execution_1.case.text = "Given-When-Then" |
30
|
|
|
self.execution_1.case.save() # will generate history object |
31
|
|
|
self.execution_1.run.summary = ( |
32
|
|
|
"Automated TR for GitHub integration on " + timezone.now().isoformat() |
33
|
|
|
) |
34
|
|
|
self.execution_1.run.save() |
35
|
|
|
|
36
|
|
|
self.component = ComponentFactory( |
37
|
|
|
name="GitHub integration", product=self.execution_1.run.plan.product |
38
|
|
|
) |
39
|
|
|
self.execution_1.case.add_component(self.component) |
40
|
|
|
|
41
|
|
|
bug_system = BugSystem.objects.create( # nosec:B106:hardcoded_password_funcarg |
42
|
|
|
name="GitHub for kiwitcms/test-github-integration", |
43
|
|
|
tracker_type="tcms.issuetracker.types.GitHub", |
44
|
|
|
base_url="https://github.com/kiwitcms/test-github-integration", |
45
|
|
|
api_password=os.getenv("GH_BUGTRACKER_INTEGRATION_TEST_API_TOKEN"), |
46
|
|
|
) |
47
|
|
|
self.integration = GitHub(bug_system, None) |
48
|
|
|
|
49
|
|
|
def test_bug_id_from_url(self): |
50
|
|
|
result = self.integration.bug_id_from_url(self.existing_bug_url) |
51
|
|
|
self.assertEqual(self.existing_bug_id, result) |
52
|
|
|
|
53
|
|
|
def test_details_for_public_url(self): |
54
|
|
|
result = self.integration.details(self.existing_bug_url) |
55
|
|
|
|
56
|
|
|
self.assertEqual("Hello GitHub", result["title"]) |
57
|
|
|
self.assertEqual( |
58
|
|
|
"This issue is used in automated tests that verify Kiwi TCMS - GitHub " |
59
|
|
|
"bug tracking integration!", |
60
|
|
|
result["description"], |
61
|
|
|
) |
62
|
|
|
|
63
|
|
|
def test_details_for_private_url(self): |
64
|
|
|
bug_system = BugSystem.objects.create( # nosec:B106:hardcoded_password_funcarg |
65
|
|
|
name="Private GitHub for kiwitcms/private-test-github-integration", |
66
|
|
|
tracker_type="GitHub", |
67
|
|
|
base_url="https://github.com/kiwitcms/private-test-github-integration", |
68
|
|
|
api_password=os.getenv("GH_BUGTRACKER_INTEGRATION_TEST_API_TOKEN"), |
69
|
|
|
) |
70
|
|
|
integration = GitHub(bug_system, None) |
71
|
|
|
|
72
|
|
|
result = integration.details( |
73
|
|
|
"https://github.com/kiwitcms/private-test-github-integration/issues/1" |
74
|
|
|
) |
75
|
|
|
|
76
|
|
|
self.assertEqual("Hello Private GitHub", result["title"]) |
77
|
|
|
self.assertEqual( |
78
|
|
|
"This issue is used in automated tests that verify " |
79
|
|
|
"Kiwi TCMS - GitHub bug tracking integration!", |
80
|
|
|
result["description"], |
81
|
|
|
) |
82
|
|
|
|
83
|
|
View Code Duplication |
def test_auto_update_bugtracker(self): |
|
|
|
|
84
|
|
|
repo_id = self.integration.it_class.repo_id(self.integration.bug_system) |
85
|
|
|
repo = self.integration.rpc.get_repo(repo_id) |
86
|
|
|
issue = repo.get_issue(self.existing_bug_id) |
87
|
|
|
|
88
|
|
|
# make sure there are no comments to confuse the test |
89
|
|
|
initial_comments_count = 0 |
90
|
|
|
for comment in issue.get_comments(): |
91
|
|
|
initial_comments_count += 1 |
92
|
|
|
self.assertNotIn(self.execution_1.run.summary, comment.body) |
93
|
|
|
|
94
|
|
|
# simulate user adding a new bug URL to a TE and clicking |
95
|
|
|
# 'Automatically update bug tracker' |
96
|
|
|
result = self.rpc_client.TestExecution.add_link( |
97
|
|
|
{ |
98
|
|
|
"execution_id": self.execution_1.pk, |
99
|
|
|
"is_defect": True, |
100
|
|
|
"url": self.existing_bug_url, |
101
|
|
|
}, |
102
|
|
|
True, |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
# making sure RPC above returned the same URL |
106
|
|
|
self.assertEqual(self.existing_bug_url, result["url"]) |
107
|
|
|
|
108
|
|
|
# wait until comments have been refreshed b/c this seem to happen async |
109
|
|
|
retries = 0 |
110
|
|
|
last_comment = None |
111
|
|
|
current_comment_count = 0 |
112
|
|
|
while current_comment_count <= initial_comments_count: |
113
|
|
|
current_comment_count = 0 |
114
|
|
|
# .get_comments() returns an iterator |
115
|
|
|
for comment in issue.get_comments(): |
116
|
|
|
current_comment_count += 1 |
117
|
|
|
last_comment = comment |
118
|
|
|
|
119
|
|
|
time.sleep(1) |
120
|
|
|
retries += 1 |
121
|
|
|
self.assertLess(retries, 20) |
122
|
|
|
|
123
|
|
|
# assert that a comment has been added as the last one |
124
|
|
|
# and also verify its text |
125
|
|
|
for expected_string in [ |
126
|
|
|
"Confirmed via test execution", |
127
|
|
|
f"TR-{self.execution_1.run_id}: {self.execution_1.run.summary}", |
128
|
|
|
self.execution_1.run.get_full_url(), |
129
|
|
|
f"TE-{self.execution_1.pk}: {self.execution_1.case.summary}", |
130
|
|
|
]: |
131
|
|
|
self.assertIn(expected_string, last_comment.body) |
132
|
|
|
|
133
|
|
|
# clean up after ourselves in case everything above looks good |
134
|
|
|
last_comment.delete() |
135
|
|
|
|
136
|
|
|
def test_report_issue_from_test_execution_1click_works(self): |
137
|
|
|
# simulate user clicking the 'Report bug' button in TE widget, TR page |
138
|
|
|
result = self.rpc_client.Bug.report( |
139
|
|
|
self.execution_1.pk, self.integration.bug_system.pk |
140
|
|
|
) |
141
|
|
|
self.assertEqual(result["rc"], 0) |
142
|
|
|
self.assertIn(self.integration.bug_system.base_url, result["response"]) |
143
|
|
|
self.assertIn("/issues/", result["response"]) |
144
|
|
|
|
145
|
|
|
new_issue_id = self.integration.bug_id_from_url(result["response"]) |
146
|
|
|
repo_id = self.integration.it_class.repo_id(self.integration.bug_system) |
147
|
|
|
repo = self.integration.rpc.get_repo(repo_id) |
148
|
|
|
issue = repo.get_issue(new_issue_id) |
149
|
|
|
|
150
|
|
|
self.assertEqual(f"Failed test: {self.execution_1.case.summary}", issue.title) |
151
|
|
|
for expected_string in [ |
152
|
|
|
f"Filed from execution {self.execution_1.get_full_url()}", |
153
|
|
|
self.execution_1.run.plan.product.name, |
154
|
|
|
self.component.name, |
155
|
|
|
"Steps to reproduce", |
156
|
|
|
self.execution_1.case.text, |
157
|
|
|
]: |
158
|
|
|
self.assertIn(expected_string, issue.body) |
159
|
|
|
|
160
|
|
|
# verify that LR has been added to TE |
161
|
|
|
self.assertTrue( |
162
|
|
|
LinkReference.objects.filter( |
163
|
|
|
execution=self.execution_1, |
164
|
|
|
url=result["response"], |
165
|
|
|
is_defect=True, |
166
|
|
|
).exists() |
167
|
|
|
) |
168
|
|
|
|
169
|
|
|
# close issue after we're done |
170
|
|
|
issue.edit(state="closed") |
171
|
|
|
|