Files
mywhoosh2garmin/tests/mywhoosh/test_tokenstore.py
Bastian Wagner 7c9e19ba0b errors
2026-08-15 20:31:57 +02:00

55 lines
1.9 KiB
Python

from pathlib import Path
from app.mywhoosh.models import MyWhooshToken
from app.mywhoosh.tokenstore import MyWhooshTokenStore
def test_tokenstore_round_trip_and_permissions(tmp_path: Path) -> None:
store = MyWhooshTokenStore(tmp_path / "tokens" / "7" / "mywhoosh.json")
token = MyWhooshToken(access_token="access", refresh_token="refresh", whoosh_id="whoosh-7")
store.save(token)
assert store.load() == token
assert oct(store.path.stat().st_mode & 0o777) == "0o600"
def test_missing_token_returns_none(tmp_path: Path) -> None:
store = MyWhooshTokenStore(tmp_path / "missing.json")
assert store.load() is None
def test_corrupt_token_file_returns_none(tmp_path: Path) -> None:
path = tmp_path / "mywhoosh.json"
path.write_bytes(b"not valid json {{{")
store = MyWhooshTokenStore(path)
assert store.load() is None
def test_token_file_missing_access_token_returns_none(tmp_path: Path) -> None:
path = tmp_path / "mywhoosh.json"
path.write_text('{"refresh_token": "r", "whoosh_id": "w"}', encoding="utf-8")
store = MyWhooshTokenStore(path)
assert store.load() is None
def test_clear_removes_token_and_load_returns_none(tmp_path: Path) -> None:
store = MyWhooshTokenStore(tmp_path / "tokens" / "mywhoosh.json")
token = MyWhooshToken(access_token="access", refresh_token="refresh", whoosh_id="whoosh-1")
store.save(token)
store.clear()
assert store.load() is None
assert not store.path.exists()
def test_get_or_create_device_id_persists_and_is_stable(tmp_path: Path) -> None:
path = tmp_path / "tokens" / "7" / "mywhoosh.json"
device_id = MyWhooshTokenStore(path).get_or_create_device_id()
reloaded_id = MyWhooshTokenStore(path).get_or_create_device_id()
assert reloaded_id == device_id
device_id_path = path.with_name("device_id")
assert device_id_path.read_text("utf-8").strip() == device_id