| 1 |
from datetime import datetime |
| 2 |
from unittest.mock import AsyncMock |
| 3 |
|
| 4 |
import aiohttp |
| 5 |
import pytest |
| 6 |
|
| 7 |
from aiogithubapi import AIOGitHubAPIException, GitHubDevice, GitHubDeviceAPI |
| 8 |
|
| 9 |
from tests.common import CLIENT_ID, load_fixture |
| 10 |
from tests.conftest import client_session |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.asyncio |
| 14 |
async def test_session(): |
| 15 |
async with aiohttp.ClientSession() as session: |
| 16 |
device = GitHubDevice("xxxxx", "", session) |
| 17 |
assert device.session == session |
| 18 |
|
| 19 |
async with GitHubDevice("xxxxx") as github: |
| 20 |
assert github.session is not None |
| 21 |
|
| 22 |
|
| 23 |
@pytest.mark.asyncio |
| 24 |
async def test_device(mock_response, github_device: GitHubDevice, asyncio_sleep: AsyncMock): |
| 25 |
device = await github_device.async_register_device() |
| 26 |
assert device.user_code == "WDJB-MJHT" |
| 27 |
|
| 28 |
mock_response.mock_data_list = [ |
| 29 |
{ |
| 30 |
"error": "authorization_pending", |
| 31 |
"error_description": "Pending user authorization", |
| 32 |
}, |
| 33 |
{ |
| 34 |
"access_token": "e72e16c7e42f292c6912e7710c838347ae178b4a", |
| 35 |
"token_type": "bearer", |
| 36 |
"scope": "user", |
| 37 |
}, |
| 38 |
] |
| 39 |
|
| 40 |
activation = await github_device.async_device_activation() |
| 41 |
assert asyncio_sleep.call_count == 1 |
| 42 |
assert asyncio_sleep.call_args[0][0] == 1 |
| 43 |
assert activation.access_token == "e72e16c7e42f292c6912e7710c838347ae178b4a" |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.asyncio |
| 47 |
async def test_no_user_response(github_device: GitHubDevice): |
| 48 |
device = await github_device.async_register_device() |
| 49 |
assert device.user_code == "WDJB-MJHT" |
| 50 |
|
| 51 |
with pytest.raises(AIOGitHubAPIException, match="User took too long to enter key"): |
| 52 |
github_device._expires = datetime.timestamp(datetime.now()) |
| 53 |
await github_device.async_device_activation() |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.asyncio |
| 57 |
async def test_device_error(mock_response, github_device: GitHubDevice): |
| 58 |
device = await github_device.async_register_device() |
| 59 |
assert device.user_code == "WDJB-MJHT" |
| 60 |
|
| 61 |
mock_response.mock_data = load_fixture("oauth_access_token_error.json", True) |
| 62 |
|
| 63 |
with pytest.raises(AIOGitHubAPIException, match="Unsupported grant type"): |
| 64 |
await github_device.async_device_activation() |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.asyncio |
| 68 |
async def test_no_device_code(github_device: GitHubDevice, asyncio_sleep: AsyncMock): |
| 69 |
with pytest.raises(AIOGitHubAPIException, match="User took too long to enter key"): |
| 70 |
github_device._expires = datetime.timestamp(datetime.now()) |
| 71 |
await github_device.async_device_activation() |
| 72 |
assert asyncio_sleep.call_count == 1 |
| 73 |
assert asyncio_sleep.call_args[0][0] == 5 |