| 1 |
from __future__ import unicode_literals, print_function |
| 2 |
from builtins import next |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
from six.moves import configparser |
| 6 |
import responses |
| 7 |
|
| 8 |
from dateutil.parser import parse as parse_date |
| 9 |
from dateutil.tz import tzlocal |
| 10 |
|
| 11 |
from bugwarrior.config import ServiceConfig |
| 12 |
from bugwarrior.services.trello import TrelloService, TrelloIssue |
| 13 |
from .base import ConfigTest, ServiceTest |
| 14 |
|
| 15 |
|
| 16 |
class TestTrelloIssue(ServiceTest): |
| 17 |
JSON = { |
| 18 |
"due": "2018-12-02T12:59:00.000Z", |
| 19 |
"id": "542bbb6583d705eb05bbe491", |
| 20 |
"idShort": 42, |
| 21 |
"name": "So long, and thanks for all the fish!", |
| 22 |
"shortLink": "AAaaBBbb", |
| 23 |
"shortUrl": "https://trello.com/c/AAaaBBbb", |
| 24 |
"url": "https://trello.com/c/AAaBBbb/42-so-long", |
| 25 |
"labels": [{'name': "foo"}, {"name": "bar"}], |
| 26 |
"desc": "some description", |
| 27 |
} |
| 28 |
|
| 29 |
def setUp(self): |
| 30 |
super(TestTrelloIssue, self).setUp() |
| 31 |
origin = dict(inline_links=True, description_length=31, |
| 32 |
import_labels_as_tags=True, default_priority='M') |
| 33 |
extra = {'boardname': 'Hyperspatial express route', |
| 34 |
'listname': 'Something'} |
| 35 |
self.issue = TrelloIssue(self.JSON, origin, extra) |
| 36 |
|
| 37 |
def test_default_description(self): |
| 38 |
""" Test the generated description """ |
| 39 |
expected_desc = "(bw)#42 - So long, and thanks for all the" \ |
| 40 |
" .. https://trello.com/c/AAaaBBbb" |
| 41 |
self.assertEqual(expected_desc, self.issue.get_default_description()) |
| 42 |
|
| 43 |
def test_to_taskwarrior__project(self): |
| 44 |
""" By default, the project is the board name """ |
| 45 |
expected_project = "Hyperspatial express route" |
| 46 |
self.assertEqual(expected_project, |
| 47 |
self.issue.to_taskwarrior().get('project', None)) |
| 48 |
|
| 49 |
|
| 50 |
class TestTrelloService(ConfigTest): |
| 51 |
BOARD = {'id': 'B04RD', 'name': 'My Board'} |
| 52 |
CARD1 = {'id': 'C4RD', 'name': 'Card 1', 'members': [{'username': 'tintin'}], |
| 53 |
'due': '2018-12-02T12:59:00.000Z', |
| 54 |
'idShort': 1, |
| 55 |
'shortLink': 'abcd', |
| 56 |
'shortUrl': 'https://trello.com/c/AAaaBBbb', |
| 57 |
'desc': 'some description', |
| 58 |
'url': 'https://trello.com/c/AAaBBbb/42-so-long'} |
| 59 |
CARD2 = {'id': 'kard', 'name': 'Card 2', 'members': [{'username': 'mario'}]} |
| 60 |
CARD3 = {'id': 'K4rD', 'name': 'Card 3', 'members': []} |
| 61 |
LIST1 = {'id': 'L15T', 'name': 'List 1'} |
| 62 |
LIST2 = {'id': 'ZZZZ', 'name': 'List 2'} |
| 63 |
COMMENT1 = { "type": "commentCard", |
| 64 |
"data": { "text": "Preums" }, |
| 65 |
"memberCreator": { "username": "luidgi" } } |
| 66 |
COMMENT2 = { "type": "commentCard", |
| 67 |
"data": { "text": "Deuz" }, |
| 68 |
"memberCreator": { "username": "mario" } } |
| 69 |
|
| 70 |
def setUp(self): |
| 71 |
super(TestTrelloService, self).setUp() |
| 72 |
self.config = configparser.RawConfigParser() |
| 73 |
self.config.add_section('general') |
| 74 |
self.config.add_section('mytrello') |
| 75 |
self.config.set('mytrello', 'trello.api_key', 'XXXX') |
| 76 |
self.config.set('mytrello', 'trello.token', 'YYYY') |
| 77 |
self.service_config = ServiceConfig( |
| 78 |
TrelloService.CONFIG_PREFIX, self.config, 'mytrello') |
| 79 |
responses.add(responses.GET, |
| 80 |
'https://api.trello.com/1/lists/L15T/cards/open', |
| 81 |
json=[self.CARD1, self.CARD2, self.CARD3]) |
| 82 |
responses.add(responses.GET, |
| 83 |
'https://api.trello.com/1/boards/B04RD/lists/open', |
| 84 |
json=[self.LIST1, self.LIST2]) |
| 85 |
responses.add(responses.GET, |
| 86 |
'https://api.trello.com/1/boards/F00', |
| 87 |
json={'id': 'F00', 'name': 'Foo Board'}) |
| 88 |
responses.add(responses.GET, |
| 89 |
'https://api.trello.com/1/boards/B4R', |
| 90 |
json={'id': 'B4R', 'name': 'Bar Board'}) |
| 91 |
responses.add(responses.GET, |
| 92 |
'https://api.trello.com/1/members/me/boards', |
| 93 |
json=[self.BOARD]) |
| 94 |
responses.add(responses.GET, |
| 95 |
'https://api.trello.com/1/cards/C4RD/actions', |
| 96 |
json=[self.COMMENT1, self.COMMENT2]) |
| 97 |
|
| 98 |
@responses.activate |
| 99 |
def test_get_boards_config(self): |
| 100 |
self.config.set('mytrello', 'trello.include_boards', 'F00, B4R') |
| 101 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 102 |
boards = service.get_boards() |
| 103 |
self.assertEqual(list(boards), [{'id': 'F00', 'name': 'Foo Board'}, |
| 104 |
{'id': 'B4R', 'name': 'Bar Board'}]) |
| 105 |
|
| 106 |
@responses.activate |
| 107 |
def test_get_boards_api(self): |
| 108 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 109 |
boards = service.get_boards() |
| 110 |
self.assertEqual(list(boards), [self.BOARD]) |
| 111 |
|
| 112 |
@responses.activate |
| 113 |
def test_get_lists(self): |
| 114 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 115 |
lists = service.get_lists('B04RD') |
| 116 |
self.assertEqual(list(lists), [self.LIST1, self.LIST2]) |
| 117 |
|
| 118 |
@responses.activate |
| 119 |
def test_get_lists_include(self): |
| 120 |
self.config.set('mytrello', 'trello.include_lists', 'List 1') |
| 121 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 122 |
lists = service.get_lists('B04RD') |
| 123 |
self.assertEqual(list(lists), [self.LIST1]) |
| 124 |
|
| 125 |
@responses.activate |
| 126 |
def test_get_lists_exclude(self): |
| 127 |
self.config.set('mytrello', 'trello.exclude_lists', 'List 1') |
| 128 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 129 |
lists = service.get_lists('B04RD') |
| 130 |
self.assertEqual(list(lists), [self.LIST2]) |
| 131 |
|
| 132 |
@responses.activate |
| 133 |
def test_get_cards(self): |
| 134 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 135 |
cards = service.get_cards('L15T') |
| 136 |
self.assertEqual(list(cards), [self.CARD1, self.CARD2, self.CARD3]) |
| 137 |
|
| 138 |
@responses.activate |
| 139 |
def test_get_cards_assigned(self): |
| 140 |
self.config.set('mytrello', 'trello.only_if_assigned', 'tintin') |
| 141 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 142 |
cards = service.get_cards('L15T') |
| 143 |
self.assertEqual(list(cards), [self.CARD1]) |
| 144 |
|
| 145 |
@responses.activate |
| 146 |
def test_get_cards_assigned_unassigned(self): |
| 147 |
self.config.set('mytrello', 'trello.only_if_assigned', 'tintin') |
| 148 |
self.config.set('mytrello', 'trello.also_unassigned', 'true') |
| 149 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 150 |
cards = service.get_cards('L15T') |
| 151 |
self.assertEqual(list(cards), [self.CARD1, self.CARD3]) |
| 152 |
|
| 153 |
@responses.activate |
| 154 |
def test_get_comments(self): |
| 155 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 156 |
comments = service.get_comments('C4RD') |
| 157 |
self.assertEqual(list(comments), [self.COMMENT1, self.COMMENT2]) |
| 158 |
|
| 159 |
@responses.activate |
| 160 |
def test_annotations(self): |
| 161 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 162 |
annotations = service.annotations(self.CARD1) |
| 163 |
self.assertEqual( |
| 164 |
list(annotations), ["@luidgi - Preums", "@mario - Deuz"]) |
| 165 |
|
| 166 |
@responses.activate |
| 167 |
def test_annotations_with_link(self): |
| 168 |
self.config.set('general', 'annotation_links', 'true') |
| 169 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 170 |
annotations = service.annotations(self.CARD1) |
| 171 |
self.assertEqual( |
| 172 |
list(annotations), |
| 173 |
["https://trello.com/c/AAaaBBbb", |
| 174 |
"@luidgi - Preums", |
| 175 |
"@mario - Deuz"]) |
| 176 |
|
| 177 |
@responses.activate |
| 178 |
def test_issues(self): |
| 179 |
self.config.set('mytrello', 'trello.include_lists', 'List 1') |
| 180 |
self.config.set('mytrello', 'trello.only_if_assigned', 'tintin') |
| 181 |
service = TrelloService(self.config, 'general', 'mytrello') |
| 182 |
issues = service.issues() |
| 183 |
expected = { |
| 184 |
'due': parse_date('2018-12-02T12:59:00.000Z'), |
| 185 |
'description': u'(bw)#1 - Card 1 .. https://trello.com/c/AAaaBBbb', |
| 186 |
'priority': 'M', |
| 187 |
'project': 'My Board', |
| 188 |
'trelloboard': 'My Board', |
| 189 |
'trellolist': 'List 1', |
| 190 |
'trellocard': 'Card 1', |
| 191 |
'trellocardid': 'C4RD', |
| 192 |
'trellocardidshort': 1, |
| 193 |
'trellodescription': 'some description', |
| 194 |
'trelloshortlink': 'abcd', |
| 195 |
'trelloshorturl': 'https://trello.com/c/AAaaBBbb', |
| 196 |
'trellourl': 'https://trello.com/c/AAaBBbb/42-so-long', |
| 197 |
'annotations': [ |
| 198 |
"@luidgi - Preums", |
| 199 |
"@mario - Deuz" ], |
| 200 |
'tags': []} |
| 201 |
actual = next(issues).get_taskwarrior_record() |
| 202 |
self.assertEqual(expected, actual) |
| 203 |
|
| 204 |
maxDiff = None |
| 205 |
|
| 206 |
@patch('bugwarrior.services.trello.die') |
| 207 |
def test_validate_config(self, die): |
| 208 |
TrelloService.validate_config(self.service_config, 'mytrello') |
| 209 |
die.assert_not_called() |
| 210 |
|
| 211 |
@patch('bugwarrior.services.trello.die') |
| 212 |
def test_valid_config_no_access_token(self, die): |
| 213 |
self.config.remove_option('mytrello', 'trello.token') |
| 214 |
TrelloService.validate_config(self.service_config, 'mytrello') |
| 215 |
die.assert_called_with("[mytrello] has no 'trello.token'") |
| 216 |
|
| 217 |
@patch('bugwarrior.services.trello.die') |
| 218 |
def test_valid_config_no_api_key(self, die): |
| 219 |
self.config.remove_option('mytrello', 'trello.api_key') |
| 220 |
TrelloService.validate_config(self.service_config, 'mytrello') |
| 221 |
die.assert_called_with("[mytrello] has no 'trello.api_key'") |
| 222 |
|
| 223 |
|
| 224 |
def test_keyring_service(self): |
| 225 |
""" Checks that the keyring service name """ |
| 226 |
keyring_service = TrelloService.get_keyring_service(self.service_config) |
| 227 |
self.assertEqual("trello://[email protected]", keyring_service) |
| 228 |
|