Skip to content

Commit e466e4c

Browse files
committed
Add more tests for chain plugin
1 parent 0105098 commit e466e4c

File tree

2 files changed

+398
-43
lines changed

2 files changed

+398
-43
lines changed

plugins/chain.py

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,60 @@ def get_hook_from_command(bot, hook_name):
5757
return possible[0] if len(possible) == 1 else None
5858

5959

60+
def handle_chainallow_add(args, notice_doc, hook_name, db):
61+
values = {"hook": hook_name}
62+
if args:
63+
allow = args.pop(0).lower()
64+
if allow == "allow":
65+
allow = True
66+
elif allow == "deny":
67+
allow = False
68+
else:
69+
return notice_doc()
70+
71+
values["allowed"] = allow
72+
73+
updated = True
74+
res = db.execute(
75+
commands.update().values(**values).where(commands.c.hook == hook_name)
76+
)
77+
if res.rowcount == 0:
78+
updated = False
79+
db.execute(commands.insert().values(**values))
80+
81+
db.commit()
82+
load_cache(db)
83+
if updated:
84+
return "Updated state of '{}' in chainallow to allowed={}".format(
85+
hook_name, allow_cache.get(hook_name)
86+
)
87+
88+
if allow_cache.get(hook_name):
89+
return f"Added '{hook_name}' as an allowed command"
90+
91+
return f"Added '{hook_name}' as a denied command"
92+
93+
94+
def handle_chainallow_del(args, notice_doc, hook_name, db):
95+
res = db.execute(commands.delete().where(commands.c.hook == hook_name))
96+
db.commit()
97+
load_cache(db)
98+
return "Deleted {}.".format(pluralize_auto(res.rowcount, "row"))
99+
100+
101+
chainallow_subcmds = {
102+
"add": handle_chainallow_add,
103+
"del": handle_chainallow_del,
104+
}
105+
106+
60107
@hook.command(permissions=["botcontrol", "snoonetstaff"])
61108
def chainallow(text, db, notice_doc, bot):
62109
"""{add [hook] [{allow|deny}]|del [hook]} - Manage the allowed list fo comands for the chain command"""
63110
args = text.split()
64111
subcmd = args.pop(0).lower()
65112

66-
if not args:
113+
if not args or subcmd not in chainallow_subcmds:
67114
return notice_doc()
68115

69116
name = args.pop(0)
@@ -73,48 +120,7 @@ def chainallow(text, db, notice_doc, bot):
73120

74121
hook_name = format_hook_name(_hook)
75122

76-
if subcmd == "add":
77-
values = {"hook": hook_name}
78-
if args:
79-
allow = args.pop(0).lower()
80-
if allow == "allow":
81-
allow = True
82-
elif allow == "deny":
83-
allow = False
84-
else:
85-
return notice_doc()
86-
87-
values["allowed"] = allow
88-
89-
updated = True
90-
res = db.execute(
91-
commands.update()
92-
.values(**values)
93-
.where(commands.c.hook == hook_name)
94-
)
95-
if res.rowcount == 0:
96-
updated = False
97-
db.execute(commands.insert().values(**values))
98-
99-
db.commit()
100-
load_cache(db)
101-
if updated:
102-
return "Updated state of '{}' in chainallow to allowed={}".format(
103-
hook_name, allow_cache.get(hook_name)
104-
)
105-
106-
if allow_cache.get(hook_name):
107-
return f"Added '{hook_name}' as an allowed command"
108-
109-
return f"Added '{hook_name}' as a denied command"
110-
111-
if subcmd == "del":
112-
res = db.execute(commands.delete().where(commands.c.hook == hook_name))
113-
db.commit()
114-
load_cache(db)
115-
return "Deleted {}.".format(pluralize_auto(res.rowcount, "row"))
116-
117-
return notice_doc()
123+
return chainallow_subcmds[subcmd](args, notice_doc, hook_name, db)
118124

119125

120126
def parse_chain(text, bot):

0 commit comments

Comments
 (0)