Skip to content

Commit

Permalink
added tests for invalid token
Browse files Browse the repository at this point in the history
  • Loading branch information
ZohebShaikh committed Nov 13, 2024
1 parent 7da35dc commit 492fe19
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/blueapi/service/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ def load_token(token) -> dict[str, Any]: ...
def delete_token(self) -> None: ...


class NoOpTokenManager(TokenManager):
def __init__(self, warning: str = "Session not configured to persist!"):
self._warning = warning

def save_token(self, token: dict[str, Any]) -> None:
print(self._warning)

def load_token(self) -> dict[str, Any]:
raise ValueError(self._warning)

def delete_token(self) -> None:
print(self._warning)


class CliTokenManager(TokenManager):
def __init__(self, token_path: Path) -> None:
self._token_path: Path = token_path
Expand Down Expand Up @@ -71,11 +57,10 @@ def __init__(
server_config: OIDCConfig,
) -> None:
self._server_config = server_config
self._token_manager: TokenManager = (
CliTokenManager(server_config.token_path)
if isinstance(server_config, CLIClientConfig)
else NoOpTokenManager()
)
assert isinstance(
server_config, CLIClientConfig
), "Please provide token_path in config"
self._token_manager: TokenManager = CliTokenManager(server_config.token_path)

@cached_property
def client(self):
Expand Down Expand Up @@ -191,3 +176,4 @@ def start_device_flow(self):
except Exception:
print("Problem with cached token, starting new session")
self._token_manager.delete_token()
self._do_device_flow()
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def cached_expired_token(tmp_path: Path, expired_token: dict[str, Any]) -> Path:
return token_path


@pytest.fixture
def cached_invalid_token(tmp_path: Path, expired_token: dict[str, Any]) -> Path:
token_path = tmp_path / "token"
with open(token_path, "w") as token_file:
token_file.write("Invalid Token")
return token_path


@pytest.fixture
def cached_valid_token(tmp_path: Path, valid_token: dict[str, Any]) -> Path:
token_path = tmp_path / "token"
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,22 @@ def test_login_edge_cases(
assert result.exit_code == 0


def test_login_when_cached_token_decode_fails(
runner: CliRunner,
config_with_auth: str,
mock_authn_server: responses.RequestsMock,
cached_invalid_token: Path,
):
result = runner.invoke(main, ["-c", config_with_auth, "login"])
assert (
"Logging in\n"
"Problem with cached token, starting new session\n"
"Please login from this URL:- https://example.com/verify\n"
"Logged in and cached new token\n" in result.output
)
assert result.exit_code == 0


def test_logout_success(
runner: CliRunner,
config_with_auth: str,
Expand Down

0 comments on commit 492fe19

Please sign in to comment.