diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/bot/exts/backend/sync/test_users.py | 29 | ||||
| -rw-r--r-- | tests/bot/exts/info/test_information.py | 4 | ||||
| -rw-r--r-- | tests/bot/exts/moderation/infraction/test_infractions.py | 50 | ||||
| -rw-r--r-- | tests/bot/exts/moderation/test_slowmode.py | 16 | ||||
| -rw-r--r-- | tests/bot/exts/utils/test_jams.py | 4 | 
5 files changed, 64 insertions, 39 deletions
| diff --git a/tests/bot/exts/backend/sync/test_users.py b/tests/bot/exts/backend/sync/test_users.py index 61673e1bb..27932be95 100644 --- a/tests/bot/exts/backend/sync/test_users.py +++ b/tests/bot/exts/backend/sync/test_users.py @@ -188,30 +188,37 @@ class UserSyncerSyncTests(unittest.IsolatedAsyncioTestCase):      """Tests for the API requests that sync users."""      def setUp(self): -        patcher = mock.patch("bot.instance", new=helpers.MockBot()) -        self.bot = patcher.start() -        self.addCleanup(patcher.stop) +        bot_patcher = mock.patch("bot.instance", new=helpers.MockBot()) +        self.bot = bot_patcher.start() +        self.addCleanup(bot_patcher.stop) + +        chunk_patcher = mock.patch("bot.exts.backend.sync._syncers.CHUNK_SIZE", 2) +        self.chunk_size = chunk_patcher.start() +        self.addCleanup(chunk_patcher.stop) + +        self.chunk_count = 2 +        self.users = [fake_user(id=i) for i in range(self.chunk_size * self.chunk_count)]      async def test_sync_created_users(self):          """Only POST requests should be made with the correct payload.""" -        users = [fake_user(id=111), fake_user(id=222)] - -        diff = _Diff(users, [], None) +        diff = _Diff(self.users, [], None)          await UserSyncer._sync(diff) -        self.bot.api_client.post.assert_called_once_with("bot/users", json=diff.created) +        self.bot.api_client.post.assert_any_call("bot/users", json=diff.created[:self.chunk_size]) +        self.bot.api_client.post.assert_any_call("bot/users", json=diff.created[self.chunk_size:]) +        self.assertEqual(self.bot.api_client.post.call_count, self.chunk_count)          self.bot.api_client.put.assert_not_called()          self.bot.api_client.delete.assert_not_called()      async def test_sync_updated_users(self):          """Only PUT requests should be made with the correct payload.""" -        users = [fake_user(id=111), fake_user(id=222)] - -        diff = _Diff([], users, None) +        diff = _Diff([], self.users, None)          await UserSyncer._sync(diff) -        self.bot.api_client.patch.assert_called_once_with("bot/users/bulk_patch", json=diff.updated) +        self.bot.api_client.patch.assert_any_call("bot/users/bulk_patch", json=diff.updated[:self.chunk_size]) +        self.bot.api_client.patch.assert_any_call("bot/users/bulk_patch", json=diff.updated[self.chunk_size:]) +        self.assertEqual(self.bot.api_client.patch.call_count, self.chunk_count)          self.bot.api_client.post.assert_not_called()          self.bot.api_client.delete.assert_not_called() diff --git a/tests/bot/exts/info/test_information.py b/tests/bot/exts/info/test_information.py index 043cce8de..80731c9f0 100644 --- a/tests/bot/exts/info/test_information.py +++ b/tests/bot/exts/info/test_information.py @@ -65,7 +65,7 @@ class InformationCogTests(unittest.IsolatedAsyncioTestCase):              permissions=discord.Permissions(0),          ) -        self.ctx.guild.roles.append([dummy_role, admin_role]) +        self.ctx.guild.roles.extend([dummy_role, admin_role])          self.cog.role_info.can_run = unittest.mock.AsyncMock()          self.cog.role_info.can_run.return_value = True @@ -355,7 +355,7 @@ class UserEmbedTests(unittest.IsolatedAsyncioTestCase):          self.assertEqual(              textwrap.dedent(f"""                  Joined: {"1 year ago"} -                Pending: {"False"} +                Verified: {"True"}                  Roles: &Moderators              """).strip(),              embed.fields[1].value diff --git a/tests/bot/exts/moderation/infraction/test_infractions.py b/tests/bot/exts/moderation/infraction/test_infractions.py index bf557a484..86c2617ea 100644 --- a/tests/bot/exts/moderation/infraction/test_infractions.py +++ b/tests/bot/exts/moderation/infraction/test_infractions.py @@ -1,10 +1,12 @@ +import inspect  import textwrap  import unittest -from unittest.mock import AsyncMock, MagicMock, Mock, patch +from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch  from bot.constants import Event +from bot.exts.moderation.infraction import _utils  from bot.exts.moderation.infraction.infractions import Infractions -from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockRole +from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockRole, MockUser, autospec  class TruncationTests(unittest.IsolatedAsyncioTestCase): @@ -132,20 +134,29 @@ class VoiceBanTests(unittest.IsolatedAsyncioTestCase):          self.assertIsNone(await self.cog.apply_voice_ban(self.ctx, self.user, "foobar"))          self.cog.mod_log.ignore.assert_called_once_with(Event.member_update, self.user.id) +    async def action_tester(self, action, reason: str) -> None: +        """Helper method to test voice ban action.""" +        self.assertTrue(inspect.iscoroutine(action)) +        await action + +        self.user.move_to.assert_called_once_with(None, reason=ANY) +        self.user.remove_roles.assert_called_once_with(self.cog._voice_verified_role, reason=reason) +      @patch("bot.exts.moderation.infraction.infractions._utils.post_infraction")      @patch("bot.exts.moderation.infraction.infractions._utils.get_active_infraction")      async def test_voice_ban_apply_infraction(self, get_active_infraction, post_infraction_mock):          """Should ignore Voice Verified role removing."""          self.cog.mod_log.ignore = MagicMock()          self.cog.apply_infraction = AsyncMock() -        self.user.remove_roles = MagicMock(return_value="my_return_value")          get_active_infraction.return_value = None          post_infraction_mock.return_value = {"foo": "bar"} -        self.assertIsNone(await self.cog.apply_voice_ban(self.ctx, self.user, "foobar")) -        self.user.remove_roles.assert_called_once_with(self.cog._voice_verified_role, reason="foobar") -        self.cog.apply_infraction.assert_awaited_once_with(self.ctx, {"foo": "bar"}, self.user, "my_return_value") +        reason = "foobar" +        self.assertIsNone(await self.cog.apply_voice_ban(self.ctx, self.user, reason)) +        self.cog.apply_infraction.assert_awaited_once_with(self.ctx, {"foo": "bar"}, self.user, ANY) + +        await self.action_tester(self.cog.apply_infraction.call_args[0][-1], reason)      @patch("bot.exts.moderation.infraction.infractions._utils.post_infraction")      @patch("bot.exts.moderation.infraction.infractions._utils.get_active_infraction") @@ -153,16 +164,33 @@ class VoiceBanTests(unittest.IsolatedAsyncioTestCase):          """Should truncate reason for voice ban."""          self.cog.mod_log.ignore = MagicMock()          self.cog.apply_infraction = AsyncMock() -        self.user.remove_roles = MagicMock(return_value="my_return_value")          get_active_infraction.return_value = None          post_infraction_mock.return_value = {"foo": "bar"}          self.assertIsNone(await self.cog.apply_voice_ban(self.ctx, self.user, "foobar" * 3000)) -        self.user.remove_roles.assert_called_once_with( -            self.cog._voice_verified_role, reason=textwrap.shorten("foobar" * 3000, 512, placeholder="...") -        ) -        self.cog.apply_infraction.assert_awaited_once_with(self.ctx, {"foo": "bar"}, self.user, "my_return_value") +        self.cog.apply_infraction.assert_awaited_once_with(self.ctx, {"foo": "bar"}, self.user, ANY) + +        # Test action +        action = self.cog.apply_infraction.call_args[0][-1] +        await self.action_tester(action, textwrap.shorten("foobar" * 3000, 512, placeholder="...")) + +    @autospec(_utils, "post_infraction", "get_active_infraction", return_value=None) +    @autospec(Infractions, "apply_infraction") +    async def test_voice_ban_user_left_guild(self, apply_infraction_mock, post_infraction_mock, _): +        """Should voice ban user that left the guild without throwing an error.""" +        infraction = {"foo": "bar"} +        post_infraction_mock.return_value = {"foo": "bar"} + +        user = MockUser() +        await self.cog.voiceban(self.cog, self.ctx, user, reason=None) +        post_infraction_mock.assert_called_once_with(self.ctx, user, "voice_ban", None, active=True) +        apply_infraction_mock.assert_called_once_with(self.cog, self.ctx, infraction, user, ANY) + +        # Test action +        action = self.cog.apply_infraction.call_args[0][-1] +        self.assertTrue(inspect.iscoroutine(action)) +        await action      async def test_voice_unban_user_not_found(self):          """Should include info to return dict when user was not found from guild.""" diff --git a/tests/bot/exts/moderation/test_slowmode.py b/tests/bot/exts/moderation/test_slowmode.py index dad751e0d..5483b7a64 100644 --- a/tests/bot/exts/moderation/test_slowmode.py +++ b/tests/bot/exts/moderation/test_slowmode.py @@ -85,22 +85,14 @@ class SlowmodeTests(unittest.IsolatedAsyncioTestCase):              self.ctx.reset_mock() -    async def test_reset_slowmode_no_channel(self) -> None: -        """Reset slowmode without a given channel.""" -        self.ctx.channel = MockTextChannel(name='careers', slowmode_delay=6) - -        await self.cog.reset_slowmode(self.cog, self.ctx, None) -        self.ctx.send.assert_called_once_with( -            f'{Emojis.check_mark} The slowmode delay for #careers has been reset to 0 seconds.' -        ) - -    async def test_reset_slowmode_with_channel(self) -> None: +    async def test_reset_slowmode_sets_delay_to_zero(self) -> None:          """Reset slowmode with a given channel."""          text_channel = MockTextChannel(name='meta', slowmode_delay=1) +        self.cog.set_slowmode = mock.AsyncMock()          await self.cog.reset_slowmode(self.cog, self.ctx, text_channel) -        self.ctx.send.assert_called_once_with( -            f'{Emojis.check_mark} The slowmode delay for #meta has been reset to 0 seconds.' +        self.cog.set_slowmode.assert_awaited_once_with( +            self.ctx, text_channel, relativedelta(seconds=0)          )      @mock.patch("bot.exts.moderation.slowmode.has_any_role") diff --git a/tests/bot/exts/utils/test_jams.py b/tests/bot/exts/utils/test_jams.py index 45e7b5b51..85d6a1173 100644 --- a/tests/bot/exts/utils/test_jams.py +++ b/tests/bot/exts/utils/test_jams.py @@ -118,11 +118,9 @@ class JamCreateTeamTests(unittest.IsolatedAsyncioTestCase):              self.assertTrue(overwrites[member].read_messages)              self.assertTrue(overwrites[member].connect) -        # Everyone and verified role overwrite +        # Everyone role overwrite          self.assertFalse(overwrites[self.guild.default_role].read_messages)          self.assertFalse(overwrites[self.guild.default_role].connect) -        self.assertFalse(overwrites[self.guild.get_role(Roles.verified)].read_messages) -        self.assertFalse(overwrites[self.guild.get_role(Roles.verified)].connect)      async def test_team_channels_creation(self):          """Should create new voice and text channel for team.""" | 
