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_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_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_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_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_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()
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_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_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_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_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
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_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_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_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
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_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_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_enable(): mock_command = MockCommand( "enable", "enable", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=3)))) assert await receive_command(mock_command) assert "✓" in mock_command.response expected_embed = permissions_embed( guild_id=3, command_wrappers=[get_wrapper("enable")]) 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 get_permission_filter( guild_id=3, command_wrapper=get_wrapper("enable")) == "role:<@&0>"
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(): mock_message = MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=2))) mock_command = MockCommand("sub", "type:(nominate or qualify)", 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 "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 assert subscriber.cache[0].channel_id == mock_message.channel.id assert subscriber.cache[0].filter == mock_command.args[0]
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]
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)
async def test_recent(): beatmapset = Beatmapset(1, "artist", "title", creator=User(2, "sometwo"), modes=["osu"]) event1 = Event("nominate", from_string("2020-01-01 00:00:00"), beatmapset, user=User(1, "someone")) event2 = Event("qualify", from_string("2020-01-01 01:00:00"), beatmapset, user=User(4, "somefour"), content="nicely done") database = Database(SCRAPER_TEST_DB_NAME) database.insert_event(event1) database.insert_event(event2) 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.startswith("✓") assert "https://osu.ppy.sh/beatmapsets/1" in mock_command.response assert mock_command.response_embed assert mock_command.response_embed.fields assert mock_command.response_embed.fields[0].name.startswith( ":heart:\u2000Qualified (**") assert mock_command.response_embed.fields[0].name.endswith("** ago)") assert "artist - title" in mock_command.response_embed.fields[0].value assert "sometwo" in mock_command.response_embed.fields[0].value assert mock_command.response_embed.footer.text == "somefour \"nicely done\"" assert mock_command.response_embed.footer.icon_url == "https://a.ppy.sh/4"
def test_get_prefix_context(): set_prefix(3, "&") mock_message = MockMessage(channel=MockChannel(guild=MockGuild(_id=3))) assert get_prefix(mock_message) == "&"
def test_guild_id(): command = Command("test", context=MockMessage( channel=MockChannel(_id=6, guild=MockGuild(_id=7)))) assert command.guild_id() == 7