| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- from __future__ import annotations
- import sys
- import types
- from types import SimpleNamespace
- from unittest.mock import AsyncMock
- import pytest
- @pytest.fixture
- def points_module(app_modules, monkeypatch):
- permissions = types.ModuleType("wbb.core.decorators.permissions")
- def admins_only(*_args, **_kwargs):
- return lambda function: function
- permissions.adminsOnly = admins_only
- monkeypatch.setitem(
- sys.modules, "wbb.core.decorators.permissions", permissions
- )
- module = app_modules.load("wbb.modules.points")
- return module
- def _checkin_message() -> SimpleNamespace:
- response = SimpleNamespace(
- chat=SimpleNamespace(id=-100, type=SimpleNamespace(value="supergroup")),
- id=11,
- )
- return SimpleNamespace(
- chat=SimpleNamespace(id=-100, type=SimpleNamespace(value="supergroup")),
- id=10,
- from_user=SimpleNamespace(
- id=20,
- username="customer",
- first_name="顾客",
- last_name=None,
- ),
- reply_text=AsyncMock(return_value=response),
- )
- @pytest.mark.parametrize(
- ("created", "expected_text"),
- [
- (True, "签到成功"),
- (False, "已经签到过"),
- ],
- )
- async def test_checkin_schedules_user_and_bot_messages_for_cleanup(
- points_module, monkeypatch, created, expected_text
- ):
- message = _checkin_message()
- account = {"balance": 10}
- cleanup = []
- monkeypatch.setattr(
- points_module,
- "award_checkin",
- AsyncMock(return_value=(account, created)),
- )
- monkeypatch.setattr(
- points_module,
- "get_point_rules",
- AsyncMock(return_value={"checkin_points": 10}),
- )
- monkeypatch.setattr(
- points_module,
- "schedule_group_cleanup",
- lambda *messages: cleanup.extend(messages),
- )
- response = await points_module.points_checkin_command(None, message)
- assert expected_text in message.reply_text.await_args.args[0]
- assert cleanup == [message, response]
- async def test_checkin_error_schedules_user_and_bot_messages_for_cleanup(
- points_module, monkeypatch
- ):
- message = _checkin_message()
- cleanup = []
- monkeypatch.setattr(
- points_module,
- "award_checkin",
- AsyncMock(side_effect=points_module.PointsError("签到暂不可用")),
- )
- monkeypatch.setattr(
- points_module,
- "schedule_group_cleanup",
- lambda *messages: cleanup.extend(messages),
- )
- response = await points_module.points_checkin_command(None, message)
- assert message.reply_text.await_args.args == ("签到暂不可用",)
- assert cleanup == [message, response]
|