test_home_guild.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Tests du serveur du bot : rien n'est accepté ailleurs. Lancer depuis la racine du projet : python -m unittest"""
  2. import unittest
  3. from unittest import mock
  4. import discord
  5. from bot.discord_bot import ChatBot, HomeTree
  6. HOME, OTHER = 1000, 2000
  7. CHANNEL_ID = 10
  8. def fake_bot(channel_found: bool = True) -> mock.Mock:
  9. bot = mock.Mock()
  10. bot.settings.channel_id = CHANNEL_ID
  11. bot.get_channel.return_value = mock.Mock(guild=mock.Mock(id=HOME)) if channel_found else None
  12. bot.home_guild_id = lambda: ChatBot.home_guild_id(bot)
  13. bot.handle_main_channel = mock.AsyncMock()
  14. bot.handle_mention = mock.AsyncMock()
  15. bot.register_members = mock.AsyncMock()
  16. bot.welcome = mock.AsyncMock()
  17. return bot
  18. def fake_interaction(guild_id: int | None) -> mock.Mock:
  19. interaction = mock.Mock(guild_id=guild_id, type=discord.InteractionType.application_command,
  20. data={"name": "memoire-serveur"})
  21. interaction.response.send_message = mock.AsyncMock()
  22. return interaction
  23. class CommandsTest(unittest.IsolatedAsyncioTestCase):
  24. async def check(self, interaction: mock.Mock, channel_found: bool = True) -> bool:
  25. tree = object.__new__(HomeTree) # sans client Discord réel
  26. tree.client = fake_bot(channel_found)
  27. return await tree.interaction_check(interaction)
  28. async def test_home_guild_accepted(self):
  29. interaction = fake_interaction(HOME)
  30. self.assertTrue(await self.check(interaction))
  31. interaction.response.send_message.assert_not_awaited()
  32. async def test_other_guild_refused(self):
  33. # Administrateur d'un autre serveur où le bot a été invité : ni lecture ni écriture de la mémoire.
  34. interaction = fake_interaction(OTHER)
  35. self.assertFalse(await self.check(interaction))
  36. interaction.response.send_message.assert_awaited_once()
  37. async def test_direct_message_refused(self):
  38. self.assertFalse(await self.check(fake_interaction(None)))
  39. async def test_refused_while_channel_unknown(self):
  40. self.assertFalse(await self.check(fake_interaction(HOME), channel_found=False))
  41. class MessagesTest(unittest.IsolatedAsyncioTestCase):
  42. async def receive(self, guild: mock.Mock | None, channel_id: int = CHANNEL_ID) -> mock.Mock:
  43. bot = fake_bot()
  44. message = mock.Mock(guild=guild, webhook_id=None, type=discord.MessageType.default)
  45. message.channel.id = channel_id
  46. message.author.bot = False
  47. message.mentions = [bot.user]
  48. await ChatBot.on_message(bot, message)
  49. return bot
  50. async def test_home_guild_answered(self):
  51. bot = await self.receive(mock.Mock(id=HOME))
  52. bot.handle_main_channel.assert_awaited_once()
  53. async def test_other_guild_ignored(self):
  54. # Même un salon qui porte le même identifiant ou une mention : rien n'est lu ni noté.
  55. bot = await self.receive(mock.Mock(id=OTHER))
  56. bot.handle_main_channel.assert_not_awaited()
  57. bot.handle_mention.assert_not_awaited()
  58. bot.register_members.assert_not_awaited()
  59. bot.welcome.assert_not_awaited()
  60. async def test_direct_message_ignored(self):
  61. bot = await self.receive(None, channel_id=99)
  62. bot.handle_mention.assert_not_awaited()
  63. bot.register_members.assert_not_awaited()
  64. if __name__ == "__main__":
  65. unittest.main()