Skip to content

Commit b9abfdd

Browse files
authored
fix(client): raise InvalidToken if the token expires while using lock() (#134)
1 parent f8d1639 commit b9abfdd

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/elmo/api/client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
CredentialError,
1515
InvalidInput,
1616
InvalidSector,
17+
InvalidToken,
1718
LockError,
1819
ParseError,
1920
QueryNotValid,
@@ -172,10 +173,12 @@ def lock(self, code):
172173
try:
173174
response.raise_for_status()
174175
except HTTPError as err:
175-
# 403: Not possible to obtain the lock, probably because of a race condition
176-
# with another application
176+
# 403: Unable obtain the lock (race condition with another application)
177177
if err.response.status_code == 403:
178178
raise LockError
179+
# 401: The token has expired
180+
if err.response.status_code == 401:
181+
raise InvalidToken
179182
raise err
180183

181184
# A wrong code returns 200 with a fail state

tests/test_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,19 @@ def test_client_lock_called_twice(server, mocker):
601601
assert len(server.calls) == 1
602602

603603

604+
def test_client_lock_invalid_token(server, mocker):
605+
"""Should raise a CodeError if the token is expired while calling Lock()."""
606+
server.add(responses.POST, "https://example.com/api/panel/syncLogin", status=401)
607+
client = ElmoClient(base_url="https://example.com", domain="domain")
608+
client._session_id = "test"
609+
mocker.patch.object(client, "unlock")
610+
# Test
611+
with pytest.raises(InvalidToken):
612+
with client.lock("test"):
613+
pass
614+
assert len(server.calls) == 1
615+
616+
604617
def test_client_lock_unknown_error(server, mocker):
605618
"""Should raise an Exception for unknown status code."""
606619
server.add(

0 commit comments

Comments
 (0)