Debian Codegraph @

tests/test_github.py

raw
1 from builtins import next
2 import datetime
3 from unittest import TestCase
4 from six.moves.configparser import RawConfigParser
5
6 import pytz
7 import responses
8
9 from bugwarrior.config import ServiceConfig
10 from bugwarrior.services.github import GithubService, GithubClient
11
12 from .base import ServiceTest, AbstractServiceTest
13
14
15 ARBITRARY_CREATED = (
16 datetime.datetime.utcnow() - datetime.timedelta(hours=1)
17 ).replace(tzinfo=pytz.UTC, microsecond=0)
18 ARBITRARY_CLOSED = (
19 datetime.datetime.utcnow() - datetime.timedelta(minutes=30)
20 ).replace(tzinfo=pytz.UTC, microsecond=0)
21 ARBITRARY_UPDATED = datetime.datetime.utcnow().replace(
22 tzinfo=pytz.UTC, microsecond=0)
23 ARBITRARY_ISSUE = {
24 'title': 'Hallo',
25 'html_url': 'https://github.com/arbitrary_username/arbitrary_repo/pull/1',
26 'url': 'https://api.github.com/repos/arbitrary_username/arbitrary_repo/issues/1',
27 'number': 10,
28 'body': 'Something',
29 'user': {'login': 'arbitrary_login'},
30 'milestone': {'title': 'alpha'},
31 'labels': [{'name': 'bugfix'}],
32 'created_at': ARBITRARY_CREATED.isoformat(),
33 'closed_at': ARBITRARY_CLOSED.isoformat(),
34 'updated_at': ARBITRARY_UPDATED.isoformat(),
35 'repo': 'arbitrary_username/arbitrary_repo',
36 'state': 'closed'
37 }
38 ARBITRARY_EXTRA = {
39 'project': 'one',
40 'type': 'issue',
41 'annotations': [],
42 'body': 'Something',
43 'namespace': 'arbitrary_username',
44 }
45
46
47 class TestGithubIssue(AbstractServiceTest, ServiceTest):
48 maxDiff = None
49 SERVICE_CONFIG = {
50 'github.login': 'arbitrary_login',
51 'github.password': 'arbitrary_password',
52 'github.username': 'arbitrary_username',
53 }
54
55 def setUp(self):
56 super(TestGithubIssue, self).setUp()
57 self.service = self.get_mock_service(GithubService)
58
59 def test_normalize_label_to_tag(self):
60 issue = self.service.get_issue_for_record(
61 ARBITRARY_ISSUE,
62 ARBITRARY_EXTRA
63 )
64 self.assertEqual(issue._normalize_label_to_tag('needs work'),
65 'needs_work')
66
67 def test_to_taskwarrior(self):
68 self.service.import_labels_as_tags = True
69 issue = self.service.get_issue_for_record(
70 ARBITRARY_ISSUE,
71 ARBITRARY_EXTRA
72 )
73
74 expected_output = {
75 'project': ARBITRARY_EXTRA['project'],
76 'priority': self.service.default_priority,
77 'annotations': [],
78 'tags': ['bugfix'],
79 'entry': ARBITRARY_CREATED,
80 'end': ARBITRARY_CLOSED,
81 issue.URL: ARBITRARY_ISSUE['html_url'],
82 issue.REPO: ARBITRARY_ISSUE['repo'],
83 issue.TYPE: ARBITRARY_EXTRA['type'],
84 issue.TITLE: ARBITRARY_ISSUE['title'],
85 issue.NUMBER: ARBITRARY_ISSUE['number'],
86 issue.UPDATED_AT: ARBITRARY_UPDATED,
87 issue.CREATED_AT: ARBITRARY_CREATED,
88 issue.CLOSED_AT: ARBITRARY_CLOSED,
89 issue.BODY: ARBITRARY_EXTRA['body'],
90 issue.MILESTONE: ARBITRARY_ISSUE['milestone']['title'],
91 issue.USER: ARBITRARY_ISSUE['user']['login'],
92 issue.NAMESPACE: 'arbitrary_username',
93 issue.STATE: 'closed',
94 }
95 actual_output = issue.to_taskwarrior()
96
97 self.assertEqual(actual_output, expected_output)
98
99 @responses.activate
100 def test_issues(self):
101 self.add_response(
102 'https://api.github.com/user/repos?per_page=100',
103 json=[{
104 'name': 'some_repo',
105 'owner': {'login': 'some_username'}
106 }])
107
108 self.add_response(
109 'https://api.github.com/users/arbitrary_username/repos?per_page=100',
110 json=[{
111 'name': 'arbitrary_repo',
112 'owner': {'login': 'arbitrary_username'}
113 }])
114
115 self.add_response(
116 'https://api.github.com/repos/arbitrary_username/arbitrary_repo/issues?per_page=100',
117 json=[ARBITRARY_ISSUE])
118
119 self.add_response(
120 'https://api.github.com/user/issues?per_page=100',
121 json=[ARBITRARY_ISSUE])
122
123 self.add_response(
124 'https://api.github.com/repos/arbitrary_username/arbitrary_repo/issues/10/comments?per_page=100',
125 json=[{
126 'user': {'login': 'arbitrary_login'},
127 'body': 'Arbitrary comment.'
128 }])
129
130 issue = next(self.service.issues())
131
132 expected = {
133 'annotations': [u'@arbitrary_login - Arbitrary comment.'],
134 'description': u'(bw)Is#10 - Hallo .. https://github.com/arbitrary_username/arbitrary_repo/pull/1',
135 'entry': ARBITRARY_CREATED,
136 'end': ARBITRARY_CLOSED,
137 'githubbody': u'Something',
138 'githubcreatedon': ARBITRARY_CREATED,
139 'githubclosedon': ARBITRARY_CLOSED,
140 'githubmilestone': u'alpha',
141 'githubnamespace': 'arbitrary_username',
142 'githubnumber': 10,
143 'githubrepo': 'arbitrary_username/arbitrary_repo',
144 'githubtitle': u'Hallo',
145 'githubtype': 'issue',
146 'githubupdatedat': ARBITRARY_UPDATED,
147 'githuburl': u'https://github.com/arbitrary_username/arbitrary_repo/pull/1',
148 'githubuser': u'arbitrary_login',
149 'githubstate': u'closed',
150 'priority': 'M',
151 'project': 'arbitrary_repo',
152 'tags': []}
153
154 self.assertEqual(issue.get_taskwarrior_record(), expected)
155
156
157 class TestGithubIssueQuery(AbstractServiceTest, ServiceTest):
158 maxDiff = None
159 SERVICE_CONFIG = {
160 'github.login': 'arbitrary_login',
161 'github.password': 'arbitrary_password',
162 'github.username': 'arbitrary_username',
163 'github.query': 'is:open reviewer:octocat',
164 'github.include_user_repos': 'False',
165 'github.include_user_issues': 'False',
166 }
167
168 def setUp(self):
169 super(TestGithubIssueQuery, self).setUp()
170 self.service = self.get_mock_service(GithubService)
171
172 def test_to_taskwarrior(self):
173 pass
174
175 @responses.activate
176 def test_issues(self):
177 self.add_response(
178 'https://api.github.com/search/issues?q=is%3Aopen+reviewer%3Aoctocat&per_page=100',
179 json={'items': [ARBITRARY_ISSUE]})
180
181 self.add_response(
182 'https://api.github.com/repos/arbitrary_username/arbitrary_repo/issues/10/comments?per_page=100',
183 json=[{
184 'user': {'login': 'arbitrary_login'},
185 'body': 'Arbitrary comment.'
186 }])
187
188 issue = list(self.service.issues())[0]
189
190 expected = {
191 'annotations': [u'@arbitrary_login - Arbitrary comment.'],
192 'description': u'(bw)Is#10 - Hallo .. https://github.com/arbitrary_username/arbitrary_repo/pull/1',
193 'entry': ARBITRARY_CREATED,
194 'end': ARBITRARY_CLOSED,
195 'githubbody': u'Something',
196 'githubcreatedon': ARBITRARY_CREATED,
197 'githubclosedon': ARBITRARY_CLOSED,
198 'githubmilestone': u'alpha',
199 'githubnamespace': 'arbitrary_username',
200 'githubnumber': 10,
201 'githubrepo': 'arbitrary_username/arbitrary_repo',
202 'githubtitle': u'Hallo',
203 'githubtype': 'issue',
204 'githubupdatedat': ARBITRARY_UPDATED,
205 'githuburl': u'https://github.com/arbitrary_username/arbitrary_repo/pull/1',
206 'githubuser': u'arbitrary_login',
207 'githubstate': u'closed',
208 'priority': 'M',
209 'project': 'arbitrary_repo',
210 'tags': []}
211
212 self.assertEqual(issue.get_taskwarrior_record(), expected)
213
214
215 class TestGithubService(TestCase):
216
217 def setUp(self):
218 self.config = RawConfigParser()
219 self.config.interactive = False
220 self.config.add_section('general')
221 self.config.add_section('mygithub')
222 self.config.set('mygithub', 'service', 'github')
223 self.config.set('mygithub', 'github.login', 'tintin')
224 self.config.set('mygithub', 'github.username', 'milou')
225 self.config.set('mygithub', 'github.password', 't0ps3cr3t')
226 self.service_config = ServiceConfig(
227 GithubService.CONFIG_PREFIX, self.config, 'mygithub')
228
229 def test_token_authorization_header(self):
230 self.config.remove_option('mygithub', 'github.password')
231 self.config.set('mygithub', 'github.token',
232 '@oracle:eval:echo 1234567890ABCDEF')
233 service = GithubService(self.config, 'general', 'mygithub')
234 self.assertEqual(service.client.session.headers['Authorization'],
235 "token 1234567890ABCDEF")
236
237 def test_default_host(self):
238 """ Check that if github.host is not set, we default to github.com """
239 service = GithubService(self.config, 'general', 'mygithub')
240 self.assertEqual("github.com", service.host)
241
242 def test_overwrite_host(self):
243 """ Check that if github.host is set, we use its value as host """
244 self.config.set('mygithub', 'github.host', 'github.example.com')
245 service = GithubService(self.config, 'general', 'mygithub')
246 self.assertEqual("github.example.com", service.host)
247
248 def test_keyring_service(self):
249 """ Checks that the keyring service name """
250 keyring_service = GithubService.get_keyring_service(self.service_config)
251 self.assertEqual("github://[email protected]/milou", keyring_service)
252
253 def test_keyring_service_host(self):
254 """ Checks that the keyring key depends on the github host. """
255 self.config.set('mygithub', 'github.host', 'github.example.com')
256 keyring_service = GithubService.get_keyring_service(self.service_config)
257 self.assertEqual("github://[email protected]/milou", keyring_service)
258
259 def test_get_repository_from_issue_url__issue(self):
260 issue = dict(repos_url="https://github.com/foo/bar")
261 repository = GithubService.get_repository_from_issue(issue)
262 self.assertEqual("foo/bar", repository)
263
264 def test_get_repository_from_issue_url__pull_request(self):
265 issue = dict(repos_url="https://github.com/foo/bar")
266 repository = GithubService.get_repository_from_issue(issue)
267 self.assertEqual("foo/bar", repository)
268
269 def test_get_repository_from_issue__enterprise_github(self):
270 issue = dict(repos_url="https://github.acme.biz/foo/bar")
271 repository = GithubService.get_repository_from_issue(issue)
272 self.assertEqual("foo/bar", repository)
273
274 def test_body_no_limit(self):
275 service = GithubService(self.config, 'general', 'mygithub')
276 issue = dict(body="A very short issue body. Fixes #42.")
277 self.assertEqual(issue["body"], service.body(issue))
278
279 def test_body_newline_style(self):
280 service = GithubService(self.config, 'general', 'mygithub')
281 issue = dict(body="An\r\nIssue\r\nWith\r\nNewlines")
282 self.assertEqual("An\nIssue\nWith\nNewlines", service.body(issue))
283
284 def test_body_length_limit(self):
285 self.config.set('mygithub', 'github.body_length', '5')
286 service = GithubService(self.config, 'general', 'mygithub')
287 issue = dict(body="A very short issue body. Fixes #42.")
288 self.assertEqual(issue["body"][:5], service.body(issue))
289
290
291 class TestGithubClient(TestCase):
292
293 def test_api_url(self):
294 auth = {'token': 'xxxx'}
295 client = GithubClient('github.com', auth)
296 self.assertEqual(
297 client._api_url('/some/path'), 'https://api.github.com/some/path')
298
299 def test_api_url_with_context(self):
300 auth = {'token': 'xxxx'}
301 client = GithubClient('github.com', auth)
302 self.assertEqual(
303 client._api_url('/some/path/{foo}', foo='bar'),
304 'https://api.github.com/some/path/bar')
305
306 def test_api_url_with_custom_host(self):
307 """ Test generating an API URL with a custom host """
308 auth = {'token': 'xxxx'}
309 client = GithubClient('github.example.com', auth)
310 self.assertEqual(
311 client._api_url('/some/path'),
312 'https://github.example.com/api/v3/some/path')