|
| 1 | +import json |
| 2 | +import os |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import click.testing |
| 6 | +import pytest |
| 7 | + |
| 8 | +from ...commands.logout import logout |
| 9 | + |
| 10 | +HOST = "https://api.example.com" |
| 11 | +CREDS_PATH = "/home/user/.config/cloudsmith/credentials.ini" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture() |
| 15 | +def runner(): |
| 16 | + return click.testing.CliRunner() |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_no_keyring_env(): |
| 21 | + """Ensure CLOUDSMITH_NO_KEYRING and CLOUDSMITH_API_KEY are not set.""" |
| 22 | + env = os.environ.copy() |
| 23 | + env.pop("CLOUDSMITH_NO_KEYRING", None) |
| 24 | + env.pop("CLOUDSMITH_API_KEY", None) |
| 25 | + with patch.dict(os.environ, env, clear=True): |
| 26 | + yield |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mock_deps(mock_no_keyring_env): |
| 31 | + """Patch keyring and CredentialsReader with sensible defaults.""" |
| 32 | + with ( |
| 33 | + patch("cloudsmith_cli.cli.commands.logout.keyring") as mk, |
| 34 | + patch("cloudsmith_cli.cli.commands.logout.CredentialsReader") as mc, |
| 35 | + ): |
| 36 | + mc.find_existing_files.return_value = [CREDS_PATH] |
| 37 | + mk.should_use_keyring.return_value = True |
| 38 | + mk.has_sso_tokens.return_value = True |
| 39 | + yield mc, mk |
| 40 | + |
| 41 | + |
| 42 | +class TestLogoutCommand: |
| 43 | + """Tests for the cloudsmith logout command.""" |
| 44 | + |
| 45 | + def test_full_logout(self, runner, mock_deps): |
| 46 | + mock_creds, mock_keyring = mock_deps |
| 47 | + |
| 48 | + result = runner.invoke(logout, ["--api-host", HOST]) |
| 49 | + |
| 50 | + assert result.exit_code == 0 |
| 51 | + mock_creds.clear_api_key.assert_called_once_with(CREDS_PATH) |
| 52 | + mock_keyring.delete_sso_tokens.assert_called_once_with(HOST) |
| 53 | + assert "Removed credentials from:" in result.output |
| 54 | + assert "Removed SSO tokens from system keyring" in result.output |
| 55 | + |
| 56 | + def test_dry_run(self, runner, mock_deps): |
| 57 | + mock_creds, mock_keyring = mock_deps |
| 58 | + |
| 59 | + result = runner.invoke(logout, ["--dry-run", "--api-host", HOST]) |
| 60 | + |
| 61 | + assert result.exit_code == 0 |
| 62 | + mock_creds.clear_api_key.assert_not_called() |
| 63 | + mock_keyring.delete_sso_tokens.assert_not_called() |
| 64 | + assert "Would remove" in result.output |
| 65 | + |
| 66 | + def test_keyring_only_and_config_only_are_mutually_exclusive(self, runner): |
| 67 | + result = runner.invoke( |
| 68 | + logout, ["--keyring-only", "--config-only", "--api-host", HOST] |
| 69 | + ) |
| 70 | + |
| 71 | + assert result.exit_code != 0 |
| 72 | + assert "mutually exclusive" in result.output |
| 73 | + |
| 74 | + @pytest.mark.parametrize( |
| 75 | + "flag, skipped_attr, note_fragment", |
| 76 | + [ |
| 77 | + ( |
| 78 | + "--keyring-only", |
| 79 | + "find_existing_files", |
| 80 | + "credentials.ini was not modified", |
| 81 | + ), |
| 82 | + ("--config-only", "has_sso_tokens", "SSO tokens were not modified"), |
| 83 | + ], |
| 84 | + ) |
| 85 | + def test_scoped_flags(self, runner, mock_deps, flag, skipped_attr, note_fragment): |
| 86 | + mock_creds, mock_keyring = mock_deps |
| 87 | + |
| 88 | + result = runner.invoke(logout, [flag, "--api-host", HOST]) |
| 89 | + |
| 90 | + assert result.exit_code == 0 |
| 91 | + # The skipped source should not have been touched |
| 92 | + target = mock_creds if hasattr(mock_creds, skipped_attr) else mock_keyring |
| 93 | + getattr(target, skipped_attr).assert_not_called() |
| 94 | + assert note_fragment in result.output |
| 95 | + |
| 96 | + @pytest.mark.parametrize( |
| 97 | + "env, expect_warning", |
| 98 | + [ |
| 99 | + ({"CLOUDSMITH_API_KEY": "secret"}, True), |
| 100 | + ({}, False), |
| 101 | + ], |
| 102 | + ) |
| 103 | + def test_env_api_key_warning(self, runner, mock_deps, env, expect_warning): |
| 104 | + with patch.dict(os.environ, env): |
| 105 | + result = runner.invoke(logout, ["--api-host", HOST]) |
| 106 | + |
| 107 | + assert result.exit_code == 0 |
| 108 | + if expect_warning: |
| 109 | + assert "unset CLOUDSMITH_API_KEY" in result.output |
| 110 | + else: |
| 111 | + assert "unset CLOUDSMITH_API_KEY" not in result.output |
| 112 | + |
| 113 | + def test_json_output(self, runner, mock_deps): |
| 114 | + """--output-format json emits structured JSON with expected keys.""" |
| 115 | + _, mock_keyring = mock_deps |
| 116 | + mock_keyring.delete_sso_tokens.return_value = True |
| 117 | + |
| 118 | + result = runner.invoke( |
| 119 | + logout, |
| 120 | + ["--output-format", "json", "--api-host", HOST], |
| 121 | + catch_exceptions=False, |
| 122 | + ) |
| 123 | + |
| 124 | + assert result.exit_code == 0 |
| 125 | + # Human messages go to stderr; extract the JSON line from stdout. |
| 126 | + json_line = [ |
| 127 | + line for line in result.output.splitlines() if line.startswith("{") |
| 128 | + ] |
| 129 | + assert json_line, f"No JSON found in output: {result.output!r}" |
| 130 | + payload = json.loads(json_line[0]) |
| 131 | + data = payload["data"] |
| 132 | + assert data["api_host"] == HOST |
| 133 | + assert "dry_run" in data |
| 134 | + sources = data["sources"] |
| 135 | + assert "credential_file" in sources |
| 136 | + assert "keyring" in sources |
| 137 | + assert "environment_api_key" in sources |
| 138 | + |
| 139 | + def test_keyring_delete_failure(self, runner, mock_deps): |
| 140 | + """When delete_sso_tokens returns False, report failure.""" |
| 141 | + _, mock_keyring = mock_deps |
| 142 | + mock_keyring.delete_sso_tokens.return_value = False |
| 143 | + |
| 144 | + result = runner.invoke(logout, ["--api-host", HOST]) |
| 145 | + |
| 146 | + assert result.exit_code == 0 |
| 147 | + assert "Failed to remove SSO tokens" in result.output |
0 commit comments