🐛 fix nested user permission update (#1208)

This commit is contained in:
Ju4tCode 2022-09-01 10:41:43 +08:00 committed by GitHub
parent f150a9ee89
commit cd30be21ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 10 deletions

View File

@ -44,7 +44,7 @@ from nonebot.exception import (
)
from .rule import Rule
from .permission import USER, Permission
from .permission import USER, User, Permission
from .adapter import Bot, Event, Message, MessageSegment, MessageTemplate
from .params import (
Depends,
@ -635,15 +635,21 @@ class Matcher(metaclass=MatcherMeta):
async def update_type(self, bot: Bot, event: Event) -> str:
updater = self.__class__._default_type_updater
if not updater:
return "message"
return await updater(bot=bot, event=event, state=self.state, matcher=self)
return (
await updater(bot=bot, event=event, state=self.state, matcher=self)
if updater
else "message"
)
async def update_permission(self, bot: Bot, event: Event) -> Permission:
updater = self.__class__._default_permission_updater
if not updater:
return USER(event.get_session_id(), perm=self.permission)
if updater := self.__class__._default_permission_updater:
return await updater(bot=bot, event=event, state=self.state, matcher=self)
permission = self.permission
if len(permission.checkers) == 1 and isinstance(
user_perm := tuple(permission.checkers)[0].call, User
):
permission = user_perm.perm
return USER(event.get_session_id(), perm=permission)
async def resolve_reject(self):
handler = current_handler.get()

View File

@ -1,10 +1,14 @@
from nonebot.matcher import Matcher
from nonebot.permission import Permission
from nonebot.permission import USER, Permission
default_permission = Permission()
test_permission_updater = Matcher.new(permission=default_permission)
test_user_permission_updater = Matcher.new(
permission=USER("test", perm=default_permission)
)
test_custom_updater = Matcher.new(permission=default_permission)

View File

@ -104,6 +104,7 @@ async def test_permission_updater(app: App, load_plugin):
default_permission,
test_custom_updater,
test_permission_updater,
test_user_permission_updater,
)
event = make_fake_event(_session_id="test")()
@ -119,6 +120,19 @@ async def test_permission_updater(app: App, load_plugin):
assert checker.users == ("test",)
assert checker.perm is default_permission
user_permission = list(test_user_permission_updater.permission.checkers)[0].call
assert isinstance(user_permission, User)
assert user_permission.perm is default_permission
async with app.test_api() as ctx:
bot = ctx.create_bot()
matcher = test_user_permission_updater()
new_perm = await matcher.update_permission(bot, event)
assert len(new_perm.checkers) == 1
checker = list(new_perm.checkers)[0].call
assert isinstance(checker, User)
assert checker.users == ("test",)
assert checker.perm is default_permission
assert test_custom_updater.permission is default_permission
async with app.test_api() as ctx:
bot = ctx.create_bot()

View File

@ -1,3 +1,5 @@
from typing import Tuple
import pytest
from nonebug import App
@ -145,7 +147,7 @@ async def test_metaevent(
("notice", "test", False),
],
)
async def test_startswith(
async def test_superuser(
app: App,
type: str,
user_id: str,
@ -163,3 +165,28 @@ async def test_startswith(
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await dependent(bot=bot, event=event) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(
"session_ids,session_id,expected",
[
(("user", "foo"), "user", True),
(("user", "foo"), "bar", False),
],
)
async def test_user(
app: App, session_ids: Tuple[str, ...], session_id: str, expected: bool
):
from nonebot.permission import USER, User
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