aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar Bast <[email protected]>2020-09-24 10:03:24 -0700
committerGravatar Bast <[email protected]>2020-09-24 10:03:24 -0700
commit1b38ad4a16d17bacfe20513c9f33a58aa6ee1b56 (patch)
treec2bfbc0fb3110373cad3f7d735a071b4b8f55fa5 /tests
parentMerge branch 'master' into feature-bast-user-token-notify (diff)
Implement review-suggested changes
userid -> user ID maybevalid -> maybe_valid remove collections import and added a new function that handles the "format user ID log message" and should_ping_everyone feature
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/exts/filters/test_token_remover.py87
1 files changed, 54 insertions, 33 deletions
diff --git a/tests/bot/exts/filters/test_token_remover.py b/tests/bot/exts/filters/test_token_remover.py
index 8742b73c5..92dce201b 100644
--- a/tests/bot/exts/filters/test_token_remover.py
+++ b/tests/bot/exts/filters/test_token_remover.py
@@ -87,7 +87,7 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
self.assertFalse(result)
def test_is_valid_hmac_valid(self):
- """Should consider hmac valid if it is a valid hmac with a variety of characters."""
+ """Should consider an HMAC valid if it has at least 3 unique characters."""
valid_hmacs = (
"VXmErH7j511turNpfURmb0rVNm8",
"Ysnu2wacjaKs7qnoo46S8Dm2us8",
@@ -97,11 +97,11 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
for hmac in valid_hmacs:
with self.subTest(msg=hmac):
- result = TokenRemover.is_maybevalid_hmac(hmac)
+ result = TokenRemover.is_maybe_valid_hmac(hmac)
self.assertTrue(result)
def test_is_invalid_hmac_invalid(self):
- """Should consider hmac invalid if it possesses too little variety."""
+ """Should consider an HMAC invalid if has fewer than 3 unique characters."""
invalid_hmacs = (
("xxxxxxxxxxxxxxxxxx", "Single character"),
("XxXxXxXxXxXxXxXxXx", "Single character alternating case"),
@@ -111,7 +111,7 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
for hmac, msg in invalid_hmacs:
with self.subTest(msg=msg):
- result = TokenRemover.is_maybevalid_hmac(hmac)
+ result = TokenRemover.is_maybe_valid_hmac(hmac)
self.assertFalse(result)
def test_mod_log_property(self):
@@ -171,11 +171,11 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
self.assertIsNone(return_value)
token_re.finditer.assert_called_once_with(self.msg.content)
- @autospec(TokenRemover, "is_valid_user_id", "is_valid_timestamp", "is_maybevalid_hmac")
+ @autospec(TokenRemover, "is_valid_user_id", "is_valid_timestamp", "is_maybe_valid_hmac")
@autospec("bot.exts.filters.token_remover", "Token")
@autospec("bot.exts.filters.token_remover", "TOKEN_RE")
- def test_find_token_valid_match(self, token_re, token_cls, is_valid_id, is_valid_timestamp, is_maybevalid_hmac):
- """The first match with a valid user ID. timestamp and hmac should be returned as a `Token`."""
+ def test_find_token_valid_match(self, token_re, token_cls, is_valid_id, is_valid_timestamp, is_maybe_valid_hmac):
+ """The first match with a valid user ID, timestamp, and HMAC should be returned as a `Token`."""
matches = [
mock.create_autospec(Match, spec_set=True, instance=True),
mock.create_autospec(Match, spec_set=True, instance=True),
@@ -189,23 +189,30 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
token_cls.side_effect = tokens
is_valid_id.side_effect = (False, True) # The 1st match will be invalid, 2nd one valid.
is_valid_timestamp.return_value = True
- is_maybevalid_hmac.return_value = True
+ is_maybe_valid_hmac.return_value = True
return_value = TokenRemover.find_token_in_message(self.msg)
self.assertEqual(tokens[1], return_value)
token_re.finditer.assert_called_once_with(self.msg.content)
- @autospec(TokenRemover, "is_valid_user_id", "is_valid_timestamp", "is_maybevalid_hmac")
+ @autospec(TokenRemover, "is_valid_user_id", "is_valid_timestamp", "is_maybe_valid_hmac")
@autospec("bot.exts.filters.token_remover", "Token")
@autospec("bot.exts.filters.token_remover", "TOKEN_RE")
- def test_find_token_invalid_matches(self, token_re, token_cls, is_valid_id, is_valid_timestamp, is_maybevalid_hmac):
+ def test_find_token_invalid_matches(
+ self,
+ token_re,
+ token_cls,
+ is_valid_id,
+ is_valid_timestamp,
+ is_maybe_valid_hmac,
+ ):
"""None should be returned if no matches have valid user IDs or 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
is_valid_timestamp.return_value = False
- is_maybevalid_hmac.return_value = False
+ is_maybe_valid_hmac.return_value = False
return_value = TokenRemover.find_token_in_message(self.msg)
@@ -261,18 +268,17 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
results = [match[0] for match in results]
self.assertCountEqual((token_1, token_2), results)
- @autospec("bot.exts.filters.token_remover", "LOG_MESSAGE", "DECODED_LOG_MESSAGE")
- def test_format_log_message(self, log_message, decoded_log_message):
+ @autospec("bot.exts.filters.token_remover", "LOG_MESSAGE")
+ def test_format_log_message(self, log_message):
"""Should correctly format the log message with info from the message and token."""
token = Token("NDcyMjY1OTQzMDYyNDEzMzMy", "XsySD_", "s45jqDV_Iisn-symw0yDRrk_jf4")
log_message.format.return_value = "Howdy"
- decoded_log_message.format.return_value = " Partner"
- return_value = TokenRemover.format_log_message(self.msg, token, 472265943062413332, None)
+ return_value = TokenRemover.format_log_message(self.msg, token)
self.assertEqual(
return_value,
- log_message.format.return_value + "\n" + decoded_log_message.format.return_value,
+ log_message.format.return_value,
)
log_message.format.assert_called_once_with(
author=self.msg.author,
@@ -283,26 +289,38 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
hmac="x" * len(token.hmac),
)
- @autospec("bot.exts.filters.token_remover", "LOG_MESSAGE", "USER_TOKEN_MESSAGE")
- def test_format_log_message_user_token(self, log_message, user_token_message):
+ @autospec("bot.exts.filters.token_remover", "DECODED_LOG_MESSAGE")
+ def test_format_userid_log_message_bot(self, decoded_log_message):
+ """
+ Should correctly format the user ID portion of the log message when the user ID is
+ not found in the server.
+ """
+ token = Token("NDcyMjY1OTQzMDYyNDEzMzMy", "XsySD_", "s45jqDV_Iisn-symw0yDRrk_jf4")
+ decoded_log_message.format.return_value = " Partner"
+ msg = MockMessage(id=555, content="hello world")
+ msg.guild.get_member = MagicMock(return_value=None)
+
+ return_value = TokenRemover.format_userid_log_message(msg, token)
+
+ self.assertEqual(
+ return_value,
+ (decoded_log_message.format.return_value, False),
+ )
+ decoded_log_message.format.assert_called_once_with(
+ user_id=472265943062413332,
+ )
+
+ @autospec("bot.exts.filters.token_remover", "USER_TOKEN_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."""
token = Token("NDY3MjIzMjMwNjUwNzc3NjQx", "XsySD_", "s45jqDV_Iisn-symw0yDRrk_jf4")
- log_message.format.return_value = "Howdy"
user_token_message.format.return_value = "Partner"
- return_value = TokenRemover.format_log_message(self.msg, token, 467223230650777641, "Bob")
+ return_value = TokenRemover.format_userid_log_message(self.msg, token)
self.assertEqual(
return_value,
- log_message.format.return_value + "\n" + user_token_message.format.return_value,
- )
- log_message.format.assert_called_once_with(
- author=self.msg.author,
- author_id=self.msg.author.id,
- channel=self.msg.channel.mention,
- user_id=token.user_id,
- timestamp=token.timestamp,
- hmac="x" * len(token.hmac),
+ (user_token_message.format.return_value, True),
)
user_token_message.format.assert_called_once_with(
user_id=467223230650777641,
@@ -311,17 +329,19 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
@mock.patch.object(TokenRemover, "mod_log", new_callable=mock.PropertyMock)
@autospec("bot.exts.filters.token_remover", "log")
- @autospec(TokenRemover, "format_log_message")
- async def test_take_action(self, format_log_message, logger, mod_log_property):
+ @autospec(TokenRemover, "format_log_message", "format_userid_log_message")
+ async def test_take_action(self, format_log_message, format_userid_log_message, logger, mod_log_property):
"""Should delete the message and send a mod log."""
cog = TokenRemover(self.bot)
mod_log = mock.create_autospec(ModLog, spec_set=True, instance=True)
token = mock.create_autospec(Token, spec_set=True, instance=True)
token.user_id = "no-id"
log_msg = "testing123"
+ userid_log_message = "userid-log-message"
mod_log_property.return_value = mod_log
format_log_message.return_value = log_msg
+ format_userid_log_message.return_value = (userid_log_message, True)
await cog.take_action(self.msg, token)
@@ -330,7 +350,8 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
token_remover.DELETION_MESSAGE_TEMPLATE.format(mention=self.msg.author.mention)
)
- format_log_message.assert_called_once_with(self.msg, token, None, "Bob")
+ format_log_message.assert_called_once_with(self.msg, token)
+ format_userid_log_message.assert_called_once_with(self.msg, token)
logger.debug.assert_called_with(log_msg)
self.bot.stats.incr.assert_called_once_with("tokens.removed_tokens")
@@ -339,7 +360,7 @@ class TokenRemoverTests(unittest.IsolatedAsyncioTestCase):
icon_url=constants.Icons.token_removed,
colour=Colour(constants.Colours.soft_red),
title="Token removed!",
- text=log_msg,
+ text=log_msg + "\n" + userid_log_message,
thumbnail=self.msg.author.avatar_url_as.return_value,
channel_id=constants.Channels.mod_alerts,
ping_everyone=True,