from pathlib import Path import pytest from apple_team_cli.config import ConfigError, Settings, write_config @pytest.fixture() def fake_key(tmp_path: Path) -> Path: key = tmp_path / "AuthKey_ABCDE12345.p8" key.write_text("-----BEGIN PRIVATE KEY-----\nnot-a-real-key\n", encoding="utf-8") key.chmod(0o600) return key def test_write_and_load_config( fake_key: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: config_path = tmp_path / ".env" write_config( "11111111-2222-4333-8444-555555555555", "ABCDE12345", fake_key, config_path, ) for name in ("ASC_ISSUER_ID", "ASC_KEY_ID", "ASC_PRIVATE_KEY_PATH", "ASC_BASE_URL"): monkeypatch.delenv(name, raising=False) monkeypatch.chdir(tmp_path) settings = Settings.load(tmp_path / "missing.toml") assert settings.key_id == "ABCDE12345" assert settings.private_key_path == fake_key assert settings.key_permissions_are_private assert config_path.stat().st_mode & 0o777 == 0o600 contents = config_path.read_text(encoding="utf-8") assert "not-a-real-key" not in contents assert 'ASC_ISSUER_ID="11111111-2222-4333-8444-555555555555"' in contents assert 'ASC_KEY_ID="ABCDE12345"' in contents assert f'ASC_PRIVATE_KEY_PATH="{fake_key}"' in contents def test_missing_configuration_is_actionable( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.chdir(tmp_path) for name in ("ASC_ISSUER_ID", "ASC_KEY_ID", "ASC_PRIVATE_KEY_PATH"): monkeypatch.delenv(name, raising=False) with pytest.raises(ConfigError, match="apple-team configure"): Settings.load(tmp_path / "missing.toml") def test_rejects_non_p8_file(tmp_path: Path) -> None: key = tmp_path / "bad.p8" key.write_text("secret", encoding="utf-8") settings = Settings( "11111111-2222-4333-8444-555555555555", "ABCDE12345", key ) with pytest.raises(ConfigError, match="not an App Store Connect"): settings.validate() def test_secure_private_key_removes_group_and_world_access(fake_key: Path) -> None: fake_key.chmod(0o644) settings = Settings( "11111111-2222-4333-8444-555555555555", "ABCDE12345", fake_key ) settings.secure_private_key() assert fake_key.stat().st_mode & 0o777 == 0o600 assert settings.key_permissions_are_private