Skip to content
Draft
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
4 changes: 3 additions & 1 deletion cloudsmith_cli/cli/commands/whoami.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
73 changes: 73 additions & 0 deletions cloudsmith_cli/cli/tests/commands/test_whoami.py
Original file line number Diff line number Diff line change
@@ -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
Loading