From 5f74192056ed2c33bf930d2f22f8c2fb417bb0a3 Mon Sep 17 00:00:00 2001 From: dam2452 <81230036+dam2452@users.noreply.github.com> Date: Thu, 9 Jan 2025 17:02:05 +0100 Subject: [PATCH] sub --- bot/database/common_messages.sql | 11 ++++++++++ bot/database/response_keys.py | 3 ++- bot/handlers/administration/start_handler.py | 1 + .../subscription_status_handler.py | 7 +++--- .../subscription_status_handler_responses.py | 19 ---------------- .../test_subscription_status.py | 22 +++++++++++-------- 6 files changed, 30 insertions(+), 33 deletions(-) diff --git a/bot/database/common_messages.sql b/bot/database/common_messages.sql index b8fc561f..3f96c180 100644 --- a/bot/database/common_messages.sql +++ b/bot/database/common_messages.sql @@ -178,3 +178,14 @@ INSERT INTO common_messages (handler_name, key, message) VALUES ('StartHandler', 'invalid_command_message', '❌ Niepoprawna komenda w menu startowym. Użyj /start, aby zobaczyć dostępne opcje. ❌'); +INSERT INTO common_messages (handler_name, key, message) VALUES +('SubscriptionStatusHandler', 'no_subscription', '🚫 Nie masz aktywnej subskrypcji.🚫'), +('SubscriptionStatusHandler', 'subscription_status', ' +✨ **Status Twojej subskrypcji** ✨ + +👤 **Użytkownik:** {} +📅 **Data zakończenia:** {} +⏳ **Pozostało dni:** {} + +Dzięki za wsparcie projektu! 🎉 +'); diff --git a/bot/database/response_keys.py b/bot/database/response_keys.py index 0e43e89b..041bab01 100644 --- a/bot/database/response_keys.py +++ b/bot/database/response_keys.py @@ -74,7 +74,8 @@ class ResponseKey(str, Enum): SUBSCRIPTIONS_MESSAGE = "subscriptions_message" SHORTCUTS_MESSAGE = "shortcuts_message" INVALID_COMMAND_MESSAGE = "invalid_command_message" - + NO_SUBSCRIPTION = "no_subscription" + SUBSCRIPTION_STATUS = "subscription_status" diff --git a/bot/handlers/administration/start_handler.py b/bot/handlers/administration/start_handler.py index 325b9c6e..0bf2f14b 100644 --- a/bot/handlers/administration/start_handler.py +++ b/bot/handlers/administration/start_handler.py @@ -27,6 +27,7 @@ get_subscriptions_message, ) from bot.utils.functions import remove_diacritics_and_lowercase + #TODO: start response class StartHandler(BotMessageHandler): diff --git a/bot/handlers/administration/subscription_status_handler.py b/bot/handlers/administration/subscription_status_handler.py index 0a44c6e4..170d750e 100644 --- a/bot/handlers/administration/subscription_status_handler.py +++ b/bot/handlers/administration/subscription_status_handler.py @@ -9,15 +9,14 @@ from aiogram.types import Message from bot.database.database_manager import DatabaseManager +from bot.database.response_keys import ResponseKey as RK from bot.handlers.bot_message_handler import ( BotMessageHandler, ValidatorFunctions, ) from bot.responses.administration.subscription_status_handler_responses import ( - format_subscription_status_response, get_log_no_active_subscription_message, get_log_subscription_status_sent_message, - get_no_subscription_message, ) @@ -36,7 +35,7 @@ async def _do_handle(self, message: Message) -> None: subscription_end, days_remaining = subscription_status user_name = message.from_user.username or message.from_user.full_name - response = format_subscription_status_response(user_name, subscription_end, days_remaining) + response = await self.get_response(RK.SUBSCRIPTION_STATUS, [user_name, str(subscription_end), str(days_remaining)]) await self._answer_markdown(message , response) await self._log_system_message(logging.INFO, get_log_subscription_status_sent_message(user_name)) @@ -52,5 +51,5 @@ async def __get_subscription_status(user_id: int) -> Optional[Tuple[date, int]]: async def __reply_no_subscription(self, message: Message) -> None: user_name = message.from_user.username or message.from_user.full_name - await self._answer(message,get_no_subscription_message()) + await self._answer(message,await self.get_response(RK.NO_SUBSCRIPTION)) await self._log_system_message(logging.INFO, get_log_no_active_subscription_message(user_name)) diff --git a/bot/responses/administration/subscription_status_handler_responses.py b/bot/responses/administration/subscription_status_handler_responses.py index b57d56ac..e9d0c911 100644 --- a/bot/responses/administration/subscription_status_handler_responses.py +++ b/bot/responses/administration/subscription_status_handler_responses.py @@ -1,22 +1,3 @@ -from datetime import date - - -def format_subscription_status_response(username: str, subscription_end: date, days_remaining: int) -> str: - return f""" - ✨ **Status Twojej subskrypcji** ✨ - -👤 **Użytkownik:** {username} -📅 **Data zakończenia:** {subscription_end} -⏳ **Pozostało dni:** {days_remaining} - - Dzięki za wsparcie projektu! 🎉 - """ - - -def get_no_subscription_message() -> str: - return "🚫 Nie masz aktywnej subskrypcji.🚫" - - def get_log_subscription_status_sent_message(username: str) -> str: return f"Subscription status sent to user '{username}'." diff --git a/bot/tests/administration/test_subscription_status.py b/bot/tests/administration/test_subscription_status.py index fa5e005b..41be6b4f 100644 --- a/bot/tests/administration/test_subscription_status.py +++ b/bot/tests/administration/test_subscription_status.py @@ -5,21 +5,23 @@ import pytest -import bot.responses.administration.subscription_status_handler_responses as msg +from bot.database.response_keys import ResponseKey as RK from bot.tests.base_test import BaseTest from bot.tests.settings import settings as s @pytest.mark.usefixtures("db_pool", "telegram_client") -class TestSubscriptionCommand(BaseTest): +class TestSubscriptionStatusHandler(BaseTest): @pytest.mark.asyncio async def test_subscription_with_active_subscription(self): days = 30 end_date = date.today() + timedelta(days=days) await self.send_command(f'/addsubscription {s.DEFAULT_ADMIN} {days}') - expected_response = msg.format_subscription_status_response( - s.TESTER_USERNAME, end_date, days, + expected_response = await self.get_response( + RK.SUBSCRIPTION_STATUS,[ + str(s.TESTER_USERNAME), str(end_date), str(days), + ], ) await self.expect_command_result_contains('/subskrypcja', [expected_response]) @@ -30,13 +32,13 @@ async def test_subscription_without_subscription(self): await self.add_test_admin_user() await self.send_command(f'/removesubscription {s.DEFAULT_ADMIN}') await self.expect_command_result_contains( - '/subskrypcja', [msg.get_no_subscription_message()], + '/subskrypcja', [await self.get_response(RK.NO_SUBSCRIPTION)], ) @pytest.mark.asyncio async def test_subscription_with_expired_subscription(self): await self.expect_command_result_contains( - '/subskrypcja', [msg.get_no_subscription_message()], + '/subskrypcja', [await self.get_response(RK.NO_SUBSCRIPTION)], ) @pytest.mark.asyncio @@ -45,8 +47,10 @@ async def test_subscription_long_duration(self): long_duration = 365 * 2 end_date = date.today() + timedelta(days=long_duration) await self.send_command(f'/addsubscription {s.DEFAULT_ADMIN} {long_duration}') - expected_response = msg.format_subscription_status_response( - s.TESTER_USERNAME, end_date, long_duration, + expected_response = await self.get_response( + RK.SUBSCRIPTION_STATUS,[ + str(s.TESTER_USERNAME), str(end_date), str(long_duration), + ], ) await self.expect_command_result_contains('/subskrypcja', [expected_response]) @@ -55,4 +59,4 @@ async def test_subscription_long_duration(self): async def test_subscription_invalid_user(self): invalid_user_id = 99999 response = await self.send_command(f'/subskrypcja {invalid_user_id}') - self.assert_response_contains(response, [msg.get_no_subscription_message()]) + self.assert_response_contains(response, [await self.get_response(RK.NO_SUBSCRIPTION)])