| 1 |
import datetime |
| 2 |
from dateutil.tz import tzutc |
| 3 |
from six.moves import configparser |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import responses |
| 7 |
|
| 8 |
from .base import ServiceTest, AbstractServiceTest, ConfigTest |
| 9 |
from bugwarrior.config import ServiceConfig |
| 10 |
from bugwarrior.services.pivotaltracker import PivotalTrackerService |
| 11 |
|
| 12 |
|
| 13 |
PROJECT = { |
| 14 |
'account_id': 100, |
| 15 |
'atom_enabled': True, |
| 16 |
'automatic_planning': True, |
| 17 |
'bugs_and_chores_are_estimatable': False, |
| 18 |
'created_at': '2019-05-14T12:00:05Z', |
| 19 |
'current_iteration_number': 15, |
| 20 |
'description': 'Expeditionary Battle Planetoid', |
| 21 |
'enable_following': True, |
| 22 |
'enable_incoming_emails': True, |
| 23 |
'enable_tasks': True, |
| 24 |
'has_google_domain': False, |
| 25 |
'id': 99, |
| 26 |
'initial_velocity': 10, |
| 27 |
'iteration_length': 1, |
| 28 |
'kind': 'project', |
| 29 |
'name': 'Death Star', |
| 30 |
'number_of_done_iterations_to_show': 4, |
| 31 |
'point_scale': '0,1,2,3', |
| 32 |
'point_scale_is_custom': False, |
| 33 |
'profile_content': "This is a machine of war such as the universe has never known. It's colossal, the size of a class-four moon. And it possesses firepower unequaled in the history of warfare.", |
| 34 |
'project_type': 'private', |
| 35 |
'public': False, |
| 36 |
'start_date': '2019-01-28', |
| 37 |
'start_time': '2019-05-14T12:00:10Z', |
| 38 |
'time_zone': |
| 39 |
{ |
| 40 |
'kind': 'time_zone', |
| 41 |
'olson_name': 'America/Los_Angeles', |
| 42 |
'offset': '-07:00' |
| 43 |
}, |
| 44 |
'updated_at': '2019-05-14T12:00:10Z', |
| 45 |
'velocity_averaged_over': 3, |
| 46 |
'version': 66, |
| 47 |
'week_start_day': 'Monday' |
| 48 |
} |
| 49 |
|
| 50 |
STORY = { |
| 51 |
'project': PROJECT, |
| 52 |
'kind': 'story', |
| 53 |
'id': 561, |
| 54 |
'created_at': '2019-05-14T12:00:00Z', |
| 55 |
'updated_at': '2019-05-14T12:00:00Z', |
| 56 |
'accepted_at': '2019-05-14T12:00:00Z', |
| 57 |
'story_type': 'story', |
| 58 |
'estimate': 3, |
| 59 |
'name': 'Tractor beam loses power intermittently', |
| 60 |
'description': 'All your base are belong to us', |
| 61 |
'current_state': 'unstarted', |
| 62 |
'requested_by_id': 106, |
| 63 |
'url': 'http://localhost/story/show/561', |
| 64 |
'project_id': 99, |
| 65 |
'owner_ids': |
| 66 |
[ |
| 67 |
106 |
| 68 |
], |
| 69 |
'labels': |
| 70 |
[ |
| 71 |
{ |
| 72 |
'kind': 'label', |
| 73 |
'id': 5101, |
| 74 |
'project_id': 99, |
| 75 |
'name': 'look sir metal', |
| 76 |
'created_at': '2019-05-14T12:00:05Z', |
| 77 |
'updated_at': '2019-05-14T12:00:05Z' |
| 78 |
}, |
| 79 |
] |
| 80 |
} |
| 81 |
|
| 82 |
USER = [ |
| 83 |
{ |
| 84 |
'created_at': '2019-05-14T12:00:00Z', |
| 85 |
'favorite': False, |
| 86 |
'id': 16200, |
| 87 |
'kind': 'project_membership', |
| 88 |
'person': |
| 89 |
{ |
| 90 |
'kind': 'person', |
| 91 |
'id': 106, |
| 92 |
'name': 'Galen Marek', |
| 93 |
'email': '[email protected]', |
| 94 |
'initials': 'GM', |
| 95 |
'username': 'starkiller' |
| 96 |
}, |
| 97 |
'project_color': 'b800bb', |
| 98 |
'project_id': 99, |
| 99 |
'role': 'member', |
| 100 |
'updated_at': '2019-05-14T12:00:00Z', |
| 101 |
'wants_comment_notification_emails': True, |
| 102 |
'will_receive_mention_notifications_or_emails': True |
| 103 |
} |
| 104 |
] |
| 105 |
|
| 106 |
TASKS = [ |
| 107 |
{ |
| 108 |
'kind': 'task', |
| 109 |
'id': 5, |
| 110 |
'story_id': 561, |
| 111 |
'description': 'Port 0', |
| 112 |
'complete': False, |
| 113 |
'position': 1, |
| 114 |
'created_at': '2019-05-14T12:00:00Z', |
| 115 |
'updated_at': '2019-05-14T12:00:00Z' |
| 116 |
}, |
| 117 |
{ |
| 118 |
'kind': 'task', |
| 119 |
'id': 6, |
| 120 |
'story_id': 561, |
| 121 |
'description': 'Port 90', |
| 122 |
'complete': False, |
| 123 |
'position': 2, |
| 124 |
'created_at': '2019-05-14T12:00:00Z', |
| 125 |
'updated_at': '2019-05-14T12:00:00Z' |
| 126 |
} |
| 127 |
] |
| 128 |
|
| 129 |
BLOCKERS = [ |
| 130 |
{ |
| 131 |
'kind': 'blocker', |
| 132 |
'id': 1100, |
| 133 |
'story_id': 561, |
| 134 |
'person_id': 106, |
| 135 |
'description': 'Set weapons to stun', |
| 136 |
'resolved': False, |
| 137 |
'created_at': '2019-05-14T12:00:00Z', |
| 138 |
'updated_at': '2019-05-14T12:00:00Z' |
| 139 |
} |
| 140 |
] |
| 141 |
|
| 142 |
QUERY = { |
| 143 |
"epics": |
| 144 |
{ |
| 145 |
"epics": |
| 146 |
[ |
| 147 |
], |
| 148 |
"total_hits": 0 |
| 149 |
}, |
| 150 |
"query": "mywork:106", |
| 151 |
"stories": { |
| 152 |
"stories": |
| 153 |
[ |
| 154 |
STORY |
| 155 |
], |
| 156 |
"total_points": 0, |
| 157 |
"total_points_completed": 0, |
| 158 |
"total_hits": 1, |
| 159 |
"total_hits_with_done": 0 |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
EXTRA = { |
| 164 |
'request_user': |
| 165 |
[ |
| 166 |
'request_user' |
| 167 |
], |
| 168 |
'owned_user': |
| 169 |
[ |
| 170 |
'owned_user' |
| 171 |
], |
| 172 |
'annotations': TASKS, |
| 173 |
'blockers': BLOCKERS, |
| 174 |
'project_name': PROJECT['name'] |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
class TestPivotalTrackerServiceConfig(ConfigTest): |
| 179 |
|
| 180 |
def setUp(self): |
| 181 |
super(TestPivotalTrackerServiceConfig, self).setUp() |
| 182 |
self.config = configparser.RawConfigParser() |
| 183 |
self.config.add_section('general') |
| 184 |
self.config.add_section('pivotal') |
| 185 |
self.service_config = ServiceConfig( |
| 186 |
PivotalTrackerService.CONFIG_PREFIX, self.config, 'pivotal') |
| 187 |
|
| 188 |
@mock.patch('bugwarrior.services.pivotaltracker.die') |
| 189 |
def test_validate_config(self, die): |
| 190 |
self.config.set('pivotal', 'pivotaltracker.account_ids', [12345]) |
| 191 |
self.config.set('pivotal', 'pivotaltracker.user_id', '12345') |
| 192 |
self.config.set('pivotal', 'pivotaltracker.token', '12345') |
| 193 |
PivotalTrackerService.validate_config(self.service_config, 'pivotal') |
| 194 |
die.assert_not_called() |
| 195 |
|
| 196 |
@mock.patch('bugwarrior.services.pivotaltracker.die') |
| 197 |
def test_validate_config_no_account_ids(self, die): |
| 198 |
self.config.set('pivotal', 'pivotaltracker.token', '123') |
| 199 |
self.config.set('pivotal', 'pivotaltracker.user_id', '12345') |
| 200 |
PivotalTrackerService.validate_config(self.service_config, 'pivotal') |
| 201 |
die.assert_called_with("[pivotal] has no 'pivotaltracker.account_ids'") |
| 202 |
|
| 203 |
@mock.patch('bugwarrior.services.pivotaltracker.die') |
| 204 |
def test_validate_config_no_user_id(self, die): |
| 205 |
self.config.set('pivotal', 'pivotaltracker.account_ids', [12345]) |
| 206 |
self.config.set('pivotal', 'pivotaltracker.token', '123') |
| 207 |
PivotalTrackerService.validate_config(self.service_config, 'pivotal') |
| 208 |
die.assert_called_with("[pivotal] has no 'pivotaltracker.user_id'") |
| 209 |
|
| 210 |
@mock.patch('bugwarrior.services.pivotaltracker.die') |
| 211 |
def test_validate_config_token(self, die): |
| 212 |
self.config.set('pivotal', 'pivotaltracker.account_ids', [12345]) |
| 213 |
self.config.set('pivotal', 'pivotaltracker.user_id', '12345') |
| 214 |
PivotalTrackerService.validate_config(self.service_config, 'pivotal') |
| 215 |
die.assert_called_with("[pivotal] has no 'pivotaltracker.token'") |
| 216 |
|
| 217 |
@mock.patch('bugwarrior.services.pivotaltracker.die') |
| 218 |
def test_validate_config_invalid_endpoint(self, die): |
| 219 |
self.config.set('pivotal', 'pivotaltracker.account_ids', [12345]) |
| 220 |
self.config.set('pivotal', 'pivotaltracker.token', '123') |
| 221 |
self.config.set('pivotal', 'pivotaltracker.user_id', '12345') |
| 222 |
self.config.set('pivotal', 'pivotaltracker.version', 'v1') |
| 223 |
PivotalTrackerService.validate_config(self.service_config, 'pivotal') |
| 224 |
die.assert_called_with("[pivotal] has an invalid 'pivotaltracker.version'") |
| 225 |
|
| 226 |
|
| 227 |
class TestPivotalTrackerIssue(AbstractServiceTest, ServiceTest): |
| 228 |
SERVICE_CONFIG = { |
| 229 |
'pivotaltracker.token': '123456', |
| 230 |
'pivotaltracker.user_id': 106, |
| 231 |
'pivotaltracker.account_ids': '100', |
| 232 |
'pivotaltracker.annotation_comments': True, |
| 233 |
'pivotaltracker.import_labels_as_tags': True, |
| 234 |
'pivotaltracker.import_blockers': True |
| 235 |
} |
| 236 |
|
| 237 |
def setUp(self): |
| 238 |
super(TestPivotalTrackerIssue, self).setUp() |
| 239 |
self.service = self.get_mock_service(PivotalTrackerService) |
| 240 |
responses.add(responses.GET, |
| 241 |
'https://www.pivotaltracker.com/services/v5/projects?account_ids=100', |
| 242 |
json=[PROJECT]) |
| 243 |
responses.add(responses.GET, |
| 244 |
'https://www.pivotaltracker.com/services/v5/projects/99/search?query=mywork:106', |
| 245 |
json=QUERY) |
| 246 |
responses.add(responses.GET, |
| 247 |
'https://www.pivotaltracker.com/services/v5/projects/99/stories/561/tasks', |
| 248 |
json=TASKS) |
| 249 |
responses.add(responses.GET, |
| 250 |
'https://www.pivotaltracker.com/services/v5/projects/99/stories/561/blockers', |
| 251 |
json=BLOCKERS) |
| 252 |
responses.add(responses.GET, |
| 253 |
'https://www.pivotaltracker.com/services/v5/projects/99/memberships', |
| 254 |
json=USER) |
| 255 |
|
| 256 |
def test_normalize_label_to_tag(self): |
| 257 |
story = self.service.get_issue_for_record(STORY, EXTRA) |
| 258 |
self.assertEqual(story._normalize_label_to_tag('needs work'), 'needs_work') |
| 259 |
|
| 260 |
def test_to_taskwarrior(self): |
| 261 |
story = self.service.get_issue_for_record( |
| 262 |
STORY, EXTRA |
| 263 |
) |
| 264 |
|
| 265 |
expected_output = { |
| 266 |
'annotations': |
| 267 |
[ |
| 268 |
{ |
| 269 |
'complete': False, |
| 270 |
'created_at': '2019-05-14T12:00:00Z', |
| 271 |
'description': 'Port 0', |
| 272 |
'id': 5, |
| 273 |
'kind': 'task', |
| 274 |
'position': 1, |
| 275 |
'story_id': 561, |
| 276 |
'updated_at': '2019-05-14T12:00:00Z' |
| 277 |
}, |
| 278 |
{ |
| 279 |
'complete': False, |
| 280 |
'created_at': '2019-05-14T12:00:00Z', |
| 281 |
'description': 'Port 90', |
| 282 |
'id': 6, |
| 283 |
'kind': 'task', |
| 284 |
'position': 2, |
| 285 |
'story_id': 561, |
| 286 |
'updated_at': '2019-05-14T12:00:00Z' |
| 287 |
} |
| 288 |
], |
| 289 |
'pivotalclosed': datetime.datetime(2019, 5, 14, 12, 0, tzinfo=tzutc()), |
| 290 |
'pivotalcreated': datetime.datetime(2019, 5, 14, 12, 0, tzinfo=tzutc()), |
| 291 |
'pivotalupdated': datetime.datetime(2019, 5, 14, 12, 0, tzinfo=tzutc()), |
| 292 |
'pivotalurl': 'http://localhost/story/show/561', |
| 293 |
'pivotalblockers': |
| 294 |
[ |
| 295 |
{ |
| 296 |
'created_at': '2019-05-14T12:00:00Z', |
| 297 |
'description': 'Set weapons to stun', |
| 298 |
'id': 1100, |
| 299 |
'kind': 'blocker', |
| 300 |
'person_id': 106, |
| 301 |
'resolved': False, |
| 302 |
'story_id': 561, |
| 303 |
'updated_at': '2019-05-14T12:00:00Z' |
| 304 |
} |
| 305 |
], |
| 306 |
'pivotaldescription': 'All your base are belong to us', |
| 307 |
'pivotalestimate': 3, |
| 308 |
'pivotalid': 561, |
| 309 |
'pivotalowners': ['owned_user'], |
| 310 |
'pivotalprojectid': 99, |
| 311 |
'pivotalprojectname': 'Death Star', |
| 312 |
'pivotalrequesters': ['request_user'], |
| 313 |
'pivotalstorytype': 'story', |
| 314 |
'priority': 'M', |
| 315 |
'project': 'death_star', |
| 316 |
'tags': |
| 317 |
[ |
| 318 |
'look_sir_metal' |
| 319 |
] |
| 320 |
} |
| 321 |
actual_output = story.to_taskwarrior() |
| 322 |
self.assertEqual(actual_output, expected_output) |
| 323 |
|
| 324 |
@responses.activate |
| 325 |
def test_issues(self): |
| 326 |
story = next(self.service.issues()) |
| 327 |
story_date = datetime.datetime(2019, 5, 14, 12, 0, tzinfo=tzutc()) |
| 328 |
expected ={ |
| 329 |
'annotations': |
| 330 |
[ |
| 331 |
'@task - Completed: False - Port 0', |
| 332 |
'@task - Completed: False - Port 90' |
| 333 |
], |
| 334 |
'description': '(bw)Story#561 - Tractor beam loses power intermittently .. ' |
| 335 |
'http://localhost/story/show/561', |
| 336 |
'pivotalclosed': story_date, |
| 337 |
'pivotalcreated': story_date, |
| 338 |
'pivotalupdated': story_date, |
| 339 |
'pivotalurl': 'http://localhost/story/show/561', |
| 340 |
'pivotalblockers': 'Description: Set weapons to stun Resovled: False', |
| 341 |
'pivotaldescription': 'All your base are belong to us', |
| 342 |
'pivotalestimate': 3, |
| 343 |
'pivotalid': 561, |
| 344 |
'pivotalowners': 'starkiller', |
| 345 |
'pivotalprojectid': 99, |
| 346 |
'pivotalprojectname': 'Death Star', |
| 347 |
'pivotalrequesters': 'starkiller', |
| 348 |
'pivotalstorytype': 'story', |
| 349 |
'priority': 'M', |
| 350 |
'project': 'death_star', |
| 351 |
'tags': ['look_sir_metal'] |
| 352 |
} |
| 353 |
self.assertEqual(story.get_taskwarrior_record(), expected) |