Skip to content

Commit

Permalink
Add auth exception for account locked
Browse files Browse the repository at this point in the history
  • Loading branch information
abmantis committed Jan 6, 2025
1 parent 6a058f1 commit 379ebdb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 14 additions & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import sys
import pytest
from http import HTTPStatus

from yarl import URL

from whirlpool.auth import Auth
from whirlpool.auth import AccountLockedError, Auth
from whirlpool.backendselector import BackendSelector

from .mock_backendselector import BackendSelectorMock, BackendSelectorMockMultipleCreds
Expand Down Expand Up @@ -118,6 +119,18 @@ async def test_auth_bad_credentials(
assert call[1]["headers"] == AUTH_HEADERS


async def test_auth_account_locked(
auth_fixture: Auth, backend_selector_mock: BackendSelector, aioresponses_mock
):
aioresponses_mock.post(AUTH_URL, status=HTTPStatus.LOCKED, repeat=True)

with pytest.raises(AccountLockedError):
await auth_fixture.do_auth(store=False)

assert auth_fixture.is_access_token_valid() is False
assert auth_fixture.get_said_list() is None


async def test_user_details_requested_only_once(
auth_fixture: Auth, backend_selector_mock: BackendSelector, aioresponses_mock
):
Expand Down
7 changes: 6 additions & 1 deletion whirlpool/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
AUTH_JSON_FILE = ".whirlpool_auth.json"


class AccountLockedError(Exception):
"""Exception for authentication failure due to account being locked."""


class Auth:
def __init__(
self,
Expand Down Expand Up @@ -68,6 +72,8 @@ async def _do_auth(self, refresh_token: str | None) -> dict[str, str]:
LOGGER.debug("Auth status: " + str(r.status))
if r.status == 200:
return await r.json()
if r.status == 423:
raise AccountLockedError()
elif refresh_token:
return await self._do_auth(refresh_token=None)

Expand Down Expand Up @@ -140,6 +146,5 @@ async def get_account_id(self) -> str | None:
data = await r.json()
self._auth_dict["accountId"] = data["accountId"]
return self._auth_dict["accountId"]

def get_said_list(self):
return self._auth_dict.get("SAID", None)

0 comments on commit 379ebdb

Please sign in to comment.