| 1 |
# pylint: disable=missing-docstring, redefined-outer-name, unused-import |
| 2 |
import json |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from aiogithubapi import ( |
| 7 |
AIOGitHubAPIAuthenticationException, |
| 8 |
AIOGitHubAPIException, |
| 9 |
AIOGitHubAPIRatelimitException, |
| 10 |
GitHub, |
| 11 |
) |
| 12 |
from aiogithubapi.const import DEFAULT_USER_AGENT |
| 13 |
|
| 14 |
from tests.common import HEADERS, HEADERS_RATELIMITED, TOKEN |
| 15 |
from tests.legacy.responses.base import bad_auth_response, bad_response, base_response |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.asyncio |
| 19 |
async def test_get(mock_response, client_session): |
| 20 |
|
| 21 |
async with GitHub(TOKEN, session=client_session) as github: |
| 22 |
await github.client.get("/") |
| 23 |
assert github.client.ratelimits.remaining == "4999" |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.asyncio |
| 27 |
async def test_headers(mock_response, client_session): |
| 28 |
mock_response.mock_headers = HEADERS |
| 29 |
|
| 30 |
async with GitHub(TOKEN, session=client_session) as github: |
| 31 |
await github.client.get("/") |
| 32 |
assert github.client.headers["User-Agent"] == DEFAULT_USER_AGENT |
| 33 |
|
| 34 |
async with GitHub( |
| 35 |
TOKEN, headers={"User-Agent": "test/client"}, session=client_session |
| 36 |
) as github: |
| 37 |
await github.client.get("/") |
| 38 |
assert github.client.headers["User-Agent"] == "test/client" |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.asyncio |
| 42 |
async def test_post(mock_response, client_session): |
| 43 |
mock_response.mock_headers = HEADERS |
| 44 |
|
| 45 |
async with GitHub(session=client_session) as github: |
| 46 |
await github.client.post("/") |
| 47 |
assert github.client.ratelimits.remaining == "4999" |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.asyncio |
| 51 |
async def test_post_with_json(mock_response, base_response, client_session): |
| 52 |
mock_response.mock_headers = HEADERS |
| 53 |
mock_response.mock_data = base_response |
| 54 |
|
| 55 |
async with GitHub(TOKEN, session=client_session) as github: |
| 56 |
await github.client.post("/", data={"test": "test"}, jsondata=True) |
| 57 |
assert github.client.ratelimits.remaining == "4999" |
| 58 |
|
| 59 |
|
| 60 |
@pytest.mark.asyncio |
| 61 |
async def test_get_ratelimited(client, mock_response): |
| 62 |
mock_response.mock_status = 403 |
| 63 |
|
| 64 |
with pytest.raises(AIOGitHubAPIRatelimitException): |
| 65 |
await client.get("/") |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.asyncio |
| 69 |
async def test_get_unauthorized(client, mock_response): |
| 70 |
mock_response.mock_status = 401 |
| 71 |
|
| 72 |
with pytest.raises(AIOGitHubAPIAuthenticationException): |
| 73 |
await client.get("/") |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.asyncio |
| 77 |
async def test_post_ratelimited(client, mock_response, base_response): |
| 78 |
mock_response.data = json.dumps(base_response) |
| 79 |
mock_response.mock_status = 403 |
| 80 |
mock_response.mock_headers = HEADERS_RATELIMITED |
| 81 |
|
| 82 |
with pytest.raises(AIOGitHubAPIRatelimitException): |
| 83 |
await client.post("/") |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.asyncio |
| 87 |
async def test_get_error(mock_response, client_session): |
| 88 |
mock_response.mock_status = 500 |
| 89 |
|
| 90 |
async with GitHub(TOKEN, session=client_session) as github: |
| 91 |
with pytest.raises(AIOGitHubAPIException): |
| 92 |
await github.client.get("/") |
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.asyncio |
| 96 |
async def test_post_error(mock_response, client_session): |
| 97 |
mock_response.mock_status = 500 |
| 98 |
|
| 99 |
async with GitHub(TOKEN, session=client_session) as github: |
| 100 |
with pytest.raises(AIOGitHubAPIException): |
| 101 |
await github.client.post("/") |
| 102 |
|
| 103 |
|
| 104 |
@pytest.mark.asyncio |
| 105 |
async def test_ok_get_auth_error(mock_response, bad_auth_response, client_session): |
| 106 |
mock_response.mock_data = bad_auth_response |
| 107 |
|
| 108 |
async with GitHub(TOKEN, session=client_session) as github: |
| 109 |
with pytest.raises(AIOGitHubAPIException): |
| 110 |
await github.client.get("/") |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.asyncio |
| 114 |
async def test_ok_get_error(mock_response, bad_response, client_session): |
| 115 |
mock_response.mock_data = bad_response |
| 116 |
|
| 117 |
async with GitHub(TOKEN, session=client_session) as github: |
| 118 |
with pytest.raises(AIOGitHubAPIException): |
| 119 |
await github.client.get("/") |
| 120 |
|
| 121 |
|
| 122 |
@pytest.mark.asyncio |
| 123 |
async def test_custom_base_url(mock_response, mock_requests, client_session): |
| 124 |
mock_response.mock_headers = HEADERS |
| 125 |
|
| 126 |
async with GitHub(TOKEN, base_url="http://example.com", session=client_session) as github: |
| 127 |
await github.client.get("/") |
| 128 |
assert github.client.ratelimits.remaining == "4999" |
| 129 |
|
| 130 |
assert mock_requests.last_request["url"] == "http://example.com/" |