Passed
Push — master ( fbd03e...630b73 )
by Alexander
02:15
created

TestJIRAIntegration.test_auto_update_bugtracker()   A

Complexity

Conditions 4

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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