diff --git a/src/elmo/api/client.py b/src/elmo/api/client.py index 7fc4487..321140d 100644 --- a/src/elmo/api/client.py +++ b/src/elmo/api/client.py @@ -14,6 +14,7 @@ CredentialError, InvalidInput, InvalidSector, + InvalidToken, LockError, ParseError, QueryNotValid, @@ -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 diff --git a/tests/test_client.py b/tests/test_client.py index dd8ad91..8f03a20 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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(