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_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_sub_parenthesis_inequality(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("sub", "type:(nominate", context=mock_message) assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "parenthes" in mock_command.response.lower() 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_recent_dm_channel(): mock_message = MockMessage(channel=MockDMChannel(_id=6)) mock_command = MockCommand("recent", "type:(nominate or qualify)", 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
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_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
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_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_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_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_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_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_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
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_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()
async def test_perms_custom_prefix(): mock_command = MockCommand( "perms", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=8)))) set_prefix(guild_id=8, prefix="&") assert await receive_command(mock_command) assert "`&enable`" in mock_command.response assert "`&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 "`&perms/&permissions [command(s)]`" in mock_command.response_embed.fields[ 0].value.lower()
async def test_unsub_no_sub(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("unsub", context=mock_message) assert not subscriber.cache assert await receive_command(mock_command) assert mock_command.response.startswith("✗") assert "nothing to unsub" in mock_command.response.lower() assert mock_command.response_embed.fields[0].name == help_embed( "unsub").fields[0].name assert mock_command.response_embed.fields[0].value == help_embed( "unsub").fields[0].value
async def test_recent_not_found(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("recent", "type:(nominate or qualify)", context=mock_message) assert await receive_command(mock_command) assert mock_command.response == "✗ No event matching `type:(nominate or qualify)` could be found." assert mock_command.response_embed.fields[0].name == help_embed( "recent").fields[0].name assert mock_command.response_embed.fields[0].value == help_embed( "recent").fields[0].value
async def test_filters_with_arg(): mock_command = MockCommand("filters", "user", context=MockMessage(channel=MockChannel())) assert await receive_command(mock_command) assert f"`{DEFAULT_PREFIX}filters`" in mock_command.response # Should suggest using this for a list of keys and gates. assert "**`user`**" in mock_command.response_embed.fields[0].name.lower() assert "the username" in mock_command.response_embed.fields[0].value.lower( ) assert "value(s)" in mock_command.response_embed.fields[1].name.lower() assert "accepts any value" in mock_command.response_embed.fields[ 1].value.lower() assert "example(s)" in mock_command.response_embed.fields[2].name.lower() assert "∙\u00a0`user:lasse`" in mock_command.response_embed.fields[ 2].value.lower()
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"))
async def test_sub_no_arg(): subscriber.add_subscription( Subscription(guild_id=2, channel_id=6, _filter="type:(nominate or qualify)")) 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`" in mock_command.response assert "🔔\u2000Current Subscription" in mock_command.response_embed.fields[ 0].name assert "type:(nominate or qualify)" in mock_command.response_embed.fields[ 0].value assert "`type:nominate or type:qualify`" in mock_command.response_embed.fields[ 0].value
async def test_sub_markdown_and_quotes(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("sub", "user:\"__don't underline this__\"", context=mock_message) assert await receive_command(mock_command) assert mock_command.response.startswith("✓") assert mock_command.response_embed assert mock_command.response_embed.fields assert "🔔\u2000Subscribed" in mock_command.response_embed.fields[0].name assert "user:\"\\_\\_don't underline this\\_\\_\"" in mock_command.response_embed.fields[ 0].value assert "`user:\"__don't underline this__\"`" in mock_command.response_embed.fields[ 0].value assert subscriber.cache[0].channel_id == mock_message.channel.id assert subscriber.cache[0].filter == mock_command.args[0]