def setUpClass(cls): """Sets up the objects that only have to be initialized once.""" cls.nonstaff_member = helpers.MockMember(name="Non-staffer") cls.staff_role = helpers.MockRole(name="Staff role", id=constants.STAFF_ROLES[0]) cls.staff_member = helpers.MockMember(name="staffer", roles=[cls.staff_role]) cls.checkmark_emoji = "\N{White Heavy Check Mark}" cls.thumbs_up_emoji = "\N{Thumbs Up Sign}" cls.unicode_duck_emoji = "\N{Duck}" cls.duck_pond_emoji = helpers.MockPartialEmoji(id=constants.DuckPond.custom_emojis[0]) cls.non_duck_custom_emoji = helpers.MockPartialEmoji(id=123)
def test_on_raw_reaction_add_returns_on_message_with_green_checkmark_placed_by_bot( self, count_ducks, is_staff): """The `on_raw_reaction_add` event should return when the message has a green check mark placed by the bot.""" channel_id = 31415926535 message_id = 27182818284 user_id = 16180339887 channel, message, member, payload = self._raw_reaction_mocks( channel_id, message_id, user_id) payload.emoji = helpers.MockPartialEmoji(name=self.unicode_duck_emoji) payload.emoji.is_custom_emoji.return_value = False message.reactions = [ helpers.MockReaction(emoji=self.checkmark_emoji, users=[self.bot.user]) ] is_staff.return_value = True count_ducks.side_effect = AssertionError( "Expected method to return before calling `self.count_ducks`") self.assertIsNone(asyncio.run(self.cog.on_raw_reaction_add(payload))) # Assert that we've made it past `self.is_staff` is_staff.assert_called_once()
def test_on_raw_reaction_remove_ignores_removal_of_non_checkmark_reactions(self): """The `on_raw_reaction_remove` listener should ignore the removal of non-check mark emojis.""" channel = helpers.MockTextChannel(id=98765) channel.fetch_message.side_effect = AssertionError( "Expected method to return before calling `channel.fetch_message`" ) self.bot.get_all_channels.return_value = (channel, ) payload = MagicMock(emoji=helpers.MockPartialEmoji(name=self.thumbs_up_emoji), channel_id=channel.id) self.assertIsNone(asyncio.run(self.cog.on_raw_reaction_remove(payload))) channel.fetch_message.assert_not_called()
async def test_on_raw_reaction_remove_prevents_removal_of_green_checkmark_depending_on_the_duck_count( self): """The `on_raw_reaction_remove` listener prevents removal of the check mark on messages with enough ducks.""" checkmark = helpers.MockPartialEmoji(name=self.checkmark_emoji) message = helpers.MockMessage(id=1234) channel = helpers.MockTextChannel(id=98765) channel.fetch_message.return_value = message self.bot.get_all_channels.return_value = (channel, ) payload = MagicMock(channel_id=channel.id, message_id=message.id, emoji=checkmark) test_cases = ( (constants.DuckPond.threshold - 1, False), (constants.DuckPond.threshold, True), (constants.DuckPond.threshold + 1, True), ) for duck_count, should_re_add_checkmark in test_cases: with patch(f"{MODULE_PATH}.DuckPond.count_ducks", new_callable=helpers.AsyncMock) as count_ducks: count_ducks.return_value = duck_count with self.subTest( duck_count=duck_count, should_re_add_checkmark=should_re_add_checkmark): await self.cog.on_raw_reaction_remove(payload) # Check if we fetched the message channel.fetch_message.assert_called_once_with(message.id) # Check if we actually counted the number of ducks count_ducks.assert_called_once_with(message) has_re_added_checkmark = message.add_reaction.called self.assertEqual(should_re_add_checkmark, has_re_added_checkmark) if should_re_add_checkmark: message.add_reaction.assert_called_once_with( self.checkmark_emoji) message.add_reaction.reset_mock() # reset mocks channel.fetch_message.reset_mock() message.reset_mock()