async def test_return_true_if_stoplight_in_message(self): green_stoplight_message = AsyncMock() green_stoplight_message.content = "This is good!! Please carry on. 🟢🟢🟢" yellow_stoplight_message = AsyncMock() yellow_stoplight_message.content = "🟡 Mmmm, could you try something else?" red_stoplight_message = AsyncMock() red_stoplight_message.content = "🔴 This is making me uncomfortable." self.assertTrue(await check_for_stoplights(green_stoplight_message)) self.assertTrue(await check_for_stoplights(yellow_stoplight_message)) self.assertTrue(await check_for_stoplights(red_stoplight_message))
def to_mock(self): """Returns an AsyncMock matching the spec for this class""" # we still have to set stuff manually but changing values is nicer mock = AsyncMock(name="Message Mock", spec=discord.Message) # Author section mock.author = MockedMember( name=self.author["name"], member_id=self.author["id"], is_bot=self.author["is_bot"], ).to_mock() # Guild options and 'is_in_guild' are mutually exclusive if not self.is_in_guild: mock.guild = None else: mock.guild = MockedGuild(name=self.guild["name"], guild_id=self.guild["id"]).to_mock() mock.channel = MockedChannel().to_mock() mock.created_at = datetime.datetime.now() mock.id = self.message_id mock.content = self.content mock.clean_content = self.clean_content return mock
async def test_alert_mods_and_return_true_if_clock_in_message(self, mocked_get): clock_message = AsyncMock() clock_message.content = "I need help. ⏰" self.assertTrue(await check_for_stoplights(clock_message)) clock_message.channel.send.assert_called_once_with(f"Moderators needed {StoplightsTest.mocked_mod_role.mention}!")
async def test_store_drone(self, fetch_storage_by_target_id, fetch_drone_with_drone_id, insert_storage, mocked_datetime): # setup message = AsyncMock() message.channel.name = channels.STORAGE_FACILITY message.content = "9813 :: 3287 :: 8 :: recharge" message.author.roles = [drone_role] message.guild.roles = [hive_mxtress_role, drone_role, development_role, stored_role] message.guild.channels = [storage_chambers] drone_member = AsyncMock() drone_member.roles = [drone_role, development_role] drone_member.mention = "<3287mention>" message.guild.get_member = Mock(return_value=drone_member) fixed_now = datetime.now() mocked_datetime.now.return_value = fixed_now # run & assert self.assertFalse(await storage.store_drone(message)) message.guild.get_member.assert_called_once_with('3287snowflake') drone_member.remove_roles.assert_called_once_with(drone_role, development_role) drone_member.add_roles.assert_called_once_with(stored_role) self.assertEqual(insert_storage.call_args.args[0].stored_by, "9813") self.assertEqual(insert_storage.call_args.args[0].target_id, "3287") self.assertEqual(insert_storage.call_args.args[0].purpose, "recharge") self.assertEqual(insert_storage.call_args.args[0].roles, f"{roles.DRONE}|{roles.DEVELOPMENT}") self.assertEqual(insert_storage.call_args.args[0].release_time, str(fixed_now + timedelta(hours=8))) storage_chambers.send.assert_called_once_with("Greetings <3287mention>. You have been stored away in the Hive Storage Chambers by 9813 for 8 hours and for the following reason: recharge")
async def test_onmessage_command(kudos: ChatKudos, async_message: AsyncMock) -> None: """Give a command, ensure we hit the command path""" async_message.content = "kudos!help" await kudos.on_message(async_message) async_message.channel.send.assert_called_once()
def test_ifTimelimitExceeded_whileProcessingMessage_thenAbortMessage(self): msg = AsyncMock() msg.content = "1000000d1000000h1" msg.channel.send = AsyncMock() msg.author.display_name = "bert" with self.assertLogs("main", logging.WARNING): with self.assertRaises(ParserTimeoutError): run(on_message(msg)) msg.channel.send.assert_called()
async def test_print_status_code_invalid(self): # setup message = AsyncMock() message.content = "9813 :: beep boop" # run response = await speech_optimization.print_status_code(message) # assert message.delete.assert_not_called() self.assertFalse(response)
async def test_print_status_code_plain(self): # setup message = AsyncMock() message.content = "9813 :: 050" # run response = await speech_optimization.print_status_code(message) # assert message.delete.assert_called_once() self.assertEqual(response, "9813 :: Code `050` :: Statement")
async def test_storage_message_invalid(self): # setup message = AsyncMock() message.channel.name = channels.STORAGE_FACILITY message.content = "beep boop wants to recharge" message.author.roles = [drone_role] # run & assert self.assertTrue(await storage.store_drone(message)) message.channel.send.assert_called_once_with(storage.REJECT_MESSAGE)
async def test_storage_message_duration_too_long(self, fetch_storage_by_target_id): # setup message = AsyncMock() message.channel.name = channels.STORAGE_FACILITY message.content = "9813 :: 3287 :: 25 :: recharge" message.author.roles = [drone_role] # run & assert self.assertTrue(await storage.store_drone(message)) fetch_storage_by_target_id.assert_called_once_with('3287') message.channel.send.assert_called_once_with("25 is not between 0 and 24.")
async def test_optimize_speech_code_and_clarification(self): # setup message = AsyncMock() message.content = "3287 :: 050 :: All drones are cute~" message.author.display_name = "⬡-Drone #3287" message.author.roles = [optimized_role] # run await speech_optimization.optimize_speech(message) # assert message.delete.assert_called_once()
async def test_optimize_speech_ignore_non_optimized(self): # setup message = AsyncMock() message.content = "It is a cute beep boop" message.author.display_name = "⬡-Drone #3287" message.author.roles = [] # run await speech_optimization.optimize_speech(message) # assert message.delete.assert_not_called()
async def test_storage_message_already_in_storage(self, fetch_storage_by_target_id): # setup message = AsyncMock() message.channel.name = channels.STORAGE_FACILITY message.content = "9813 :: 3287 :: 6 :: recharge" message.author.roles = [drone_role] # run & assert self.assertTrue(await storage.store_drone(message)) fetch_storage_by_target_id.assert_called_once_with('3287') message.channel.send.assert_called_once_with("3287 is already in storage.")
def test_fire_registered_event_cancel(self): bot = Bot("app_name", "version") on_message = AsyncMock() on_message.__name__ = "on_message" on_message.return_value = False bot.register_event(on_message) watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) message = AsyncMock() message.content = "hello there" self._await(bot.on_message(message)) on_message.assert_awaited_once_with(message) watcher_callback.assert_not_awaited()
def test_no_mention(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "hello there" self._await(bot.on_message(message)) simple_callback.assert_not_awaited() watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_not_awaited()
def fixture_async_message() -> AsyncMock: """Fixture""" message = AsyncMock() message.content = "<#!111> + <!222#> + Oh yeah baby!" message.guild.name = "Testing Guild" message.guild.id = 111 message.guild.owner.id = 333 message.author.id = 222 message.author.display_name = "Tester" message.channel.type = "text" message.mentions = [ AsyncMock(id="111", display_name="Tester01"), AsyncMock(id="222", display_name="Tester02"), ] return message
def test_mention_command_regex(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False bot.client.user.id = "12345" regex_callback = AsyncMock() bot.register_command("^t[eo]a?st$", regex_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> toast hey" self._await(bot.on_message(message)) regex_callback.assert_awaited_once_with(bot.client, message, "toast", "hey") watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_not_awaited()
def test_mention_self(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False bot.client.user.id = "12345" simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> test arg0 arg1" message.author = bot.client.user self._await(bot.on_message(message)) simple_callback.assert_not_awaited() watcher_callback.assert_not_awaited() fallback_callback.assert_not_awaited()
def test_mention_direct(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False bot.client.user.id = "12345" simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> test arg0 arg1" message.channel.type == discord.ChannelType.private self._await(bot.on_message(message)) simple_callback.assert_awaited_once_with(bot.client, message, "test", "arg0", "arg1") watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_not_awaited()
def test_lower_command_names_off(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False bot.lower_command_names = False bot.client.user.id = "12345" simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> Test arg0 arg1" self._await(bot.on_message(message)) simple_callback.assert_not_awaited() watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_awaited_once_with(bot.client, message, "Test", "arg0", "arg1")
async def test_optimize_speech(self, get_webhook_for_channel, send_webhook_with_specific_output): # setup message = AsyncMock() message.content = "3287 :: 122" message.author.display_name = "⬡-Drone #3287" message.author.roles = [optimized_role] webhook = AsyncMock() get_webhook_for_channel.return_value = webhook # run await speech_optimization.optimize_speech(message) # assert message.delete.assert_called_once() get_webhook_for_channel.assert_called_once_with(message.channel) send_webhook_with_specific_output.assert_called_once_with( message, webhook, "3287 :: Code `122` :: Statement :: You are cute.")
def test_mention_no_permission(self): bot = Bot("app_name", "version") bot.client.user.id = "12345" simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> test hey" message.channel.__repr__ = lambda *a: "test_channel" message.guild.__repr__ = lambda *a: "test_guild" permissions = AsyncMock() permissions.send_messages = False message.channel.permissions_for = lambda u: permissions self._await(bot.on_message(message)) simple_callback.assert_not_awaited() watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_not_awaited() message.author.create_dm.assert_awaited_once() message.author.dm_channel.send.assert_awaited_once_with( f"Hi, this bot doesn't have the permission to send a message to" f" #test_channel in server 'test_guild'")
def test_no_mention_minial(self): bot = Bot("app_name", "version") message = AsyncMock() message.content = "hello there" self._await(bot.on_message(message))
async def test_return_false_if_no_stoplight_or_clock_in_message(self): benign_message = AsyncMock() benign_message.content = "9813 :: It feels good to obey. Beep boop." self.assertFalse(await check_for_stoplights(benign_message))