-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtest_admin_page.py
63 lines (46 loc) · 2.16 KB
/
test_admin_page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from pathlib import Path
from unittest.mock import patch
import pytest
from nonebug import App
from .utils import fake_admin_user, fake_private_message_event
@pytest.mark.asyncio
async def test_command(app: App):
from nonebot.adapters.onebot.v11.bot import Bot
from nonebot.adapters.onebot.v11.message import Message
from nonebot_bison.plugin_config import plugin_config
from nonebot_bison.admin_page import register_get_token_handler
from nonebot_bison.admin_page.token_manager import token_manager as tm
with patch.object(tm, "get_user_token", return_value="test_token"):
register_get_token_handler()
async with app.test_matcher() as ctx:
bot = ctx.create_bot(base=Bot)
event_1 = fake_private_message_event(
message=Message("后台管理"),
sender=fake_admin_user,
to_me=True,
)
ctx.receive_event(bot, event_1)
ctx.should_call_send(event_1, f"请访问: {plugin_config.outer_url}auth/test_token", True)
ctx.should_finished()
event_2 = fake_private_message_event(message=Message("管理后台"), sender=fake_admin_user, to_me=True)
ctx.receive_event(bot, event_2)
ctx.should_call_send(event_2, f"请访问: {plugin_config.outer_url}auth/test_token", True)
ctx.should_finished()
@pytest.mark.asyncio
async def test_log(app: App, tmp_path: Path):
import io
import contextlib
from nonebot import get_driver
from nonebot.drivers.fastapi import Driver
from nonebot.log import logger, default_format
from nonebot_bison.admin_page import init_fastapi
driver = get_driver()
assert isinstance(driver, Driver)
log_path = tmp_path / "temp.log"
logger.add(log_path, level="INFO", format=default_format, rotation="1 day")
with contextlib.redirect_stderr(io.StringIO()) as f:
init_fastapi(driver)
with log_path.open("r", encoding="utf-8") as f:
log = f.read()
assert "Nonebot Bison frontend will be running at" in log
assert "该页面不能被直接访问,请私聊bot 后台管理 以获取可访问地址" in log