2022-09-29 16:56:06 +08:00
|
|
|
from typing import Tuple, Optional
|
2022-09-01 10:41:43 +08:00
|
|
|
|
2022-03-17 21:11:37 +08:00
|
|
|
import pytest
|
|
|
|
from nonebug import App
|
|
|
|
|
|
|
|
from utils import make_fake_event
|
2023-02-22 23:32:48 +08:00
|
|
|
from nonebot.exception import SkippedException
|
|
|
|
from nonebot.permission import (
|
|
|
|
USER,
|
|
|
|
NOTICE,
|
|
|
|
MESSAGE,
|
|
|
|
REQUEST,
|
|
|
|
METAEVENT,
|
|
|
|
SUPERUSER,
|
|
|
|
User,
|
|
|
|
Notice,
|
|
|
|
Message,
|
|
|
|
Request,
|
|
|
|
MetaEvent,
|
|
|
|
SuperUser,
|
|
|
|
Permission,
|
|
|
|
)
|
2022-03-17 21:11:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_permission(app: App):
|
|
|
|
async def falsy():
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def truthy():
|
|
|
|
return True
|
|
|
|
|
|
|
|
async def skipped() -> bool:
|
|
|
|
raise SkippedException
|
|
|
|
|
|
|
|
def _is_eq(a: Permission, b: Permission) -> bool:
|
|
|
|
return {d.call for d in a.checkers} == {d.call for d in b.checkers}
|
|
|
|
|
|
|
|
assert _is_eq(Permission(truthy) | None, Permission(truthy))
|
|
|
|
assert _is_eq(Permission(truthy) | falsy, Permission(truthy, falsy))
|
|
|
|
assert _is_eq(Permission(truthy) | Permission(falsy), Permission(truthy, falsy))
|
|
|
|
|
|
|
|
assert _is_eq(None | Permission(truthy), Permission(truthy))
|
|
|
|
assert _is_eq(truthy | Permission(falsy), Permission(truthy, falsy))
|
|
|
|
|
|
|
|
event = make_fake_event()()
|
|
|
|
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await Permission(falsy)(bot, event) == False
|
|
|
|
assert await Permission(truthy)(bot, event) == True
|
|
|
|
assert await Permission(skipped)(bot, event) == False
|
|
|
|
assert await Permission(truthy, falsy)(bot, event) == True
|
|
|
|
assert await Permission(truthy, skipped)(bot, event) == True
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-02-22 23:32:48 +08:00
|
|
|
@pytest.mark.parametrize("type, expected", [("message", True), ("notice", False)])
|
|
|
|
async def test_message(type: str, expected: bool):
|
2022-03-17 21:11:37 +08:00
|
|
|
dependent = list(MESSAGE.checkers)[0]
|
|
|
|
checker = dependent.call
|
|
|
|
|
|
|
|
assert isinstance(checker, Message)
|
|
|
|
|
|
|
|
event = make_fake_event(_type=type)()
|
|
|
|
assert await dependent(event=event) == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-02-22 23:32:48 +08:00
|
|
|
@pytest.mark.parametrize("type, expected", [("message", False), ("notice", True)])
|
|
|
|
async def test_notice(type: str, expected: bool):
|
2022-03-17 21:11:37 +08:00
|
|
|
dependent = list(NOTICE.checkers)[0]
|
|
|
|
checker = dependent.call
|
|
|
|
|
|
|
|
assert isinstance(checker, Notice)
|
|
|
|
|
|
|
|
event = make_fake_event(_type=type)()
|
|
|
|
assert await dependent(event=event) == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-02-22 23:32:48 +08:00
|
|
|
@pytest.mark.parametrize("type, expected", [("message", False), ("request", True)])
|
|
|
|
async def test_request(type: str, expected: bool):
|
2022-03-17 21:11:37 +08:00
|
|
|
dependent = list(REQUEST.checkers)[0]
|
|
|
|
checker = dependent.call
|
|
|
|
|
|
|
|
assert isinstance(checker, Request)
|
|
|
|
|
|
|
|
event = make_fake_event(_type=type)()
|
|
|
|
assert await dependent(event=event) == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-02-22 23:32:48 +08:00
|
|
|
@pytest.mark.parametrize("type, expected", [("message", False), ("meta_event", True)])
|
|
|
|
async def test_metaevent(type: str, expected: bool):
|
2022-03-17 21:11:37 +08:00
|
|
|
dependent = list(METAEVENT.checkers)[0]
|
|
|
|
checker = dependent.call
|
|
|
|
|
|
|
|
assert isinstance(checker, MetaEvent)
|
|
|
|
|
|
|
|
event = make_fake_event(_type=type)()
|
|
|
|
assert await dependent(event=event) == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
2023-02-22 23:32:48 +08:00
|
|
|
"type, user_id, expected",
|
2022-03-17 21:11:37 +08:00
|
|
|
[
|
|
|
|
("message", "test", True),
|
|
|
|
("message", "foo", False),
|
|
|
|
("message", "faketest", True),
|
2022-09-29 16:56:06 +08:00
|
|
|
("message", None, False),
|
2022-09-18 22:33:36 +08:00
|
|
|
("notice", "test", True),
|
2022-03-17 21:11:37 +08:00
|
|
|
],
|
|
|
|
)
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_superuser(app: App, type: str, user_id: str, expected: bool):
|
2022-03-17 21:11:37 +08:00
|
|
|
dependent = list(SUPERUSER.checkers)[0]
|
|
|
|
checker = dependent.call
|
|
|
|
|
|
|
|
assert isinstance(checker, SuperUser)
|
|
|
|
|
|
|
|
event = make_fake_event(_type=type, _user_id=user_id)()
|
|
|
|
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await dependent(bot=bot, event=event) == expected
|
2022-09-01 10:41:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
2023-02-22 23:32:48 +08:00
|
|
|
"session_ids, session_id, expected",
|
2022-09-01 10:41:43 +08:00
|
|
|
[
|
|
|
|
(("user", "foo"), "user", True),
|
|
|
|
(("user", "foo"), "bar", False),
|
2022-09-29 16:56:06 +08:00
|
|
|
(("user", "foo"), None, False),
|
2022-09-01 10:41:43 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_user(
|
2022-09-29 16:56:06 +08:00
|
|
|
app: App, session_ids: Tuple[str, ...], session_id: Optional[str], expected: bool
|
2022-09-01 10:41:43 +08:00
|
|
|
):
|
|
|
|
dependent = list(USER(*session_ids).checkers)[0]
|
|
|
|
checker = dependent.call
|
|
|
|
|
|
|
|
assert isinstance(checker, User)
|
|
|
|
|
|
|
|
event = make_fake_event(_session_id=session_id)()
|
|
|
|
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await dependent(bot=bot, event=event) == expected
|