Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): raise InvalidToken if the token expires while using lock() #134

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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