feat: add multi-user support using contextvars

This commit is contained in:
Alex
2026-06-25 01:02:14 +03:00
parent ce2ba35d5d
commit 27b0edcc48
4 changed files with 133 additions and 73 deletions
+13 -7
View File
@@ -5,12 +5,17 @@ from pathlib import Path
import pytest
from miband_tracker.config import ConfigError, Settings, parse_single_user_id
from miband_tracker.config import ConfigError, Settings, parse_user_ids
def test_parse_single_user_id_rejects_multiple_values() -> None:
def test_parse_user_ids_accepts_multiple_values() -> None:
assert parse_user_ids("1,2", required=True) == [1, 2]
assert parse_user_ids("1", required=True) == [1]
def test_parse_user_ids_rejects_invalid_values() -> None:
with pytest.raises(ConfigError):
parse_single_user_id("1,2", required=True)
parse_user_ids("1,invalid", required=True)
def test_settings_user_paths_prefer_existing_user_files(tmp_path: Path) -> None:
@@ -25,12 +30,13 @@ def test_settings_user_paths_prefer_existing_user_files(tmp_path: Path) -> None:
status_path=tmp_path / "status.json",
bot_state_db_path=tmp_path / "fitness_bot_state.db",
telegram_bot_token="token",
telegram_allowed_user_id=user_id,
telegram_allowed_user_ids=[user_id],
sync_interval=900,
query_duration=2,
enable_fds_sleep_details=True,
)
assert settings.telegram_allowed_user_id == user_id
assert settings.user_db_path() == tmp_path / f"miband_{user_id}.db"
assert settings.user_status_path() == tmp_path / f"status_{user_id}.json"
assert settings.token_path() == tmp_path / f"token_{user_id}.json"
@@ -43,7 +49,7 @@ def test_settings_falls_back_to_legacy_db_and_status(tmp_path: Path) -> None:
status_path=tmp_path / "status.json",
bot_state_db_path=tmp_path / "fitness_bot_state.db",
telegram_bot_token="token",
telegram_allowed_user_id=123,
telegram_allowed_user_ids=[123],
sync_interval=900,
query_duration=2,
enable_fds_sleep_details=True,
@@ -55,7 +61,7 @@ def test_settings_falls_back_to_legacy_db_and_status(tmp_path: Path) -> None:
def test_settings_from_env_rejects_invalid_interval(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("TELEGRAM_ALLOWED_USER_ID", "123")
monkeypatch.setenv("TELEGRAM_ALLOWED_USER_IDS", "123")
monkeypatch.setenv("SYNC_INTERVAL", "soon")
with pytest.raises(ConfigError, match="SYNC_INTERVAL"):
@@ -63,7 +69,7 @@ def test_settings_from_env_rejects_invalid_interval(monkeypatch: pytest.MonkeyPa
def test_settings_from_env_rejects_zero_query_duration(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("TELEGRAM_ALLOWED_USER_ID", "123")
monkeypatch.setenv("TELEGRAM_ALLOWED_USER_IDS", "123")
monkeypatch.setenv("QUERY_DURATION", "0")
with pytest.raises(ConfigError, match="QUERY_DURATION"):