def test_parse_command(): assert parse_command("+test") == Command("test") assert parse_command("+test 123") == Command("test", "123") assert parse_command("+test 1 2 3") == Command("test", "1", "2", "3") assert not parse_command("123") assert not parse_command("+ test") assert not parse_command("++test") assert not parse_command("123 +test")
def parse_command(content: str, context: Message=None, client: Client=None) -> Command: """Returns the given content string as a command, if it's formatted as one, else None. Optionally with the given message as context.""" match = regex.search(r"^" + regex.escape(get_prefix(context)) + r"([A-Za-z]+) ?(.+)?", content) if match: name = match.group(1) args = match.group(2) if args: args_respecting_quotes = [unescape(arg) for arg, _ in split_unescaped(args, (" ",))] return Command(name, *args_respecting_quotes, context=context, client=client) return Command(name, context=context, client=client) return None
def test_can_execute_admin(): mock_message = MockMessage(channel=MockChannel(_id=44, guild=MockGuild(_id=3)), author=MockUser(_id=2, is_admin=True)) command = Command(name="test2", context=mock_message) assert permissions.can_execute(command)
async def test_validate_filter_complex(): command = Command(name="test", context=MockMessage(channel=MockChannel())) assert await validate_filter( command = command, _filter = "type:(nom or qual or reset or dq) and not user:(banchobot or peppy)", filter_context = filter_context )
async def test_receive_command_without_optional_arg(): mock_channel = MockChannel() mock_message = MockMessage("+greet someone", channel=mock_channel) assert await receive_command( Command("greet", "someone", context=mock_message)) assert mock_channel.messages[0].content == "hi someone"
async def test_receive_command_missing_arg(): mock_channel = MockChannel() mock_message = MockMessage("+greet", channel=mock_channel) assert not await receive_command(Command("greet", context=mock_message)) assert mock_channel.messages[0].content.startswith("✗") assert "missing required argument" in mock_channel.messages[ 0].content.lower() assert "`<name>`" in mock_channel.messages[0].content.lower()
async def test_receive_command_lacking_permission(): mock_channel = MockChannel(guild=MockGuild(_id=3)) mock_message = MockMessage("+test", channel=mock_channel, author=MockUser(is_admin=False)) assert not await receive_command(Command("test", context=mock_message)) assert mock_channel.typing_triggered assert "✗ lacking permission" in mock_channel.messages[0].content.lower()
async def test_command_respond(): mock_channel = MockChannel() mock_message = MockMessage("+test 1 2 3", channel=mock_channel) command = Command("test", "1", "2", "3", context=mock_message) assert await command.respond("success") assert command.response == "success" assert command.response_embed is None assert mock_channel.messages[0].content == "success" assert mock_channel.messages[0].embed is None
def test_prefix_custom(): command = Command("test", context=MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=7)))) set_prefix(guild_id=7, prefix="&") assert command.prefix() == "&" set_prefix(guild_id=7, prefix=None) assert command.prefix() == DEFAULT_PREFIX
async def test_command_respond_forbidden(): mock_error = Forbidden(MockResponse(status=403, reason="forbidden"), "lacking permissions") mock_channel = MockErrorChannel(raise_on_send=mock_error) mock_message = MockMessage("+test 1 2 3", channel=mock_channel) command = Command("test", "1", "2", "3", context=mock_message) assert not await command.respond( "e.g. lacking send message permissions in the channel") assert command.response is None assert command.response_embed is None assert not mock_channel.messages
def test_parse_command_custom_prefix(): context = MockMessage(channel=MockChannel(guild=MockGuild(_id=3))) set_prefix(guild_id=3, prefix="&") assert not parse_command("+test", context=context) assert parse_command("&test", context=context) == Command("test", context=context) assert parse_command("&test 123", context=context) == Command("test", "123", context=context) assert parse_command("&test 1 2 3", context=context) == Command("test", "1", "2", "3", context=context) assert not parse_command("123", context=context) assert not parse_command("& test", context=context) assert not parse_command("&&test", context=context) assert not parse_command("123 &test", context=context)
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)
async def test_command_respond_httpexception(): mock_error = HTTPException(MockResponse(status=404, reason="not found"), "e.g. some channel doesn't exist") mock_channel = MockErrorChannel(raise_on_send=mock_error) mock_message = MockMessage("+test 1 2 3", channel=mock_channel) command = Command("test", "1", "2", "3", context=mock_message) with pytest.raises(HTTPException): await command.respond("e.g. API did not respond with 200: OK") assert command.response is None assert command.response_embed is None assert not mock_channel.messages
async def test_command_respond_embed(): mock_channel = MockChannel() mock_message = MockMessage("+test 1 2 3", channel=mock_channel) command = Command("test", "1", "2", "3", context=mock_message) embed = Embed() embed.add_field(name="test", value="success") assert await command.respond("", embed=embed) assert command.response == "" assert command.response_embed == embed assert mock_channel.messages[0].content == "" assert mock_channel.messages[0].embed == embed
async def test_validate_filter_invalid_value(): command = Command(name="test", context=MockMessage(channel=MockChannel())) assert not await validate_filter(command=command, _filter="type:undefined", filter_context=filter_context) assert "✗" in command.response assert "invalid value" in command.response.lower() embed = filter_embed(key="type", filter_context=filter_context) assert command.response_embed.fields[0].name == embed.fields[0].name assert command.response_embed.fields[0].value == embed.fields[0].value assert command.response_embed.fields[1].name == embed.fields[1].name assert command.response_embed.fields[1].value == embed.fields[1].value assert command.response_embed.fields[2].name == embed.fields[2].name assert command.response_embed.fields[2].value == embed.fields[2].value
async def test_validate_filter_invalid_word(): command = Command(name="test", context=MockMessage(channel=MockChannel())) assert not await validate_filter(command=command, _filter="user:sometwo annd type:qualify", filter_context=filter_context) assert "✗" in command.response assert "invalid word" in command.response.lower() embed = filters_embed(filter_context=filter_context) assert command.response_embed.title == embed.title assert command.response_embed.description == embed.description assert command.response_embed.fields[0].name == embed.fields[0].name assert command.response_embed.fields[0].value == embed.fields[0].value assert command.response_embed.fields[1].name == embed.fields[1].name assert command.response_embed.fields[1].value == embed.fields[1].value
async def test_validate_filter_invalid_key(): command = Command(name="test", context=MockMessage(channel=MockChannel())) assert not await validate_filter(command=command, _filter="undefined:undefined", filter_context=filter_context) assert "✗" in command.response assert "invalid key" in command.response.lower() embed = filters_embed(filter_context=filter_context) assert command.response_embed.title == embed.title assert command.response_embed.description == embed.description assert command.response_embed.fields[0].name == embed.fields[0].name assert command.response_embed.fields[0].value == embed.fields[0].value assert command.response_embed.fields[1].name == embed.fields[1].name assert command.response_embed.fields[1].value == embed.fields[1].value
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)
async def test_validate_filter_missing_gate(): command = Command(name="test", context=MockMessage(channel=MockChannel())) assert not await validate_filter(command=command, _filter="user:sometwo type:qualify", filter_context=filter_context) assert "✗" in command.response assert "missing gate" in command.response.lower() assert "between `user:sometwo` and `type:qualify`" in command.response.lower() embed = filters_embed(filter_context=filter_context) assert command.response_embed.title == embed.title assert command.response_embed.description == embed.description assert command.response_embed.fields[0].name == embed.fields[0].name assert command.response_embed.fields[0].value == embed.fields[0].value assert command.response_embed.fields[1].name == embed.fields[1].name assert command.response_embed.fields[1].value == embed.fields[1].value
def test_command_help_embed(): register( category="Some Category", names=["test"], 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) embed1 = Command("test").help_embed() embed2 = help_embed("test") assert embed1.fields[0].name == embed2.fields[0].name assert embed1.fields[0].value == embed2.fields[0].value
async def test_command_respond_err(): register( category="Some Category", names=["test"], 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) embed = help_embed("test") mock_channel = MockChannel() mock_message = MockMessage("+test 1 2 3", channel=mock_channel) command = Command("test", "1", "2", "3", context=mock_message) assert await command.respond_err("error") assert command.response == "✗ error" assert command.response_embed.fields[0].name == embed.fields[0].name assert command.response_embed.fields[0].value == embed.fields[0].value
async def test_receive_command_alias(): mock_channel = MockChannel() mock_message = MockMessage("+alias", channel=mock_channel) assert await receive_command(Command("alias", context=mock_message)) assert mock_channel.typing_triggered assert mock_channel.messages[0].content == "hi"
async def test_validate_filter(): command = Command(name="test", context=MockMessage(channel=MockChannel())) assert await validate_filter(command=command, _filter="type:nominate", filter_context=filter_context)
def test_prefix_default(): command = Command("test", context=MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=7)))) assert command.prefix() == DEFAULT_PREFIX
def test_parse_command_nested_quotes(): assert parse_command("+test \"nested \"quotes\"\"") == Command( "test", "nested \"quotes\"")
def test_guild_id(): command = Command("test", context=MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=7)))) assert command.guild_id() == 7
def test_guild_id_dm_channel(): command = Command("test", context=MockMessage(channel=MockDMChannel())) assert command.guild_id() is None
async def test_receive_command_unrecognized(): assert not await receive_command(Command("undefined"))
def test_can_execute_dm(): mock_message = MockMessage(channel=MockDMChannel(_id=44), author=MockUser(_id=2, is_dm=True)) command = Command(name="test2", context=mock_message) assert permissions.can_execute(command)
def test_parse_command_quotes(): assert parse_command("+test \"123\"") == Command("test", "123") assert parse_command("+test \"1 2\" \"3 4 5\"") == Command( "test", "1 2", "3 4 5")