예제 #1
0
    def test_register_event_existing(self):
        async def on_ready():
            pass

        bot = Bot("app_name", "version")
        bot.client.event = MagicMock()
        bot.register_event(on_ready)
        bot.client.event.assert_not_called()
예제 #2
0
    def test_register_event_normal(self):
        async def on_connect():
            pass

        bot = Bot("app_name", "version")
        bot.client.event = MagicMock()
        bot.register_event(on_connect)
        bot.client.event.assert_called_once_with(on_connect)
예제 #3
0
 def test_long(self):
     bot = Bot("app_name", "version")
     bot.register_command("test1", None, None, "long desc")
     message = AsyncMock()
     self._await(bot.help(None, message, "help", "test1"))
     message.channel.send.assert_awaited_once_with("long desc",
                                                   reference=message,
                                                   mention_author=False)
예제 #4
0
 def test_not_found(self):
     bot = Bot("app_name", "version")
     message = AsyncMock()
     self._await(bot.help(None, message, "help", "notfound"))
     message.channel.send.assert_awaited_once_with(
         f"Command `notfound` not found",
         reference=message,
         mention_author=False,
     )
예제 #5
0
 def test_add_regex(self):
     bot = Bot("app_name", "version")
     self.assertEqual(2, len(bot._Bot__commands))
     callback = AsyncMock()
     bot.register_command("^t[eo]a?st$", callback, "short", "long")
     self.assertEqual(3, len(bot._Bot__commands))
     cmd = bot._Bot__commands[0]
     self.assertEqual("^t[eo]a?st$", cmd.regex)
     self.assertEqual(callback, cmd.compute)
     self.assertEqual("short", cmd.help_short)
     self.assertEqual("long", cmd.help_long)
예제 #6
0
 def test_no_log(self, client_mock):
     bot = Bot("app_name", "version")
     bot.guild_logs_file = None
     ex = Exception("test")
     client_mock.change_presence.side_effect = ex
     try:
         with patch("discord.Game") as game_mock:
             game_mock.return_value = "activity"
             self._await(bot.on_ready())
     except Exception as error:
         self.assertEqual(ex, error)
     client_mock.change_presence.assert_called_with(
         activity="activity", status=discord.Status.online)
예제 #7
0
 def test_list_alias(self):
     bot = Bot("app_name", "version", alias="¡")
     message = AsyncMock()
     self._await(bot.help(None, message, "help"))
     message.channel.send.assert_awaited_once_with(
         f"```\n"
         f"List of available commands:\n"
         f"* ¡info: show description\n"
         f"* ¡help: show this help\n"
         f"```",
         reference=message,
         mention_author=False,
     )
예제 #8
0
 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()
예제 #9
0
 def test(self):
     bot = Bot("app_name", "version")
     message = AsyncMock()
     t0 = datetime.now()
     bot._Bot__t0 = t0
     bot.client.guilds = [None, None, None]
     self._await(bot.info(None, message, "info"))
     message.channel.send.assert_awaited_once_with(
         f"```\n"
         f"app_name vversion\n"
         f"* Started at {t0:%Y-%m-%d %H:%M}\n"
         f"* Connected to 3 guilds\n"
         f"```",
         reference=message,
         mention_author=False,
     )
예제 #10
0
 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()
예제 #11
0
 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()
예제 #12
0
 def test_alias(self):
     bot = Bot("app_name", "version", alias="alias")
     self.assertEqual("app_name", bot.app_name)
     self.assertEqual("version", bot.version)
     self.assertEqual("alias", bot.alias)
     discord.Client.assert_called_once()
     self.assertEqual(2, len(bot._Bot__commands))
     self.assertEqual(3, len(bot.games))
예제 #13
0
 def test_list_functions(self):
     bot = Bot("app_name", "version")
     bot.register_command("", None, "test1: desc1", None)
     bot.register_command("", None, "test2: desc2", None)
     message = AsyncMock()
     self._await(bot.help(None, message, "help"))
     message.channel.send.assert_awaited_once_with(
         f"```\n"
         f"List of available commands:\n"
         f"* test2: desc2\n"
         f"* test1: desc1\n"
         f"* info: show description\n"
         f"* help: show this help\n"
         f"```",
         reference=message,
         mention_author=False,
     )
예제 #14
0
 def test_log_exists(self, client_mock):
     bot = Bot("app_name", "version")
     bot.guild_logs_file = self.LOG_PATH
     client_mock.change_presence.side_effect = Exception("test")
     client_mock.guilds = [MagicMock()]
     client_mock.guilds[0].id = "id1"
     client_mock.guilds[0].name = "name1"
     with open(self.LOG_PATH, encoding="utf-8", mode="w") as f:
         f.write("test")
     try:
         with patch("discord.Game") as game_mock:
             game_mock.return_value = "activity"
             self._await(bot.on_ready())
     except:
         pass
     with open(self.LOG_PATH, encoding="utf-8", mode="r") as f:
         self.assertEqual(f"test", f.read())
예제 #15
0
 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()
예제 #16
0
 def test_log(self):
     bot = Bot("app_name", "version")
     bot.guild_logs_file = self.LOG_PATH
     guild1 = AsyncMock()
     guild1.id = "id1"
     guild1.name = "name1"
     guild2 = AsyncMock()
     guild2.id = "id2"
     guild2.name = "name2"
     d = datetime.now()
     self._await(bot.on_guild_remove(guild1))
     self._await(bot.on_guild_remove(guild2))
     self.assertTrue(path.exists(self.LOG_PATH))
     with open(self.LOG_PATH, encoding="utf-8", mode="r") as f:
         self.assertEqual(
             f"{d:%Y-%m-%d %H:%M} -id1: name1\n"
             f"{d:%Y-%m-%d %H:%M} -id2: name2\n",
             f.read(),
         )
예제 #17
0
 def test_log_create(self, client_mock):
     bot = Bot("app_name", "version")
     bot.guild_logs_file = self.LOG_PATH
     client_mock.change_presence.side_effect = Exception("test")
     client_mock.guilds = [MagicMock(), MagicMock()]
     client_mock.guilds[0].id = "id1"
     client_mock.guilds[0].name = "name1"
     client_mock.guilds[1].id = "id2"
     client_mock.guilds[1].name = "name2"
     d = datetime.now()
     try:
         with patch("discord.Game") as game_mock:
             game_mock.return_value = "activity"
             self._await(bot.on_ready())
     except:
         pass
     with open(self.LOG_PATH, encoding="utf-8", mode="r") as f:
         self.assertEqual(
             f"{d:%Y-%m-%d %H:%M} +id1: name1\n"
             f"{d:%Y-%m-%d %H:%M} +id2: name2\n",
             f.read(),
         )
예제 #18
0
 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()
예제 #19
0
 def test_fire_registered_event_cancel(self):
     bot = Bot("app_name", "version")
     bot.guild_logs_file = self.LOG_PATH
     on_guild_remove = AsyncMock()
     on_guild_remove.__name__ = "on_guild_remove"
     on_guild_remove.return_value = False
     bot.register_event(on_guild_remove)
     guild = AsyncMock()
     self._await(bot.on_guild_remove(guild))
     on_guild_remove.assert_awaited_once_with(guild)
     self.assertFalse(path.exists(self.LOG_PATH))
예제 #20
0
 def test_fire_registered_event(self):
     bot = Bot("app_name", "version")
     bot.guild_logs_file = self.LOG_PATH
     on_guild_join = AsyncMock()
     on_guild_join.__name__ = "on_guild_join"
     on_guild_join.return_value = True
     bot.register_event(on_guild_join)
     guild = AsyncMock()
     self._await(bot.on_guild_join(guild))
     on_guild_join.assert_awaited_once_with(guild)
     self.assertTrue(path.exists(self.LOG_PATH))
예제 #21
0
 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'")
예제 #22
0
 def test_fire_registered_event_cancel(self, client_mock):
     bot = Bot("app_name", "version")
     bot.guild_logs_file = None
     on_ready = AsyncMock()
     on_ready.__name__ = "on_ready"
     on_ready.return_value = False
     bot.register_event(on_ready)
     ex = Exception("test")
     client_mock.change_presence.side_effect = ex
     try:
         with patch("discord.Game") as game_mock:
             game_mock.return_value = "activity"
             self._await(bot.on_ready())
     except Exception as error:
         self.assertEqual(ex, error)
     on_ready.assert_awaited_once()
     client_mock.change_presence.assert_not_called()
예제 #23
0
 def test_no_log(self):
     bot = Bot("app_name", "version")
     bot.guild_logs_file = None
     guild = AsyncMock()
     self._await(bot.on_guild_join(guild))
     self.assertFalse(path.exists(self.LOG_PATH))
예제 #24
0
 def test_add_simple(self):
     bot = Bot("app_name", "version")
     bot.register_command("test", None, None, None)
     cmd = bot._Bot__commands[0]
     self.assertEqual("^test$", cmd.regex)
예제 #25
0
 def test_no_mention_minial(self):
     bot = Bot("app_name", "version")
     message = AsyncMock()
     message.content = "hello there"
     self._await(bot.on_message(message))
예제 #26
0
 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")