Skip to content

Commit bfab80e

Browse files
committed
Backport pull request #2650 for 8.0.2
privileges, url: fix privilege check in `.urlban` command family
1 parent c5b3ac0 commit bfab80e

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
@@ -62,7 +62,7 @@ class UrlSection(types.StaticSection):
6262
"""A list of regular expressions to match URLs for which the title should not be shown."""
6363
exclude_required_access = types.ChoiceAttribute(
6464
'exclude_required_access',
65-
choices=privileges.__all__,
65+
choices=[level.name for level in privileges.AccessLevel],
6666
default='OP',
6767
)
6868
"""Minimum channel access level required to edit ``exclude`` list using chat commands."""
@@ -159,7 +159,7 @@ def _user_can_change_excludes(bot: SopelWrapper, trigger: Trigger) -> bool:
159159
channel = bot.channels[trigger.sender]
160160
user_access = channel.privileges[trigger.nick]
161161

162-
if user_access >= getattr(privileges, required_access):
162+
if user_access >= getattr(privileges.AccessLevel, required_access):
163163
return True
164164

165165
return False

test/builtins/test_builtins_url.py

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

7272

73+
PRELOADED_CONFIG = """
74+
[core]
75+
owner = testnick
76+
nick = TestBot
77+
enable =
78+
coretasks
79+
url
80+
"""
81+
82+
83+
@pytest.fixture
84+
def preloadedbot(configfactory, botfactory):
85+
tmpconfig = configfactory('preloaded.cfg', PRELOADED_CONFIG)
86+
return botfactory.preloaded(tmpconfig, ['url'])
87+
88+
7389
@pytest.mark.parametrize("site", INVALID_URLS)
7490
def test_find_title_invalid(site):
7591
# All local for invalid ones
@@ -103,3 +119,30 @@ def test_url_triggers_rules_and_auto_title(mockbot):
103119
labels = sorted(result[0].get_rule_label() for result in results)
104120
expected = ['handle_urls_https', 'title_auto']
105121
assert labels == expected
122+
123+
124+
@pytest.mark.parametrize('level, result', (
125+
('NOTHING', False),
126+
('VOICE', False),
127+
('HALFOP', False),
128+
('OP', True),
129+
('ADMIN', True),
130+
('OWNER', True),
131+
))
132+
def test_url_ban_privilege(preloadedbot, ircfactory, triggerfactory, level, result):
133+
"""Make sure the urlban command privilege check functions correctly."""
134+
irc = ircfactory(preloadedbot)
135+
irc.channel_joined('#test', [
136+
'Unothing', 'Uvoice', 'Uhalfop', 'Uop', 'Uadmin', 'Uowner'])
137+
irc.mode_set('#test', '+vhoaq', [
138+
'Uvoice', 'Uhalfop', 'Uop', 'Uadmin', 'Uowner'])
139+
140+
nick = f'U{level.title()}'
141+
user = level.lower()
142+
line = f':{nick}!{user}@example.com PRIVMSG #test :.urlban *'
143+
wrapper = triggerfactory.wrapper(preloadedbot, line)
144+
match = triggerfactory(preloadedbot, line)
145+
146+
# parameter matrix assumes the default `url.exclude_required_access` config
147+
# value, which was 'OP' at the time of test creation
148+
assert url._user_can_change_excludes(wrapper, match) == result

0 commit comments

Comments
 (0)