|
| 1 | +import unittest |
| 2 | +from unittest.async_case import IsolatedAsyncioTestCase |
| 3 | +from unittest.mock import AsyncMock, MagicMock |
| 4 | + |
| 5 | +from discord import Interaction, NotFound |
| 6 | + |
| 7 | +from ui.modals.edit_post import EditPostModal |
| 8 | + |
| 9 | +from src.cogs.admin.edit_post import EditPost |
| 10 | + |
| 11 | + |
| 12 | +class TestEditPost(IsolatedAsyncioTestCase): |
| 13 | + def setUp(self): |
| 14 | + self.mock_bot = MagicMock() |
| 15 | + self.mock_bot.pool = MagicMock() |
| 16 | + self.mock_bot.user = MagicMock() |
| 17 | + self.mock_bot.user.id = 123456789 |
| 18 | + self.cog = EditPost(self.mock_bot) |
| 19 | + |
| 20 | + def _make_interaction(self, *, channel_id=999, done=False): |
| 21 | + interaction = AsyncMock(spec=Interaction) |
| 22 | + interaction.response = AsyncMock() |
| 23 | + interaction.response.send_modal = AsyncMock() |
| 24 | + interaction.response.send_message = AsyncMock() |
| 25 | + interaction.response.is_done = MagicMock(return_value=done) |
| 26 | + interaction.channel = AsyncMock() |
| 27 | + interaction.channel_id = channel_id |
| 28 | + interaction.user = MagicMock() |
| 29 | + return interaction |
| 30 | + |
| 31 | + async def test_edit_post_sends_modal_for_valid_bot_message(self): |
| 32 | + interaction = self._make_interaction() |
| 33 | + |
| 34 | + mock_message = AsyncMock() |
| 35 | + mock_message.id = 42 |
| 36 | + mock_message.content = "original content" |
| 37 | + mock_message.author = MagicMock() |
| 38 | + mock_message.author.id = self.mock_bot.user.id |
| 39 | + |
| 40 | + interaction.channel.fetch_message = AsyncMock(return_value=mock_message) |
| 41 | + |
| 42 | + await self.cog.edit_post.callback( |
| 43 | + self.cog, |
| 44 | + interaction=interaction, |
| 45 | + message_id="42", |
| 46 | + ) |
| 47 | + |
| 48 | + interaction.response.send_modal.assert_awaited_once() |
| 49 | + sent_modal = interaction.response.send_modal.call_args[0][0] |
| 50 | + self.assertIsInstance(sent_modal, EditPostModal) |
| 51 | + self.assertEqual(sent_modal.post_id, 42) |
| 52 | + self.assertEqual(sent_modal.channel_id, 999) |
| 53 | + self.assertEqual(sent_modal.message.default, "original content") |
| 54 | + |
| 55 | + async def test_edit_post_message_not_found(self): |
| 56 | + interaction = self._make_interaction() |
| 57 | + interaction.channel.fetch_message = AsyncMock( |
| 58 | + side_effect=NotFound(MagicMock(), "not found") |
| 59 | + ) |
| 60 | + |
| 61 | + await self.cog.edit_post.callback( |
| 62 | + self.cog, |
| 63 | + interaction=interaction, |
| 64 | + message_id="42", |
| 65 | + ) |
| 66 | + |
| 67 | + interaction.response.send_message.assert_awaited_once_with( |
| 68 | + "Message not found in this channel.", ephemeral=True |
| 69 | + ) |
| 70 | + interaction.response.send_modal.assert_not_awaited() |
| 71 | + |
| 72 | + async def test_edit_post_invalid_message_id(self): |
| 73 | + interaction = self._make_interaction() |
| 74 | + |
| 75 | + await self.cog.edit_post.callback( |
| 76 | + self.cog, |
| 77 | + interaction=interaction, |
| 78 | + message_id="not-an-int", |
| 79 | + ) |
| 80 | + |
| 81 | + interaction.response.send_message.assert_awaited_once_with( |
| 82 | + "Message not found in this channel.", ephemeral=True |
| 83 | + ) |
| 84 | + interaction.response.send_modal.assert_not_awaited() |
| 85 | + |
| 86 | + async def test_edit_post_refuses_non_bot_message(self): |
| 87 | + interaction = self._make_interaction() |
| 88 | + |
| 89 | + mock_message = AsyncMock() |
| 90 | + mock_message.id = 42 |
| 91 | + mock_message.content = "someone else's post" |
| 92 | + mock_message.author = MagicMock() |
| 93 | + mock_message.author.id = 999999999 # not the bot |
| 94 | + |
| 95 | + interaction.channel.fetch_message = AsyncMock(return_value=mock_message) |
| 96 | + |
| 97 | + await self.cog.edit_post.callback( |
| 98 | + self.cog, |
| 99 | + interaction=interaction, |
| 100 | + message_id="42", |
| 101 | + ) |
| 102 | + |
| 103 | + interaction.response.send_message.assert_awaited_once_with( |
| 104 | + "Cannot edit message that isn't from PPH bot!", ephemeral=True |
| 105 | + ) |
| 106 | + interaction.response.send_modal.assert_not_awaited() |
| 107 | + |
| 108 | + async def test_edit_post_returns_early_when_no_channel_id(self): |
| 109 | + interaction = self._make_interaction(channel_id=None) |
| 110 | + |
| 111 | + await self.cog.edit_post.callback( |
| 112 | + self.cog, |
| 113 | + interaction=interaction, |
| 114 | + message_id="42", |
| 115 | + ) |
| 116 | + |
| 117 | + interaction.response.send_message.assert_not_awaited() |
| 118 | + interaction.response.send_modal.assert_not_awaited() |
| 119 | + |
| 120 | + async def test_edit_post_refuses_non_bot_message_when_bot_user_is_none(self): |
| 121 | + self.mock_bot.user = None |
| 122 | + interaction = self._make_interaction() |
| 123 | + |
| 124 | + mock_message = AsyncMock() |
| 125 | + mock_message.id = 42 |
| 126 | + mock_message.content = "some content" |
| 127 | + mock_message.author = MagicMock() |
| 128 | + mock_message.author.id = 999999999 |
| 129 | + |
| 130 | + interaction.channel.fetch_message = AsyncMock(return_value=mock_message) |
| 131 | + |
| 132 | + await self.cog.edit_post.callback( |
| 133 | + self.cog, |
| 134 | + interaction=interaction, |
| 135 | + message_id="42", |
| 136 | + ) |
| 137 | + |
| 138 | + # bot.user is None, so the author check is skipped, modal should open |
| 139 | + interaction.response.send_modal.assert_awaited_once() |
| 140 | + |
| 141 | + |
| 142 | +class TestEditPostModal(IsolatedAsyncioTestCase): |
| 143 | + async def test_modal_default_content_is_set(self): |
| 144 | + modal = EditPostModal( |
| 145 | + channel_id=999, |
| 146 | + post_id=42, |
| 147 | + original_content="edit me", |
| 148 | + bot=MagicMock(), |
| 149 | + ) |
| 150 | + |
| 151 | + self.assertEqual(modal.message.default, "edit me") |
| 152 | + self.assertEqual(modal.post_id, 42) |
| 153 | + self.assertEqual(modal.channel_id, 999) |
| 154 | + |
| 155 | + async def test_on_submit_fetches_and_edits_message(self): |
| 156 | + mock_bot = MagicMock() |
| 157 | + mock_channel = AsyncMock() |
| 158 | + mock_message = AsyncMock() |
| 159 | + |
| 160 | + mock_bot.fetch_channel = AsyncMock(return_value=mock_channel) |
| 161 | + mock_channel.fetch_message = AsyncMock(return_value=mock_message) |
| 162 | + mock_message.edit = AsyncMock() |
| 163 | + |
| 164 | + mock_interaction = AsyncMock(spec=Interaction) |
| 165 | + mock_interaction.response = AsyncMock() |
| 166 | + mock_interaction.response.send_message = AsyncMock() |
| 167 | + |
| 168 | + modal = EditPostModal( |
| 169 | + channel_id=999, |
| 170 | + post_id=42, |
| 171 | + original_content="old content", |
| 172 | + bot=mock_bot, |
| 173 | + ) |
| 174 | + modal.message = MagicMock() |
| 175 | + modal.message.value = "new content" |
| 176 | + |
| 177 | + await modal.on_submit(mock_interaction) |
| 178 | + |
| 179 | + mock_bot.fetch_channel.assert_awaited_once_with(999) |
| 180 | + mock_channel.fetch_message.assert_awaited_once_with(42) |
| 181 | + mock_message.edit.assert_awaited_once_with(content="new content") |
| 182 | + mock_interaction.response.send_message.assert_awaited_once_with( |
| 183 | + "Success", ephemeral=True |
| 184 | + ) |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + unittest.main() |
0 commit comments