aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Bast <[email protected]>2020-09-24 14:16:10 -0700
committerGravatar Bast <[email protected]>2020-09-24 14:16:10 -0700
commitce80892eb3928c7c312a221c9d0271698f1563f4 (patch)
tree69d8a294f81e340724667659d4d2494171d52820
parentAdd BOT vs USER token detection, properly handling bot tokens for bots in the... (diff)
Change the mod alert message component for the user token detection
Clean up mock usage, docstrings, unnecessarily split-lined function calls
-rw-r--r--bot/exts/filters/token_remover.py18
-rw-r--r--tests/bot/exts/filters/test_token_remover.py51
2 files changed, 23 insertions, 46 deletions
diff --git a/bot/exts/filters/token_remover.py b/bot/exts/filters/token_remover.py
index 87d4aa135..87072e161 100644
--- a/bot/exts/filters/token_remover.py
+++ b/bot/exts/filters/token_remover.py
@@ -18,10 +18,10 @@ LOG_MESSAGE = (
"Censored a seemingly valid token sent by {author} (`{author_id}`) in {channel}, "
"token was `{user_id}.{timestamp}.{hmac}`"
)
-UNKNOWN_USER_LOG_MESSAGE = "The token user_id decodes into {user_id}."
+UNKNOWN_USER_LOG_MESSAGE = "Decoded user ID: `{user_id}` (Not present in server)."
KNOWN_USER_LOG_MESSAGE = (
- "The token user_id decodes into {user_id}, "
- "which matches `{user_name}` and means this is a valid {kind} token."
+ "Decoded user ID: `{user_id}` **(Present in server)**.\n"
+ "This matches `{user_name}` and means this is likely a valid **{kind}** token."
)
DELETION_MESSAGE_TEMPLATE = (
"Hey {mention}! I noticed you posted a seemingly valid Discord API "
@@ -117,10 +117,12 @@ class TokenRemover(Cog):
@classmethod
def format_userid_log_message(cls, msg: Message, token: Token) -> t.Tuple[str, bool]:
"""
- Format the potion of the log message that includes details about the detected user ID.
+ Format the portion of the log message that includes details about the detected user ID.
- Includes the user ID and, if present on the server, their name and a toggle to
- mention everyone.
+ If the user is resolved to a member, the format includes the user ID, name, and the
+ kind of user detected.
+
+ If we resolve to a member and it is not a bot, we also return True to ping everyone.
Returns a tuple of (log_message, mention_everyone)
"""
@@ -139,7 +141,7 @@ class TokenRemover(Cog):
@staticmethod
def format_log_message(msg: Message, token: Token) -> str:
"""Return the generic portion of the log message to send for `token` being censored in `msg`."""
- message = LOG_MESSAGE.format(
+ return LOG_MESSAGE.format(
author=msg.author,
author_id=msg.author.id,
channel=msg.channel.mention,
@@ -148,8 +150,6 @@ class TokenRemover(Cog):
hmac='x' * len(token.hmac),
)
- return message
-
@classmethod
def find_token_in_message(cls, msg: Message) -> t.Optional[Token]:
"""Return a seemingly valid token found in `msg` or `None` if no token is found."""
diff --git a/tests/bot/exts/filters/test_token_remover.py b/tests/bot/exts/filters/test_token_remover.py
index 90d40d1df..5f28ab571 100644
--- a/tests/bot/exts/filters/test_token_remover.py
+++ b/tests/bot/exts/filters/test_token_remover.py
@@ -22,12 +22,8 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
self.msg = MockMessage(id=555, content="hello world")
self.msg.channel.mention = "#lemonade-stand"
- self.msg.guild.get_member = MagicMock(
- return_value=MagicMock(
- bot=False,
- __str__=MagicMock(return_value="Woody"),
- ),
- )
+ self.msg.guild.get_member.return_value.bot = False
+ self.msg.guild.get_member.return_value.__str__.return_value = "Woody"
self.msg.author.__str__ = MagicMock(return_value=self.msg.author.name)
self.msg.author.avatar_url_as.return_value = "picture-lemon.png"
@@ -212,7 +208,7 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
is_valid_timestamp,
is_maybe_valid_hmac,
):
- """None should be returned if no matches have valid user IDs or timestamps."""
+ """None should be returned if no matches have valid user IDs, HMACs, and timestamps."""
token_re.finditer.return_value = [mock.create_autospec(Match, spec_set=True, instance=True)]
token_cls.return_value = mock.create_autospec(Token, spec_set=True, instance=True)
is_valid_id.return_value = False
@@ -281,10 +277,7 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
return_value = TokenRemover.format_log_message(self.msg, token)
- self.assertEqual(
- return_value,
- log_message.format.return_value,
- )
+ self.assertEqual(return_value, log_message.format.return_value)
log_message.format.assert_called_once_with(
author=self.msg.author,
author_id=self.msg.author.id,
@@ -296,42 +289,29 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
@autospec("bot.exts.filters.token_remover", "UNKNOWN_USER_LOG_MESSAGE")
def test_format_userid_log_message_unknown(self, unknown_user_log_message):
- """
- Should correctly format the user ID portion of the log message when the user ID is
- not found in the server.
- """
+ """Should correctly format the user ID portion when the actual user it belongs to is unknown."""
token = Token("NDcyMjY1OTQzMDYyNDEzMzMy", "XsySD_", "s45jqDV_Iisn-symw0yDRrk_jf4")
unknown_user_log_message.format.return_value = " Partner"
msg = MockMessage(id=555, content="hello world")
- msg.guild.get_member = MagicMock(return_value=None)
+ msg.guild.get_member.return_value = None
return_value = TokenRemover.format_userid_log_message(msg, token)
- self.assertEqual(
- return_value,
- (unknown_user_log_message.format.return_value, False),
- )
- unknown_user_log_message.format.assert_called_once_with(
- user_id=472265943062413332,
- )
+ self.assertEqual(return_value, (unknown_user_log_message.format.return_value, False))
+ unknown_user_log_message.format.assert_called_once_with(user_id=472265943062413332)
@autospec("bot.exts.filters.token_remover", "KNOWN_USER_LOG_MESSAGE")
def test_format_userid_log_message_bot(self, known_user_log_message):
- """
- Should correctly format the user ID portion of the log message when the user ID is
- not found in the server.
- """
+ """Should correctly format the user ID portion when the ID belongs to a known bot."""
token = Token("NDcyMjY1OTQzMDYyNDEzMzMy", "XsySD_", "s45jqDV_Iisn-symw0yDRrk_jf4")
known_user_log_message.format.return_value = " Partner"
msg = MockMessage(id=555, content="hello world")
- msg.guild.get_member = MagicMock(return_value=MagicMock(__str__=MagicMock(return_value="Sam"), bot=True))
+ msg.guild.get_member.return_value.__str__.return_value = "Sam"
+ msg.guild.get_member.return_value.bot = True
return_value = TokenRemover.format_userid_log_message(msg, token)
- self.assertEqual(
- return_value,
- (known_user_log_message.format.return_value, False),
- )
+ self.assertEqual(return_value, (known_user_log_message.format.return_value, False))
known_user_log_message.format.assert_called_once_with(
user_id=472265943062413332,
@@ -341,16 +321,13 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
@autospec("bot.exts.filters.token_remover", "KNOWN_USER_LOG_MESSAGE")
def test_format_log_message_user_token_user(self, user_token_message):
- """Should correctly format the log message with info from the message and token."""
+ """Should correctly format the user ID portion when the ID belongs to a known user."""
token = Token("NDY3MjIzMjMwNjUwNzc3NjQx", "XsySD_", "s45jqDV_Iisn-symw0yDRrk_jf4")
user_token_message.format.return_value = "Partner"
return_value = TokenRemover.format_userid_log_message(self.msg, token)
- self.assertEqual(
- return_value,
- (user_token_message.format.return_value, True),
- )
+ self.assertEqual(return_value, (user_token_message.format.return_value, True))
user_token_message.format.assert_called_once_with(
user_id=467223230650777641,
user_name="Woody",