test_points_commands.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from __future__ import annotations
  2. import sys
  3. import types
  4. from types import SimpleNamespace
  5. from unittest.mock import AsyncMock
  6. import pytest
  7. @pytest.fixture
  8. def points_module(app_modules, monkeypatch):
  9. permissions = types.ModuleType("wbb.core.decorators.permissions")
  10. def admins_only(*_args, **_kwargs):
  11. return lambda function: function
  12. permissions.adminsOnly = admins_only
  13. monkeypatch.setitem(
  14. sys.modules, "wbb.core.decorators.permissions", permissions
  15. )
  16. module = app_modules.load("wbb.modules.points")
  17. return module
  18. def _checkin_message() -> SimpleNamespace:
  19. response = SimpleNamespace(
  20. chat=SimpleNamespace(id=-100, type=SimpleNamespace(value="supergroup")),
  21. id=11,
  22. )
  23. return SimpleNamespace(
  24. chat=SimpleNamespace(id=-100, type=SimpleNamespace(value="supergroup")),
  25. id=10,
  26. from_user=SimpleNamespace(
  27. id=20,
  28. username="customer",
  29. first_name="顾客",
  30. last_name=None,
  31. ),
  32. reply_text=AsyncMock(return_value=response),
  33. )
  34. @pytest.mark.parametrize("created", [True, False])
  35. async def test_checkin_schedules_user_and_bot_messages_for_cleanup(
  36. points_module, monkeypatch, created
  37. ):
  38. message = _checkin_message()
  39. account = {"balance": 10}
  40. cleanup = []
  41. monkeypatch.setattr(
  42. points_module,
  43. "award_checkin",
  44. AsyncMock(return_value=(account, created)),
  45. )
  46. monkeypatch.setattr(
  47. points_module,
  48. "get_point_rules",
  49. AsyncMock(
  50. return_value={
  51. "checkin_points": 10,
  52. "checkin_success_message": "领取成功 +{points},余额 {balance}",
  53. "checkin_already_message": "今天领过了,余额 {balance}",
  54. }
  55. ),
  56. )
  57. monkeypatch.setattr(
  58. points_module,
  59. "schedule_group_cleanup",
  60. lambda *messages: cleanup.extend(messages),
  61. )
  62. response = await points_module.points_checkin_command(None, message)
  63. expected_message = (
  64. "领取成功 +10,余额 10" if created else "今天领过了,余额 10"
  65. )
  66. assert message.reply_text.await_args.args == (expected_message,)
  67. assert (
  68. message.reply_text.await_args.kwargs["parse_mode"]
  69. == points_module.ParseMode.DISABLED
  70. )
  71. assert cleanup == [message, response]
  72. async def test_checkin_error_schedules_user_and_bot_messages_for_cleanup(
  73. points_module, monkeypatch
  74. ):
  75. message = _checkin_message()
  76. cleanup = []
  77. monkeypatch.setattr(
  78. points_module,
  79. "award_checkin",
  80. AsyncMock(side_effect=points_module.PointsError("签到暂不可用")),
  81. )
  82. monkeypatch.setattr(
  83. points_module,
  84. "schedule_group_cleanup",
  85. lambda *messages: cleanup.extend(messages),
  86. )
  87. response = await points_module.points_checkin_command(None, message)
  88. assert message.reply_text.await_args.args == ("签到暂不可用",)
  89. assert (
  90. message.reply_text.await_args.kwargs["parse_mode"]
  91. == points_module.ParseMode.DISABLED
  92. )
  93. assert cleanup == [message, response]