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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
import datetime
from builtins import next
from builtins import object
from unittest import mock
from collections import namedtuple
from six.moves 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, records):
self.records = records
def query(self, query):
return [namedtuple('Record', list(record.keys()))(**record)
for record in self.records]
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': [],
'assigned_to': None,
}
arbitrary_datetime = datetime.datetime.now(tz=datetime.timezone.utc)
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])
service._get_assigned_date = (
lambda issues: self.arbitrary_datetime.isoformat())
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)
def test_only_if_assigned(self):
with mock.patch('bugzilla.Bugzilla'):
self.service = self.get_mock_service(
BugzillaService,
config_overrides={
'bugzilla.only_if_assigned': 'hello',
})
assigned_records = [
{
'product': 'Product',
'component': 'Something',
'priority': 'urgent',
'status': 'ASSIGNED',
'summary': 'This is the issue summary',
'id': 1234568,
'flags': [],
'assigned_to': 'hello'
},
{
'product': 'Product',
'component': 'Something',
'priority': 'urgent',
'status': 'ASSIGNED',
'summary': 'This is the issue summary',
'id': 1234569,
'flags': [],
'assigned_to': 'somebodyelse'
},
]
self.service.bz.records.extend(assigned_records)
issues = self.service.issues()
expected = {
'annotations': [],
'bugzillaassignedon': self.arbitrary_datetime,
'bugzillabugid': 1234568,
'bugzillastatus': 'ASSIGNED',
'bugzillasummary': 'This is the issue summary',
'bugzillaurl': u'https://http://one.com//show_bug.cgi?id=1234568',
'bugzillaproduct': 'Product',
'bugzillacomponent': 'Something',
'description': u'(bw)Is#1234568 - This is the issue summary .. https://http://one.com//show_bug.cgi?id=1234568',
'priority': 'H',
'project': 'Something',
'tags': []}
self.assertEqual(next(issues).get_taskwarrior_record(), expected)
# Only one issue is assigned.
self.assertRaises(StopIteration, lambda: next(issues))
def test_also_unassigned(self):
with mock.patch('bugzilla.Bugzilla'):
self.service = self.get_mock_service(
BugzillaService,
config_overrides={
'bugzilla.only_if_assigned': 'hello',
'bugzilla.also_unassigned': True,
})
assigned_records = [
{
'product': 'Product',
'component': 'Something',
'priority': 'urgent',
'status': 'ASSIGNED',
'summary': 'This is the issue summary',
'id': 1234568,
'flags': [],
'assigned_to': 'hello'
},
{
'product': 'Product',
'component': 'Something',
'priority': 'urgent',
'status': 'ASSIGNED',
'summary': 'This is the issue summary',
'id': 1234569,
'flags': [],
'assigned_to': 'somebodyelse'
},
]
self.service.bz.records.extend(assigned_records)
issues = self.service.issues()
self.assertIn(next(issues).get_taskwarrior_record()['bugzillabugid'],
[1234567, 1234568])
self.assertIn(next(issues).get_taskwarrior_record()['bugzillabugid'],
[1234567, 1234568])
# Only two issues are assigned to the user or unassigned.
self.assertRaises(StopIteration, lambda: next(issues))
|