nonebot-bison/tests/test_admin_page.py
Sherkey 1997b57761
BISON_OUTER_URL配置改进 (#405)
* 📝 修复文档中的错误链接

*  bison_outer_url使用property包装

* 📝 通过vue动态生成BISON_OUTER_URL的配置建议

* 💄 auto fix by pre-commit hooks

* 📝 优化文档视觉效果

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-10-29 20:54:41 +08:00

64 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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