Skip to content

Commit

Permalink
Backport pull request #2650 for 8.0.2
Browse files Browse the repository at this point in the history
privileges, url: fix privilege check in `.urlban` command family
  • Loading branch information
dgw committed Jan 29, 2025
1 parent c5b3ac0 commit bfab80e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sopel/builtins/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class UrlSection(types.StaticSection):
"""A list of regular expressions to match URLs for which the title should not be shown."""
exclude_required_access = types.ChoiceAttribute(
'exclude_required_access',
choices=privileges.__all__,
choices=[level.name for level in privileges.AccessLevel],
default='OP',
)
"""Minimum channel access level required to edit ``exclude`` list using chat commands."""
Expand Down Expand Up @@ -159,7 +159,7 @@ def _user_can_change_excludes(bot: SopelWrapper, trigger: Trigger) -> bool:
channel = bot.channels[trigger.sender]
user_access = channel.privileges[trigger.nick]

if user_access >= getattr(privileges, required_access):
if user_access >= getattr(privileges.AccessLevel, required_access):
return True

return False
Expand Down
43 changes: 43 additions & 0 deletions test/builtins/test_builtins_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ def callback(bot, trigger, match):
return sopel


PRELOADED_CONFIG = """
[core]
owner = testnick
nick = TestBot
enable =
coretasks
url
"""


@pytest.fixture
def preloadedbot(configfactory, botfactory):
tmpconfig = configfactory('preloaded.cfg', PRELOADED_CONFIG)
return botfactory.preloaded(tmpconfig, ['url'])


@pytest.mark.parametrize("site", INVALID_URLS)
def test_find_title_invalid(site):
# All local for invalid ones
Expand Down Expand Up @@ -103,3 +119,30 @@ def test_url_triggers_rules_and_auto_title(mockbot):
labels = sorted(result[0].get_rule_label() for result in results)
expected = ['handle_urls_https', 'title_auto']
assert labels == expected


@pytest.mark.parametrize('level, result', (
('NOTHING', False),
('VOICE', False),
('HALFOP', False),
('OP', True),
('ADMIN', True),
('OWNER', True),
))
def test_url_ban_privilege(preloadedbot, ircfactory, triggerfactory, level, result):
"""Make sure the urlban command privilege check functions correctly."""
irc = ircfactory(preloadedbot)
irc.channel_joined('#test', [
'Unothing', 'Uvoice', 'Uhalfop', 'Uop', 'Uadmin', 'Uowner'])
irc.mode_set('#test', '+vhoaq', [
'Uvoice', 'Uhalfop', 'Uop', 'Uadmin', 'Uowner'])

nick = f'U{level.title()}'
user = level.lower()
line = f':{nick}!{user}@example.com PRIVMSG #test :.urlban *'
wrapper = triggerfactory.wrapper(preloadedbot, line)
match = triggerfactory(preloadedbot, line)

# parameter matrix assumes the default `url.exclude_required_access` config
# value, which was 'OP' at the time of test creation
assert url._user_can_change_excludes(wrapper, match) == result

0 comments on commit bfab80e

Please sign in to comment.