Files
chronicle.nixg.ru/telegram-archiver/tests/test_config.py
T

74 lines
2.5 KiB
Python

"""Tests for configuration."""
import pytest
import os
from pathlib import Path
from config import Settings
class TestSettings:
"""Test Settings model."""
def test_settings_from_env(self, monkeypatch):
"""Test loading settings from environment."""
monkeypatch.setenv("API_ID", "12345678")
monkeypatch.setenv("API_HASH", "abcdef1234567890")
monkeypatch.setenv("PHONE", "+79991234567")
monkeypatch.setenv("MAX_FILE_SIZE", "104857600")
settings = Settings()
assert settings.api_id == 12345678
assert settings.api_hash == "abcdef1234567890"
assert settings.phone == "+79991234567"
assert settings.max_file_size == 104857600
def test_settings_default_values(self, monkeypatch):
"""Test default setting values."""
monkeypatch.setenv("API_ID", "12345678")
monkeypatch.setenv("API_HASH", "abcdef1234567890")
monkeypatch.setenv("PHONE", "+79991234567")
settings = Settings()
assert settings.session_name == "telegram-archiver"
assert settings.max_file_size == 209715200 # 200 MB default
assert settings.log_level == "INFO"
assert settings.otel_enabled is False
def test_settings_session_path(self, monkeypatch):
"""Test session_path property."""
monkeypatch.setenv("API_ID", "12345678")
monkeypatch.setenv("API_HASH", "abcdef1234567890")
monkeypatch.setenv("PHONE", "+79991234567")
monkeypatch.setenv("SESSION_NAME", "test_session")
settings = Settings()
session_path = settings.session_path
assert session_path.name == "test_session.session"
assert session_path.suffix == ".session"
def test_settings_validate_credentials_invalid(self, monkeypatch):
"""Test credential validation."""
monkeypatch.setenv("API_ID", "0") # Invalid
monkeypatch.setenv("API_HASH", "abcdef1234567890")
monkeypatch.setenv("PHONE", "+79991234567")
settings = Settings()
with pytest.raises(ValueError, match="API_ID must be a positive integer"):
settings.validate_credentials()
def test_settings_validate_phone_format(self, monkeypatch):
"""Test phone number format validation."""
monkeypatch.setenv("API_ID", "12345678")
monkeypatch.setenv("API_HASH", "abcdef1234567890")
monkeypatch.setenv("PHONE", "79991234567") # Missing +
settings = Settings()
with pytest.raises(ValueError, match="PHONE must start with +"):
settings.validate_credentials()