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_ping_small(): mock_client = MockClient(latency=0.00029) mock_message = MockMessage(channel=MockChannel()) mock_command = MockCommand("ping", context=mock_message, client=mock_client) assert await receive_command(mock_command) assert mock_command.response == "< 1 ms"
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_unsub(): subs = [ Subscription(guild_id=2, channel_id=6, _filter="type:nominate"), Subscription(guild_id=2, channel_id=4, _filter="user:someone"), Subscription(guild_id=1, channel_id=6, _filter="type:nominate") ] database = Database(BOT_TEST_DB_NAME) for sub in subs: database.insert_subscription(sub) subscriber.load() mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("unsub", context=mock_message) assert all(sub in subscriber.cache for sub in subs) assert await receive_command(mock_command) assert mock_command.response.startswith("✓") assert "🔕" in mock_command.response_embed.fields[0].name.lower() assert "unsubscribed from" in mock_command.response_embed.fields[ 0].name.lower() assert "type:nominate" in mock_command.response_embed.fields[0].value assert "`type:nominate`" in mock_command.response_embed.fields[0].value assert subs[0] not in subscriber.cache assert subs[1] in subscriber.cache assert subs[2] in subscriber.cache
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_disable_dm_channel(): mock_command = MockCommand("disable", "disable", context=MockMessage(channel=MockDMChannel())) assert await receive_command(mock_command) assert "✗" in mock_command.response assert "dm channel" in mock_command.response.lower() assert mock_command.response_embed.fields[0].name == help_embed("disable").fields[0].name assert mock_command.response_embed.fields[0].value == help_embed("disable").fields[0].value
async def test_filters_custom_prefix(): mock_command = MockCommand( "filters", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=8)))) set_prefix(guild_id=8, prefix="&") assert await receive_command(mock_command) assert f"`&filters <key>`" in mock_command.response
async def test_prefix_dm_channel(): mock_command = MockCommand("prefix", "&", context=MockMessage(channel=MockDMChannel())) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "DM channel" in mock_command.response
async def test_ping(): mock_channel = MockChannel() mock_client = MockClient(latency=0.13429) mock_message = MockMessage(channel=mock_channel) mock_command = MockCommand("ping", context=mock_message, client=mock_client) assert await receive_command(mock_command) assert mock_command.response == "134 ms" assert mock_channel.messages[0].content == "134 ms"
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_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_prefix_whitespace_in_symbol(): mock_command = MockCommand( "prefix", "a b", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=8)))) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "whitespace" in mock_command.response.lower()
async def test_prefix_very_long_symbol(): mock_command = MockCommand( "prefix", "a" * 2000, context=MockMessage(channel=MockChannel(guild=MockGuild(_id=8)))) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "cannot exceed" in mock_command.response
async def test_sub_undefined_type(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("sub", "type:undefined", context=mock_message) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "`type:undefined`" in mock_command.response.lower() assert not subscriber.cache
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_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"))
async def test_help(): mock_command = MockCommand("help", context=MockMessage(channel=MockChannel())) assert await receive_command(mock_command) assert f"`{DEFAULT_PREFIX}help <command>`" in mock_command.response # Should suggest using this for more details of a specific command. assert mock_command.response_embed.fields[0].name == general_help_embed( ).fields[0].name assert mock_command.response_embed.fields[0].value == general_help_embed( ).fields[0].value
async def test_recent_invalid_word(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("recent", "type:nominate eand type:qualify", context=mock_message) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "invalid word" in mock_command.response.lower()
async def test_recent_invalid_value(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("recent", "type:undefined", context=mock_message) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "invalid value" in mock_command.response.lower()
async def test_prefix(): mock_command = MockCommand( "prefix", "&", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=8)))) assert await receive_command(mock_command) assert mock_command.response.startswith("✓") assert "`+`" in mock_command.response assert "`&`" in mock_command.response assert mock_command.prefix() == "&"
async def test_sub_no_arg_no_sub(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("sub", context=mock_message) assert await receive_command(mock_command) assert f"`{DEFAULT_PREFIX}sub <filter>`" in mock_command.response # Should suggest these for if the user intended something else. assert f"`{DEFAULT_PREFIX}unsub`" not in mock_command.response # Don't need to suggest unsubscribing if we don't have a sub. assert "🔔\u2000Current Subscription" in mock_command.response_embed.fields[ 0].name assert "None" in mock_command.response_embed.fields[0].value
async def test_sub_undefined_key(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("sub", "undefined:nominate or undefined:qualify", context=mock_message) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert mock_command.response.lower().count("`undefined`") == 1 assert not subscriber.cache
async def test_help_with_arg(): mock_command = MockCommand("help", "name", context=MockMessage(channel=MockChannel())) assert await receive_command(mock_command) assert f"`{DEFAULT_PREFIX}help`" in mock_command.response # Should suggest using this for a list of commands. assert mock_command.response_embed.fields[0].name == help_embed( "name").fields[0].name assert mock_command.response_embed.fields[0].value == help_embed( "name").fields[0].value
async def test_sub_typoed_and(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("sub", "type:qualify annd type:nominate", context=mock_message) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "`annd`" in mock_command.response assert not subscriber.cache
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_filters_unrecognized_command(): mock_command = MockCommand("filters", "undefined", context=MockMessage(channel=MockChannel())) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "`undefined`" in mock_command.response assert mock_command.response_embed.fields[0].name == help_embed( "filters").fields[0].name assert mock_command.response_embed.fields[0].value == help_embed( "filters").fields[0].value
async def test_sub_dm_channel(): mock_message = MockMessage(channel=MockDMChannel(_id=6)) mock_command = MockCommand("sub", "type:nominate", context=mock_message) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "DM channel" in mock_command.response assert mock_command.response_embed.fields[ 0].name == mock_command.help_embed().fields[0].name assert mock_command.response_embed.fields[ 0].value == mock_command.help_embed().fields[0].value assert not subscriber.cache
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
async def test_perms(): mock_command = MockCommand( "perms", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=3)))) assert await receive_command(mock_command) assert f"`{DEFAULT_PREFIX}enable`" in mock_command.response assert f"`{DEFAULT_PREFIX}disable`" in mock_command.response assert "permissions" in mock_command.response_embed.fields[0].name.lower() assert mock_command.response_embed.fields[0].value.lower().startswith( "***admin-only***") assert f"`{DEFAULT_PREFIX}perms/{DEFAULT_PREFIX}permissions [command(s)]`" in mock_command.response_embed.fields[ 0].value.lower()