|
| 1 | +import time |
| 2 | +from typing import Any |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | +from responses import matchers |
| 8 | + |
| 9 | +from qi2_shared.authentication import AuthorisationError, IdentityProvider, OauthDeviceSession |
| 10 | +from qi2_shared.settings import ApiSettings, AuthSettings, TokenInfo |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def identity_provider_mock() -> MagicMock: |
| 15 | + return MagicMock(spec=IdentityProvider) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def api_settings_mock(auth_settings: AuthSettings) -> MagicMock: |
| 20 | + api_settings = MagicMock(spec=ApiSettings) |
| 21 | + api_settings.default_host = "https://host.com" |
| 22 | + api_settings.auths = {api_settings.default_host: auth_settings} |
| 23 | + return api_settings |
| 24 | + |
| 25 | + |
| 26 | +def test_oauth_device_session_refresh_no_token(api_settings_mock: MagicMock, identity_provider_mock: MagicMock) -> None: |
| 27 | + # Arrange |
| 28 | + api_settings_mock.auths[api_settings_mock.default_host].tokens = None |
| 29 | + session = OauthDeviceSession("https://host.com", api_settings_mock, identity_provider_mock) |
| 30 | + |
| 31 | + # Act & Assert |
| 32 | + with pytest.raises(AuthorisationError): |
| 33 | + session.refresh() |
| 34 | + |
| 35 | + |
| 36 | +def test_oauth_device_session_refresh_token_not_expired( |
| 37 | + api_settings_mock: MagicMock, identity_provider_mock: MagicMock |
| 38 | +) -> None: |
| 39 | + # Arrange |
| 40 | + auth_settings = api_settings_mock.auths[api_settings_mock.default_host] |
| 41 | + auth_settings.tokens.generated_at = time.time() |
| 42 | + session = OauthDeviceSession("https://host.com", api_settings_mock, identity_provider_mock) |
| 43 | + |
| 44 | + # Act |
| 45 | + token_info = session.refresh() |
| 46 | + |
| 47 | + # Assert |
| 48 | + assert token_info == auth_settings.tokens |
| 49 | + |
| 50 | + identity_provider_mock.refresh_access_token.assert_not_called() |
| 51 | + |
| 52 | + |
| 53 | +def test_oauth_device_session_refresh_token_expired( |
| 54 | + api_settings_mock: MagicMock, identity_provider_mock: MagicMock |
| 55 | +) -> None: |
| 56 | + # Arrange |
| 57 | + session = OauthDeviceSession("https://host.com", api_settings_mock, identity_provider_mock) |
| 58 | + new_token_info: dict[str, Any] = { |
| 59 | + "access_token": "new_access_token", |
| 60 | + "expires_in": 100, |
| 61 | + "refresh_token": "new_refresh_token", |
| 62 | + "refresh_expires_in": 1000, |
| 63 | + "generated_at": time.time(), |
| 64 | + } |
| 65 | + |
| 66 | + identity_provider_mock.refresh_access_token.return_value = new_token_info |
| 67 | + |
| 68 | + # Act |
| 69 | + token_info = session.refresh() |
| 70 | + |
| 71 | + # Assert |
| 72 | + assert token_info == TokenInfo(**new_token_info) |
| 73 | + |
| 74 | + identity_provider_mock.refresh_access_token.assert_called_once_with("client_id", "refresh_token") |
| 75 | + api_settings_mock.store_tokens.assert_called_once_with("https://host.com", token_info) |
| 76 | + |
| 77 | + |
| 78 | +@responses.activate |
| 79 | +def test_identity_provider_refresh_access_token() -> None: |
| 80 | + # Arrange |
| 81 | + token_info = {"token": "something", "some": "other_data"} |
| 82 | + client_id = "some_client" |
| 83 | + old_refresh_token = "old_token" |
| 84 | + |
| 85 | + responses.get( |
| 86 | + "https://host.com/well-known-endpoint", |
| 87 | + json={ |
| 88 | + "token_endpoint": "https://host.com/token-endpoint", |
| 89 | + "device_authorization_endpoint": "https://host.com/device-endpoint", |
| 90 | + }, |
| 91 | + ) |
| 92 | + responses.post( |
| 93 | + "https://host.com/token-endpoint", |
| 94 | + json=token_info, |
| 95 | + match=[ |
| 96 | + matchers.urlencoded_params_matcher( |
| 97 | + {"grant_type": "refresh_token", "client_id": client_id, "refresh_token": old_refresh_token} |
| 98 | + ) |
| 99 | + ], |
| 100 | + ) |
| 101 | + |
| 102 | + # Act |
| 103 | + provider = IdentityProvider("https://host.com/well-known-endpoint") |
| 104 | + |
| 105 | + token = provider.refresh_access_token(client_id, old_refresh_token) |
| 106 | + |
| 107 | + # Assert |
| 108 | + assert token == token_info |
0 commit comments