fix: refresh webapp profile after activation

This commit is contained in:
3252a8
2026-05-24 23:25:21 +03:00
parent 7cffb4667f
commit 85ecd644b8
7 changed files with 110 additions and 8 deletions
+43
View File
@@ -2,11 +2,23 @@ from types import SimpleNamespace
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, patch
import bot.app.web.subscription_webapp # noqa: F401
from bot.app.web.webapp import billing as billing_module
from bot.payment_providers.base import WebAppPaymentContext
from bot.payment_providers.yookassa import create_webapp_payment
class _SessionFactory:
def __call__(self):
return self
async def __aenter__(self):
return SimpleNamespace()
async def __aexit__(self, exc_type, exc, tb):
return False
class WebAppPaymentStatusTests(IsolatedAsyncioTestCase):
async def test_yookassa_pending_payment_refresh_processes_succeeded_provider_status(self):
payment = SimpleNamespace(
@@ -123,3 +135,34 @@ class WebAppPaymentStatusTests(IsolatedAsyncioTestCase):
yookassa_service.create_payment.await_args.kwargs["save_payment_method"],
False,
)
async def test_payment_status_invalidates_profile_cache_for_succeeded_payment(self):
settings = SimpleNamespace()
payment = SimpleNamespace(payment_id=42, user_id=1001, status="succeeded")
request = SimpleNamespace(
app={"settings": settings, "async_session_factory": _SessionFactory()},
match_info={"payment_id": "42"},
)
with (
patch.object(billing_module, "_require_user_id", return_value=1001),
patch.object(
billing_module.payment_dal,
"get_payment_by_db_id",
AsyncMock(return_value=payment),
),
patch.object(
billing_module,
"_refresh_yookassa_payment_status",
AsyncMock(return_value=payment),
),
patch.object(
billing_module,
"invalidate_webapp_user_caches",
AsyncMock(),
) as invalidate_cache,
):
response = await billing_module.payment_status_route(request)
invalidate_cache.assert_awaited_once_with(settings, 1001)
self.assertEqual(response.status, 200)
+41
View File
@@ -0,0 +1,41 @@
import json
from types import SimpleNamespace
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, patch
import bot.app.web.subscription_webapp # noqa: F401
from bot.app.web.webapp import account as account_module
class WebAppProfileCacheTests(IsolatedAsyncioTestCase):
async def test_me_route_fresh_bypasses_cache_and_invalidates_payload(self):
settings = SimpleNamespace(WEBAPP_ME_CACHE_TTL_SECONDS=60)
request = SimpleNamespace(app={"settings": settings}, query={"fresh": "1"})
with (
patch.object(account_module, "_require_user_id", return_value=42),
patch.object(
account_module,
"_invalidate_webapp_user_caches",
AsyncMock(),
) as invalidate_cache,
patch.object(
account_module,
"_build_user_payload",
AsyncMock(return_value={"user": {"id": 42}, "subscription": {"active": True}}),
) as build_payload,
patch.object(
account_module,
"webapp_cached_user_payload",
AsyncMock(),
) as cached_payload,
):
response = await account_module.me_route(request)
invalidate_cache.assert_awaited_once_with(settings, 42)
build_payload.assert_awaited_once_with(request, 42)
cached_payload.assert_not_awaited()
self.assertEqual(
json.loads(response.text),
{"ok": True, "user": {"id": 42}, "subscription": {"active": True}},
)