Skip to content

Commit 834930e

Browse files
authored
Merge pull request #2650 from sopel-irc/privilege-names
privileges, url: fix privilege check in `.urlban` command family
2 parents 99c4a00 + 04ea15c commit 834930e

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

sopel/builtins/url.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class UrlSection(types.StaticSection):
6464
"""A list of regular expressions to match URLs for which the title should not be shown."""
6565
exclude_required_access = types.ChoiceAttribute(
6666
'exclude_required_access',
67-
choices=privileges.__all__,
67+
choices=[level.name for level in privileges.AccessLevel],
6868
default='OP',
6969
)
7070
"""Minimum channel access level required to edit ``exclude`` list using chat commands."""
@@ -168,7 +168,7 @@ def _user_can_change_excludes(bot: SopelWrapper, trigger: Trigger) -> bool:
168168
channel = bot.channels[trigger.sender]
169169
user_access = channel.privileges[trigger.nick]
170170

171-
if user_access >= getattr(privileges, required_access):
171+
if user_access >= getattr(privileges.AccessLevel, required_access):
172172
return True
173173

174174
return False

test/builtins/test_builtins_url.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ def callback(bot, trigger, match):
7676
return sopel
7777

7878

79+
PRELOADED_CONFIG = """
80+
[core]
81+
owner = testnick
82+
nick = TestBot
83+
enable =
84+
coretasks
85+
url
86+
"""
87+
88+
89+
@pytest.fixture
90+
def preloadedbot(configfactory, botfactory):
91+
tmpconfig = configfactory('preloaded.cfg', PRELOADED_CONFIG)
92+
return botfactory.preloaded(tmpconfig, ['url'])
93+
94+
7995
@pytest.mark.parametrize("site", INVALID_URLS)
8096
def test_find_title_invalid(site):
8197
# All local for invalid ones
@@ -114,3 +130,30 @@ def test_url_triggers_rules_and_auto_title(mockbot):
114130
labels = sorted(result[0].get_rule_label() for result in results)
115131
expected = ['handle_urls_https', 'title_auto']
116132
assert labels == expected
133+
134+
135+
@pytest.mark.parametrize('level, result', (
136+
('NOTHING', False),
137+
('VOICE', False),
138+
('HALFOP', False),
139+
('OP', True),
140+
('ADMIN', True),
141+
('OWNER', True),
142+
))
143+
def test_url_ban_privilege(preloadedbot, ircfactory, triggerfactory, level, result):
144+
"""Make sure the urlban command privilege check functions correctly."""
145+
irc = ircfactory(preloadedbot)
146+
irc.channel_joined('#test', [
147+
'Unothing', 'Uvoice', 'Uhalfop', 'Uop', 'Uadmin', 'Uowner'])
148+
irc.mode_set('#test', '+vhoaq', [
149+
'Uvoice', 'Uhalfop', 'Uop', 'Uadmin', 'Uowner'])
150+
151+
nick = f'U{level.title()}'
152+
user = level.lower()
153+
line = f':{nick}!{user}@example.com PRIVMSG #test :.urlban *'
154+
wrapper = triggerfactory.wrapper(preloadedbot, line)
155+
match = triggerfactory(preloadedbot, line)
156+
157+
# parameter matrix assumes the default `url.exclude_required_access` config
158+
# value, which was 'OP' at the time of test creation
159+
assert url._user_can_change_excludes(wrapper, match) == result

0 commit comments

Comments
 (0)