File: test_bugzilla.py

package info (click to toggle)
bugwarrior 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,008 kB
  • sloc: python: 7,762; makefile: 153
file content (139 lines) | stat: -rw-r--r-- 4,997 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from builtins import next
from builtins import object
import mock
from collections import namedtuple
import configparser

from bugwarrior.services.bz import BugzillaService

from .base import ConfigTest, ServiceTest, AbstractServiceTest
from bugwarrior.config import ServiceConfig


class FakeBugzillaLib(object):
    def __init__(self, record):
        self.record = record

    def query(self, query):
        Record = namedtuple('Record', list(self.record.keys()))
        return [Record(**self.record)]


class TestBugzillaServiceConfig(ConfigTest):

    def setUp(self):
        super(TestBugzillaServiceConfig, self).setUp()
        self.config = configparser.RawConfigParser()
        self.config.add_section('general')
        self.config.add_section('mybz')
        self.service_config = ServiceConfig(
            BugzillaService.CONFIG_PREFIX, self.config, 'mybz')

    @mock.patch('bugwarrior.services.bz.die')
    def test_validate_config_username_password(self, die):
        self.config.set('mybz', 'bugzilla.base_uri', 'http://one.com/')
        self.config.set('mybz', 'bugzilla.username', 'me')
        self.config.set('mybz', 'bugzilla.password', 'mypas')
        BugzillaService.validate_config(self.service_config, 'mybz')
        die.assert_not_called()

    @mock.patch('bugwarrior.services.bz.die')
    def test_validate_config_api_key(self, die):
        self.config.set('mybz', 'bugzilla.base_uri', 'http://one.com/')
        self.config.set('mybz', 'bugzilla.username', 'me')
        self.config.set('mybz', 'bugzilla.api_key', '123')
        BugzillaService.validate_config(self.service_config, 'mybz')
        die.assert_not_called()

    @mock.patch('bugwarrior.services.bz.die')
    def test_validate_config_api_key_no_username(self, die):
        self.config.set('mybz', 'bugzilla.base_uri', 'http://one.com/')
        self.config.set('mybz', 'bugzilla.api_key', '123')
        BugzillaService.validate_config(self.service_config, 'mybz')
        die.assert_called_with("[mybz] has no 'bugzilla.username'")


class TestBugzillaService(AbstractServiceTest, ServiceTest):
    SERVICE_CONFIG = {
        'bugzilla.base_uri': 'http://one.com/',
        'bugzilla.username': 'hello',
        'bugzilla.password': 'there',
    }

    arbitrary_record = {
        'product': 'Product',
        'component': 'Something',
        'priority': 'urgent',
        'status': 'NEW',
        'summary': 'This is the issue summary',
        'id': 1234567,
        'flags': [],
    }

    def setUp(self):
        super(TestBugzillaService, self).setUp()
        with mock.patch('bugzilla.Bugzilla'):
            self.service = self.get_mock_service(BugzillaService)

    def get_mock_service(self, *args, **kwargs):
        service = super(TestBugzillaService, self).get_mock_service(
            *args, **kwargs)
        service.bz = FakeBugzillaLib(self.arbitrary_record)
        return service

    def test_api_key_supplied(self):
        with mock.patch('bugzilla.Bugzilla'):
            self.service = self.get_mock_service(
                BugzillaService,
                config_overrides={
                    'bugzilla.base_uri': 'http://one.com/',
                    'bugzilla.username': 'me',
                    'bugzilla.api_key': '123',
                })

    def test_to_taskwarrior(self):
        arbitrary_extra = {
            'url': 'http://path/to/issue/',
            'annotations': [
                'Two',
            ],
        }

        issue = self.service.get_issue_for_record(
            self.arbitrary_record,
            arbitrary_extra,
        )

        expected_output = {
            'project': self.arbitrary_record['component'],
            'priority': issue.PRIORITY_MAP[self.arbitrary_record['priority']],
            'annotations': arbitrary_extra['annotations'],

            issue.STATUS: self.arbitrary_record['status'],
            issue.URL: arbitrary_extra['url'],
            issue.SUMMARY: self.arbitrary_record['summary'],
            issue.BUG_ID: self.arbitrary_record['id'],
            issue.PRODUCT: self.arbitrary_record['product'],
            issue.COMPONENT: self.arbitrary_record['component'],
        }
        actual_output = issue.to_taskwarrior()

        self.assertEqual(actual_output, expected_output)

    def test_issues(self):
        issue = next(self.service.issues())

        expected = {
            'annotations': [],
            'bugzillabugid': 1234567,
            'bugzillastatus': 'NEW',
            'bugzillasummary': 'This is the issue summary',
            'bugzillaurl': u'https://http://one.com//show_bug.cgi?id=1234567',
            'bugzillaproduct': 'Product',
            'bugzillacomponent': 'Something',
            'description': u'(bw)Is#1234567 - This is the issue summary .. https://http://one.com//show_bug.cgi?id=1234567',
            'priority': 'H',
            'project': 'Something',
            'tags': []}

        self.assertEqual(issue.get_taskwarrior_record(), expected)