Debian Codegraph @

tests/test_gitlab.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 standard_library
2 standard_library.install_aliases()
3 from builtins import next
4 from six.moves import configparser
5 import datetime
6
7 import pytz
8 import responses
9
10 from bugwarrior.config import ServiceConfig
11 from bugwarrior.services.gitlab import GitlabService
12
13 from .base import ConfigTest, ServiceTest, AbstractServiceTest
14
15
16 class TestGitlabService(ConfigTest):
17
18 def setUp(self):
19 super(TestGitlabService, self).setUp()
20 self.config = configparser.RawConfigParser()
21 self.config.add_section('general')
22 self.config.add_section('myservice')
23 self.config.set('myservice', 'gitlab.login', 'foobar')
24 self.config.set('myservice', 'gitlab.token', 'XXXXXX')
25 self.service_config = ServiceConfig(
26 GitlabService.CONFIG_PREFIX, self.config, 'myservice')
27
28 def test_get_keyring_service_default_host(self):
29 self.assertEqual(
30 GitlabService.get_keyring_service(self.service_config),
31 'gitlab://[email protected]')
32
33 def test_get_keyring_service_custom_host(self):
34 self.config.set('myservice', 'gitlab.host', 'gitlab.example.com')
35 self.assertEqual(
36 GitlabService.get_keyring_service(self.service_config),
37 'gitlab://[email protected]')
38
39 def test_add_default_namespace_to_included_repos(self):
40 self.config.set('myservice', 'gitlab.include_repos', 'baz, banana/tree')
41 service = GitlabService(self.config, 'general', 'myservice')
42 self.assertEqual(service.include_repos, ['foobar/baz', 'banana/tree'])
43
44 def test_add_default_namespace_to_excluded_repos(self):
45 self.config.set('myservice', 'gitlab.exclude_repos', 'baz, banana/tree')
46 service = GitlabService(self.config, 'general', 'myservice')
47 self.assertEqual(service.exclude_repos, ['foobar/baz', 'banana/tree'])
48
49 def test_filter_repos_default(self):
50 service = GitlabService(self.config, 'general', 'myservice')
51 repo = {'path_with_namespace': 'foobar/baz'}
52 self.assertTrue(service.filter_repos(repo))
53
54 def test_filter_repos_exclude(self):
55 self.config.set('myservice', 'gitlab.exclude_repos', 'foobar/baz')
56 service = GitlabService(self.config, 'general', 'myservice')
57 repo = {'path_with_namespace': 'foobar/baz', 'id': 1234}
58 self.assertFalse(service.filter_repos(repo))
59
60 def test_filter_repos_exclude_id(self):
61 self.config.set('myservice', 'gitlab.exclude_repos', 'id:1234')
62 service = GitlabService(self.config, 'general', 'myservice')
63 repo = {'path_with_namespace': 'foobar/baz', 'id': 1234}
64 self.assertFalse(service.filter_repos(repo))
65
66 def test_filter_repos_include(self):
67 self.config.set('myservice', 'gitlab.include_repos', 'foobar/baz')
68 service = GitlabService(self.config, 'general', 'myservice')
69 repo = {'path_with_namespace': 'foobar/baz', 'id': 1234}
70 self.assertTrue(service.filter_repos(repo))
71
72 def test_filter_repos_include_id(self):
73 self.config.set('myservice', 'gitlab.include_repos', 'id:1234')
74 service = GitlabService(self.config, 'general', 'myservice')
75 repo = {'path_with_namespace': 'foobar/baz', 'id': 1234}
76 self.assertTrue(service.filter_repos(repo))
77
78
79 class TestGitlabIssue(AbstractServiceTest, ServiceTest):
80 maxDiff = None
81 SERVICE_CONFIG = {
82 'gitlab.host': 'gitlab.example.com',
83 'gitlab.login': 'arbitrary_login',
84 'gitlab.token': 'arbitrary_token',
85 }
86
87 def setUp(self):
88 super(TestGitlabIssue, self).setUp()
89 self.service = self.get_mock_service(GitlabService)
90 self.arbitrary_created = (
91 datetime.datetime.utcnow() - datetime.timedelta(hours=1)
92 ).replace(tzinfo=pytz.UTC, microsecond=0)
93 self.arbitrary_updated = datetime.datetime.utcnow().replace(
94 tzinfo=pytz.UTC, microsecond=0)
95 self.arbitrary_duedate = (
96 datetime.datetime.combine(datetime.date.today(),
97 datetime.datetime.min.time())
98 ).replace(tzinfo=pytz.UTC)
99 self.arbitrary_issue = {
100 "id": 42,
101 "iid": 3,
102 "project_id": 8,
103 "title": "Add user settings",
104 "description": "",
105 "labels": [
106 "feature"
107 ],
108 "milestone": {
109 "id": 1,
110 "title": "v1.0",
111 "description": "",
112 "due_date": self.arbitrary_duedate.date().isoformat(),
113 "state": "closed",
114 "updated_at": "2012-07-04T13:42:48Z",
115 "created_at": "2012-07-04T13:42:48Z"
116 },
117 "assignee": {
118 "id": 2,
119 "username": "jack_smith",
120 "email": "[email protected]",
121 "name": "Jack Smith",
122 "state": "active",
123 "created_at": "2012-05-23T08:01:01Z"
124 },
125 "author": {
126 "id": 1,
127 "username": "john_smith",
128 "email": "[email protected]",
129 "name": "John Smith",
130 "state": "active",
131 "created_at": "2012-05-23T08:00:58Z"
132 },
133 "state": "opened",
134 "updated_at": self.arbitrary_updated.isoformat(),
135 "created_at": self.arbitrary_created.isoformat(),
136 "weight": 3,
137 "work_in_progress": "true"
138 }
139 self.arbitrary_extra = {
140 'issue_url': 'https://gitlab.example.com/arbitrary_username/project/issues/3',
141 'project': 'project',
142 'namespace': 'arbitrary_namespace',
143 'type': 'issue',
144 'annotations': [],
145 }
146
147 def test_normalize_label_to_tag(self):
148 issue = self.service.get_issue_for_record(
149 self.arbitrary_issue,
150 self.arbitrary_extra
151 )
152 self.assertEqual(issue._normalize_label_to_tag('needs work'),
153 'needs_work')
154
155 def test_to_taskwarrior(self):
156 self.service.import_labels_as_tags = True
157 issue = self.service.get_issue_for_record(
158 self.arbitrary_issue,
159 self.arbitrary_extra
160 )
161
162 expected_output = {
163 'project': self.arbitrary_extra['project'],
164 'priority': self.service.default_priority,
165 'annotations': [],
166 'tags': [u'feature'],
167 'due': self.arbitrary_duedate.replace(microsecond=0),
168 'entry': self.arbitrary_created.replace(microsecond=0),
169 issue.URL: self.arbitrary_extra['issue_url'],
170 issue.REPO: 'project',
171 issue.STATE: self.arbitrary_issue['state'],
172 issue.TYPE: self.arbitrary_extra['type'],
173 issue.TITLE: self.arbitrary_issue['title'],
174 issue.NUMBER: str(self.arbitrary_issue['iid']),
175 issue.UPDATED_AT: self.arbitrary_updated.replace(microsecond=0),
176 issue.CREATED_AT: self.arbitrary_created.replace(microsecond=0),
177 issue.DUEDATE: self.arbitrary_duedate,
178 issue.DESCRIPTION: self.arbitrary_issue['description'],
179 issue.MILESTONE: self.arbitrary_issue['milestone']['title'],
180 issue.UPVOTES: 0,
181 issue.DOWNVOTES: 0,
182 issue.WORK_IN_PROGRESS: 1,
183 issue.AUTHOR: 'john_smith',
184 issue.ASSIGNEE: 'jack_smith',
185 issue.NAMESPACE: 'arbitrary_namespace',
186 issue.WEIGHT: 3,
187 }
188 actual_output = issue.to_taskwarrior()
189
190 self.assertEqual(actual_output, expected_output)
191
192 def test_work_in_progress(self):
193 self.arbitrary_issue['work_in_progress'] = 'false'
194 self.service.import_labels_as_tags = True
195 issue = self.service.get_issue_for_record(
196 self.arbitrary_issue,
197 self.arbitrary_extra
198 )
199
200 expected_output = {
201 'project': self.arbitrary_extra['project'],
202 'priority': self.service.default_priority,
203 'annotations': [],
204 'tags': [u'feature'],
205 'due': self.arbitrary_duedate.replace(microsecond=0),
206 'entry': self.arbitrary_created.replace(microsecond=0),
207 issue.URL: self.arbitrary_extra['issue_url'],
208 issue.REPO: 'project',
209 issue.STATE: self.arbitrary_issue['state'],
210 issue.TYPE: self.arbitrary_extra['type'],
211 issue.TITLE: self.arbitrary_issue['title'],
212 issue.NUMBER: str(self.arbitrary_issue['iid']),
213 issue.UPDATED_AT: self.arbitrary_updated.replace(microsecond=0),
214 issue.CREATED_AT: self.arbitrary_created.replace(microsecond=0),
215 issue.DUEDATE: self.arbitrary_duedate,
216 issue.DESCRIPTION: self.arbitrary_issue['description'],
217 issue.MILESTONE: self.arbitrary_issue['milestone']['title'],
218 issue.UPVOTES: 0,
219 issue.DOWNVOTES: 0,
220 issue.WORK_IN_PROGRESS: 0,
221 issue.AUTHOR: 'john_smith',
222 issue.ASSIGNEE: 'jack_smith',
223 issue.NAMESPACE: 'arbitrary_namespace',
224 issue.WEIGHT: 3,
225 }
226 actual_output = issue.to_taskwarrior()
227
228 self.assertEqual(actual_output, expected_output)
229
230 @responses.activate
231 def test_issues(self):
232 self.add_response(
233 'https://gitlab.example.com/api/v4/projects?simple=True&per_page=100&page=1',
234 json=[{
235 'id': 1,
236 'path': 'arbitrary_username/project',
237 'web_url': 'example.com',
238 "namespace": {
239 "full_path": "arbitrary_username"
240 }
241 }])
242
243 self.add_response(
244 'https://gitlab.example.com/api/v4/projects/1/issues?state=opened&per_page=100&page=1',
245 json=[self.arbitrary_issue])
246
247 self.add_response(
248 'https://gitlab.example.com/api/v4/projects/1/issues/3/notes?per_page=100&page=1',
249 json=[{
250 'author': {'username': 'john_smith'},
251 'body': 'Some comment.'
252 }])
253
254 issue = next(self.service.issues())
255
256 expected = {
257 'annotations': [u'@john_smith - Some comment.'],
258 'description':
259 u'(bw)Is#3 - Add user settings .. example.com/issues/3',
260 'due': self.arbitrary_duedate,
261 'entry': self.arbitrary_created,
262 'gitlabassignee': u'jack_smith',
263 'gitlabauthor': u'john_smith',
264 'gitlabcreatedon': self.arbitrary_created,
265 'gitlabdescription': u'',
266 'gitlabdownvotes': 0,
267 'gitlabmilestone': u'v1.0',
268 'gitlabnamespace': u'arbitrary_username',
269 'gitlabnumber': '3',
270 'gitlabrepo': u'arbitrary_username/project',
271 'gitlabstate': u'opened',
272 'gitlabtitle': u'Add user settings',
273 'gitlabtype': 'issue',
274 'gitlabupdatedat': self.arbitrary_updated,
275 'gitlabduedate': self.arbitrary_duedate,
276 'gitlabupvotes': 0,
277 'gitlaburl': u'example.com/issues/3',
278 'gitlabwip': 1,
279 'gitlabweight': 3,
280 'priority': 'M',
281 'project': u'arbitrary_username/project',
282 'tags': []}
283
284 self.assertEqual(issue.get_taskwarrior_record(), expected)