|
| 1 | +"""Tests for asynchronous Model Target authentication.""" |
| 2 | + |
| 3 | +import secrets |
| 4 | +from http import HTTPStatus |
| 5 | + |
| 6 | +import pytest |
| 7 | +from mock_vws import ( |
| 8 | + MockVWS, |
| 9 | + ModelTargetFailureResponse, |
| 10 | +) |
| 11 | + |
| 12 | +from tests.model_target_credentials import CLIENT_CREDENTIALS, CLIENT_ID |
| 13 | +from vws import AsyncModelTargetService |
| 14 | +from vws.exceptions.custom_exceptions import ServerError |
| 15 | +from vws.exceptions.model_target_exceptions import ( |
| 16 | + ModelTargetAuthenticationError, |
| 17 | + ModelTargetError, |
| 18 | + ModelTargetOAuth2Error, |
| 19 | +) |
| 20 | +from vws.exceptions.vws_exceptions import TooManyRequestsError |
| 21 | +from vws.model_target_datasets import ( |
| 22 | + ModelTargetDatasetType, |
| 23 | + ModelTargetModel, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +async def _assert_dataset_error_response( |
| 28 | + *, |
| 29 | + model_target_model: ModelTargetModel, |
| 30 | + status_code: HTTPStatus, |
| 31 | + body: str, |
| 32 | + expected_exception: ( |
| 33 | + type[ModelTargetError | TooManyRequestsError | ServerError] |
| 34 | + ), |
| 35 | +) -> None: |
| 36 | + """Assert that a mocked dataset failure maps to an exception.""" |
| 37 | + async with AsyncModelTargetService( |
| 38 | + client_id=CLIENT_ID, |
| 39 | + client_secret=CLIENT_CREDENTIALS[1], |
| 40 | + ) as client: |
| 41 | + with pytest.raises( |
| 42 | + expected_exception=( |
| 43 | + ModelTargetError, |
| 44 | + TooManyRequestsError, |
| 45 | + ServerError, |
| 46 | + ) |
| 47 | + ) as exc: |
| 48 | + await client.create_dataset( |
| 49 | + name="dataset", |
| 50 | + target_sdk="11.0", |
| 51 | + models=[model_target_model], |
| 52 | + dataset_type=ModelTargetDatasetType.STANDARD, |
| 53 | + ) |
| 54 | + |
| 55 | + assert isinstance(exc.value, expected_exception) |
| 56 | + assert exc.value.response.status_code == status_code |
| 57 | + assert exc.value.response.text == body |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_token_is_a_bearer_token( |
| 62 | + *, |
| 63 | + async_model_target_client: AsyncModelTargetService, |
| 64 | +) -> None: |
| 65 | + """An access token is given for valid credentials.""" |
| 66 | + assert bool(await async_model_target_client.get_access_token()) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +@pytest.mark.usefixtures("_mock_model_targets") |
| 71 | +async def test_invalid_credentials() -> None: |
| 72 | + """An exception is raised when the credentials are not known.""" |
| 73 | + async with AsyncModelTargetService( |
| 74 | + client_id="not-a-client-id", |
| 75 | + client_secret=secrets.token_hex(), |
| 76 | + ) as client: |
| 77 | + with pytest.raises( |
| 78 | + expected_exception=ModelTargetOAuth2Error, |
| 79 | + ) as exc: |
| 80 | + await client.get_access_token() |
| 81 | + |
| 82 | + assert exc.value.response.status_code == HTTPStatus.UNAUTHORIZED |
| 83 | + assert exc.value.error == "invalid_client" |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.asyncio |
| 87 | +@pytest.mark.parametrize( |
| 88 | + argnames=("status_code", "body", "expected_exception"), |
| 89 | + argvalues=[ |
| 90 | + pytest.param( |
| 91 | + HTTPStatus.UNAUTHORIZED, |
| 92 | + '{"error":{"code":"AUTHENTICATION_ERROR","message":"No"}}', |
| 93 | + ModelTargetAuthenticationError, |
| 94 | + id="authentication", |
| 95 | + ), |
| 96 | + pytest.param( |
| 97 | + HTTPStatus.FORBIDDEN, |
| 98 | + '{"error":{"code":"FORBIDDEN","message":"Denied"}}', |
| 99 | + ModelTargetError, |
| 100 | + id="generic-json", |
| 101 | + ), |
| 102 | + pytest.param( |
| 103 | + HTTPStatus.CONFLICT, |
| 104 | + "not json", |
| 105 | + ModelTargetError, |
| 106 | + id="generic-non-json", |
| 107 | + ), |
| 108 | + pytest.param( |
| 109 | + HTTPStatus.TOO_MANY_REQUESTS, |
| 110 | + "rate limited", |
| 111 | + TooManyRequestsError, |
| 112 | + id="rate-limit", |
| 113 | + ), |
| 114 | + pytest.param( |
| 115 | + HTTPStatus.BAD_GATEWAY, |
| 116 | + "server error", |
| 117 | + ServerError, |
| 118 | + id="server-error", |
| 119 | + ), |
| 120 | + ], |
| 121 | +) |
| 122 | +async def test_dataset_error_response( |
| 123 | + *, |
| 124 | + model_target_model: ModelTargetModel, |
| 125 | + status_code: HTTPStatus, |
| 126 | + body: str, |
| 127 | + expected_exception: ( |
| 128 | + type[ModelTargetError | TooManyRequestsError | ServerError] |
| 129 | + ), |
| 130 | +) -> None: |
| 131 | + """Dataset failures map to exceptions through the mock.""" |
| 132 | + failure = ModelTargetFailureResponse( |
| 133 | + status_code=status_code, |
| 134 | + body=body, |
| 135 | + ) |
| 136 | + |
| 137 | + with MockVWS(model_target_failure_response=failure): |
| 138 | + await _assert_dataset_error_response( |
| 139 | + model_target_model=model_target_model, |
| 140 | + status_code=status_code, |
| 141 | + body=body, |
| 142 | + expected_exception=expected_exception, |
| 143 | + ) |
0 commit comments