Skip to content

Commit

Permalink
sub
Browse files Browse the repository at this point in the history
  • Loading branch information
dam2452 committed Jan 9, 2025
1 parent 2f98cb9 commit 5f74192
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
11 changes: 11 additions & 0 deletions bot/database/common_messages.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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! 🎉
');
3 changes: 2 additions & 1 deletion bot/database/response_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"



1 change: 1 addition & 0 deletions bot/handlers/administration/start_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get_subscriptions_message,
)
from bot.utils.functions import remove_diacritics_and_lowercase

#TODO: start response

class StartHandler(BotMessageHandler):
Expand Down
7 changes: 3 additions & 4 deletions bot/handlers/administration/subscription_status_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand All @@ -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))
Expand All @@ -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))
Original file line number Diff line number Diff line change
@@ -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}'."

Expand Down
22 changes: 13 additions & 9 deletions bot/tests/administration/test_subscription_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand All @@ -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])
Expand All @@ -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)])

0 comments on commit 5f74192

Please sign in to comment.