Debian Codegraph @

tests/test_bugzilla.py

raw
1 import datetime
2 from builtins import next
3 from builtins import object
4 from unittest import mock
5 from collections import namedtuple
6 from six.moves import configparser
7
8 from bugwarrior.services.bz import BugzillaService
9
10 from .base import ConfigTest, ServiceTest, AbstractServiceTest
11 from bugwarrior.config import ServiceConfig
12
13
14 class FakeBugzillaLib(object):
15 def __init__(self, records):
16 self.records = records
17
18 def query(self, query):
19 return [namedtuple('Record', list(record.keys()))(**record)
20 for record in self.records]
21
22
23 class TestBugzillaServiceConfig(ConfigTest):
24
25 def setUp(self):
26 super(TestBugzillaServiceConfig, self).setUp()
27 self.config = configparser.RawConfigParser()
28 self.config.add_section('general')
29 self.config.add_section('mybz')
30 self.service_config = ServiceConfig(
31 BugzillaService.CONFIG_PREFIX, self.config, 'mybz')
32
33 @mock.patch('bugwarrior.services.bz.die')
34 def test_validate_config_username_password(self, die):
35 self.config.set('mybz', 'bugzilla.base_uri', 'http://one.com/')
36 self.config.set('mybz', 'bugzilla.username', 'me')
37 self.config.set('mybz', 'bugzilla.password', 'mypas')
38 BugzillaService.validate_config(self.service_config, 'mybz')
39 die.assert_not_called()
40
41 @mock.patch('bugwarrior.services.bz.die')
42 def test_validate_config_api_key(self, die):
43 self.config.set('mybz', 'bugzilla.base_uri', 'http://one.com/')
44 self.config.set('mybz', 'bugzilla.username', 'me')
45 self.config.set('mybz', 'bugzilla.api_key', '123')
46 BugzillaService.validate_config(self.service_config, 'mybz')
47 die.assert_not_called()
48
49 @mock.patch('bugwarrior.services.bz.die')
50 def test_validate_config_api_key_no_username(self, die):
51 self.config.set('mybz', 'bugzilla.base_uri', 'http://one.com/')
52 self.config.set('mybz', 'bugzilla.api_key', '123')
53 BugzillaService.validate_config(self.service_config, 'mybz')
54 die.assert_called_with("[mybz] has no 'bugzilla.username'")
55
56
57 class TestBugzillaService(AbstractServiceTest, ServiceTest):
58 SERVICE_CONFIG = {
59 'bugzilla.base_uri': 'http://one.com/',
60 'bugzilla.username': 'hello',
61 'bugzilla.password': 'there',
62 }
63
64 arbitrary_record = {
65 'product': 'Product',
66 'component': 'Something',
67 'priority': 'urgent',
68 'status': 'NEW',
69 'summary': 'This is the issue summary',
70 'id': 1234567,
71 'flags': [],
72 'assigned_to': None,
73 }
74
75 arbitrary_datetime = datetime.datetime.now(tz=datetime.timezone.utc)
76
77 def setUp(self):
78 super(TestBugzillaService, self).setUp()
79 with mock.patch('bugzilla.Bugzilla'):
80 self.service = self.get_mock_service(BugzillaService)
81
82 def get_mock_service(self, *args, **kwargs):
83 service = super(TestBugzillaService, self).get_mock_service(
84 *args, **kwargs)
85 service.bz = FakeBugzillaLib([self.arbitrary_record])
86 service._get_assigned_date = (
87 lambda issues: self.arbitrary_datetime.isoformat())
88 return service
89
90 def test_api_key_supplied(self):
91 with mock.patch('bugzilla.Bugzilla'):
92 self.service = self.get_mock_service(
93 BugzillaService,
94 config_overrides={
95 'bugzilla.base_uri': 'http://one.com/',
96 'bugzilla.username': 'me',
97 'bugzilla.api_key': '123',
98 })
99
100 def test_to_taskwarrior(self):
101 arbitrary_extra = {
102 'url': 'http://path/to/issue/',
103 'annotations': [
104 'Two',
105 ],
106 }
107
108 issue = self.service.get_issue_for_record(
109 self.arbitrary_record,
110 arbitrary_extra,
111 )
112
113 expected_output = {
114 'project': self.arbitrary_record['component'],
115 'priority': issue.PRIORITY_MAP[self.arbitrary_record['priority']],
116 'annotations': arbitrary_extra['annotations'],
117
118 issue.STATUS: self.arbitrary_record['status'],
119 issue.URL: arbitrary_extra['url'],
120 issue.SUMMARY: self.arbitrary_record['summary'],
121 issue.BUG_ID: self.arbitrary_record['id'],
122 issue.PRODUCT: self.arbitrary_record['product'],
123 issue.COMPONENT: self.arbitrary_record['component'],
124 }
125 actual_output = issue.to_taskwarrior()
126
127 self.assertEqual(actual_output, expected_output)
128
129 def test_issues(self):
130 issue = next(self.service.issues())
131
132 expected = {
133 'annotations': [],
134 'bugzillabugid': 1234567,
135 'bugzillastatus': 'NEW',
136 'bugzillasummary': 'This is the issue summary',
137 'bugzillaurl': u'https://http://one.com//show_bug.cgi?id=1234567',
138 'bugzillaproduct': 'Product',
139 'bugzillacomponent': 'Something',
140 'description': u'(bw)Is#1234567 - This is the issue summary .. https://http://one.com//show_bug.cgi?id=1234567',
141 'priority': 'H',
142 'project': 'Something',
143 'tags': []}
144
145 self.assertEqual(issue.get_taskwarrior_record(), expected)
146
147 def test_only_if_assigned(self):
148 with mock.patch('bugzilla.Bugzilla'):
149 self.service = self.get_mock_service(
150 BugzillaService,
151 config_overrides={
152 'bugzilla.only_if_assigned': 'hello',
153 })
154
155 assigned_records = [
156 {
157 'product': 'Product',
158 'component': 'Something',
159 'priority': 'urgent',
160 'status': 'ASSIGNED',
161 'summary': 'This is the issue summary',
162 'id': 1234568,
163 'flags': [],
164 'assigned_to': 'hello'
165 },
166 {
167 'product': 'Product',
168 'component': 'Something',
169 'priority': 'urgent',
170 'status': 'ASSIGNED',
171 'summary': 'This is the issue summary',
172 'id': 1234569,
173 'flags': [],
174 'assigned_to': 'somebodyelse'
175 },
176 ]
177 self.service.bz.records.extend(assigned_records)
178
179 issues = self.service.issues()
180
181 expected = {
182 'annotations': [],
183 'bugzillaassignedon': self.arbitrary_datetime,
184 'bugzillabugid': 1234568,
185 'bugzillastatus': 'ASSIGNED',
186 'bugzillasummary': 'This is the issue summary',
187 'bugzillaurl': u'https://http://one.com//show_bug.cgi?id=1234568',
188 'bugzillaproduct': 'Product',
189 'bugzillacomponent': 'Something',
190 'description': u'(bw)Is#1234568 - This is the issue summary .. https://http://one.com//show_bug.cgi?id=1234568',
191 'priority': 'H',
192 'project': 'Something',
193 'tags': []}
194
195 self.assertEqual(next(issues).get_taskwarrior_record(), expected)
196
197 # Only one issue is assigned.
198 self.assertRaises(StopIteration, lambda: next(issues))
199
200 def test_also_unassigned(self):
201 with mock.patch('bugzilla.Bugzilla'):
202 self.service = self.get_mock_service(
203 BugzillaService,
204 config_overrides={
205 'bugzilla.only_if_assigned': 'hello',
206 'bugzilla.also_unassigned': True,
207 })
208
209 assigned_records = [
210 {
211 'product': 'Product',
212 'component': 'Something',
213 'priority': 'urgent',
214 'status': 'ASSIGNED',
215 'summary': 'This is the issue summary',
216 'id': 1234568,
217 'flags': [],
218 'assigned_to': 'hello'
219 },
220 {
221 'product': 'Product',
222 'component': 'Something',
223 'priority': 'urgent',
224 'status': 'ASSIGNED',
225 'summary': 'This is the issue summary',
226 'id': 1234569,
227 'flags': [],
228 'assigned_to': 'somebodyelse'
229 },
230 ]
231 self.service.bz.records.extend(assigned_records)
232
233 issues = self.service.issues()
234
235 self.assertIn(next(issues).get_taskwarrior_record()['bugzillabugid'],
236 [1234567, 1234568])
237 self.assertIn(next(issues).get_taskwarrior_record()['bugzillabugid'],
238 [1234567, 1234568])
239 # Only two issues are assigned to the user or unassigned.
240 self.assertRaises(StopIteration, lambda: next(issues))