refactor: move cryptopay env-config into module

This commit is contained in:
3252a8
2026-05-18 16:32:19 +03:00
parent 1e93d25a40
commit cceba2e98e
12 changed files with 226 additions and 99 deletions
+44
View File
@@ -0,0 +1,44 @@
"""Test fixtures that isolate the suite from the developer's local .env.
Provider configs now declare their own ``BaseSettings`` with ``env_file=".env"``,
so a developer who runs ``pytest`` from a project that has real credentials in
``.env`` would otherwise see provider services try to connect (e.g. CryptoPay
spinning up an aiohttp session in __init__).
We set ``PROVIDER_ENV_FILE=""`` (consumed by each provider's
``ProviderEnvConfig.model_config["env_file"]`` factory) and strip real
provider env vars from the test process.
"""
from __future__ import annotations
import os
import pytest
@pytest.fixture(autouse=True)
def _isolate_provider_env(monkeypatch):
monkeypatch.setenv("PROVIDER_ENV_FILE", "")
for key in list(os.environ.keys()):
if any(
key.startswith(prefix)
for prefix in (
"FREEKASSA_",
"PLATEGA_",
"SEVERPAY_",
"WATA_",
"HELEKET_",
"CRYPTOPAY_",
"YOOKASSA_",
"PAYMENT_FREEKASSA_",
"PAYMENT_PLATEGA_",
"PAYMENT_SEVERPAY_",
"PAYMENT_WATA_",
"PAYMENT_HELEKET_",
"PAYMENT_CRYPTOPAY_",
"PAYMENT_YOOKASSA_",
"PAYMENT_STARS_",
)
):
monkeypatch.delenv(key, raising=False)
+18 -1
View File
@@ -13,11 +13,12 @@ silently swallows the wiring step.
"""
import json
import os
import tempfile
import unittest
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
from bot.app.factories.build_services import build_core_services
from bot.payment_providers.yookassa import YooKassaService
@@ -26,6 +27,22 @@ from bot.services.subscription_service import SubscriptionService
from config.settings import Settings
# Strip all provider env so per-provider BaseSettings models don't pick up
# real credentials from the local .env file during tests.
_PROVIDER_ENV_PREFIXES = (
"FREEKASSA_", "PLATEGA_", "SEVERPAY_", "WATA_", "HELEKET_",
"CRYPTOPAY_", "YOOKASSA_", "STARS_",
)
def _clean_env() -> dict[str, str]:
return {
k: v for k, v in os.environ.items()
if not any(k.startswith(p) for p in _PROVIDER_ENV_PREFIXES)
and not k.startswith("PAYMENT_")
}
def _make_settings(tmpdir: str, **overrides: Any) -> Settings:
config_path = Path(tmpdir) / "tariffs.json"
config_path.write_text(
+15 -3
View File
@@ -332,15 +332,27 @@ class WebAppAssetTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(plans[1]["stars_price"], 2500)
def test_serialize_payment_methods_respects_runtime_provider_toggles(self):
# Provider toggles now live in per-provider BaseSettings models. Disable
# all providers by giving each one an empty bundle (default ENABLED is
# False for everything except cryptopay/yookassa, so override those too).
from bot.payment_providers import (
build_provider_configs,
current_provider_configs,
)
build_provider_configs()
configs = current_provider_configs()
for service_key in ("cryptopay_service", "yookassa_service"):
bundle = configs.get(service_key)
if bundle and bundle.config is not None and hasattr(bundle.config, "ENABLED"):
bundle.config.ENABLED = False
settings = Settings(
_env_file=None,
BOT_TOKEN="token",
POSTGRES_USER="app_user",
POSTGRES_PASSWORD="app_password",
TARIFFS_CONFIG_PATH="missing-tariffs.json",
CRYPTOPAY_ENABLED=False,
FREEKASSA_ENABLED=False,
SEVERPAY_ENABLED=False,
YOOKASSA_ENABLED=False,
PLATEGA_ENABLED=False,
STARS_ENABLED=False,