async def test_get_user_count(self, create_example_tracker_subclass):
        with pytest.raises(TypeError):
            await create_example_tracker_subclass.get_member_count(dict())

        with pytest.raises(TypeError):
            await create_example_tracker_subclass.get_member_count(set())

        with pytest.raises(TypeError):
            await create_example_tracker_subclass.get_member_count([1, 2, 3])

        with pytest.raises(MemberNotFound):
            await create_example_tracker_subclass.get_member_count(
                MockedMessage(is_in_guild=False).to_mock())

        with pytest.raises(MemberNotFound):
            await create_example_tracker_subclass.get_member_count(
                MockedMessage(guild_id=1).to_mock())

        # Actual tests which *should* work
        await create_example_tracker_subclass.member_tracking.set_member_data(
            1,
            1,
            {
                "timestamps": [
                    datetime.datetime.now(datetime.timezone.utc),
                ],
                "has_been_muted": True,
            },
        )
        r_1 = await create_example_tracker_subclass.get_member_count(
            MockedMessage(author_id=1, guild_id=1).to_mock())
        assert r_1 == 1
    async def test_do_punishment_kick(self, create_example_tracker_subclass):
        bot_msg = MockedMessage(author_id=98987).to_mock()
        # Should hit first return
        await create_example_tracker_subclass.do_punishment(bot_msg)

        # Should hit first except
        await create_example_tracker_subclass.do_punishment(
            MockedMessage().to_mock())

        # Stop appeasing coverage and start actually testing shit
        await create_example_tracker_subclass.member_tracking.set_member_data(
            1,
            1,
            {
                "timestamps": [
                    datetime.datetime.now(datetime.timezone.utc),
                ],
                "has_been_muted": True,
            },
        )

        msg_1 = MockedMessage(author_id=1, guild_id=1).to_mock()
        msg_1.guild.kick = kick_meth = AsyncMock()
        msg_1.channel.send = channel_sender = AsyncMock()

        assert channel_sender.call_count == 0
        with mock.patch("asyncio.sleep", new_callable=AsyncMock):
            await create_example_tracker_subclass.do_punishment(msg_1)

        assert channel_sender.call_count == 1
        assert kick_meth.call_count == 1
Ejemplo n.º 3
0
    def test_edge_dict_to_embed(self):
        """Tests edge cases for dict_to_embed"""
        data = {
            "author": {
                "icon_url": "$USERAVATAR",
                "name": "test"
            },
            "footer": {
                "icon_url": "$USERAVATAR"
            },
            "timestamp": True,
        }

        mock_message = MockedMessage().to_mock()

        test_embed = dict_to_embed(data, mock_message, {
            "warn_count": 1,
            "kick_count": 2
        })

        user_footer = substitute_args("$USERAVATAR", mock_message, {
            "warn_count": 1,
            "kick_count": 2
        })

        embed = discord.Embed(timestamp=mock_message.created_at)
        embed.set_footer(icon_url=user_footer)
        embed.set_author(
            name="test",
            icon_url=user_footer,
        )

        assert embed.to_dict() == test_embed.to_dict()
Ejemplo n.º 4
0
    def test_further_edge_cases_for_to_dict(self):
        data = {
            "author": {
                "icon_url": "y",
                "name": "test"
            },
            "footer": {
                "icon_url": "x"
            },
            "timestamp": True,
        }

        mock_message = MockedMessage().to_mock()

        test_embed = dict_to_embed(data, mock_message, {
            "warn_count": 1,
            "kick_count": 2
        })

        embed = discord.Embed(timestamp=mock_message.created_at)
        embed.set_footer(icon_url="x")
        embed.set_author(
            name="test",
            icon_url="y",
        )

        assert embed.to_dict() == test_embed.to_dict()
    async def test_do_punishment_warn(self, create_example_tracker_subclass):
        await create_example_tracker_subclass.member_tracking.set_member_data(
            1,
            1,
            {
                "timestamps": [
                    datetime.datetime.now(datetime.timezone.utc),
                    datetime.datetime.now(datetime.timezone.utc),
                    datetime.datetime.now(datetime.timezone.utc),
                ],
                "has_been_muted":
                False,
            },
        )

        msg_1 = MockedMessage(author_id=1, guild_id=1).to_mock()
        msg_1.guild.kick = kick_meth = AsyncMock()
        msg_1.channel.send = channel_sender = AsyncMock()
        msg_1.author.add_roles = add_roles = AsyncMock()

        assert channel_sender.call_count == 0
        with mock.patch("asyncio.sleep", new_callable=AsyncMock):
            await create_example_tracker_subclass.do_punishment(msg_1)

        assert channel_sender.call_count == 2
        assert kick_meth.call_count == 0
        assert add_roles.call_count == 1
Ejemplo n.º 6
0
    async def test_not_implementeds(self, create_base: Base):
        with pytest.raises(NotImplementedError):
            await create_base.get_substitute_args(MockedMessage().to_mock())

        with pytest.raises(NotImplementedError):
            await create_base.lib_embed_as_dict(MockClass())

        with pytest.raises(NotImplementedError):
            await create_base.dict_to_lib_embed(dict())
Ejemplo n.º 7
0
    def test_to_dict_colors(self):
        data = {"colour": 0xFFFFFF, "color": 0x000000}
        mock_message = MockedMessage().to_mock()
        data_embed = dict_to_embed(data, mock_message, {
            "warn_count": 1,
            "kick_count": 2
        })
        embed = discord.Embed(color=0xFFFFFF)

        assert embed.to_dict() == data_embed.to_dict()
    async def test_update_cache(
            self, create_example_tracker_subclass: MyCustomTracker):
        message = MockedMessage(guild_id=1, author_id=1).to_mock()
        data = CorePayload(member_should_be_punished_this_message=True)

        await create_example_tracker_subclass.update_cache(message, data)

        values = await create_example_tracker_subclass.member_tracking.get_member_data(
            1, 1)
        assert isinstance(values, dict)
        assert "timestamps" in values
        assert len(values["timestamps"]) == 1

        message = MockedMessage(message_id=2, author_id=1,
                                guild_id=1).to_mock()
        await create_example_tracker_subclass.update_cache(message, data)

        values = await create_example_tracker_subclass.member_tracking.get_member_data(
            1, 1)
        assert isinstance(values, dict)
        assert "timestamps" in values
        assert len(values["timestamps"]) == 2
Ejemplo n.º 9
0
    def test_visualizer(self):
        message = visualizer(
            "Hello $MENTIONUSER",
            MockedMessage().to_mock(),
            {
                "warn_count": 1,
                "kick_count": 2
            },
        )
        assert "Hello <@12345>" == message

        warn_embed_dict = '{"title": "test"}'
        test_embed = visualizer(
            warn_embed_dict,
            MockedMessage().to_mock(),
            {
                "warn_count": 1,
                "kick_count": 2
            },
        )
        embed = discord.Embed(title="test")

        assert isinstance(test_embed, discord.Embed)
        assert embed.to_dict() == test_embed.to_dict()
    async def test_get_user_has_been_muted(self,
                                           create_example_tracker_subclass):
        with pytest.raises(MemberNotFound):
            await create_example_tracker_subclass.get_member_has_been_muted(
                MockedMessage().to_mock())

        await create_example_tracker_subclass.member_tracking.set_member_data(
            1, 1, {
                "timestamps": [],
                "has_been_muted": True
            })
        await create_example_tracker_subclass.member_tracking.set_member_data(
            2, 1, {
                "timestamps": [],
                "has_been_muted": False
            })

        r_1 = await create_example_tracker_subclass.get_member_has_been_muted(
            MockedMessage(author_id=1, guild_id=1).to_mock())
        assert r_1 is True

        r_2 = await create_example_tracker_subclass.get_member_has_been_muted(
            MockedMessage(author_id=2, guild_id=1).to_mock())
        assert r_2 is False
Ejemplo n.º 11
0
    def test_transform_message(self):
        warn_embed_dict = {
            "title":
            "**Dear $USERNAME**",
            "description":
            "You are being warned for spam, please stop!",
            "color":
            0xFF0000,
            "footer": {
                "text": "$BOTNAME"
            },
            "author": {
                "name": "$GUILDNAME"
            },
            "fields": [
                {
                    "name": "Current warns:",
                    "value": "$WARNCOUNT"
                },
                {
                    "name": "Current kicks:",
                    "value": "$KICKCOUNT",
                    "inline": False
                },
            ],
        }

        mock_message = MockedMessage().to_mock()
        warn_dict = {"warn_count": 1, "kick_count": 2}

        test_embed = transform_message(warn_embed_dict, mock_message,
                                       warn_dict)

        embed = discord.Embed(
            title="**Dear Skelmis**",
            description="You are being warned for spam, please stop!",
            color=0xFF0000,
        )
        embed.set_footer(text="Bot")
        embed.set_author(name="Guild")
        embed.add_field(name="Current warns:", value="1")
        embed.add_field(name="Current kicks:", value="2", inline=False)

        assert embed.to_dict() == test_embed.to_dict()

        message = transform_message("Hello $MENTIONUSER", mock_message,
                                    warn_dict)
        assert "Hello <@12345>" == message
Ejemplo n.º 12
0
    def test_dict_to_embed(self):
        warn_embed_dict = {
            "title":
            "**Dear Dave**",
            "description":
            "You are being warned for spam, please stop!",
            "color":
            0xFF0000,
            "footer": {
                "text": "BOT"
            },
            "author": {
                "name": "Guild"
            },
            "fields": [
                {
                    "name": "Current warns:",
                    "value": "1"
                },
                {
                    "name": "Current kicks:",
                    "value": "2",
                    "inline": False
                },
            ],
        }

        mock_message = MockedMessage().to_mock()

        test_embed = dict_to_embed(warn_embed_dict, mock_message, {
            "warn_count": 1,
            "kick_count": 2
        })

        embed = discord.Embed(
            title="**Dear Dave**",
            description="You are being warned for spam, please stop!",
            color=0xFF0000,
        )
        embed.set_footer(text="BOT")
        embed.set_author(name="Guild")
        embed.add_field(name="Current warns:", value="1")
        embed.add_field(name="Current kicks:", value="2", inline=False)

        assert embed.to_dict() == test_embed.to_dict()
Ejemplo n.º 13
0
    async def fetch_message(message_id):
        from tests.mocks import MockedMessage

        return MockedMessage(message_id=message_id).to_mock()
Ejemplo n.º 14
0
async def test_base_prop():
    b = BasePlugin()
    with pytest.raises(NotImplementedError):
        await b.propagate(MockedMessage().to_mock())
 async def test_update_cache_no_update(self,
                                       create_example_tracker_subclass):
     """Appease coverage"""
     message = MockedMessage(guild_id=1, author_id=1).to_mock()
     data = CorePayload()
     await create_example_tracker_subclass.update_cache(message, data)