Skip to content
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
16 changes: 14 additions & 2 deletions isic_cli/cli/user.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import json
import sys
from typing import TYPE_CHECKING

from authlib.integrations.base_client.errors import OAuthError
import click

if TYPE_CHECKING:
Expand All @@ -22,8 +24,18 @@ def login(obj: IsicContext):
if obj.user:
click.echo(f'Hello {obj.user["email"]}!')
else:
obj.oauth.login()
click.echo("Success!")
try:
obj.oauth.login()
except OAuthError as e:
if e.error == "invalid_grant":
click.secho(
"Logging in timed out or had an unexpected error. Please try again.", fg="red"
)
sys.exit(1)
else:
raise
else:
click.secho("Success!", fg="green")


@user.command()
Expand Down
14 changes: 14 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from authlib.integrations.base_client.errors import OAuthError
from girder_cli_oauth_client import GirderCliOAuthClient
import pytest


Expand All @@ -8,3 +10,15 @@ def test_user_login_logged_in(cli_run):
result = cli_run(["user", "login"])
assert result.exit_code == 0
assert "Hello" in result.output


@pytest.mark.usefixtures("_isolated_filesystem")
def test_user_login_oauth_timeout(cli_run, mocker):
mock_login = mocker.patch.object(
GirderCliOAuthClient, "login", side_effect=OAuthError(error="invalid_grant")
)

result = cli_run(["user", "login"])
assert result.exit_code == 1
assert "Logging in timed out or had an unexpected error" in result.output
mock_login.assert_called_once()
Loading