fix: assign default tariff to referral welcome bonuses

This commit is contained in:
3252a8
2026-06-07 22:28:30 +03:00
parent d5b23d8306
commit 5cee619dd0
4 changed files with 95 additions and 0 deletions
+5
View File
@@ -1497,6 +1497,10 @@ async def _grant_referral_welcome_bonus_if_eligible(
return None return None
subscription_service: SubscriptionService = request.app["subscription_service"] subscription_service: SubscriptionService = request.app["subscription_service"]
default_tariff_key = None
tariffs_config = getattr(settings, "tariffs_config", None)
if tariffs_config:
default_tariff_key = getattr(tariffs_config, "default_tariff", None)
try: try:
if await subscription_service.has_active_subscription(session, int(user.user_id)): if await subscription_service.has_active_subscription(session, int(user.user_id)):
return None return None
@@ -1508,6 +1512,7 @@ async def _grant_referral_welcome_bonus_if_eligible(
int(user.user_id), int(user.user_id),
referral_welcome_days, referral_welcome_days,
reason="referral_welcome_bonus", reason="referral_welcome_bonus",
tariff_key=default_tariff_key,
) )
+5
View File
@@ -658,12 +658,17 @@ async def start_command_handler(
) )
if referred_by_user_id and referral_welcome_days > 0: if referred_by_user_id and referral_welcome_days > 0:
try: try:
default_tariff_key = None
tariffs_config = getattr(settings, "tariffs_config", None)
if tariffs_config:
default_tariff_key = getattr(tariffs_config, "default_tariff", None)
referral_bonus_end_date = ( referral_bonus_end_date = (
await subscription_service.extend_active_subscription_days( await subscription_service.extend_active_subscription_days(
session, session,
user_id, user_id,
referral_welcome_days, referral_welcome_days,
reason="referral_welcome_bonus", reason="referral_welcome_bonus",
tariff_key=default_tariff_key,
) )
) )
if referral_bonus_end_date: if referral_bonus_end_date:
@@ -0,0 +1,83 @@
from datetime import datetime, timezone
from types import SimpleNamespace
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, Mock, patch
from bot.handlers.user.start import start_command_handler
class StartReferralWelcomeBonusTests(IsolatedAsyncioTestCase):
async def test_start_referral_welcome_bonus_passes_default_tariff(self):
end_date = datetime(2026, 1, 9, tzinfo=timezone.utc)
settings = SimpleNamespace(
DEFAULT_LANGUAGE="en",
ADMIN_IDS=[],
DISABLE_WELCOME_MESSAGE=False,
REFERRAL_WELCOME_BONUS_DAYS=3,
tariffs_config=SimpleNamespace(default_tariff="standard"),
)
i18n = SimpleNamespace(gettext=lambda lang, key, **kw: key)
subscription_service = SimpleNamespace(
extend_active_subscription_days=AsyncMock(return_value=end_date)
)
session = AsyncMock()
message = SimpleNamespace(
from_user=SimpleNamespace(
id=42,
username="alice",
first_name="Alice",
last_name="Example",
full_name="Alice Example",
),
bot=AsyncMock(),
answer=AsyncMock(),
)
state = SimpleNamespace(clear=AsyncMock())
ref_match = Mock()
ref_match.group.return_value = "ABC123"
created_user = SimpleNamespace(user_id=42, referred_by_id=7)
with (
patch(
"bot.handlers.user.start._resolve_referrer_from_start_ref",
AsyncMock(return_value=7),
),
patch(
"bot.handlers.user.start.user_dal.get_user_by_id",
AsyncMock(return_value=None),
),
patch(
"bot.handlers.user.start.user_dal.create_user",
AsyncMock(return_value=(created_user, True)),
),
patch(
"bot.handlers.user.start.ensure_required_channel_subscription",
AsyncMock(return_value=True),
),
patch(
"bot.handlers.user.start.send_main_menu",
AsyncMock(),
),
patch(
"bot.services.notification_service.NotificationService",
return_value=SimpleNamespace(notify_new_user_registration=AsyncMock()),
),
):
await start_command_handler(
message=message,
state=state,
settings=settings,
i18n_data={"current_language": "en", "i18n_instance": i18n},
subscription_service=subscription_service,
referral_service=AsyncMock(),
session=session,
ref_match=ref_match,
)
subscription_service.extend_active_subscription_days.assert_awaited_once_with(
session,
42,
3,
reason="referral_welcome_bonus",
tariff_key="standard",
)
@@ -45,6 +45,7 @@ class WebAppReferralWelcomeBonusTests(IsolatedAsyncioTestCase):
REFERRAL_WELCOME_BONUS_DAYS=3, REFERRAL_WELCOME_BONUS_DAYS=3,
REFERRAL_WELCOME_BONUS_WITHOUT_TELEGRAM_ENABLED=True, REFERRAL_WELCOME_BONUS_WITHOUT_TELEGRAM_ENABLED=True,
DISPOSABLE_EMAIL_DOMAINS="mailinator.com", DISPOSABLE_EMAIL_DOMAINS="mailinator.com",
tariffs_config=SimpleNamespace(default_tariff="standard"),
) )
user = SimpleNamespace( user = SimpleNamespace(
user_id=42, user_id=42,
@@ -75,4 +76,5 @@ class WebAppReferralWelcomeBonusTests(IsolatedAsyncioTestCase):
42, 42,
3, 3,
reason="referral_welcome_bonus", reason="referral_welcome_bonus",
tariff_key="standard",
) )