forked from nonebot/nonebot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_permission.py
152 lines (115 loc) · 4.21 KB
/
test_permission.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from typing import Tuple, Optional
import pytest
from nonebug import App
from utils import make_fake_event
from nonebot.exception import SkippedException
from nonebot.permission import (
USER,
NOTICE,
MESSAGE,
REQUEST,
METAEVENT,
SUPERUSER,
User,
Notice,
Message,
Request,
MetaEvent,
SuperUser,
Permission,
)
@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) is False
assert await Permission(truthy)(bot, event) is True
assert await Permission(skipped)(bot, event) is False
assert await Permission(truthy, falsy)(bot, event) is True
assert await Permission(truthy, skipped)(bot, event) is True
@pytest.mark.asyncio
@pytest.mark.parametrize(("type", "expected"), [("message", True), ("notice", False)])
async def test_message(type: str, expected: bool):
dependent = next(iter(MESSAGE.checkers))
checker = dependent.call
assert isinstance(checker, Message)
event = make_fake_event(_type=type)()
assert await dependent(event=event) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(("type", "expected"), [("message", False), ("notice", True)])
async def test_notice(type: str, expected: bool):
dependent = next(iter(NOTICE.checkers))
checker = dependent.call
assert isinstance(checker, Notice)
event = make_fake_event(_type=type)()
assert await dependent(event=event) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(("type", "expected"), [("message", False), ("request", True)])
async def test_request(type: str, expected: bool):
dependent = next(iter(REQUEST.checkers))
checker = dependent.call
assert isinstance(checker, Request)
event = make_fake_event(_type=type)()
assert await dependent(event=event) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(
("type", "expected"), [("message", False), ("meta_event", True)]
)
async def test_metaevent(type: str, expected: bool):
dependent = next(iter(METAEVENT.checkers))
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(
("type", "user_id", "expected"),
[
("message", "test", True),
("message", "foo", False),
("message", "faketest", True),
("message", None, False),
("notice", "test", True),
],
)
async def test_superuser(app: App, type: str, user_id: str, expected: bool):
dependent = next(iter(SUPERUSER.checkers))
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
@pytest.mark.asyncio
@pytest.mark.parametrize(
("session_ids", "session_id", "expected"),
[
(("user", "foo"), "user", True),
(("user", "foo"), "bar", False),
(("user", "foo"), None, False),
],
)
async def test_user(
app: App, session_ids: Tuple[str, ...], session_id: Optional[str], expected: bool
):
dependent = next(iter(USER(*session_ids).checkers))
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