voice-dictation/tests/unit/test_credentials.py
2026-10-20 12:07:33 +03:00

34 lines
1.1 KiB
Python

"""Private profile credentials must not leak into preferences or other profiles."""
from __future__ import annotations
import os
from pathlib import Path
import pytest
from voice_dictation.infrastructure.credentials import read_api_key, save_api_key
def test_key_roundtrip_private_and_isolated(tmp_path: Path) -> None:
beta = tmp_path / "beta"
save_api_key(beta, "test-key-not-real")
assert read_api_key(beta) == "test-key-not-real"
assert read_api_key(tmp_path / "main") == ""
if os.name == "posix":
assert (beta / "credentials.json").stat().st_mode & 0o777 == 0o600
save_api_key(beta, "replacement-not-real")
assert read_api_key(beta) == "replacement-not-real"
assert not list(beta.glob(".credentials-*"))
def test_invalid_key_preserves_previous(tmp_path: Path) -> None:
save_api_key(tmp_path, "test-key-not-real")
with pytest.raises(ValueError):
save_api_key(tmp_path, "bad key")
assert read_api_key(tmp_path) == "test-key-not-real"
def test_invalid_file_does_not_crash(tmp_path: Path) -> None:
(tmp_path / "credentials.json").write_text("[]")
assert read_api_key(tmp_path) == ""