feat: group referral bonus display by tariff

This commit is contained in:
3252a8
2026-05-29 22:55:01 +03:00
parent 6803c7801f
commit 7fe8e676cd
8 changed files with 511 additions and 60 deletions
+93
View File
@@ -20,6 +20,7 @@ from bot.keyboards.inline.user_keyboards import (
payment_methods_back_callback,
payment_options_back_callback,
)
from config.tariffs_config import TariffsConfig
class JsonI18nStub:
@@ -304,3 +305,95 @@ class UserBotMenuTests(unittest.TestCase):
self.assertLess(text.index("Telegram link:"), text.index("Web link:"))
self.assertLess(text.index("Web link:"), text.index("Invitation bonuses:"))
def test_single_tariff_referral_text_uses_period_rows_without_tariff_name(self):
settings = SimpleNamespace(
tariffs_config=TariffsConfig.model_validate(
{
"default_tariff": "standard",
"tariffs": [
{
"key": "standard",
"names": {"en": "Standard"},
"descriptions": {},
"squad_uuids": ["standard"],
"billing_model": "period",
"monthly_gb": 100,
"prices_rub": {"2": 400, "4": 800},
"prices_stars": {},
"referral_bonus_days_inviter": {"2": 5, "4": 10},
"referral_bonus_days_referee": {"2": 1, "4": 2},
"enabled_periods": [2, 4],
"enabled": True,
}
],
}
),
subscription_options={},
referral_bonus_inviter={},
referral_bonus_referee={},
)
text = referral._build_referral_bonus_details_text(
settings,
lambda key, **kwargs: self.i18n.gettext("en", key, **kwargs),
"en",
)
self.assertIn("For a friend's 2-month subscription", text)
self.assertIn("For a friend's 4-month subscription", text)
self.assertNotIn("Standard", text)
def test_multiple_tariff_referral_text_uses_tariff_ranges(self):
settings = SimpleNamespace(
tariffs_config=TariffsConfig.model_validate(
{
"default_tariff": "standard",
"tariffs": [
{
"key": "standard",
"names": {"en": "Standard"},
"descriptions": {},
"squad_uuids": ["standard"],
"billing_model": "period",
"monthly_gb": 100,
"prices_rub": {"2": 400, "4": 800},
"prices_stars": {},
"referral_bonus_days_inviter": {"2": 5, "4": 10},
"referral_bonus_days_referee": {"2": 1, "4": 2},
"enabled_periods": [2, 4],
"enabled": True,
},
{
"key": "premium",
"names": {"en": "Premium"},
"descriptions": {},
"squad_uuids": ["premium"],
"billing_model": "period",
"monthly_gb": 500,
"prices_rub": {"1": 700, "3": 1800},
"prices_stars": {},
"referral_bonus_days_inviter": {"1": 8, "3": 24},
"referral_bonus_days_referee": {"1": 3, "3": 9},
"enabled_periods": [1, 3],
"enabled": True,
},
],
}
),
subscription_options={},
referral_bonus_inviter={},
referral_bonus_referee={},
)
text = referral._build_referral_bonus_details_text(
settings,
lambda key, **kwargs: self.i18n.gettext("en", key, **kwargs),
"en",
)
self.assertIn("Standard", text)
self.assertIn("Premium", text)
self.assertIn("from 5 to 10 days", text)
self.assertIn("from 8 to 24 days", text)
self.assertNotIn("2-month subscription", text)
+69 -4
View File
@@ -141,7 +141,7 @@ class WebAppAssetTests(unittest.IsolatedAsyncioTestCase):
"tariff_key": "standard",
"tariff_name": "Standard",
"months": 2,
"title": "Standard - 2 months",
"title": "2 months",
"inviter_days": 5,
"friend_days": 1,
},
@@ -150,7 +150,7 @@ class WebAppAssetTests(unittest.IsolatedAsyncioTestCase):
"tariff_key": "standard",
"tariff_name": "Standard",
"months": 4,
"title": "Standard - 4 months",
"title": "4 months",
"inviter_days": 10,
"friend_days": 2,
},
@@ -159,7 +159,7 @@ class WebAppAssetTests(unittest.IsolatedAsyncioTestCase):
"tariff_key": "standard",
"tariff_name": "Standard",
"months": 8,
"title": "Standard - 8 months",
"title": "8 months",
"inviter_days": 0,
"friend_days": 4,
},
@@ -168,13 +168,78 @@ class WebAppAssetTests(unittest.IsolatedAsyncioTestCase):
"tariff_key": "standard",
"tariff_name": "Standard",
"months": 16,
"title": "Standard - 16 months",
"title": "16 months",
"inviter_days": 40,
"friend_days": 0,
},
],
)
def test_referral_bonus_details_group_multiple_tariffs(self):
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / "tariffs.json"
path.write_text(
json.dumps(
{
"default_tariff": "standard",
"tariffs": [
{
"key": "standard",
"names": {"en": "Standard"},
"descriptions": {},
"squad_uuids": ["standard"],
"billing_model": "period",
"monthly_gb": 100,
"prices_rub": {"2": 400, "4": 800},
"prices_stars": {},
"referral_bonus_days_inviter": {"2": 5, "4": 10},
"referral_bonus_days_referee": {"2": 1, "4": 2},
"enabled_periods": [2, 4],
"enabled": True,
},
{
"key": "premium",
"names": {"en": "Premium"},
"descriptions": {},
"squad_uuids": ["premium"],
"billing_model": "period",
"monthly_gb": 500,
"prices_rub": {"1": 700, "3": 1800},
"prices_stars": {},
"referral_bonus_days_inviter": {"1": 8, "3": 24},
"referral_bonus_days_referee": {"1": 3, "3": 9},
"enabled_periods": [1, 3],
"enabled": True,
},
],
}
),
encoding="utf-8",
)
settings = Settings(
_env_file=None,
BOT_TOKEN="token",
POSTGRES_USER="app_user",
POSTGRES_PASSWORD="app_password",
TARIFFS_CONFIG_PATH=str(path),
)
details = subscription_webapp._serialize_referral_bonus_details(settings, "en")
self.assertEqual([item["type"] for item in details], ["tariff_summary", "tariff_summary"])
self.assertEqual(details[0]["title"], "Standard")
self.assertEqual(details[0]["inviter_min_days"], 5)
self.assertEqual(details[0]["inviter_max_days"], 10)
self.assertEqual(details[0]["friend_min_days"], 1)
self.assertEqual(details[0]["friend_max_days"], 2)
self.assertEqual(
[item["title"] for item in details[0]["details"]],
["2 months", "4 months"],
)
self.assertEqual(details[1]["title"], "Premium")
self.assertEqual(details[1]["inviter_min_days"], 8)
self.assertEqual(details[1]["inviter_max_days"], 24)
def test_subscription_template_does_not_block_on_telegram_sdk(self):
html = subscription_webapp.TEMPLATE_PATH.read_text(encoding="utf-8")