Enable duplicate code detection for Python code
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 JIRA |
||
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 TestJIRAIntegration(APITestCase): |
||
21 | existing_bug_id = "JIRA-1" |
||
22 | existing_bug_url = "https://kiwitcms.atlassian.net/browse/JIRA-1" |
||
23 | |||
24 | View Code Duplication | def _fixture_setup(self): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
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 JIRA integration on " + timezone.now().isoformat() |
||
33 | ) |
||
34 | self.execution_1.run.save() |
||
35 | |||
36 | # this is the name of the JIRA Project |
||
37 | self.execution_1.run.plan.product.name = "Integration with JIRA" |
||
38 | self.execution_1.run.plan.product.save() |
||
39 | |||
40 | self.component = ComponentFactory( |
||
41 | name="JIRA integration", product=self.execution_1.run.plan.product |
||
42 | ) |
||
43 | self.execution_1.case.add_component(self.component) |
||
44 | |||
45 | bug_system = BugSystem.objects.create( # nosec:B106:hardcoded_password_funcarg |
||
46 | name="JIRA at kiwitcms.atlassian.net", |
||
47 | tracker_type="tcms.issuetracker.types.JIRA", |
||
48 | base_url="https://kiwitcms.atlassian.net", |
||
49 | api_username=os.getenv("JIRA_BUGTRACKER_INTEGRATION_API_USERNAME"), |
||
50 | api_password=os.getenv("JIRA_BUGTRACKER_INTEGRATION_API_TOKEN"), |
||
51 | ) |
||
52 | self.integration = JIRA(bug_system, None) |
||
53 | |||
54 | def test_bug_id_from_url(self): |
||
55 | result = self.integration.bug_id_from_url(self.existing_bug_url) |
||
56 | self.assertEqual(self.existing_bug_id, result) |
||
57 | |||
58 | def test_details_for_url(self): |
||
59 | result = self.integration.details(self.existing_bug_url) |
||
60 | |||
61 | self.assertEqual("Hello Jira Cloud", result["title"]) |
||
62 | self.assertEqual( |
||
63 | "This ticket is used in automated tests that verify Kiwi TCMS - JIRA " |
||
64 | "bug tracking integration.", |
||
65 | result["description"], |
||
66 | ) |
||
67 | |||
68 | def test_auto_update_bugtracker(self): |
||
69 | issue = self.integration.rpc.issue(self.existing_bug_id) |
||
70 | |||
71 | # make sure there are no comments to confuse the test |
||
72 | initial_comments_count = 0 |
||
73 | for comment in self.integration.rpc.comments(issue): |
||
74 | initial_comments_count += 1 |
||
75 | self.assertNotIn(self.execution_1.run.summary, comment.body) |
||
76 | |||
77 | # simulate user adding a new bug URL to a TE and clicking |
||
78 | # 'Automatically update bug tracker' |
||
79 | result = self.rpc_client.TestExecution.add_link( |
||
80 | { |
||
81 | "execution_id": self.execution_1.pk, |
||
82 | "is_defect": True, |
||
83 | "url": self.existing_bug_url, |
||
84 | }, |
||
85 | True, |
||
86 | ) |
||
87 | |||
88 | # making sure RPC above returned the same URL |
||
89 | self.assertEqual(self.existing_bug_url, result["url"]) |
||
90 | |||
91 | # wait until comments have been refreshed b/c this seem to happen async |
||
92 | retries = 0 |
||
93 | current_comment_count = 0 |
||
94 | while current_comment_count <= initial_comments_count: |
||
95 | current_comment_count = len(self.integration.rpc.comments(issue)) |
||
96 | time.sleep(1) |
||
97 | retries += 1 |
||
98 | self.assertLess(retries, 20) |
||
99 | |||
100 | # assert that a comment has been added as the last one |
||
101 | # and also verify its text |
||
102 | last_comment = self.integration.rpc.comments(issue)[-1] |
||
103 | for expected_string in [ |
||
104 | "Confirmed via test execution", |
||
105 | f"TR-{self.execution_1.run_id}: {self.execution_1.run.summary}", |
||
106 | self.execution_1.run.get_full_url(), |
||
107 | f"TE-{self.execution_1.pk}: {self.execution_1.case.summary}", |
||
108 | ]: |
||
109 | self.assertIn(expected_string, last_comment.body) |
||
110 | |||
111 | # clean up after ourselves in case everything above looks good |
||
112 | last_comment.delete() |
||
113 | |||
114 | def test_report_issue_from_test_execution_1click_works(self): |
||
115 | # simulate user clicking the 'Report bug' button in TE widget, TR page |
||
116 | result = self.rpc_client.Bug.report( |
||
117 | self.execution_1.pk, self.integration.bug_system.pk |
||
118 | ) |
||
119 | self.assertEqual(result["rc"], 0) |
||
120 | self.assertIn(self.integration.bug_system.base_url, result["response"]) |
||
121 | self.assertIn("https://kiwitcms.atlassian.net/browse/JIRA-", result["response"]) |
||
122 | |||
123 | new_issue_id = self.integration.bug_id_from_url(result["response"]) |
||
124 | issue = self.integration.rpc.issue(new_issue_id) |
||
125 | |||
126 | self.assertEqual( |
||
127 | f"Failed test: {self.execution_1.case.summary}", issue.fields.summary |
||
128 | ) |
||
129 | for expected_string in [ |
||
130 | f"Filed from execution {self.execution_1.get_full_url()}", |
||
131 | self.execution_1.run.plan.product.name, |
||
132 | self.component.name, |
||
133 | "Steps to reproduce", |
||
134 | self.execution_1.case.text, |
||
135 | ]: |
||
136 | self.assertIn(expected_string, issue.fields.description) |
||
137 | |||
138 | # verify that LR has been added to TE |
||
139 | self.assertTrue( |
||
140 | LinkReference.objects.filter( |
||
141 | execution=self.execution_1, |
||
142 | url=result["response"], |
||
143 | is_defect=True, |
||
144 | ).exists() |
||
145 | ) |
||
146 | |||
147 | # close issue after we're done |
||
148 | self.integration.rpc.transition_issue(issue, "DONE") |
||
149 |