|
| 1 | +import base64 |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | +import tomlkit |
| 8 | +from typer.testing import CliRunner |
| 9 | + |
| 10 | +from taskbadger.cli_main import app |
| 11 | +from taskbadger.config import Config |
| 12 | +from taskbadger.mug import Badger, _local |
| 13 | +from taskbadger.sdk import _parse_token, init |
| 14 | + |
| 15 | + |
| 16 | +def _make_project_key(org="myorg", project="myproject", key="secret123"): |
| 17 | + return base64.b64encode(f"{org}/{project}/{key}".encode()).decode() |
| 18 | + |
| 19 | + |
| 20 | +# --- _parse_token tests --- |
| 21 | + |
| 22 | + |
| 23 | +class TestParseToken: |
| 24 | + def test_valid_project_key(self): |
| 25 | + token = _make_project_key("org1", "proj1", "apikey") |
| 26 | + result = _parse_token(token) |
| 27 | + assert result == ("org1", "proj1", "apikey") |
| 28 | + |
| 29 | + def test_legacy_key(self): |
| 30 | + result = _parse_token("some-legacy-api-key") |
| 31 | + assert result is None |
| 32 | + |
| 33 | + def test_invalid_base64(self): |
| 34 | + result = _parse_token("!!!not-base64!!!") |
| 35 | + assert result is None |
| 36 | + |
| 37 | + def test_base64_but_wrong_format_two_parts(self): |
| 38 | + token = base64.b64encode(b"only/two").decode() |
| 39 | + result = _parse_token(token) |
| 40 | + assert result is None |
| 41 | + |
| 42 | + def test_base64_but_wrong_format_four_parts(self): |
| 43 | + token = base64.b64encode(b"a/b/c/d").decode() |
| 44 | + result = _parse_token(token) |
| 45 | + assert result is None |
| 46 | + |
| 47 | + def test_base64_with_empty_parts(self): |
| 48 | + token = base64.b64encode(b"org//key").decode() |
| 49 | + result = _parse_token(token) |
| 50 | + assert result is None |
| 51 | + |
| 52 | + def test_empty_string(self): |
| 53 | + result = _parse_token("") |
| 54 | + assert result is None |
| 55 | + |
| 56 | + |
| 57 | +# --- init() tests --- |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture(autouse=True) |
| 61 | +def _reset_badger(): |
| 62 | + b_global = Badger.current |
| 63 | + _local.set(Badger()) |
| 64 | + yield |
| 65 | + _local.set(b_global) |
| 66 | + |
| 67 | + |
| 68 | +class TestInitWithProjectKey: |
| 69 | + def test_init_with_project_key(self): |
| 70 | + token = _make_project_key("org1", "proj1", "apikey") |
| 71 | + init(token=token) |
| 72 | + settings = Badger.current.settings |
| 73 | + assert settings.organization_slug == "org1" |
| 74 | + assert settings.project_slug == "proj1" |
| 75 | + assert settings.token == "apikey" |
| 76 | + |
| 77 | + def test_init_project_key_overrides_org_project(self): |
| 78 | + token = _make_project_key("org1", "proj1", "apikey") |
| 79 | + init(organization_slug="ignored", project_slug="ignored", token=token) |
| 80 | + settings = Badger.current.settings |
| 81 | + assert settings.organization_slug == "org1" |
| 82 | + assert settings.project_slug == "proj1" |
| 83 | + assert settings.token == "apikey" |
| 84 | + |
| 85 | + def test_init_project_key_via_env(self): |
| 86 | + token = _make_project_key("org1", "proj1", "apikey") |
| 87 | + with mock.patch.dict(os.environ, {"TASKBADGER_API_KEY": token}): |
| 88 | + init() |
| 89 | + settings = Badger.current.settings |
| 90 | + assert settings.organization_slug == "org1" |
| 91 | + assert settings.project_slug == "proj1" |
| 92 | + assert settings.token == "apikey" |
| 93 | + |
| 94 | + def test_init_project_key_no_deprecation_warning(self, recwarn): |
| 95 | + token = _make_project_key() |
| 96 | + init(token=token) |
| 97 | + deprecation_warnings = [w for w in recwarn if issubclass(w.category, DeprecationWarning)] |
| 98 | + assert len(deprecation_warnings) == 0 |
| 99 | + |
| 100 | + def test_init_legacy_key_emits_deprecation_warning(self): |
| 101 | + with pytest.warns(DeprecationWarning, match="Legacy API keys are deprecated"): |
| 102 | + init("org", "project", "legacy-token") |
| 103 | + |
| 104 | + def test_init_legacy_key_still_works(self): |
| 105 | + with pytest.warns(DeprecationWarning): |
| 106 | + init("org", "project", "legacy-token") |
| 107 | + settings = Badger.current.settings |
| 108 | + assert settings.organization_slug == "org" |
| 109 | + assert settings.project_slug == "project" |
| 110 | + assert settings.token == "legacy-token" |
| 111 | + |
| 112 | + |
| 113 | +# --- Config.from_dict tests --- |
| 114 | + |
| 115 | + |
| 116 | +class TestConfigFromDictWithProjectKey: |
| 117 | + def test_project_key_in_config(self): |
| 118 | + token = _make_project_key("org1", "proj1", "apikey") |
| 119 | + config = Config.from_dict({"auth": {"token": token}}) |
| 120 | + assert config.organization_slug == "org1" |
| 121 | + assert config.project_slug == "proj1" |
| 122 | + # Token remains as original base64 string (decoded by _init at init time) |
| 123 | + assert config.token == token |
| 124 | + |
| 125 | + def test_project_key_overrides_config_org_project(self): |
| 126 | + token = _make_project_key("org1", "proj1", "apikey") |
| 127 | + config = Config.from_dict( |
| 128 | + { |
| 129 | + "auth": {"token": token}, |
| 130 | + "defaults": {"org": "old-org", "project": "old-project"}, |
| 131 | + } |
| 132 | + ) |
| 133 | + assert config.organization_slug == "org1" |
| 134 | + assert config.project_slug == "proj1" |
| 135 | + |
| 136 | + def test_project_key_via_env(self): |
| 137 | + token = _make_project_key("org1", "proj1", "apikey") |
| 138 | + with mock.patch.dict(os.environ, {"TASKBADGER_API_KEY": token}): |
| 139 | + config = Config.from_dict({}) |
| 140 | + assert config.organization_slug == "org1" |
| 141 | + assert config.project_slug == "proj1" |
| 142 | + assert config.token == token |
| 143 | + |
| 144 | + def test_project_key_is_valid(self): |
| 145 | + token = _make_project_key("org1", "proj1", "apikey") |
| 146 | + config = Config.from_dict({"auth": {"token": token}}) |
| 147 | + assert config.is_valid() |
| 148 | + |
| 149 | + |
| 150 | +# --- CLI configure tests --- |
| 151 | + |
| 152 | +runner = CliRunner() |
| 153 | + |
| 154 | + |
| 155 | +@pytest.fixture() |
| 156 | +def mock_config_location(): |
| 157 | + config_path = Path(__file__).parent / "_mock_config_project_key" |
| 158 | + with mock.patch("taskbadger.config._get_config_path", return_value=config_path): |
| 159 | + yield config_path |
| 160 | + if config_path.exists(): |
| 161 | + os.remove(config_path) |
| 162 | + |
| 163 | + |
| 164 | +class TestCLIConfigureProjectKey: |
| 165 | + def test_configure_with_project_key(self, mock_config_location): |
| 166 | + token = _make_project_key("myorg", "myproj", "mykey") |
| 167 | + result = runner.invoke(app, ["configure"], input=f"{token}\n") |
| 168 | + assert result.exit_code == 0 |
| 169 | + assert "Project key detected" in result.stdout |
| 170 | + assert "myorg" in result.stdout |
| 171 | + assert "myproj" in result.stdout |
| 172 | + |
| 173 | + with mock_config_location.open("rt", encoding="utf-8") as fp: |
| 174 | + raw_config = tomlkit.load(fp) |
| 175 | + config_dict = raw_config.unwrap() |
| 176 | + assert config_dict["defaults"]["org"] == "myorg" |
| 177 | + assert config_dict["defaults"]["project"] == "myproj" |
| 178 | + assert config_dict["auth"]["token"] == token |
| 179 | + |
| 180 | + def test_configure_with_legacy_key(self, mock_config_location): |
| 181 | + result = runner.invoke(app, ["configure"], input="a-token\nan-org\na-project\n") |
| 182 | + assert result.exit_code == 0 |
| 183 | + assert "Project key detected" not in result.stdout |
| 184 | + |
| 185 | + with mock_config_location.open("rt", encoding="utf-8") as fp: |
| 186 | + raw_config = tomlkit.load(fp) |
| 187 | + config_dict = raw_config.unwrap() |
| 188 | + assert config_dict["defaults"]["org"] == "an-org" |
| 189 | + assert config_dict["defaults"]["project"] == "a-project" |
| 190 | + assert config_dict["auth"]["token"] == "a-token" |
0 commit comments