Passed
Push — master ( 174f4e...fbd03e )
by Alexander
01:57
created

TestGitHubIntegration.test_details_for_url()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
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 TestGitHubIntegration(APITestCase):
21
    existing_bug_id = 'JIRA-1'
22
    existing_bug_url = 'https://kiwitcms.atlassian.net/browse/JIRA-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 = "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_url='https://kiwitcms.atlassian.net',
48
            api_username=os.getenv('JIRA_BUGTRACKER_INTEGRATION_API_USERNAME'),
49
            api_password=os.getenv('JIRA_BUGTRACKER_INTEGRATION_API_TOKEN'),
50
        )
51
        self.integration = JIRA(bug_system)
52
53
    def test_bug_id_from_url(self):
54
        result = self.integration.bug_id_from_url(self.existing_bug_url)
55
        self.assertEqual(self.existing_bug_id, result)
56
57
    def test_details_for_url(self):
58
        result = self.integration.details(self.existing_bug_url)
59
60
        self.assertEqual('Hello Jira Cloud', result['title'])
61
        self.assertEqual(
62
            "This ticket is used in automated tests that verify Kiwi TCMS - JIRA "
63
            "bug tracking integration.",
64
            result['description'])
65
66
    def test_auto_update_bugtracker(self):
67
        issue = self.integration.rpc.issue(self.existing_bug_id)
68
69
        # make sure there are no comments to confuse the test
70
        initial_comments_count = 0
71
        for comment in self.integration.rpc.comments(issue):
72
            initial_comments_count += 1
73
            self.assertNotIn(self.execution_1.run.summary, comment.body)
74
75
        # simulate user adding a new bug URL to a TE and clicking
76
        # 'Automatically update bug tracker'
77
        result = self.rpc_client.TestExecution.add_link({
78
            'execution_id': self.execution_1.pk,
79
            'is_defect': True,
80
            'url': self.existing_bug_url,
81
        }, True)
82
83
        # making sure RPC above returned the same URL
84
        self.assertEqual(self.existing_bug_url, result['url'])
85
86
        # wait until comments have been refreshed b/c this seem to happen async
87
        retries = 0
88
        current_comment_count = 0
89
        while current_comment_count <= initial_comments_count:
90
            current_comment_count = len(self.integration.rpc.comments(issue))
91
            time.sleep(1)
92
            retries += 1
93
            self.assertLess(retries, 20)
94
95
        # assert that a comment has been added as the last one
96
        # and also verify its text
97
        last_comment = self.integration.rpc.comments(issue)[-1]
98
        for expected_string in [
99
                'Confirmed via test execution',
100
                "TR-%d: %s" % (self.execution_1.run_id, self.execution_1.run.summary),
101
                self.execution_1.run.get_full_url(),
102
                "TE-%d: %s" % (self.execution_1.pk, self.execution_1.case.summary)]:
103
            self.assertIn(expected_string, last_comment.body)
104
105
        # clean up after ourselves in case everything above looks good
106
        last_comment.delete()
107
108 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...
109
        # simulate user clicking the 'Report bug' button in TE widget, TR page
110
        result = self.rpc_client.Bug.report(self.execution_1.pk, self.integration.bug_system.pk)
111
        self.assertEqual(result['rc'], 0)
112
        self.assertIn(self.integration.bug_system.base_url, result['response'])
113
        self.assertIn('https://kiwitcms.atlassian.net/browse/JIRA-', result['response'])
114
115
        new_issue_id = self.integration.bug_id_from_url(result['response'])
116
        issue = self.integration.rpc.issue(new_issue_id)
117
118
        self.assertEqual("Failed test: %s" % self.execution_1.case.summary, issue.fields.summary)
119
        for expected_string in [
120
                "Filed from execution %s" % self.execution_1.get_full_url(),
121
                self.execution_1.run.plan.product.name,
122
                self.component.name,
123
                "Steps to reproduce",
124
                self.execution_1.case.text]:
125
            self.assertIn(expected_string, issue.fields.description)
126
127
        # verify that LR has been added to TE
128
        self.assertTrue(LinkReference.objects.filter(
129
            execution=self.execution_1,
130
            url=result['response'],
131
            is_defect=True,
132
        ).exists())
133
134
        # close issue after we're done
135
        self.integration.rpc.transition_issue(issue, 'DONE')
136