async def test_remove_role_from_none(announce_cog): guild: discord.Guild = dpytest.get_config().guilds[0] author: discord.Member = guild.members[0] channel: discord.TextChannel = guild.channels[0] roles = guild.roles make_message(guild, announce_cog) assert announce_cog.roles[guild.id] == [] msg_mock: discord.Message = dpytest.back.make_message( str(roles[0].id), author, channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg_mock)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce remove', channel=channel) assert dpytest.verify().message().content( "Please enter the roles you want to remove separated by space, I'll wait for 60 seconds, no rush." ) assert dpytest.verify().message() assert announce_cog.has_active_msg(guild.id) assert announce_cog.roles[guild.id] == [] msg_mock: discord.Message = dpytest.back.make_message( "12345", author, channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg_mock)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce remove', channel=channel) assert dpytest.verify().message().content( "Please enter the roles you want to remove separated by space, I'll wait for 60 seconds, no rush." ) assert dpytest.verify().message() assert announce_cog.has_active_msg(guild.id) assert announce_cog.roles[guild.id] == []
async def test_filter_various_emails_with_regex(tf_cog): with session_manager() as session: await dpytest.message( koalabot.COMMAND_PREFIX + r"filter_regex [a-z0-9]+[\._]?[a-z0-9]+[@]+[herts]+[.ac.uk]") assert_filtered_confirmation( r"[a-z0-9]+[\._]?[a-z0-9]+[@]+[herts]+[.ac.uk]", "banned") # Should delete and warn await dpytest.message("hey [email protected]") assert_banned_warning("hey [email protected]") # Should delete and warn await dpytest.message("hey [email protected]") assert_banned_warning("hey [email protected]") # Should not warn await dpytest.message("hey herts.ac.uk") assert dpytest.verify().message().nothing() # Should not warn await dpytest.message("hey stefan@herts") assert dpytest.verify().message().nothing() cleanup(dpytest.get_config().guilds[0].id, tf_cog, session)
async def test_create_message_to_no_dm_user(bot: discord.Client, announce_cog): guild: discord.Guild = bot.guilds[0] author: discord.Member = guild.members[0] channel: discord.TextChannel = guild.channels[0] msg_mock: discord.Message = dpytest.back.make_message( 'testMessage', author, channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg_mock)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce create', channel=channel) assert dpytest.verify().message().content( "Please enter a message, I'll wait for 60 seconds, no rush.") assert dpytest.verify().message().content( f"An announcement has been created for guild {guild.name}") assert dpytest.verify().message() assert dpytest.verify().message() assert announce_cog.has_active_msg(guild.id) assert announce_cog.messages[guild.id].description == "testMessage" assert announce_cog.messages[guild.id].title == "" # sending the message with mock.patch('discord.Member.send', mock.Mock(side_effect=Exception('AttributeError'))): with pytest.raises( discord.ext.commands.errors.CommandInvokeError) as e_info: await dpytest.message(koalabot.COMMAND_PREFIX + 'announce send', channel=channel)
async def test_create_multiple_message(announce_cog): guild: discord.Guild = dpytest.get_config().guilds[0] author: discord.Member = guild.members[0] channel: discord.TextChannel = guild.channels[0] msg_mock: discord.Message = dpytest.back.make_message( 'testMessage', author, channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg_mock)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce create', channel=channel) assert dpytest.verify().message().content( "Please enter a message, I'll wait for 60 seconds, no rush.") assert dpytest.verify().message().content( f"An announcement has been created for guild {guild.name}") assert dpytest.verify().message() assert dpytest.verify().message() assert announce_cog.has_active_msg(guild.id) assert announce_cog.messages[guild.id].description == "testMessage" assert announce_cog.messages[guild.id].title == "" msg2_mock: discord.Message = dpytest.back.make_message( 'testMessage2', author, channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg2_mock)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce create', channel=channel) assert dpytest.verify().message().content( "There is currently an active announcement being created, you can use 'k!announce cancel' " "or 'k!announce send' to complete it") assert announce_cog.has_active_msg(guild.id) assert announce_cog.messages[guild.id].description == "testMessage" assert announce_cog.messages[guild.id].title == ""
async def test_send_announce_roles(bot: discord.Client, number_of_roles, announce_cog): guild: discord.Guild = dpytest.get_config().guilds[0] author: discord.Member = guild.members[0] channel: discord.TextChannel = guild.channels[0] roles = guild.roles make_message(guild, announce_cog) assert announce_cog.roles[guild.id] == [] role_list = "" role_id_list = [] for i in range(number_of_roles): role_list = role_list + str(roles[i].id) + " " role_id_list.append(roles[i].id) msg_mock: discord.Message = dpytest.back.make_message( role_list, author, channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg_mock)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce add', channel=channel) assert dpytest.verify().message().content( "Please enter the roles you want to tag separated by space, I'll wait for 60 seconds, no rush." ) assert dpytest.verify().message() assert announce_cog.has_active_msg(guild.id) assert announce_cog.roles[guild.id] == role_id_list await dpytest.message(koalabot.COMMAND_PREFIX + 'announce send', channel=channel) for _ in guild.members: assert dpytest.verify().message() assert dpytest.verify().message().content( "The announcement was made successfully")
async def test_announce_db_no_update_time_from_illegal_use(announce_cog): guild: discord.Guild = dpytest.get_config().guilds[0] author: discord.Member = guild.members[0] channel: discord.TextChannel = guild.channels[0] assert announce_cog.announce_database_manager.get_last_use_date( guild.id) is None current_time = int(time.time()) announce_cog.announce_database_manager.set_last_use_date( guild.id, current_time) assert announce_cog.announce_database_manager.get_last_use_date( guild.id) == current_time msg_mock: discord.Message = dpytest.back.make_message( 'testMessage', author, channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg_mock)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce create', channel=channel) assert dpytest.verify().message().content( "You have recently sent an announcement and cannot use this function for 30 days" ) assert not announce_cog.has_active_msg(guild.id) assert announce_cog.announce_database_manager.get_last_use_date( guild.id) == current_time await dpytest.message(koalabot.COMMAND_PREFIX + 'announce send', channel=channel) assert dpytest.verify().message().content( "There is currently no active announcement") assert announce_cog.announce_database_manager.get_last_use_date( guild.id) == current_time
async def test_player_remove_multi_guild(guild_factory, text_channel_factory): guild_a = guild_factory.make(0) guild_b = guild_factory.make(1) text_channel_a = await text_channel_factory.make(guild_a) text_channel_b = await text_channel_factory.make(guild_b) await message("!player add 76561198029447509", channel=text_channel_a) assert verify() \ .message() \ .contains() \ .content("Successfully linked") await message("!player add 76561198029447509", channel=text_channel_b) assert verify() \ .message() \ .contains() \ .content("Successfully linked") await message("!player remove", channel=text_channel_a) assert verify() \ .message() \ .contains() \ .content("Successfully unlinked") await message("!player remove", channel=text_channel_b) assert verify() \ .message() \ .contains() \ .content("Successfully unlinked")
async def test_help(bot, help_message): await dpytest.message(f"{PREFIX} aide") assert ErrorMessages.COMMAND_NOT_FOUND.format( "aide") not in dpytest.get_embed(peek=True).description assert not dpytest.verify().message().nothing() a = await Help.build_with_args() assert dpytest.verify().message().embed(help_message)
async def test_rfr_delete_message(): with session_manager() as session: config: dpytest.RunnerConfig = dpytest.get_config() guild: discord.Guild = config.guilds[0] channel: discord.TextChannel = guild.text_channels[0] message: discord.Message = await dpytest.message("rfr") msg_id = message.id DBManager.add_rfr_message(guild.id, channel.id, msg_id) await dpytest.empty_queue() with mock.patch('koala.cogs.ReactForRole.get_rfr_message_from_prompts', mock.AsyncMock(return_value=(message, channel))): with mock.patch('koala.cogs.ReactForRole.prompt_for_input', mock.AsyncMock(return_value="Y")): with mock.patch('discord.Message.delete') as mock_msg_delete: await dpytest.message(koalabot.COMMAND_PREFIX + "rfr deleteMessage") mock_msg_delete.assert_called_once() assert dpytest.verify().message().content( "Okay, this will delete an existing react for role message. I'll need some details first though." ) assert dpytest.verify().message() assert dpytest.verify().message() assert dpytest.verify().message() assert not independent_get_guild_rfr_message( session, guild.id, channel.id, msg_id)
async def test_rfr_create_message(bot): config: dpytest.RunnerConfig = dpytest.get_config() guild: discord.Guild = config.guilds[0] channel: discord.TextChannel = guild.text_channels[0] embed_channel: discord.TextChannel = dpytest.back.make_text_channel( 'EmbedChannel', guild) author: discord.Member = config.members[0] test_embed = discord.Embed(title="React for Role", description="Roles below!", colour=KOALA_GREEN) test_embed.set_footer(text="ReactForRole") test_embed.set_thumbnail(url=koalabot.KOALA_IMAGE_URL) with mock.patch('koala.cogs.ReactForRole.prompt_for_input', mock.AsyncMock(return_value=embed_channel.mention)): with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=None)): with mock.patch('koala.cogs.ReactForRole.is_user_alive', mock.AsyncMock(return_value=True)): with mock.patch( 'koala.cogs.ReactForRole.overwrite_channel_add_reaction_perms' ) as mock_edit_channel_perms: with mock.patch('discord.Message.delete') as mock_delete: await dpytest.message(koalabot.COMMAND_PREFIX + "rfr createMessage") mock_edit_channel_perms.assert_called_once_with( guild, embed_channel) assert dpytest.verify().message().content( "Okay, this will create a new react for role message in a channel of your choice." "\nNote: The channel you specify will have its permissions edited to make it such that the " "@ everyone role is unable to add new reactions to messages, they can only reaction with " "existing ones. Please keep this in mind, or setup another channel entirely for this." ) assert dpytest.verify().message().content( "This should be a thing sent in the right channel." ) assert dpytest.verify().message().content( "Okay, what would you like the title of the react for role message to be? Please enter within 60 seconds." ) assert dpytest.verify().message().content( "Okay, didn't receive a title. Do you actually want to continue? Send anything to confirm this." ) assert dpytest.verify().message().content( "Okay, I'll just put in a default value for you, you can edit it later by using the k!rfr edit commands." ) assert dpytest.verify().message().content( "Okay, the title of the message will be \"React for Role\". What do you want the description to be? I'll wait 60 seconds, don't worry" ) assert dpytest.verify().message().content( "Okay, didn't receive a description. Do you actually want to continue? Send anything to confirm this." ) assert dpytest.verify().message().content( "Okay, I'll just put in a default value for you, you can edit it later by using the k!rfr edit command." ) assert dpytest.verify().message().content( "Okay, the description of the message will be \"Roles below!\".\n Okay, I'll create the react for role message now." ) assert dpytest.verify().message() msg = dpytest.sent_queue.get_nowait() assert "You can use the other k!rfr subcommands to change the message and add functionality as required." in msg.content mock_delete.assert_called_once()
async def test_prompt_for_input_str(msg_content, utils_cog, rfr_cog): config: dpytest.RunnerConfig = dpytest.get_config() author: discord.Member = config.members[0] guild: discord.Guild = config.guilds[0] channel: discord.TextChannel = guild.text_channels[0] await dpytest.message(koalabot.COMMAND_PREFIX + "store_ctx") ctx: commands.Context = utils_cog.get_last_ctx() await dpytest.empty_queue() if not msg_content: with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=None)): result = await rfr_cog.prompt_for_input(ctx, "test") assert dpytest.verify().message().content( "Please enter test so I can progress further. I'll wait 60 seconds, don't worry." ) assert dpytest.verify().message().content( "Okay, I'll cancel the command.") assert not result else: msg: discord.Message = dpytest.back.make_message(content=msg_content, author=author, channel=channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg)): result = await rfr_cog.prompt_for_input(ctx, "test") assert dpytest.verify().message().content( "Please enter test so I can progress further. I'll wait 60 seconds, don't worry." ) assert result == msg_content
async def test_discord_create_vote_wrong(): with session_manager() as session: config = dpytest.get_config() guild = config.guilds[0] session.add( Votes(vote_id=111, author_id=guild.members[0].id, guild_id=guild.id, title="Test Vote")) session.commit() await dpytest.message(f"{koalabot.COMMAND_PREFIX}vote create Test Vote" ) assert dpytest.verify().message().content( "You already have a vote with title Test Vote sent!") await dpytest.message( f"{koalabot.COMMAND_PREFIX}vote create aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" f"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" f"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) assert dpytest.verify().message().content("Title too long") await dpytest.message( f"{koalabot.COMMAND_PREFIX}vote create Test Vote 2") assert dpytest.verify().message().content( f"Vote titled `Test Vote 2` created for guild {guild.name}. Use `{koalabot.COMMAND_PREFIX}help vote` " f"to see how to configure it.") await dpytest.message( f"{koalabot.COMMAND_PREFIX}vote create Test Vote 3") assert dpytest.verify().message().content( f"You already have an active vote in {guild.name}. Please send that with `{koalabot.COMMAND_PREFIX}vote " f"send` before creating a new one.")
async def test_send_announce_roles_with_no_dm_user(bot: discord.Client, number_of_roles, announce_cog): guild: discord.Guild = dpytest.get_config().guilds[0] author: discord.Member = guild.members[0] channel: discord.TextChannel = guild.channels[0] roles = guild.roles make_message(guild, announce_cog) assert announce_cog.roles[guild.id] == [] role_list = "" role_id_list = [] for i in range(number_of_roles): role_list = role_list + str(roles[i].id) + " " role_id_list.append(roles[i].id) msg_mock: discord.Message = dpytest.back.make_message( role_list, author, channel) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=msg_mock)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce add', channel=channel) assert dpytest.verify().message().content( "Please enter the roles you want to tag separated by space, I'll wait for 60 seconds, no rush." ) assert dpytest.verify().message() assert announce_cog.has_active_msg(guild.id) assert announce_cog.roles[guild.id] == role_id_list # sending the message with mock.patch('discord.Member.send', mock.Mock(side_effect=Exception('AttributeError'))): with pytest.raises( discord.ext.commands.errors.CommandInvokeError) as e_info: await dpytest.message(koalabot.COMMAND_PREFIX + 'announce send', channel=channel)
async def test_remove_activity(): await test_list_activity_show_all() await dpytest.message(koalabot.COMMAND_PREFIX + "activity remove 1") assert dpytest.verify().message().content( "Removed:" "\n1, playing, None, test, 2020-01-01 00:00:00, 2020-01-01 01:00:00") await dpytest.message(koalabot.COMMAND_PREFIX + "activity list true") assert dpytest.verify().message().content("Activities:")
async def test_message_peek(bot): guild = bot.guilds[0] channel = guild.text_channels[0] await channel.send("Hello, world !") # peek option doesn't remove the message fro the queue assert dpytest.verify().message().peek().content("Hello, world !") # verify_message (without peek) WILL remove message from the queue assert dpytest.verify().message().content("Hello, world !")
async def test_preview_consistent(announce_cog): guild: discord.Guild = dpytest.get_config().guilds[0] channel: discord.TextChannel = guild.channels[0] make_message(guild, announce_cog) embed: discord.Embed = announce_cog.construct_embed(guild) await dpytest.message(koalabot.COMMAND_PREFIX + 'announce preview', channel=channel) assert dpytest.verify().message().embed(embed=embed) assert dpytest.verify().message()
async def test_feature_remove_feature_flag_player(feature_flag): await message(f"!feature enable {feature_flag}") assert verify() \ .message() \ .content(f"Enabled {feature_flag} feature!") await message(f"!feature disable {feature_flag}") assert verify() \ .message() \ .content(f"Disabled {feature_flag} feature!")
async def test_feature_enable_feature_flag_already_enabled(feature_flag): await message(f"!feature enable {feature_flag}") assert verify() \ .message() \ .content(f"Enabled {feature_flag} feature!") await message(f"!feature enable {feature_flag}") assert verify() \ .message() \ .content(f"Feature flag {feature_flag} is already enabled!")
async def test_recentsong_negative(player): await message("!recentsong -1") if player: assert not verify().message().nothing() await empty_queue() else: assert verify() \ .message() \ .contains() \ .content("Player not found!")
async def test_custom_colour_no_no_colour_role(): guild: discord.Guild = dpytest.get_config().guilds[0] role = (await make_list_of_roles(guild, 1))[0] DBManager.add_colour_change_role_perms(guild.id, role.id) member: discord.Member = dpytest.get_config().members[0] await member.add_roles(role) await dpytest.message(koalabot.COMMAND_PREFIX + "custom_colour no", member=0) assert dpytest.verify().message().content("Okay, removing your old custom colour role then, if you have one.") assert dpytest.verify().message().content(f"{member.mention} you don't have any colour roles to remove.") assert dpytest.verify().message().nothing()
async def test_send_welcome_message_timeout(): with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=None)): await dpytest.message(koalabot.COMMAND_PREFIX + "send_welcome_message") assert dpytest.verify().message().content( "This will DM 1 people. Are you sure you wish to do this? Y/N") assert dpytest.verify().message().content('Timed out.') assert dpytest.verify().message().content( "Okay, I won't send out the welcome message then.") assert dpytest.verify().message().nothing()
async def test_discord_cancel_vote(): config = dpytest.get_config() guild = config.guilds[0] await dpytest.message(f"{koalabot.COMMAND_PREFIX}vote create Test Vote") assert dpytest.verify().message().content( f"Vote titled `Test Vote` created for guild {guild.name}. Use `{koalabot.COMMAND_PREFIX}help vote` to see how " f"to configure it.") await dpytest.message(f"{koalabot.COMMAND_PREFIX}vote cancel Test Vote") assert dpytest.verify().message().content( "Vote Test Vote has been cancelled.")
async def test_dm_plural_group_message(): test_message = 'default message' test_member = dpytest.get_config().members[0] test_member_2 = await dpytest.member_join() await dpytest.empty_queue() x = await koalabot.dm_group_message([test_member, test_member_2], test_message) assert dpytest.verify().message().content(test_message) assert dpytest.verify().message().content(test_message) assert x == 2
async def test_feature_enable_feature_flag_player(feature_flag): await message(f"!player add 76561198029447509") assert verify() \ .message() \ .contains() \ .content("Successfully linked") await message(f"!feature enable {feature_flag}") assert verify() \ .message() \ .content(f"Enabled {feature_flag} feature!")
async def test_verify_activity_matches(bot): fake_act = discord.Activity(name="Streaming", url="http://mystreamingfeed.xyz", type=discord.ActivityType.streaming) await bot.change_presence(activity=fake_act) assert dpytest.verify().activity().matches(fake_act) other_act = discord.Activity(name="Playing Around", type=discord.ActivityType.playing) await bot.change_presence(activity=other_act) assert not dpytest.verify().activity().matches(fake_act)
async def test_create_message_timeout(): guild: discord.Guild = dpytest.get_config().guilds[0] channel: discord.TextChannel = guild.channels[0] with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=None)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce create', channel=channel) assert dpytest.verify().message().content( "Please enter a message, I'll wait for 60 seconds, no rush.") assert dpytest.verify().message().content( "Okay, I'll cancel the command.")
async def test_player_remove(): await message("!player add 76561198029447509") assert verify() \ .message() \ .contains() \ .content("Successfully linked") await message("!player remove") assert verify() \ .message() \ .contains() \ .content("Successfully unlinked")
async def test_player_add_add(): await message("!player add 76561198029447509") assert verify() \ .message() \ .contains() \ .content("Successfully linked") await message("!player add 76561198029447509") assert verify() \ .message() \ .contains() \ .content("has already been added!")
async def test_channel_add_add(text_channel): await message("!channel add", channel=text_channel) assert verify()\ .message()\ .contains()\ .content(f"Channel **{text_channel.name}** has successfully set as the notification channel!") await message("!channel add", channel=text_channel) assert verify()\ .message()\ .contains()\ .content(f"Channel **{text_channel.name}** has already been set as the notification channel!")
async def test_other_timeout(command_word, prompt_message, announce_cog): guild: discord.Guild = dpytest.get_config().guilds[0] channel: discord.TextChannel = guild.channels[0] make_message(guild, announce_cog) with mock.patch('discord.client.Client.wait_for', mock.AsyncMock(return_value=None)): await dpytest.message(koalabot.COMMAND_PREFIX + 'announce ' + command_word, channel=channel) assert dpytest.verify().message().content(prompt_message) assert dpytest.verify().message().content( "Okay, I'll cancel the command.")