From 60f655f124db97548e89292676b96614e3242a2b Mon Sep 17 00:00:00 2001 From: Bartosz Blizniak Date: Wed, 8 Jul 2026 15:39:43 +0100 Subject: [PATCH] fix(no-ticket): whoami exits 1 when not authenticated The whoami command previously always exited 0, even for anonymous (unauthenticated) requests, so scripts and CI pipelines had to parse the JSON output to detect authentication status. It now exits with code 1 when is_authenticated is false and 0 when true, across all output formats. Output is unchanged. Co-Authored-By: Claude Fable 5 --- cloudsmith_cli/cli/commands/whoami.py | 4 +- .../cli/tests/commands/test_whoami.py | 73 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 cloudsmith_cli/cli/tests/commands/test_whoami.py diff --git a/cloudsmith_cli/cli/commands/whoami.py b/cloudsmith_cli/cli/commands/whoami.py index 37a8f95a..45d1f0fe 100644 --- a/cloudsmith_cli/cli/commands/whoami.py +++ b/cloudsmith_cli/cli/commands/whoami.py @@ -172,12 +172,14 @@ def whoami(ctx, opts): data["auth"] = _get_verbose_auth_data(opts, api_host) if utils.maybe_print_as_json(opts, data): + if not is_auth: + ctx.exit(1) return if not is_auth: click.echo("You are authenticated as:") click.secho("Nobody (i.e. anonymous user)", fg="yellow") - return + ctx.exit(1) if opts.verbose: _print_verbose_text(data) diff --git a/cloudsmith_cli/cli/tests/commands/test_whoami.py b/cloudsmith_cli/cli/tests/commands/test_whoami.py new file mode 100644 index 00000000..18743223 --- /dev/null +++ b/cloudsmith_cli/cli/tests/commands/test_whoami.py @@ -0,0 +1,73 @@ +import json +from unittest.mock import patch + +import click.testing +import pytest + +from ...commands.whoami import whoami + +HOST = "https://api.example.com" +ARGS = ["--api-host", HOST, "--api-key", "fake-api-key"] + + +@pytest.fixture() +def runner(): + return click.testing.CliRunner() + + +def invoke_whoami(runner, user_brief, extra_args=None): + """Invoke whoami with get_user_brief mocked to return user_brief.""" + with patch( + "cloudsmith_cli.cli.commands.whoami.get_user_brief" + ) as get_user_brief_mock: + get_user_brief_mock.return_value = user_brief + return runner.invoke(whoami, ARGS + (extra_args or [])) + + +class TestWhoamiCommand: + """Tests for the cloudsmith whoami command.""" + + def test_authenticated_exits_zero(self, runner): + result = invoke_whoami( + runner, (True, "test-user", "test@example.com", "Test User") + ) + + assert result.exit_code == 0 + output = result.output.splitlines() + assert output[0] == "Retrieving your authentication status from the API ... OK" + assert output[1] == "You are authenticated as:" + assert output[2] == "User: Test User (slug: test-user, email: test@example.com)" + + def test_unauthenticated_exits_one(self, runner): + result = invoke_whoami(runner, (False, None, None, None)) + + assert result.exit_code == 1 + output = result.output.splitlines() + assert output[0] == "Retrieving your authentication status from the API ... OK" + assert output[1] == "You are authenticated as:" + assert output[2] == "Nobody (i.e. anonymous user)" + + def test_json_authenticated_exits_zero(self, runner): + result = invoke_whoami( + runner, + (True, "test-user", "test@example.com", "Test User"), + extra_args=["--output-format", "json"], + ) + + assert result.exit_code == 0 + payload = json.loads( + "".join(line for line in result.output.splitlines() if line.startswith("{")) + ) + assert payload["data"]["is_authenticated"] is True + assert payload["data"]["username"] == "test-user" + + def test_json_unauthenticated_exits_one(self, runner): + result = invoke_whoami( + runner, (False, None, None, None), extra_args=["--output-format", "json"] + ) + + assert result.exit_code == 1 + payload = json.loads( + "".join(line for line in result.output.splitlines() if line.startswith("{")) + ) + assert payload["data"]["is_authenticated"] is False