diff options
author | 2019-10-02 16:59:03 +0200 | |
---|---|---|
committer | 2019-10-11 17:42:21 +0200 | |
commit | c4213744c18be23e3e4484f126ae0b2d0eba4437 (patch) | |
tree | fa26b8d115eac7b9d46fd2abae966c3030f32e78 /tests/cogs/test_token_remover.py | |
parent | Merge pull request #505 from python-discord/user-log-display-name-changes (diff) |
Migrate pytest to unittest
After a discussion in the core developers channel, we have decided to
migrate from `pytest` to `unittest` as the testing framework. This
commit sets up the repository to use `unittest` and migrates the
first couple of tests files to the new framework.
What I have done to migrate to `unitest`:
- Removed all `pytest` test files, since they are incompatible.
- Removed `pytest`-related dependencies from the Pipfile.
- Added `coverage.py` to the Pipfile dev-packages and relocked.
- Added convenience scripts to Pipfile for running the test suite.
- Adjust to `azure-pipelines.yml` to use `coverage.py` and `unittest`.
- Migrated four test files from `pytest` to `unittest` format.
In addition, I've added five helper Mock subclasses in `helpers.py`
and created a `TestCase` subclass in `base.py` to add an assertion
that asserts that no log records were logged within the context of
the context manager. Obviously, these new utility functions and
classes are fully tested in their respective `test_` files.
Finally, I've started with an introductory guide for writing tests
for our bot in `README.md`.
Diffstat (limited to 'tests/cogs/test_token_remover.py')
-rw-r--r-- | tests/cogs/test_token_remover.py | 133 |
1 files changed, 0 insertions, 133 deletions
diff --git a/tests/cogs/test_token_remover.py b/tests/cogs/test_token_remover.py deleted file mode 100644 index 9d46b3a05..000000000 --- a/tests/cogs/test_token_remover.py +++ /dev/null @@ -1,133 +0,0 @@ -import asyncio -from unittest.mock import MagicMock - -import pytest -from discord import Colour - -from bot.cogs.token_remover import ( - DELETION_MESSAGE_TEMPLATE, - TokenRemover, - setup as setup_cog, -) -from bot.constants import Channels, Colours, Event, Icons -from tests.helpers import AsyncMock - - -def token_remover(): - bot = MagicMock() - bot.get_cog.return_value = MagicMock() - bot.get_cog.return_value.send_log_message = AsyncMock() - return TokenRemover(bot=bot) - - -def message(): - message = MagicMock() - message.author.__str__.return_value = 'lemon' - message.author.bot = False - message.author.avatar_url_as.return_value = 'picture-lemon.png' - message.author.id = 42 - message.author.mention = '@lemon' - message.channel.send = AsyncMock() - message.channel.mention = '#lemonade-stand' - message.content = '' - message.delete = AsyncMock() - message.id = 555 - return message - - - ('content', 'expected'), - ( - ('MTIz', True), # 123 - ('YWJj', False), # abc - ) -) -def test_is_valid_user_id(content: str, expected: bool): - assert TokenRemover.is_valid_user_id(content) is expected - - - ('content', 'expected'), - ( - ('DN9r_A', True), # stolen from dapi, thanks to the author of the 'token' tag! - ('MTIz', False), # 123 - ) -) -def test_is_valid_timestamp(content: str, expected: bool): - assert TokenRemover.is_valid_timestamp(content) is expected - - -def test_mod_log_property(token_remover): - token_remover.bot.get_cog.return_value = 'lemon' - assert token_remover.mod_log == 'lemon' - token_remover.bot.get_cog.assert_called_once_with('ModLog') - - -def test_ignores_bot_messages(token_remover, message): - message.author.bot = True - coroutine = token_remover.on_message(message) - assert asyncio.run(coroutine) is None - - [email protected]('content', ('', 'lemon wins')) -def test_ignores_messages_without_tokens(token_remover, message, content): - message.content = content - coroutine = token_remover.on_message(message) - assert asyncio.run(coroutine) is None - - [email protected]('content', ('foo.bar.baz', 'x.y.')) -def test_ignores_invalid_tokens(token_remover, message, content): - message.content = content - coroutine = token_remover.on_message(message) - assert asyncio.run(coroutine) is None - - - 'content, censored_token', - ( - ('MTIz.DN9R_A.xyz', 'MTIz.DN9R_A.xxx'), - ) -) -def test_censors_valid_tokens( - token_remover, message, content, censored_token, caplog -): - message.content = content - coroutine = token_remover.on_message(message) - assert asyncio.run(coroutine) is None # still no rval - - # asyncio logs some stuff about its reactor, discard it - [_, record] = caplog.records - assert record.message == ( - "Censored a seemingly valid token sent by lemon (`42`) in #lemonade-stand, " - f"token was `{censored_token}`" - ) - - message.delete.assert_called_once_with() - message.channel.send.assert_called_once_with( - DELETION_MESSAGE_TEMPLATE.format(mention='@lemon') - ) - token_remover.bot.get_cog.assert_called_with('ModLog') - message.author.avatar_url_as.assert_called_once_with(static_format='png') - - mod_log = token_remover.bot.get_cog.return_value - mod_log.ignore.assert_called_once_with(Event.message_delete, message.id) - mod_log.send_log_message.assert_called_once_with( - icon_url=Icons.token_removed, - colour=Colour(Colours.soft_red), - title="Token removed!", - text=record.message, - thumbnail='picture-lemon.png', - channel_id=Channels.mod_alerts - ) - - -def test_setup(caplog): - bot = MagicMock() - setup_cog(bot) - [record] = caplog.records - - bot.add_cog.assert_called_once() - assert record.message == "Cog loaded: TokenRemover" |