Co-authored-by: Clément Drouin <clement.drouin@mistral.ai>
Co-authored-by: Clément Sirieix <clement.sirieix@mistral.ai>
Co-authored-by: Cyprien <courtot.c@gmail.com>
Co-authored-by: Guillaume LE GOFF <guillaume.lgf@gmail.com>
Co-authored-by: Jean Burellier <sheplu@users.noreply.github.com>
Co-authored-by: Kim-Adeline Miguel <51720070+kimadeline@users.noreply.github.com>
Co-authored-by: Mathias Gesbert <mathias.gesbert@mistral.ai>
Co-authored-by: Nelson PROIA <144663685+Nelson-PROIA@users.noreply.github.com>
Co-authored-by: Paul VEZIA <166131032+le-codeur-rapide@users.noreply.github.com>
Co-authored-by: Pierre Rossinès <pierre.rossines@mistral.ai>
Co-authored-by: Quentin <quentin.torroba@mistral.ai>
Co-authored-by: Vincent G <10739306+VinceOPS@users.noreply.github.com>
Co-authored-by: josephine-delas <57808586+josephine-delas@users.noreply.github.com>
Co-authored-by: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
Laure Hugo 2026-06-25 16:08:45 +02:00 committed by GitHub
parent 725d3a56ce
commit e607ccbb00
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
242 changed files with 7372 additions and 1974 deletions

View file

@ -7,7 +7,7 @@ import keyring
from keyring.errors import KeyringError, NoKeyringError, PasswordDeleteError
import pytest
from vibe.core.config import ProviderConfig
from vibe.core.config import ProviderConfig, resolve_api_key
from vibe.core.paths import GLOBAL_ENV_FILE
from vibe.core.types import Backend
from vibe.setup.auth.api_key_persistence import persist_api_key, remove_api_key
@ -45,6 +45,22 @@ def test_persist_stores_in_keyring_and_clears_stale_env(
assert "CUSTOM_API_KEY" not in dotenv_values(GLOBAL_ENV_FILE.path)
def test_persist_updates_cached_keyring_value(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("CUSTOM_API_KEY", raising=False)
monkeypatch.setattr(keyring, "get_password", lambda service, username: "old-key")
monkeypatch.setattr(
keyring, "set_password", lambda service, username, password: None
)
assert resolve_api_key("CUSTOM_API_KEY") == "old-key"
result = persist_api_key(_provider(), "new-key")
monkeypatch.delenv("CUSTOM_API_KEY", raising=False)
assert result == "completed"
assert resolve_api_key("CUSTOM_API_KEY") == "new-key"
def test_persist_falls_back_to_env_when_keyring_unavailable(
monkeypatch: pytest.MonkeyPatch,
) -> None:
@ -62,6 +78,27 @@ def test_persist_falls_back_to_env_when_keyring_unavailable(
assert dotenv_values(GLOBAL_ENV_FILE.path)["CUSTOM_API_KEY"] == "new-key"
def test_persist_fallback_clears_stale_cached_keyring_value(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.delenv("CUSTOM_API_KEY", raising=False)
monkeypatch.setattr(keyring, "get_password", lambda service, username: "old-key")
def _unavailable(service: str, username: str, password: str) -> None:
raise KeyringError("no keyring")
assert resolve_api_key("CUSTOM_API_KEY") == "old-key"
monkeypatch.setattr(keyring, "set_password", _unavailable)
monkeypatch.setattr(keyring, "get_password", lambda service, username: None)
result = persist_api_key(_provider(), "new-key")
monkeypatch.delenv("CUSTOM_API_KEY", raising=False)
assert result == "completed"
assert resolve_api_key("CUSTOM_API_KEY") is None
def test_persist_returns_env_var_error_for_empty_env_var(
monkeypatch: pytest.MonkeyPatch,
) -> None:
@ -78,21 +115,39 @@ def test_persist_returns_env_var_error_for_empty_env_var(
def test_remove_deletes_keyring_env_and_process_env(
monkeypatch: pytest.MonkeyPatch,
) -> None:
deleted: list[str] = []
deleted: list[tuple[str, str]] = []
monkeypatch.setenv("CUSTOM_API_KEY", "live-key")
monkeypatch.setattr(
keyring, "delete_password", lambda service, username: deleted.append(username)
keyring,
"delete_password",
lambda service, username: deleted.append((service, username)),
)
GLOBAL_ENV_FILE.path.parent.mkdir(parents=True, exist_ok=True)
set_key(GLOBAL_ENV_FILE.path, "CUSTOM_API_KEY", "file-key")
remove_api_key(_provider())
assert deleted == ["CUSTOM_API_KEY"]
assert deleted == [
("ai.mistral.vibe", "CUSTOM_API_KEY"),
("vibe", "CUSTOM_API_KEY"),
]
assert "CUSTOM_API_KEY" not in dotenv_values(GLOBAL_ENV_FILE.path)
assert "CUSTOM_API_KEY" not in os.environ
def test_remove_clears_cached_keyring_value(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("CUSTOM_API_KEY", raising=False)
monkeypatch.setattr(keyring, "get_password", lambda service, username: "live-key")
monkeypatch.setattr(keyring, "delete_password", lambda service, username: None)
assert resolve_api_key("CUSTOM_API_KEY") == "live-key"
monkeypatch.setattr(keyring, "get_password", lambda service, username: None)
remove_api_key(_provider())
assert resolve_api_key("CUSTOM_API_KEY") is None
def test_remove_ignores_keyring_unavailable_and_still_clears_env(
monkeypatch: pytest.MonkeyPatch,
) -> None:
@ -117,7 +172,7 @@ def test_remove_ignores_missing_keyring_entry_and_still_clears_env(
monkeypatch.setenv("CUSTOM_API_KEY", "live-key")
def _missing(service: str, username: str) -> None:
raise PasswordDeleteError("not found")
raise PasswordDeleteError()
monkeypatch.setattr(keyring, "delete_password", _missing)
GLOBAL_ENV_FILE.path.parent.mkdir(parents=True, exist_ok=True)