Debian Codegraph @

tests/legacy/objects/repository/test_issue.py

raw
1 # pylint: disable=missing-docstring, redefined-outer-name, unused-import
2
3 import json
4
5 import pytest
6
7 from aiogithubapi import AIOGitHubAPINotModifiedException, GitHub
8
9 from tests.common import TOKEN
10 from tests.legacy.responses.issue_comments import issue_comments_response
11 from tests.legacy.responses.issue_fixture import issue_response, issues_response
12 from tests.legacy.responses.repository_fixture import repository_response
13
14
15 @pytest.mark.asyncio
16 async def test_issue(aresponses, repository_response, issue_response):
17 aresponses.add(
18 "api.github.com",
19 "/repos/octocat/Hello-World",
20 "get",
21 aresponses.Response(
22 text=json.dumps(repository_response),
23 status=200,
24 headers={
25 "X-RateLimit-Remaining": "1337",
26 "Content-Type": "application/json",
27 "Etag": "xyz..zyx",
28 },
29 ),
30 )
31 aresponses.add(
32 "api.github.com",
33 "/repos/octocat/Hello-World/issues/1",
34 "get",
35 aresponses.Response(
36 text=json.dumps(issue_response),
37 status=200,
38 headers={
39 "X-RateLimit-Remaining": "1337",
40 "Content-Type": "application/json",
41 "Etag": "xyz..zyx",
42 },
43 ),
44 )
45
46 async with GitHub(TOKEN) as github:
47 repository = await github.get_repo("octocat/Hello-World")
48 issue = await repository.get_issue(1)
49 assert issue.html_url == "https://github.com/octocat/Hello-World/issues/1347"
50 assert issue.number == 1347
51 assert issue.labels == [
52 {
53 "id": 208045946,
54 "node_id": "MDU6TGFiZWwyMDgwNDU5NDY=",
55 "url": "https://api.github.com/repos/octocat/Hello-World/labels/bug",
56 "name": "bug",
57 "description": "Something isn't working",
58 "color": "f29513",
59 "default": True,
60 }
61 ]
62 assert issue.title == "Found a bug"
63 assert issue.state == "open"
64 assert issue.assignees == [
65 {
66 "login": "octocat",
67 "id": 1,
68 "node_id": "MDQ6VXNlcjE=",
69 "avatar_url": "https://github.com/images/error/octocat_happy.gif",
70 "gravatar_id": "",
71 "url": "https://api.github.com/users/octocat",
72 "html_url": "https://github.com/octocat",
73 "followers_url": "https://api.github.com/users/octocat/followers",
74 "following_url": "https://api.github.com/users/octocat/following{/other_user}",
75 "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
76 "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
77 "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
78 "organizations_url": "https://api.github.com/users/octocat/orgs",
79 "repos_url": "https://api.github.com/users/octocat/repos",
80 "events_url": "https://api.github.com/users/octocat/events{/privacy}",
81 "received_events_url": "https://api.github.com/users/octocat/received_events",
82 "type": "User",
83 "site_admin": False,
84 }
85 ]
86 assert issue.body == "I'm having a problem with this."
87 user = issue.user
88 assert user.login == "octocat"
89 assert user.id == 1
90 assert user.avatar_url == "https://github.com/images/error/octocat_happy.gif"
91 assert user.html_url == "https://github.com/octocat"
92 assert user.type == "User"
93 assert not user.site_admin
94
95
96 @pytest.mark.asyncio
97 async def test_update_issue(aresponses, repository_response, issue_response):
98 aresponses.add(
99 "api.github.com",
100 "/repos/octocat/Hello-World",
101 "get",
102 aresponses.Response(
103 text=json.dumps(repository_response),
104 status=200,
105 headers={
106 "X-RateLimit-Remaining": "1337",
107 "Content-Type": "application/json",
108 "Etag": "xyz..zyx",
109 },
110 ),
111 )
112 aresponses.add(
113 "api.github.com",
114 "/repos/octocat/Hello-World/issues/1",
115 "get",
116 aresponses.Response(
117 text=json.dumps(issue_response),
118 status=200,
119 headers={
120 "X-RateLimit-Remaining": "1337",
121 "Content-Type": "application/json",
122 "Etag": "xyz..zyx",
123 },
124 ),
125 )
126 aresponses.add(
127 "api.github.com",
128 "/repos/octocat/Hello-World/issues/1",
129 "post",
130 aresponses.Response(
131 status=200,
132 headers={
133 "X-RateLimit-Remaining": "1337",
134 "Content-Type": "application/json",
135 "Etag": "xyz..zyx",
136 },
137 ),
138 )
139 async with GitHub(TOKEN) as github:
140 repository = await github.get_repo("octocat/Hello-World")
141 issue = await repository.get_issue(1)
142 data = {
143 "title": "test",
144 "body": "body",
145 "state": "closed",
146 "milestone": "v1.0",
147 "labels": ["test"],
148 "assignees": ["octocat"],
149 }
150 await issue.update(**data)
151
152
153 @pytest.mark.asyncio
154 async def test_get_issue_comments(
155 aresponses, repository_response, issue_response, issue_comments_response
156 ):
157 aresponses.add(
158 "api.github.com",
159 "/repos/octocat/Hello-World",
160 "get",
161 aresponses.Response(
162 text=json.dumps(repository_response),
163 status=200,
164 headers={
165 "X-RateLimit-Remaining": "1337",
166 "Content-Type": "application/json",
167 "Etag": "xyz..zyx",
168 },
169 ),
170 )
171 aresponses.add(
172 "api.github.com",
173 "/repos/octocat/Hello-World/issues/1",
174 "get",
175 aresponses.Response(
176 text=json.dumps(issue_response),
177 status=200,
178 headers={
179 "X-RateLimit-Remaining": "1337",
180 "Content-Type": "application/json",
181 "Etag": "xyz..zyx",
182 },
183 ),
184 )
185 aresponses.add(
186 "api.github.com",
187 "/repos/octocat/Hello-World/issues/1/comments",
188 "get",
189 aresponses.Response(
190 text=json.dumps(issue_comments_response),
191 status=200,
192 headers={
193 "X-RateLimit-Remaining": "1337",
194 "Content-Type": "application/json",
195 "Etag": "xyz..zyx",
196 },
197 ),
198 )
199 async with GitHub(TOKEN) as github:
200 repository = await github.get_repo("octocat/Hello-World")
201 issue = await repository.get_issue(1)
202 comments = await issue.get_comments()
203 assert comments[0].body == "Me too"
204
205
206 @pytest.mark.asyncio
207 async def test_comment_on_issue(aresponses, repository_response, issue_response):
208 aresponses.add(
209 "api.github.com",
210 "/repos/octocat/Hello-World",
211 "get",
212 aresponses.Response(
213 text=json.dumps(repository_response),
214 status=200,
215 headers={
216 "X-RateLimit-Remaining": "1337",
217 "Content-Type": "application/json",
218 "Etag": "xyz..zyx",
219 },
220 ),
221 )
222 aresponses.add(
223 "api.github.com",
224 "/repos/octocat/Hello-World/issues/1",
225 "get",
226 aresponses.Response(
227 text=json.dumps(issue_response),
228 status=200,
229 headers={
230 "X-RateLimit-Remaining": "1337",
231 "Content-Type": "application/json",
232 "Etag": "xyz..zyx",
233 },
234 ),
235 )
236 aresponses.add(
237 "api.github.com",
238 "/repos/octocat/Hello-World/issues/1/comments",
239 "post",
240 aresponses.Response(
241 status=200,
242 headers={
243 "X-RateLimit-Remaining": "1337",
244 "Content-Type": "application/json",
245 "Etag": "xyz..zyx",
246 },
247 ),
248 )
249 async with GitHub(TOKEN) as github:
250 repository = await github.get_repo("octocat/Hello-World")
251 issue = await repository.get_issue(1)
252 await issue.comment("test")
253
254
255 @pytest.mark.asyncio
256 async def test_issue_comments(
257 aresponses, repository_response, issue_response, issue_comments_response
258 ):
259 aresponses.add(
260 "api.github.com",
261 "/repos/octocat/Hello-World",
262 "get",
263 aresponses.Response(
264 text=json.dumps(repository_response),
265 status=200,
266 headers={
267 "X-RateLimit-Remaining": "1337",
268 "Content-Type": "application/json",
269 "Etag": "xyz..zyx",
270 },
271 ),
272 )
273 aresponses.add(
274 "api.github.com",
275 "/repos/octocat/Hello-World/issues/1",
276 "get",
277 aresponses.Response(
278 text=json.dumps(issue_response),
279 status=200,
280 headers={
281 "X-RateLimit-Remaining": "1337",
282 "Content-Type": "application/json",
283 "Etag": "xyz..zyx",
284 },
285 ),
286 )
287 aresponses.add(
288 "api.github.com",
289 "/repos/octocat/Hello-World/issues/1/comments",
290 "get",
291 aresponses.Response(
292 text=json.dumps(issue_comments_response),
293 status=200,
294 headers={
295 "X-RateLimit-Remaining": "1337",
296 "Content-Type": "application/json",
297 "Etag": "xyz..zyx",
298 },
299 ),
300 )
301 async with GitHub(TOKEN) as github:
302 repository = await github.get_repo("octocat/Hello-World")
303 issue = await repository.get_issue(1)
304 comments = await issue.get_comments()
305 first = comments[0]
306 assert first.html_url == "https://github.com/octocat/Hello-World/issues/1347#issuecomment-1"
307 assert first.body == "Me too"
308 assert first.created_at == "2011-04-14T16:00:49Z"
309 assert first.updated_at == "2011-04-14T16:00:49Z"
310 user = first.user
311 assert user.login == "octocat"
312 assert user.id == 1
313 assert user.avatar_url == "https://github.com/images/error/octocat_happy.gif"
314 assert user.html_url == "https://github.com/octocat"
315 assert user.type == "User"
316 assert not user.site_admin
317
318
319 @pytest.mark.asyncio
320 async def test_update_issue_comment(
321 aresponses, repository_response, issue_response, issue_comments_response
322 ):
323 aresponses.add(
324 "api.github.com",
325 "/repos/octocat/Hello-World",
326 "get",
327 aresponses.Response(
328 text=json.dumps(repository_response),
329 status=200,
330 headers={
331 "X-RateLimit-Remaining": "1337",
332 "Content-Type": "application/json",
333 "Etag": "xyz..zyx",
334 },
335 ),
336 )
337 aresponses.add(
338 "api.github.com",
339 "/repos/octocat/Hello-World/issues/1",
340 "get",
341 aresponses.Response(
342 text=json.dumps(issue_response),
343 status=200,
344 headers={
345 "X-RateLimit-Remaining": "1337",
346 "Content-Type": "application/json",
347 "Etag": "xyz..zyx",
348 },
349 ),
350 )
351 aresponses.add(
352 "api.github.com",
353 "/repos/octocat/Hello-World/issues/1/comments",
354 "get",
355 aresponses.Response(
356 text=json.dumps(issue_comments_response),
357 status=200,
358 headers={
359 "X-RateLimit-Remaining": "1337",
360 "Content-Type": "application/json",
361 "Etag": "xyz..zyx",
362 },
363 ),
364 )
365 aresponses.add(
366 "api.github.com",
367 "/repos/octocat/Hello-World/issues/comments/1",
368 "post",
369 aresponses.Response(
370 status=200,
371 headers={
372 "X-RateLimit-Remaining": "1337",
373 "Content-Type": "application/json",
374 "Etag": "xyz..zyx",
375 },
376 ),
377 )
378 async with GitHub(TOKEN) as github:
379 repository = await github.get_repo("octocat/Hello-World")
380 issue = await repository.get_issue(1)
381 comments = await issue.get_comments()
382 await comments[0].update("test")
383
384
385 @pytest.mark.asyncio
386 async def test_get_issues(aresponses, repository_response, issues_response):
387 aresponses.add(
388 "api.github.com",
389 "/repos/octocat/Hello-World",
390 "get",
391 aresponses.Response(
392 text=json.dumps(repository_response),
393 status=200,
394 headers={
395 "X-RateLimit-Remaining": "1337",
396 "Content-Type": "application/json",
397 "Etag": "xyz..zyx",
398 },
399 ),
400 )
401 aresponses.add(
402 "api.github.com",
403 "/repos/octocat/Hello-World/issues",
404 "get",
405 aresponses.Response(
406 text=json.dumps(issues_response),
407 status=200,
408 headers={
409 "X-RateLimit-Remaining": "1337",
410 "Content-Type": "application/json",
411 "Etag": "xyz..zyx",
412 },
413 ),
414 )
415 aresponses.add(
416 "api.github.com",
417 "/repos/octocat/Hello-World/issues",
418 "get",
419 aresponses.Response(status=304),
420 )
421
422 async with GitHub(TOKEN) as github:
423 repository = await github.get_repo("octocat/Hello-World")
424 issue = await repository.get_issues()
425 assert len(issue) == 1
426
427 with pytest.raises(AIOGitHubAPINotModifiedException):
428 await repository.get_issues(etag=github.client.last_response.etag)