Debian Codegraph @

tests/test_trello.py

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