| 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 AIOGitHubAPINotModifiedException, GitHub |
| 8 |
|
| 9 |
from tests.common import TOKEN |
| 10 |
from tests.legacy.responses.contents import contents_file_response |
| 11 |
from tests.legacy.responses.repository_fixture import repository_response |
| 12 |
from tests.legacy.responses.tree import tree_response |
| 13 |
|
| 14 |
|
| 15 |
@pytest.mark.asyncio |
| 16 |
async def test_tree_content(aresponses, repository_response, tree_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/git/trees/main", |
| 34 |
"get", |
| 35 |
aresponses.Response( |
| 36 |
text=json.dumps(tree_response), |
| 37 |
status=200, |
| 38 |
headers={ |
| 39 |
"X-RateLimit-Remaining": "1337", |
| 40 |
"Content-Type": "application/json", |
| 41 |
"Etag": "xyz..zyx", |
| 42 |
}, |
| 43 |
), |
| 44 |
) |
| 45 |
aresponses.add( |
| 46 |
"api.github.com", |
| 47 |
"/repos/octocat/Hello-World/git/trees/main", |
| 48 |
"get", |
| 49 |
aresponses.Response(status=304), |
| 50 |
) |
| 51 |
async with GitHub(TOKEN) as github: |
| 52 |
repository = await github.get_repo("octocat/Hello-World") |
| 53 |
contents = await repository.get_tree("main") |
| 54 |
assert len([x for x in contents if x.is_directory]) == 1 |
| 55 |
assert contents[0].full_path == "subdir/file.txt" |
| 56 |
assert not contents[0].is_directory |
| 57 |
assert contents[0].path == "subdir" |
| 58 |
assert contents[0].filename == "file.txt" |
| 59 |
assert ( |
| 60 |
contents[0].url |
| 61 |
== "https://api.github.com/repos/octocat/Hello-World/git/7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b" |
| 62 |
) |
| 63 |
assert ( |
| 64 |
contents[0].download_url |
| 65 |
== "https://raw.githubusercontent.com/octocat/Hello-World/main/subdir/file.txt" |
| 66 |
) |
| 67 |
|
| 68 |
with pytest.raises(AIOGitHubAPINotModifiedException): |
| 69 |
await repository.get_tree("main", etag=github.client.last_response.etag) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.asyncio |
| 73 |
async def test_content(aresponses, repository_response, contents_file_response): |
| 74 |
aresponses.add( |
| 75 |
"api.github.com", |
| 76 |
"/repos/octocat/Hello-World", |
| 77 |
"get", |
| 78 |
aresponses.Response( |
| 79 |
text=json.dumps(repository_response), |
| 80 |
status=200, |
| 81 |
headers={ |
| 82 |
"X-RateLimit-Remaining": "1337", |
| 83 |
"Content-Type": "application/json", |
| 84 |
"Etag": "xyz..zyx", |
| 85 |
}, |
| 86 |
), |
| 87 |
) |
| 88 |
aresponses.add( |
| 89 |
"api.github.com", |
| 90 |
"/repos/octocat/Hello-World/contents/README.md", |
| 91 |
"get", |
| 92 |
aresponses.Response( |
| 93 |
text=json.dumps(contents_file_response), |
| 94 |
status=200, |
| 95 |
headers={ |
| 96 |
"X-RateLimit-Remaining": "1337", |
| 97 |
"Content-Type": "application/json", |
| 98 |
"Etag": "xyz..zyx", |
| 99 |
}, |
| 100 |
), |
| 101 |
) |
| 102 |
aresponses.add( |
| 103 |
"api.github.com", |
| 104 |
"/repos/octocat/Hello-World/contents/README.md", |
| 105 |
"get", |
| 106 |
aresponses.Response(status=304), |
| 107 |
) |
| 108 |
async with GitHub(TOKEN) as github: |
| 109 |
repository = await github.get_repo("octocat/Hello-World") |
| 110 |
content = await repository.get_contents("README.md", "main") |
| 111 |
assert content.type == "file" |
| 112 |
assert content.encoding == "base64" |
| 113 |
assert content.name == "README.md" |
| 114 |
assert content.path == "README.md" |
| 115 |
assert content.content == "" |
| 116 |
assert ( |
| 117 |
content.download_url |
| 118 |
== "https://raw.githubusercontent.com/octokit/octokit.rb/main/README.md" |
| 119 |
) |
| 120 |
|
| 121 |
with pytest.raises(AIOGitHubAPINotModifiedException): |
| 122 |
await repository.get_contents( |
| 123 |
"README.md", "main", etag=github.client.last_response.etag |
| 124 |
) |