Skip to content

Commit

Permalink
Merge pull request #2650 from sopel-irc/privilege-names
Browse files Browse the repository at this point in the history
privileges, url: fix privilege check in `.urlban` command family
  • Loading branch information
dgw authored Jan 29, 2025
2 parents 99c4a00 + 04ea15c commit 834930e
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 @@ -64,7 +64,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 @@ -168,7 +168,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 @@ -76,6 +76,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 @@ -114,3 +130,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

1 comment on commit 834930e

@dgw
Copy link
Member Author

@dgw dgw commented on 834930e Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-ran CI to see if a random failure in unrelated code (builtins/rand.py) recurred. It didn't.

Preserving the error trace here as a comment for future reference, in case this happens again and we need to try debugging.

=================================== FAILURES ===================================
_____________________________ test_example_rand_4 ______________________________
sopel/tests/pytest_plugin.py:139: in test
    assert len(outputs) == expected_output_count
E   AssertionError: assert 9 == 10
E    +  where 9 = len(['random(0, 2): 0', 'random(0, 2): 0', 'random(0, 2): 0', 'random(0, 2): 0', 'random(0, 2): 0', 'random(0, 2): 2', ...])

Please sign in to comment.