| 1 |
# pylint: disable=missing-docstring, redefined-outer-name, unused-import |
| 2 |
import datetime |
| 3 |
import json |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from aiogithubapi import AIOGitHubAPIException, AIOGitHubAPINotModifiedException, GitHub |
| 8 |
|
| 9 |
from tests.common import TOKEN |
| 10 |
from tests.legacy.responses.base import base_response |
| 11 |
from tests.legacy.responses.branch import branch_response |
| 12 |
from tests.legacy.responses.contents import ( |
| 13 |
contents_file_response, |
| 14 |
contents_list_response, |
| 15 |
) |
| 16 |
from tests.legacy.responses.issue_fixture import issue_response |
| 17 |
from tests.legacy.responses.releases import releases_response |
| 18 |
from tests.legacy.responses.repository_fixture import repository_response |
| 19 |
from tests.legacy.responses.tree import tree_response |
| 20 |
|
| 21 |
|
| 22 |
@pytest.mark.asyncio |
| 23 |
async def test_get_repository(aresponses, repository_response): |
| 24 |
aresponses.add( |
| 25 |
"api.github.com", |
| 26 |
"/repos/octocat/Hello-World", |
| 27 |
"get", |
| 28 |
aresponses.Response( |
| 29 |
text=json.dumps(repository_response), |
| 30 |
status=200, |
| 31 |
headers={ |
| 32 |
"X-RateLimit-Remaining": "1337", |
| 33 |
"Content-Type": "application/json", |
| 34 |
"Etag": "xyz..zyx", |
| 35 |
}, |
| 36 |
), |
| 37 |
) |
| 38 |
async with GitHub(TOKEN) as github: |
| 39 |
repository = await github.get_repo("octocat/Hello-World") |
| 40 |
assert repository.description == "This your first repo!" |
| 41 |
assert repository.id == 1296269 |
| 42 |
assert repository.name == "Hello-World" |
| 43 |
assert repository.full_name == "octocat/Hello-World" |
| 44 |
assert repository.pushed_at == datetime.datetime(2011, 1, 26, 19, 6, 43) |
| 45 |
assert not repository.archived |
| 46 |
assert repository.topics == ["octocat", "atom", "electron", "api"] |
| 47 |
assert not repository.fork |
| 48 |
assert repository.forks_count == 9 |
| 49 |
assert repository.default_branch == "main" |
| 50 |
assert repository.last_commit is None |
| 51 |
assert repository.homepage == "https://github.com" |
| 52 |
assert repository.stargazers_count == 80 |
| 53 |
assert repository.watchers_count == 80 |
| 54 |
assert repository.owner.login == "octocat" |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.asyncio |
| 58 |
async def test_set_last_commit(aresponses, repository_response, branch_response): |
| 59 |
aresponses.add( |
| 60 |
"api.github.com", |
| 61 |
"/repos/octocat/Hello-World", |
| 62 |
"get", |
| 63 |
aresponses.Response( |
| 64 |
text=json.dumps(repository_response), |
| 65 |
status=200, |
| 66 |
headers={ |
| 67 |
"X-RateLimit-Remaining": "1337", |
| 68 |
"Content-Type": "application/json", |
| 69 |
"Etag": "xyz..zyx", |
| 70 |
}, |
| 71 |
), |
| 72 |
) |
| 73 |
aresponses.add( |
| 74 |
"api.github.com", |
| 75 |
"/repos/octocat/Hello-World/branches/main", |
| 76 |
"get", |
| 77 |
aresponses.Response( |
| 78 |
text=json.dumps(branch_response), |
| 79 |
status=200, |
| 80 |
headers={ |
| 81 |
"X-RateLimit-Remaining": "1337", |
| 82 |
"Content-Type": "application/json", |
| 83 |
"Etag": "xyz..zyx", |
| 84 |
}, |
| 85 |
), |
| 86 |
) |
| 87 |
aresponses.add( |
| 88 |
"api.github.com", |
| 89 |
"/repos/octocat/Hello-World/branches/main", |
| 90 |
"get", |
| 91 |
aresponses.Response(status=304), |
| 92 |
) |
| 93 |
async with GitHub(TOKEN) as github: |
| 94 |
repository = await github.get_repo("octocat/Hello-World") |
| 95 |
assert repository.last_commit is None |
| 96 |
await repository.set_last_commit() |
| 97 |
assert repository.last_commit == "7fd1a60" |
| 98 |
|
| 99 |
with pytest.raises(AIOGitHubAPINotModifiedException): |
| 100 |
await repository.set_last_commit(github.client.last_response.etag) |
| 101 |
|
| 102 |
|
| 103 |
@pytest.mark.asyncio |
| 104 |
async def test_get_contents_file(aresponses, repository_response, contents_file_response): |
| 105 |
aresponses.add( |
| 106 |
"api.github.com", |
| 107 |
"/repos/octocat/Hello-World", |
| 108 |
"get", |
| 109 |
aresponses.Response( |
| 110 |
text=json.dumps(repository_response), |
| 111 |
status=200, |
| 112 |
headers={ |
| 113 |
"X-RateLimit-Remaining": "1337", |
| 114 |
"Content-Type": "application/json", |
| 115 |
"Etag": "xyz..zyx", |
| 116 |
}, |
| 117 |
), |
| 118 |
) |
| 119 |
aresponses.add( |
| 120 |
"api.github.com", |
| 121 |
"/repos/octocat/Hello-World/contents/README.md", |
| 122 |
"get", |
| 123 |
aresponses.Response( |
| 124 |
text=json.dumps(contents_file_response), |
| 125 |
status=200, |
| 126 |
headers={ |
| 127 |
"X-RateLimit-Remaining": "1337", |
| 128 |
"Content-Type": "application/json", |
| 129 |
"Etag": "xyz..zyx", |
| 130 |
}, |
| 131 |
), |
| 132 |
) |
| 133 |
async with GitHub(TOKEN) as github: |
| 134 |
repository = await github.get_repo("octocat/Hello-World") |
| 135 |
contents = await repository.get_contents("README.md", "main") |
| 136 |
assert contents.name == "README.md" |
| 137 |
|
| 138 |
|
| 139 |
@pytest.mark.asyncio |
| 140 |
async def test_get_contents_list(aresponses, repository_response, contents_list_response): |
| 141 |
aresponses.add( |
| 142 |
"api.github.com", |
| 143 |
"/repos/octocat/Hello-World", |
| 144 |
"get", |
| 145 |
aresponses.Response( |
| 146 |
text=json.dumps(repository_response), |
| 147 |
status=200, |
| 148 |
headers={ |
| 149 |
"X-RateLimit-Remaining": "1337", |
| 150 |
"Content-Type": "application/json", |
| 151 |
"Etag": "xyz..zyx", |
| 152 |
}, |
| 153 |
), |
| 154 |
) |
| 155 |
aresponses.add( |
| 156 |
"api.github.com", |
| 157 |
"/repos/octocat/Hello-World/contents/", |
| 158 |
"get", |
| 159 |
aresponses.Response( |
| 160 |
text=json.dumps(contents_list_response), |
| 161 |
status=200, |
| 162 |
headers={ |
| 163 |
"X-RateLimit-Remaining": "1337", |
| 164 |
"Content-Type": "application/json", |
| 165 |
"Etag": "xyz..zyx", |
| 166 |
}, |
| 167 |
), |
| 168 |
) |
| 169 |
async with GitHub(TOKEN) as github: |
| 170 |
repository = await github.get_repo("octocat/Hello-World") |
| 171 |
contents = await repository.get_contents("", "main") |
| 172 |
assert len(contents) == 2 |
| 173 |
|
| 174 |
|
| 175 |
@pytest.mark.asyncio |
| 176 |
async def test_get_tree(aresponses, repository_response, tree_response): |
| 177 |
aresponses.add( |
| 178 |
"api.github.com", |
| 179 |
"/repos/octocat/Hello-World", |
| 180 |
"get", |
| 181 |
aresponses.Response( |
| 182 |
text=json.dumps(repository_response), |
| 183 |
status=200, |
| 184 |
headers={ |
| 185 |
"X-RateLimit-Remaining": "1337", |
| 186 |
"Content-Type": "application/json", |
| 187 |
"Etag": "xyz..zyx", |
| 188 |
}, |
| 189 |
), |
| 190 |
) |
| 191 |
aresponses.add( |
| 192 |
"api.github.com", |
| 193 |
"/repos/octocat/Hello-World/git/trees/main", |
| 194 |
"get", |
| 195 |
aresponses.Response( |
| 196 |
text=json.dumps(tree_response), |
| 197 |
status=200, |
| 198 |
headers={ |
| 199 |
"X-RateLimit-Remaining": "1337", |
| 200 |
"Content-Type": "application/json", |
| 201 |
"Etag": "xyz..zyx", |
| 202 |
}, |
| 203 |
), |
| 204 |
) |
| 205 |
async with GitHub(TOKEN) as github: |
| 206 |
repository = await github.get_repo("octocat/Hello-World") |
| 207 |
contents = await repository.get_tree("main") |
| 208 |
assert contents[0].full_path == "subdir/file.txt" |
| 209 |
with pytest.raises(AIOGitHubAPIException): |
| 210 |
await repository.get_tree() |
| 211 |
|
| 212 |
|
| 213 |
@pytest.mark.asyncio |
| 214 |
async def test_get_rendered_contents(aresponses, repository_response): |
| 215 |
aresponses.add( |
| 216 |
"api.github.com", |
| 217 |
"/repos/octocat/Hello-World", |
| 218 |
"get", |
| 219 |
aresponses.Response( |
| 220 |
text=json.dumps(repository_response), |
| 221 |
status=200, |
| 222 |
headers={ |
| 223 |
"X-RateLimit-Remaining": "1337", |
| 224 |
"Content-Type": "application/json", |
| 225 |
"Etag": "xyz..zyx", |
| 226 |
}, |
| 227 |
), |
| 228 |
) |
| 229 |
aresponses.add( |
| 230 |
"api.github.com", |
| 231 |
"/repos/octocat/Hello-World/contents/README.md", |
| 232 |
"get", |
| 233 |
aresponses.Response( |
| 234 |
text="test", |
| 235 |
status=200, |
| 236 |
headers={ |
| 237 |
"X-RateLimit-Remaining": "1337", |
| 238 |
"Content-Type": "application/json", |
| 239 |
"Etag": "xyz..zyx", |
| 240 |
}, |
| 241 |
), |
| 242 |
) |
| 243 |
aresponses.add( |
| 244 |
"api.github.com", |
| 245 |
"/repos/octocat/Hello-World/contents/README.md", |
| 246 |
"get", |
| 247 |
aresponses.Response(status=304), |
| 248 |
) |
| 249 |
async with GitHub(TOKEN) as github: |
| 250 |
repository = await github.get_repo("octocat/Hello-World") |
| 251 |
rendered = await repository.get_rendered_contents("README.md", "main") |
| 252 |
assert rendered == "test" |
| 253 |
|
| 254 |
with pytest.raises(AIOGitHubAPINotModifiedException): |
| 255 |
await repository.get_rendered_contents( |
| 256 |
"README.md", "main", etag=github.client.last_response.etag |
| 257 |
) |
| 258 |
|
| 259 |
|
| 260 |
@pytest.mark.asyncio |
| 261 |
async def test_get_releases(aresponses, repository_response, releases_response): |
| 262 |
aresponses.add( |
| 263 |
"api.github.com", |
| 264 |
"/repos/octocat/Hello-World", |
| 265 |
"get", |
| 266 |
aresponses.Response( |
| 267 |
text=json.dumps(repository_response), |
| 268 |
status=200, |
| 269 |
headers={ |
| 270 |
"X-RateLimit-Remaining": "1337", |
| 271 |
"Content-Type": "application/json", |
| 272 |
"Etag": "xyz..zyx", |
| 273 |
}, |
| 274 |
), |
| 275 |
) |
| 276 |
aresponses.add( |
| 277 |
"api.github.com", |
| 278 |
"/repos/octocat/Hello-World/releases", |
| 279 |
"get", |
| 280 |
aresponses.Response( |
| 281 |
text=json.dumps(releases_response), |
| 282 |
status=200, |
| 283 |
headers={ |
| 284 |
"X-RateLimit-Remaining": "1337", |
| 285 |
"Content-Type": "application/json", |
| 286 |
"Etag": "xyz..zyx", |
| 287 |
}, |
| 288 |
), |
| 289 |
) |
| 290 |
aresponses.add( |
| 291 |
"api.github.com", |
| 292 |
"/repos/octocat/Hello-World/releases", |
| 293 |
"get", |
| 294 |
aresponses.Response( |
| 295 |
text=json.dumps(releases_response), |
| 296 |
status=200, |
| 297 |
headers={ |
| 298 |
"X-RateLimit-Remaining": "1337", |
| 299 |
"Content-Type": "application/json", |
| 300 |
"Etag": "xyz..zyx", |
| 301 |
}, |
| 302 |
), |
| 303 |
) |
| 304 |
aresponses.add( |
| 305 |
"api.github.com", |
| 306 |
"/repos/octocat/Hello-World/releases", |
| 307 |
"get", |
| 308 |
aresponses.Response( |
| 309 |
text=json.dumps(releases_response), |
| 310 |
status=200, |
| 311 |
headers={ |
| 312 |
"X-RateLimit-Remaining": "1337", |
| 313 |
"Content-Type": "application/json", |
| 314 |
"Etag": "xyz..zyx", |
| 315 |
}, |
| 316 |
), |
| 317 |
) |
| 318 |
aresponses.add( |
| 319 |
"api.github.com", |
| 320 |
"/repos/octocat/Hello-World/releases", |
| 321 |
"get", |
| 322 |
aresponses.Response(status=304), |
| 323 |
) |
| 324 |
async with GitHub(TOKEN) as github: |
| 325 |
repository = await github.get_repo("octocat/Hello-World") |
| 326 |
releases = await repository.get_releases() |
| 327 |
assert len(releases) == 1 |
| 328 |
releases = await repository.get_releases(prerelease=True) |
| 329 |
assert len(releases) == 2 |
| 330 |
assert releases[0].tag_name == "v1.0.0" |
| 331 |
assert not releases[0].prerelease |
| 332 |
releases = await repository.get_releases(prerelease=True, returnlimit=1) |
| 333 |
assert len(releases) == 1 |
| 334 |
|
| 335 |
with pytest.raises(AIOGitHubAPINotModifiedException): |
| 336 |
await repository.get_releases(etag=github.client.last_response.as_dict()["etag"]) |
| 337 |
|
| 338 |
|
| 339 |
@pytest.mark.asyncio |
| 340 |
async def test_get_issue(aresponses, repository_response, issue_response): |
| 341 |
aresponses.add( |
| 342 |
"api.github.com", |
| 343 |
"/repos/octocat/Hello-World", |
| 344 |
"get", |
| 345 |
aresponses.Response( |
| 346 |
text=json.dumps(repository_response), |
| 347 |
status=200, |
| 348 |
headers={ |
| 349 |
"X-RateLimit-Remaining": "1337", |
| 350 |
"Content-Type": "application/json", |
| 351 |
"Etag": "xyz..zyx", |
| 352 |
}, |
| 353 |
), |
| 354 |
) |
| 355 |
aresponses.add( |
| 356 |
"api.github.com", |
| 357 |
"/repos/octocat/Hello-World/issues/1", |
| 358 |
"get", |
| 359 |
aresponses.Response( |
| 360 |
text=json.dumps(issue_response), |
| 361 |
status=200, |
| 362 |
headers={ |
| 363 |
"X-RateLimit-Remaining": "1337", |
| 364 |
"Content-Type": "application/json", |
| 365 |
"Etag": "xyz..zyx", |
| 366 |
}, |
| 367 |
), |
| 368 |
) |
| 369 |
aresponses.add( |
| 370 |
"api.github.com", |
| 371 |
"/repos/octocat/Hello-World/issues/1", |
| 372 |
"get", |
| 373 |
aresponses.Response(status=304), |
| 374 |
) |
| 375 |
async with GitHub(TOKEN) as github: |
| 376 |
repository = await github.get_repo("octocat/Hello-World") |
| 377 |
issue = await repository.get_issue(1) |
| 378 |
assert issue.title == "Found a bug" |
| 379 |
|
| 380 |
with pytest.raises(AIOGitHubAPINotModifiedException): |
| 381 |
await repository.get_issue(1, etag=github.client.last_response.etag) |
| 382 |
|
| 383 |
|
| 384 |
@pytest.mark.asyncio |
| 385 |
async def test_create_issue(aresponses, repository_response): |
| 386 |
aresponses.add( |
| 387 |
"api.github.com", |
| 388 |
"/repos/octocat/Hello-World", |
| 389 |
"get", |
| 390 |
aresponses.Response( |
| 391 |
text=json.dumps(repository_response), |
| 392 |
status=200, |
| 393 |
headers={ |
| 394 |
"X-RateLimit-Remaining": "1337", |
| 395 |
"Content-Type": "application/json", |
| 396 |
"Etag": "xyz..zyx", |
| 397 |
}, |
| 398 |
), |
| 399 |
) |
| 400 |
aresponses.add( |
| 401 |
"api.github.com", |
| 402 |
"/repos/octocat/Hello-World/issues", |
| 403 |
"post", |
| 404 |
aresponses.Response( |
| 405 |
status=200, |
| 406 |
headers={ |
| 407 |
"X-RateLimit-Remaining": "1337", |
| 408 |
"Content-Type": "application/json", |
| 409 |
"Etag": "xyz..zyx", |
| 410 |
}, |
| 411 |
), |
| 412 |
) |
| 413 |
async with GitHub(TOKEN) as github: |
| 414 |
repository = await github.get_repo("octocat/Hello-World") |
| 415 |
data = { |
| 416 |
"title": "test", |
| 417 |
"body": "body", |
| 418 |
"state": "closed", |
| 419 |
"milestone": "v1.0", |
| 420 |
"labels": ["test"], |
| 421 |
"assignees": ["octocat"], |
| 422 |
} |
| 423 |
await repository.create_issue(**data) |
| 424 |
|
| 425 |
|
| 426 |
@pytest.mark.asyncio |
| 427 |
async def test_get_last_commit(aresponses, repository_response, branch_response): |
| 428 |
aresponses.add( |
| 429 |
"api.github.com", |
| 430 |
"/repos/octocat/Hello-World", |
| 431 |
"get", |
| 432 |
aresponses.Response( |
| 433 |
text=json.dumps(repository_response), |
| 434 |
status=200, |
| 435 |
headers={ |
| 436 |
"X-RateLimit-Remaining": "1337", |
| 437 |
"Content-Type": "application/json", |
| 438 |
"Etag": "xyz..zyx", |
| 439 |
}, |
| 440 |
), |
| 441 |
) |
| 442 |
aresponses.add( |
| 443 |
"api.github.com", |
| 444 |
"/repos/octocat/Hello-World/branches/main", |
| 445 |
"get", |
| 446 |
aresponses.Response( |
| 447 |
text=json.dumps(branch_response), |
| 448 |
status=200, |
| 449 |
headers={ |
| 450 |
"X-RateLimit-Remaining": "1337", |
| 451 |
"Content-Type": "application/json", |
| 452 |
"Etag": "xyz..zyx", |
| 453 |
}, |
| 454 |
), |
| 455 |
) |
| 456 |
aresponses.add( |
| 457 |
"api.github.com", |
| 458 |
"/repos/octocat/Hello-World/branches/main", |
| 459 |
"get", |
| 460 |
aresponses.Response(status=304), |
| 461 |
) |
| 462 |
async with GitHub(TOKEN) as github: |
| 463 |
repository = await github.get_repo("octocat/Hello-World") |
| 464 |
assert repository.last_commit is None |
| 465 |
commit = await repository.get_last_commit() |
| 466 |
assert commit.sha == "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d" |
| 467 |
|
| 468 |
with pytest.raises(AIOGitHubAPINotModifiedException): |
| 469 |
await repository.get_last_commit(etag=github.client.last_response.etag) |