Skip to content

Commit

Permalink
fix(client): raise InvalidToken if the token expires while using `l…
Browse files Browse the repository at this point in the history
…ock()`
  • Loading branch information
palazzem committed Dec 20, 2023
1 parent f8d1639 commit 0aa8c49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/elmo/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CredentialError,
InvalidInput,
InvalidSector,
InvalidToken,
LockError,
ParseError,
QueryNotValid,
Expand Down Expand Up @@ -172,10 +173,12 @@ def lock(self, code):
try:
response.raise_for_status()
except HTTPError as err:
# 403: Not possible to obtain the lock, probably because of a race condition
# with another application
# 403: Unable obtain the lock (race condition with another application)
if err.response.status_code == 403:
raise LockError
# 401: The token has expired
if err.response.status_code == 401:
raise InvalidToken
raise err

# A wrong code returns 200 with a fail state
Expand Down
13 changes: 13 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,19 @@ def test_client_lock_called_twice(server, mocker):
assert len(server.calls) == 1


def test_client_lock_invalid_token(server, mocker):
"""Should raise a CodeError if the token is expired while calling Lock()."""
server.add(responses.POST, "https://example.com/api/panel/syncLogin", status=401)
client = ElmoClient(base_url="https://example.com", domain="domain")
client._session_id = "test"
mocker.patch.object(client, "unlock")
# Test
with pytest.raises(InvalidToken):
with client.lock("test"):
pass
assert len(server.calls) == 1


def test_client_lock_unknown_error(server, mocker):
"""Should raise an Exception for unknown status code."""
server.add(
Expand Down

0 comments on commit 0aa8c49

Please sign in to comment.