aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helpers.py (unfollow)
Commit message (Collapse)AuthorLines
2020-05-28Adjust find_token_in_message tests for the recent cog changesGravatar MarkKoz-19/+20
It now supports the changes that switched to finditer, added match groups, and added the Token NamedTuple. It also accounts for the is_maybe_token function being removed. For the sake of simplicity, call assertions on is_valid_user_id and is_valid_timestamp were not made.
2020-05-28Remove is_maybe_token testsGravatar MarkKoz-33/+0
The function was removed due to redundancy. Therefore, its tests are obsolete.
2020-05-27Add more thorough and realistic inputs for token ID and timestamp testsGravatar MarkKoz-18/+52
The tests for valid inputs and invalid inputs were split to make them more readable.
2020-05-27Adjust token remover tests to use the Token NamedTupleGravatar MarkKoz-6/+8
2020-05-27Switch findall to finditer in assertionsGravatar MarkKoz-4/+4
`find_token_in_message` now uses the latter so the tests should adjust accordingly.
2020-05-25Use real token values for testing multiple matches in regexGravatar MarkKoz-3/+4
2020-05-25Fix multiple match text for token regexGravatar MarkKoz-2/+3
It has to account for the addition of groups. It's easiest to compare the entire string so `finditer` is used to return re.Match objects; the tuples of `findall` would be cumbersome. Also threw in a change to use `assertCountEqual` cause the order doesn't really matter.
2020-05-25Fix valid token regex testGravatar MarkKoz-2/+2
It was broken due to the addition of groups. Rather than returning the full match, `findall` returns groups if any exist. The test was comparing a tuple of groups to the token string, which was of course failing. Now `fullmatch` is used cause it's simpler - just check for `None` and don't worry about iterating matches to search.
2020-05-25Add more valid tokens to test the regex withGravatar MarkKoz-3/+5
2020-05-25Test token regex won't match non-base64 charactersGravatar MarkKoz-4/+3
2020-05-25Token remover: specify Discord epoch in secondsGravatar MarkKoz-1/+1
The timestamp in the token is in seconds and is being compared against the epoch. To make life easier, they should use the same unit. Previously, the epoch was in milliseconds.
2020-05-25Token remover: use finditer instead of findallGravatar MarkKoz-4/+3
It makes more sense to use the lazy function when the loop is already short-circuiting on the first valid token it finds.
2020-05-25Token remover: use regex groups and pass the token as a NamedTupleGravatar MarkKoz-27/+20
It felt redundant to be splitting the token in two different functions when regex could take care of this from the outset. ' A NamedTuple was created to house the token. This is nicer than passing an re.Match object, because it's clearer which attributes are available. Even if the regex used named groups, it wouldn't be as obvious which group names exist. Without the split, `is_maybe_token` is dwindled down to a redundant function. Therefore, it's been removed.
2020-05-23Token remover: escape dashes in regexGravatar MarkKoz-1/+1
They need to be escaped when they're in a character set. By default, they are interpreted as part of the character range syntax.
2020-05-21Token remover: match only base64 in regexGravatar MarkKoz-7/+6
Making the regex more accurate reduces false positives at an earlier stage. There's no benefit to matching non-base64 as that would just be weeded out as invalid at a later stage anyway when it tries to decode it.
2020-05-21Token remover: decode ID using URL-safe base64Gravatar MarkKoz-1/+1
Though I've not seen an ID with neither + and \ nor - and _, given that the timestamp uses URL-safe encoding, the ID probably does too.
2020-05-16Add a utility function to pad base64 dataGravatar MarkKoz-2/+8
2020-05-15Token remover: use strict check for digits in token IDGravatar MarkKoz-2/+5
`isnumeric` would be true for a wide range of characters in Unicode, but the ID must only consist of the characters 0-9 (ASCII digits). In fact, `isdigit` on its own would also match other Unicode characters too.
2020-05-14Token remover: fix timestamp checkGravatar MarkKoz-10/+16
The timestamp calculation was incorrect. The bytes need to be interpreted as big-endian and the result is just a timestamp rather than a snowflake.
2020-05-13Token remover: add logs to clarify why token is invalidGravatar MarkKoz-2/+4
2020-05-13Add missing comma to token remover log messageGravatar MarkKoz-1/+1
2020-05-13Fix a test needlessly being a coroutineGravatar MarkKoz-1/+1
2020-05-11Use subtests for valid ID/timestamp tests and test non-ASCII inputsGravatar MarkKoz-18/+25
2020-05-11Clean up token remover test importsGravatar MarkKoz-16/+12
2020-05-11Replace deprecated assertion methodsGravatar MarkKoz-2/+2
2020-05-11Refactor `TokenRemoverSetupTests` and add a more thorough testGravatar MarkKoz-4/+8
The test now ensures the cog is instantiated and that the instance is passed as an argument to `add_cog`.
2020-05-11Test TokenRemover.take_actionGravatar MarkKoz-43/+30
* Remove `bot.get_cog` mocks in `setUp` * Mock the logger cause it's easier to assert logs * Remove subtests * Assert helper functions were called * Create an autospec for ModLog
2020-05-11Test TokenRemover.format_log_messageGravatar MarkKoz-0/+16
2020-05-11Test token remover's message deletionGravatar MarkKoz-0/+9
2020-05-11Simplify token remover's message mockGravatar MarkKoz-9/+3
* Rely on default values for the author * Set the content to a non-empty string
2020-05-11Avoid instantiating the cog when testing static/class methodsGravatar MarkKoz-10/+5
2020-05-11Token remover: use a string template for the log messageGravatar MarkKoz-4/+11
2020-05-11Token remover: split some of `take_action` into separate functionsGravatar MarkKoz-11/+21
2020-05-11Correct the return type annotation for the autospec decoratorGravatar MarkKoz-2/+2
2020-05-11Test token regex matches valid tokensGravatar MarkKoz-0/+21
2020-05-11Test is_maybe_tokenGravatar MarkKoz-7/+24
2020-05-11Token remover: fix `is_maybe_token` returning None instead of FalseGravatar MarkKoz-0/+2
It's annotated as returning a bool and when the split fails it already returns False. To be consistent, it should always return a bool.
2020-05-11Test `is_maybe_token` returns False for missing partsGravatar MarkKoz-0/+10
In practice, this won't ever happen since the regex wouldn't match strings with missing parts. However, the function does check it so may as well test it. It's not necessarily bound to always use inputs from the regex either I suppose.
2020-05-11Fix autospec decorator when used with multiple attributesGravatar MarkKoz-16/+12
The original approach of messing with the `attribute_name` didn't work for reasons I won't discuss here (would require knowledge of patcher internals). The new approach doesn't use patch.multiple but mimics it by applying multiple patch decorators to the function. As a consequence, this can no longer be used as a context manager.
2020-05-11Test token regex doesn't match invalid tokensGravatar MarkKoz-7/+25
2020-05-11Test `find_token_in_message` returns the found tokenGravatar MarkKoz-0/+24
2020-05-11Test `find_token_in_message` returns None if no matches foundGravatar MarkKoz-0/+14
2020-05-11Fix test for token remover ignoring bot messagesGravatar MarkKoz-4/+9
It's not possible to test this via asserting the return value of `on_message` since it never returns anything. Instead, the actual relevant unit, `find_token_in_message,` should be tested.
2020-05-11Allow using arbitrary parameter names with the autospec decoratorGravatar MarkKoz-2/+15
This gives the caller more flexibility. Sometimes attribute names are too long or they don't follow a naming scheme accepted by the linter.
2020-05-11Test token remover skips messages without tokensGravatar MarkKoz-0/+11
2020-05-11Test token remover takes action if a token is foundGravatar MarkKoz-1/+13
2020-05-11Add a test helper function to patch multiple attributes with autospecsGravatar MarkKoz-0/+9
This helper reduces redundancy/boilerplate by setting default values. It also has the consequence of shortening the length of the invocation, which makes it faster to use and easier to read.
2020-05-11Test on_message_edit of token remover uses on_messageGravatar MarkKoz-2/+10
2020-05-11Token remover: reduce duplicated code in `on_message_edit`Gravatar MarkKoz-3/+1
2020-05-11Token remover: catch ValueError when non-ASCII chars are presentGravatar MarkKoz-2/+2
The token uses base64 and base64 only allows ASCII characters. Thus, if a match has non-ASCII characters, it's not a valid token. Catching the ValueError is simpler than trying to adjust the regex to only match valid base64. Fixes #928 Fixes BOT-3X