async def test_disable(): mock_command = MockCommand("disable", "disable", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=3)))) set_permission_filter(guild_id=3, command_wrapper=get_wrapper("disable"), permission_filter="user:<@0>") assert await receive_command(mock_command) assert "✓" in mock_command.response expected_embed = permissions_embed(guild_id=3, command_wrappers=[get_wrapper("disable")]) assert mock_command.response_embed.fields[0].name == expected_embed.fields[0].name assert mock_command.response_embed.fields[0].value == expected_embed.fields[0].value assert not get_permission_filter(guild_id=3, command_wrapper=get_wrapper("disable"))
def test_get_wrapper(): register( category="Some Category", names=["test", "alias"], required_args=["one", "two"], optional_args=["three"], description= "A command that uses `<one>`, `<two>`, and `[three]` to do stuff.", example_args=["one two", "1 2 3", "\"o n e\" two three"])(None) assert get_wrapper("test") == registered_commands["test"] assert get_wrapper("alias") == registered_commands["test"]
def test_can_execute_user_perm_fail(): command_wrapper = get_wrapper(name="test1") mock_message = MockMessage(channel=MockChannel(_id=44, guild=MockGuild(_id=3)), author=MockUser(_id=2, is_admin=False)) command = Command(name="test2", context=mock_message) set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter="user:<@88>") assert not permissions.can_execute(command)
def get_command_wrappers(commands_str: str) -> List[FunctionWrapper]: """Returns the respective command wrappers given the comma separated command names.""" if not commands_str: return [] wrappers = [] for name in commands_str.replace("+", "").split(","): # `strip` ensures any spaces between commas are ignored (e.g. "one, two" is same as "one,two"). wrapper = commands.get_wrapper(name.strip()) if wrapper: wrappers.append(wrapper) return wrappers
def test_can_execute_role_perm(): command_wrapper = get_wrapper(name="test1") mock_message = MockMessage(channel=MockChannel(_id=44, guild=MockGuild(_id=3)), author=MockUser(_id=2, roles=[MockRole(_id=66)], is_admin=False)) command = Command(name="test2", context=mock_message) set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter="role:<@&66>") assert permissions.can_execute(command)
def can_execute(command: Command) -> bool: """Returns whether the given command has permissions to execute within its current context (channel/author/roles of that guild). Administrators bypass any permission.""" command_wrapper = commands.get_wrapper(command.name) perm_filter = get_permission_filter(command.guild_id(), command_wrapper) has_permission = filter_context.test( perm_filter, command.context) if perm_filter else False caller = command.context.author # The `guild_permissions` attribute is only available in guilds, for DM channels we skip this. is_admin_or_dm = not hasattr( caller, "guild_permissions") or caller.guild_permissions.administrator return has_permission or is_admin_or_dm
async def test_enable_invalid_value(): mock_command = MockCommand( "enable", "enable", "user:name", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=3)))) assert await receive_command(mock_command) assert "✗" in mock_command.response assert "invalid value" in mock_command.response.lower() expected_embed = filter_embed(key="user", filter_context=filter_context) assert mock_command.response_embed.fields[0].name == expected_embed.fields[ 0].name assert mock_command.response_embed.fields[ 0].value == expected_embed.fields[0].value assert not get_permission_filter(guild_id=3, command_wrapper=get_wrapper("enable"))
def test_get_wrapper_unrecognized(): assert not get_wrapper("unrecognized")