-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_models.py
More file actions
350 lines (293 loc) · 10.9 KB
/
Copy pathtest_models.py
File metadata and controls
350 lines (293 loc) · 10.9 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
LuckPermsAPI 模型单元测试。
"""
import time
import pytest
from luckperms.models import Group, Node, PermissionHolder, Track, User
class TestNode:
def test_node_creation(self):
n = Node("plugin.chat", True)
assert n.key == "plugin.chat"
assert n.value is True
assert n.context == {}
assert n.expiry is None
def test_node_defaults(self):
n = Node("test")
assert n.value is True
assert n.context == {}
assert n.expiry is None
def test_node_with_context(self):
n = Node("group.mute", False, {"group_id": "123"})
assert n.context == {"group_id": ["123"]}
assert n.matches_context({"group_id": "123"}) is True
assert n.matches_context({"group_id": "456"}) is False
assert n.matches_context({}) is False
def test_node_with_multivalue_context(self):
n = Node("group.mute", False, {"group_id": ["123", "456"]})
assert n.context == {"group_id": ["123", "456"]}
assert n.matches_context({"group_id": "123"}) is True
assert n.matches_context({"group_id": ["789", "456"]}) is True
assert n.matches_context({"group_id": "789"}) is False
def test_node_context_subset(self):
"""节点 context 是查询 context 的子集时才匹配。"""
n = Node("perm", True, {"a": "1", "b": "2"})
assert n.matches_context({"a": "1", "b": "2", "c": "3"}) is True
assert n.matches_context({"a": "1"}) is False
assert n.matches_context({"a": "1", "b": "3"}) is False
def test_node_expiry(self):
n = Node("temp", True, expiry=time.time() - 1)
assert n.is_expired() is True
n2 = Node("temp", True, expiry=time.time() + 3600)
assert n2.is_expired() is False
n3 = Node("perm")
assert n3.is_expired() is False
def test_node_serde(self):
n = Node("a.b", False, {"k": "v"}, 1234567890.0)
d = n.to_dict()
assert d == {"key": "a.b", "value": False, "context": {"k": ["v"]}, "expiry": 1234567890.0}
n2 = Node.from_dict(d)
assert n == n2
def test_node_serde_minimal(self):
n = Node("simple")
d = n.to_dict()
assert "context" not in d
assert "expiry" not in d
n2 = Node.from_dict(d)
assert n == n2
def test_node_hash_eq(self):
n1 = Node("a", True, {"k": "v"})
n2 = Node("a", True, {"k": "v"})
n3 = Node("a", True, {"k": "x"})
assert n1 == n2
assert hash(n1) == hash(n2)
assert n1 != n3
def test_node_eq_different_type(self):
n = Node("a")
assert n != "not a node"
class TestPermissionHolder:
def test_holder_identifier(self):
h = PermissionHolder("id123", "Display")
assert h.identifier == "id123"
assert h.display_name == "Display"
def test_add_node_no_duplicate(self):
h = PermissionHolder("1")
h.add_node(Node("x", True))
h.add_node(Node("x", False))
assert len(h.nodes) == 1
assert h.nodes[0].value is False
def test_add_node_different_context(self):
h = PermissionHolder("1")
h.add_node(Node("x", True, {"ctx": "a"}))
h.add_node(Node("x", False, {"ctx": "b"}))
assert len(h.nodes) == 2
def test_remove_node(self):
h = PermissionHolder("1")
h.add_node(Node("x"))
assert h.remove_node("x") is True
assert h.remove_node("x") is False
def test_remove_node_with_context(self):
h = PermissionHolder("1")
h.add_node(Node("x", True, {"k": "v"}))
assert h.remove_node("x") is False # 不匹配 context
assert h.remove_node("x", {"k": "v"}) is True
def test_clear_nodes(self):
h = PermissionHolder("1")
h.add_node(Node("a"))
h.add_node(Node("b"))
h.clear_nodes()
assert h.nodes == []
def test_parents(self):
h = PermissionHolder("1")
h.add_parent("admin")
h.add_parent("mod")
h.add_parent("admin") # 重复添加无效
assert h.parents == ["admin", "mod"]
h.remove_parent("admin")
assert h.parents == ["mod"]
def test_to_dict_abstract_raises(self):
h = PermissionHolder("1")
d = h.to_dict()
assert d["id"] == "1"
with pytest.raises(NotImplementedError):
PermissionHolder.from_dict(d)
class TestUser:
def test_user_creation(self):
u = User("123456", "Alice")
assert u.unique_id == "123456"
assert u.display_name == "Alice"
def test_user_default_display_name(self):
u = User("123")
assert u.display_name == "123"
def test_add_remove_node(self):
u = User("1")
n = Node("plugin.admin")
u.add_node(n)
assert len(u.nodes) == 1
assert u.remove_node("plugin.admin") is True
assert u.remove_node("plugin.admin") is False
def test_replace_node(self):
u = User("1")
u.add_node(Node("x", True))
u.add_node(Node("x", False))
assert len(u.nodes) == 1
assert u.nodes[0].value is False
def test_parents(self):
u = User("1")
u.add_parent("admin")
u.add_parent("mod")
assert u.parents == ["admin", "mod"]
u.remove_parent("admin")
assert u.parents == ["mod"]
def test_serde(self):
u = User("1", "Test")
u.add_node(Node("a", True))
u.add_parent("g1")
d = u.to_dict()
assert d["type"] == "user"
u2 = User.from_dict(d)
assert u2.unique_id == "1"
assert u2.display_name == "Test"
assert len(u2.nodes) == 1
assert u2.parents == ["g1"]
def test_serde_empty(self):
u = User("1")
d = u.to_dict()
u2 = User.from_dict(d)
assert u2.unique_id == "1"
assert u2.nodes == []
assert u2.parents == []
class TestGroup:
def test_group_creation(self):
g = Group("admin", "管理员")
assert g.name == "admin"
assert g.display_name == "管理员"
def test_group_default_display_name(self):
g = Group("mod")
assert g.display_name == "mod"
def test_group_inherit(self):
g = Group("mod")
g.add_parent("admin")
assert g.parents == ["admin"]
g.remove_parent("admin")
assert g.parents == []
def test_group_nodes(self):
g = Group("admin")
g.add_node(Node("plugin.*", True))
g.add_node(Node("plugin.ban", False))
assert len(g.nodes) == 2
g.remove_node("plugin.*")
assert len(g.nodes) == 1
def test_group_serde(self):
g = Group("admin", "管理员")
g.add_node(Node("a", True))
g.add_parent("super")
d = g.to_dict()
assert d["type"] == "group"
assert d["id"] == "admin"
g2 = Group.from_dict(d)
assert g2.name == "admin"
assert g2.display_name == "管理员"
assert len(g2.nodes) == 1
assert g2.parents == ["super"]
def test_group_weight(self):
g = Group("admin", weight=100)
assert g.weight == 100
d = g.to_dict()
assert d["weight"] == 100
def test_group_weight_default_zero(self):
g = Group("mod")
assert g.weight == 0
d = g.to_dict()
assert "weight" not in d
def test_group_weight_roundtrip(self):
g = Group("vip", weight=50)
d = g.to_dict()
g2 = Group.from_dict(d)
assert g2.weight == 50
class TestTrack:
def test_track_creation(self):
t = Track("staff", ["member", "mod", "admin"])
assert t.groups == ["member", "mod", "admin"]
def test_track_modify(self):
t = Track("t")
t.append_group("a")
t.append_group("b")
t.append_group("a") # 重复无效
assert t.groups == ["a", "b"]
assert t.remove_group("a") is True
assert t.groups == ["b"]
assert t.remove_group("c") is False
def test_track_set_groups(self):
t = Track("t", ["a", "b"])
t.set_groups(["c", "d"])
assert t.groups == ["c", "d"]
def test_track_serde(self):
t = Track("staff", ["a", "b"])
d = t.to_dict()
assert d == {"name": "staff", "groups": ["a", "b"]}
t2 = Track.from_dict(d)
assert t2.name == "staff"
assert t2.groups == ["a", "b"]
def test_track_serde_empty(self):
t = Track("empty")
d = t.to_dict()
t2 = Track.from_dict(d)
assert t2.groups == []
class TestMetaNodes:
def test_meta_weight_as_node(self):
g = Group("admin")
g.add_node(Node("weight.100", True))
assert g.weight == 100
def test_meta_prefix_priority(self):
g = Group("admin")
g.add_node(Node("prefix.50.&c[Mod]", True))
g.add_node(Node("prefix.100.&4[Admin]", True))
assert g.get_meta("prefix") == "&4[Admin]"
def test_group_weight_node_sync(self):
g = Group("vip")
g.weight = 50
assert any(n.key == "weight.50" for n in g.nodes)
assert g.weight == 50
g.weight = 100
assert not any(n.key == "weight.50" for n in g.nodes)
assert any(n.key == "weight.100" for n in g.nodes)
assert g.weight == 100
def test_group_weight_fallback(self):
g = Group("mod", weight=10)
assert g.weight == 10
def test_group_to_dict_no_duplicate_weight(self):
g = Group("admin")
g.weight = 100
d = g.to_dict()
# 节点中有 weight.100,不应再输出 weight 字段
assert "weight" not in d
assert any(n["key"] == "weight.100" for n in d["nodes"])
def test_node_is_meta(self):
assert Node("prefix.100.&cAdmin").is_meta is True
assert Node("suffix.50.&7Member").is_meta is True
assert Node("displayname.Custom").is_meta is True
assert Node("weight.100").is_meta is True
assert Node("plugin.chat").is_meta is False
def test_node_meta_type(self):
assert Node("prefix.100.&cAdmin").meta_type == "prefix"
assert Node("weight.100").meta_type == "weight"
assert Node("plugin.chat").meta_type is None
def test_node_meta_value(self):
assert Node("prefix.100.&cAdmin").meta_value == "&cAdmin"
assert Node("weight.100").meta_value == "100"
assert Node("plugin.chat").meta_value is None
def test_remove_nodes_by_prefix(self):
h = PermissionHolder("1")
h.add_node(Node("weight.10", True))
h.add_node(Node("weight.20", True))
h.add_node(Node("plugin.chat", True))
removed = h.remove_nodes_by_prefix("weight.")
assert removed == 2
assert len(h.nodes) == 1
assert h.nodes[0].key == "plugin.chat"
def test_transient_context(self):
u = User("1")
u.set_transient_context("world", "nether")
assert u.transient_contexts == {"world": "nether"}
u.clear_transient_contexts()
assert u.transient_contexts == {}