| 1 |
"""Test device_flow.""" |
| 2 |
# pylint: disable=missing-docstring,protected-access |
| 3 |
from datetime import datetime |
| 4 |
from unittest.mock import AsyncMock |
| 5 |
from aiogithubapi.exceptions import GitHubAuthenticationException |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from aiogithubapi import ( |
| 10 |
DeviceFlowError, |
| 11 |
GitHubDeviceAPI, |
| 12 |
GitHubException, |
| 13 |
GitHubRequestKwarg, |
| 14 |
) |
| 15 |
|
| 16 |
from tests.common import CLIENT_ID, DEVICE_CODE, TOKEN, MockedRequests, MockResponse |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.asyncio |
| 20 |
async def test_register_device( |
| 21 |
github_device_api: GitHubDeviceAPI, |
| 22 |
mock_requests: MockedRequests, |
| 23 |
): |
| 24 |
response = await github_device_api.register() |
| 25 |
assert response.status == 200 |
| 26 |
assert response.data.verification_uri == "https://github.com/login/device" |
| 27 |
assert mock_requests.called == 1 |
| 28 |
assert mock_requests.last_request["url"] == "https://github.com/login/device/code" |
| 29 |
assert mock_requests.last_request["params"]["scope"] == "" |
| 30 |
assert mock_requests.last_request["params"]["client_id"] == CLIENT_ID |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.asyncio |
| 34 |
async def test_register_device_custom_scope( |
| 35 |
github_device_api: GitHubDeviceAPI, |
| 36 |
mock_requests: MockedRequests, |
| 37 |
): |
| 38 |
response = await github_device_api.register(**{GitHubRequestKwarg.SCOPE: "custom"}) |
| 39 |
assert response.status == 200 |
| 40 |
assert response.data.verification_uri == "https://github.com/login/device" |
| 41 |
assert mock_requests.called == 1 |
| 42 |
assert mock_requests.last_request["url"] == "https://github.com/login/device/code" |
| 43 |
assert mock_requests.last_request["params"]["scope"] == "custom" |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.asyncio |
| 47 |
async def test_device_activation( |
| 48 |
github_device_api: GitHubDeviceAPI, |
| 49 |
mock_requests: MockedRequests, |
| 50 |
): |
| 51 |
response = await github_device_api.activation(device_code=DEVICE_CODE) |
| 52 |
assert response.status == 200 |
| 53 |
assert response.data.access_token == TOKEN |
| 54 |
assert mock_requests.called == 1 |
| 55 |
assert mock_requests.last_request["url"] == "https://github.com/login/oauth/access_token" |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.asyncio |
| 59 |
async def test_missing_expiration(github_device_api: GitHubDeviceAPI): |
| 60 |
github_device_api._expires = None |
| 61 |
with pytest.raises(GitHubException, match="Expiration has passed, re-run the registration"): |
| 62 |
await github_device_api.activation(device_code=DEVICE_CODE) |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.asyncio |
| 66 |
async def test_user_too_slow(github_device_api: GitHubDeviceAPI): |
| 67 |
github_device_api._expires = datetime.timestamp(datetime.now()) - 1 |
| 68 |
with pytest.raises(GitHubException, match="User took too long to enter key"): |
| 69 |
await github_device_api.activation(device_code=DEVICE_CODE) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.asyncio |
| 73 |
async def test_wait_for_confirmation( |
| 74 |
github_device_api: GitHubDeviceAPI, |
| 75 |
mock_requests: MockedRequests, |
| 76 |
mock_response: MockResponse, |
| 77 |
caplog: pytest.CaptureFixture, |
| 78 |
asyncio_sleep: AsyncMock, |
| 79 |
): |
| 80 |
mock_response.mock_data_list = [ |
| 81 |
{ |
| 82 |
"error": DeviceFlowError.AUTHORIZATION_PENDING, |
| 83 |
"error_description": "Pending user authorization", |
| 84 |
}, |
| 85 |
{ |
| 86 |
"error": DeviceFlowError.AUTHORIZATION_PENDING, |
| 87 |
"error_description": "Pending user authorization", |
| 88 |
}, |
| 89 |
{ |
| 90 |
"error": DeviceFlowError.SLOW_DOWN, |
| 91 |
"error_description": "Too many requests have been made in the same timeframe.", |
| 92 |
"interval": 20, |
| 93 |
}, |
| 94 |
{ |
| 95 |
"error": DeviceFlowError.AUTHORIZATION_PENDING, |
| 96 |
"error_description": "Pending user authorization", |
| 97 |
}, |
| 98 |
{ |
| 99 |
"access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 100 |
"token_type": "bearer", |
| 101 |
"scope": "user", |
| 102 |
}, |
| 103 |
] |
| 104 |
response = await github_device_api.activation(device_code=DEVICE_CODE) |
| 105 |
assert response.status == 200 |
| 106 |
assert mock_requests.called == 5 |
| 107 |
assert asyncio_sleep.call_count == 4 |
| 108 |
|
| 109 |
# Use default interval |
| 110 |
assert asyncio_sleep.call_args_list[-3][0][0] == 1 |
| 111 |
# Use new interval from API for all next calls |
| 112 |
assert asyncio_sleep.call_args_list[-2][0][0] == 20 |
| 113 |
assert asyncio_sleep.call_args_list[-1][0][0] == 20 |
| 114 |
|
| 115 |
assert mock_requests.last_request["url"] == "https://github.com/login/oauth/access_token" |
| 116 |
assert "Pending user authorization" in caplog.text |
| 117 |
assert "Got new interval instruction of 20 from the API" in caplog.text |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.asyncio |
| 121 |
async def test_error_while_waiting( |
| 122 |
github_device_api: GitHubDeviceAPI, |
| 123 |
mock_response: MockResponse, |
| 124 |
): |
| 125 |
mock_response.mock_data = {"error": "any", "error_description": "Any error message"} |
| 126 |
with pytest.raises(GitHubException, match="Any error message"): |
| 127 |
await github_device_api.activation(device_code=DEVICE_CODE) |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.asyncio |
| 131 |
async def test_secondary_rate_limit( |
| 132 |
github_device_api: GitHubDeviceAPI, |
| 133 |
mock_response: MockResponse, |
| 134 |
): |
| 135 |
mock_response.mock_data = "<html><p>You have exceeded a secondary rate limit.<br></html>" |
| 136 |
with pytest.raises(GitHubAuthenticationException, match="Secondary rate limit exceeded"): |
| 137 |
await github_device_api.activation(device_code=DEVICE_CODE) |