From 0d7e303b17d76719c77906617c23bbb3674ce451 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Thu, 3 Jan 2019 20:01:48 +0100 Subject: Add role and user differ unit tests. --- tests/cogs/sync/test_roles.py | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/cogs/sync/test_roles.py (limited to 'tests/cogs/sync/test_roles.py') diff --git a/tests/cogs/sync/test_roles.py b/tests/cogs/sync/test_roles.py new file mode 100644 index 000000000..7def815cc --- /dev/null +++ b/tests/cogs/sync/test_roles.py @@ -0,0 +1,64 @@ +from bot.cogs.sync.syncers import get_roles_for_sync, Role + + +def test_get_roles_for_sync_empty_return_for_equal_roles(): + api_roles = {Role(id=41, name='name', colour=33, permissions=0x8)} + guild_roles = {Role(id=41, name='name', colour=33, permissions=0x8)} + + assert get_roles_for_sync(guild_roles, api_roles) == (set(), set()) + + +def test_get_roles_for_sync_returns_roles_to_update_with_non_id_diff(): + api_roles = {Role(id=41, name='old name', colour=35, permissions=0x8)} + guild_roles = {Role(id=41, name='new name', colour=33, permissions=0x8)} + + assert get_roles_for_sync(guild_roles, api_roles) == ( + set(), + guild_roles + ) + + +def test_get_roles_only_returns_roles_that_require_update(): + api_roles = { + Role(id=41, name='old name', colour=33, permissions=0x8), + Role(id=53, name='other role', colour=55, permissions=0) + } + guild_roles = { + Role(id=41, name='new name', colour=35, permissions=0x8), + Role(id=53, name='other role', colour=55, permissions=0) + } + + assert get_roles_for_sync(guild_roles, api_roles) == ( + set(), + {Role(id=41, name='new name', colour=35, permissions=0x8)}, + ) + + +def test_get_roles_returns_new_roles_in_first_tuple_element(): + api_roles = { + Role(id=41, name='name', colour=35, permissions=0x8), + } + guild_roles = { + Role(id=41, name='name', colour=35, permissions=0x8), + Role(id=53, name='other role', colour=55, permissions=0) + } + + assert get_roles_for_sync(guild_roles, api_roles) == ( + {Role(id=53, name='other role', colour=55, permissions=0)}, + set() + ) + + +def test_get_roles_returns_roles_to_update_and_new_roles(): + api_roles = { + Role(id=41, name='old name', colour=35, permissions=0x8), + } + guild_roles = { + Role(id=41, name='new name', colour=40, permissions=0x16), + Role(id=53, name='other role', colour=55, permissions=0) + } + + assert get_roles_for_sync(guild_roles, api_roles) == ( + {Role(id=53, name='other role', colour=55, permissions=0)}, + {Role(id=41, name='new name', colour=40, permissions=0x16)} + ) -- cgit v1.2.3 From 1ff9aa88628d7c25484da1b85ef4d1eeb0049b63 Mon Sep 17 00:00:00 2001 From: Scragly <29337040+scragly@users.noreply.github.com> Date: Sat, 9 Mar 2019 17:52:23 +1000 Subject: Fix the linting issues with Bot so builds will work. --- bot/cogs/bigbrother.py | 14 +++++++------- bot/cogs/defcon.py | 2 +- bot/cogs/doc.py | 2 +- bot/cogs/information.py | 2 +- bot/cogs/modlog.py | 5 +---- bot/cogs/reminders.py | 6 ++---- bot/cogs/superstarify/__init__.py | 16 ++++------------ bot/cogs/tags.py | 7 ++----- bot/utils/moderation.py | 2 +- tests/cogs/sync/test_roles.py | 2 +- tests/cogs/sync/test_users.py | 1 + tox.ini | 2 +- 12 files changed, 23 insertions(+), 38 deletions(-) (limited to 'tests/cogs/sync/test_roles.py') diff --git a/bot/cogs/bigbrother.py b/bot/cogs/bigbrother.py index a6962efea..df7a0b576 100644 --- a/bot/cogs/bigbrother.py +++ b/bot/cogs/bigbrother.py @@ -4,10 +4,12 @@ import re from collections import defaultdict, deque from typing import List, Union -from discord import Color, Embed, Guild, Member, Message, TextChannel, User +from discord import Color, Embed, Guild, Member, Message, User from discord.ext.commands import Bot, Context, group -from bot.constants import BigBrother as BigBrotherConfig, Channels, Emojis, Guild as GuildConfig, Keys, Roles, URLs +from bot.constants import ( + BigBrother as BigBrotherConfig, Channels, Emojis, Guild as GuildConfig, Roles +) from bot.decorators import with_role from bot.pagination import LinePaginator from bot.utils import messages @@ -73,12 +75,11 @@ class BigBrother: ) self.watched_users.remove(user.id) del self.channel_queues[user.id] - await channel.send( + await self.channel.send( f"{Emojis.bb_message}:hammer: {user} got banned, so " f"`BigBrother` will no longer relay their messages." ) - async def on_message(self, msg: Message): """Queues up messages sent by watched users.""" @@ -100,7 +101,7 @@ class BigBrother: log.trace("Begin consuming messages.") channel_queues = self.channel_queues.copy() self.channel_queues.clear() - for user_id, queues in channel_queues.items(): + for _, queues in channel_queues.items(): for queue in queues.values(): while queue: msg = queue.popleft() @@ -219,13 +220,12 @@ class BigBrother: if user.id in self.watched_users: return await ctx.send(":x: That user is already watched.") - created_infraction = await post_infraction( + await post_infraction( ctx, user, type='watch', reason=reason, hidden=True ) self.watched_users.add(user.id) await ctx.send(f":ok_hand: will now relay messages sent by {user}") - @bigbrother_group.command(name='unwatch', aliases=('uw',)) @with_role(Roles.owner, Roles.admin, Roles.moderator) async def unwatch_command(self, ctx: Context, user: User): diff --git a/bot/cogs/defcon.py b/bot/cogs/defcon.py index 8fa80020a..29979de83 100644 --- a/bot/cogs/defcon.py +++ b/bot/cogs/defcon.py @@ -5,7 +5,7 @@ from discord import Colour, Embed, Member from discord.ext.commands import Bot, Context, group from bot.cogs.modlog import ModLog -from bot.constants import Channels, Emojis, Icons, Keys, Roles, URLs +from bot.constants import Channels, Emojis, Icons, Keys, Roles from bot.decorators import with_role log = logging.getLogger(__name__) diff --git a/bot/cogs/doc.py b/bot/cogs/doc.py index 860ec7f62..d427acc3a 100644 --- a/bot/cogs/doc.py +++ b/bot/cogs/doc.py @@ -13,7 +13,7 @@ from markdownify import MarkdownConverter from requests import ConnectionError from sphinx.ext import intersphinx -from bot.constants import Keys, Roles +from bot.constants import Roles from bot.converters import ValidPythonIdentifier, ValidURL from bot.decorators import with_role from bot.pagination import LinePaginator diff --git a/bot/cogs/information.py b/bot/cogs/information.py index 476820feb..92b2444a3 100644 --- a/bot/cogs/information.py +++ b/bot/cogs/information.py @@ -4,7 +4,7 @@ import textwrap from discord import CategoryChannel, Colour, Embed, Member, TextChannel, VoiceChannel from discord.ext.commands import Bot, Context, command -from bot.constants import Emojis, Keys, Roles, URLs +from bot.constants import Emojis, Keys, Roles from bot.decorators import with_role from bot.utils.time import time_since diff --git a/bot/cogs/modlog.py b/bot/cogs/modlog.py index 911b5da03..66e80e778 100644 --- a/bot/cogs/modlog.py +++ b/bot/cogs/modlog.py @@ -3,7 +3,6 @@ import logging from datetime import datetime from typing import List, Optional, Union -from aiohttp import ClientResponseError from dateutil.relativedelta import relativedelta from deepdiff import DeepDiff from discord import ( @@ -16,9 +15,7 @@ from discord.abc import GuildChannel from discord.ext.commands import Bot from bot.constants import ( - Channels, Colours, Emojis, - Event, Guild as GuildConstant, Icons, - Keys, Roles, URLs + Channels, Colours, Emojis, Event, Guild as GuildConstant, Icons, URLs ) from bot.utils.time import humanize_delta diff --git a/bot/cogs/reminders.py b/bot/cogs/reminders.py index b86fecd5c..fa1be307c 100644 --- a/bot/cogs/reminders.py +++ b/bot/cogs/reminders.py @@ -5,19 +5,17 @@ import textwrap from datetime import datetime from operator import itemgetter -from aiohttp import ClientResponseError from dateutil.relativedelta import relativedelta from discord import Colour, Embed from discord.ext.commands import Bot, Context, group from bot.constants import ( - Channels, Icons, Keys, NEGATIVE_REPLIES, - POSITIVE_REPLIES, Roles, URLs + Channels, Icons, NEGATIVE_REPLIES, POSITIVE_REPLIES, Roles ) from bot.converters import ExpirationDate from bot.pagination import LinePaginator from bot.utils.scheduling import Scheduler -from bot.utils.time import humanize_delta, parse_rfc1123, wait_until +from bot.utils.time import humanize_delta, wait_until log = logging.getLogger(__name__) diff --git a/bot/cogs/superstarify/__init__.py b/bot/cogs/superstarify/__init__.py index e14336b52..efa02cb43 100644 --- a/bot/cogs/superstarify/__init__.py +++ b/bot/cogs/superstarify/__init__.py @@ -8,14 +8,10 @@ from discord.ext.commands import Bot, Context, command from bot.cogs.moderation import Moderation from bot.cogs.modlog import ModLog -from bot.constants import ( - Icons, Keys, - NEGATIVE_REPLIES, POSITIVE_REPLIES, - Roles, URLs -) +from bot.cogs.superstarify.stars import get_nick +from bot.constants import Icons, POSITIVE_REPLIES, Roles from bot.converters import ExpirationDate from bot.decorators import with_role -from bot.cogs.superstarify.stars import get_nick from bot.utils.moderation import post_infraction log = logging.getLogger(__name__) @@ -117,8 +113,7 @@ class Superstarify: forced_nick = get_nick(infraction['id'], member.id) await member.edit(nick=forced_nick) end_timestamp_human = ( - datetime.fromisoformat(response['expires_at'][:-1]) - .strftime('%c') + datetime.fromisoformat(infraction['expires_at'][:-1]).strftime('%c') ) try: @@ -157,8 +152,7 @@ class Superstarify: @command(name='superstarify', aliases=('force_nick', 'star')) @with_role(Roles.admin, Roles.owner, Roles.moderator) async def superstarify( - self, ctx: Context, member: Member, - expiration: ExpirationDate, reason: str = None + self, ctx: Context, member: Member, expiration: ExpirationDate, reason: str = None ): """ This command will force a random superstar name (like Taylor Swift) to be the user's @@ -180,7 +174,6 @@ class Superstarify: f"See infraction **#{active_superstarifies[0]['id']}**." ) - infraction = await post_infraction( ctx, member, type='superstar', reason=reason or ('old nick: ' + member.display_name), @@ -256,7 +249,6 @@ class Superstarify: ":x: There is no active superstarify infraction for this user." ) - [infraction] = active_superstarifies await self.bot.api_client.patch( 'bot/infractions/' + str(infraction['id']), diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 5a0198db8..bb4d6ba71 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -2,13 +2,10 @@ import logging import time from discord import Colour, Embed -from discord.ext.commands import ( - BadArgument, Bot, - Context, group -) +from discord.ext.commands import Bot, Context, group from bot.constants import Channels, Cooldowns, Keys, Roles -from bot.converters import TagContentConverter, TagNameConverter, ValidURL +from bot.converters import TagContentConverter, TagNameConverter from bot.decorators import with_role from bot.pagination import LinePaginator diff --git a/bot/utils/moderation.py b/bot/utils/moderation.py index 2611ee993..fcdf3c4d5 100644 --- a/bot/utils/moderation.py +++ b/bot/utils/moderation.py @@ -6,7 +6,7 @@ from aiohttp import ClientError from discord import Member, Object, User from discord.ext.commands import Context -from bot.constants import Keys, URLs +from bot.constants import Keys log = logging.getLogger(__name__) diff --git a/tests/cogs/sync/test_roles.py b/tests/cogs/sync/test_roles.py index 7def815cc..18682f39f 100644 --- a/tests/cogs/sync/test_roles.py +++ b/tests/cogs/sync/test_roles.py @@ -1,4 +1,4 @@ -from bot.cogs.sync.syncers import get_roles_for_sync, Role +from bot.cogs.sync.syncers import Role, get_roles_for_sync def test_get_roles_for_sync_empty_return_for_equal_roles(): diff --git a/tests/cogs/sync/test_users.py b/tests/cogs/sync/test_users.py index ecf1d3926..a863ae35b 100644 --- a/tests/cogs/sync/test_users.py +++ b/tests/cogs/sync/test_users.py @@ -1,5 +1,6 @@ from bot.cogs.sync.syncers import User, get_users_for_sync + def fake_user(**kwargs): kwargs.setdefault('id', 43) kwargs.setdefault('name', 'bob the test man') diff --git a/tox.ini b/tox.ini index c6fa513f4..c84827570 100644 --- a/tox.ini +++ b/tox.ini @@ -2,5 +2,5 @@ max-line-length=120 application_import_names=bot exclude=.cache,.venv -ignore=B311,W503,E226,S311 +ignore=B311,W503,E226,S311,T000 import-order-style=pycharm -- cgit v1.2.3 From bb6e9a050d14c375243fecb15f8fdffe7ad67787 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> Date: Thu, 15 Aug 2019 20:52:29 +0200 Subject: Updating role sync tests for position and adding tests for deletion detection --- tests/cogs/sync/test_roles.py | 81 ++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 21 deletions(-) (limited to 'tests/cogs/sync/test_roles.py') diff --git a/tests/cogs/sync/test_roles.py b/tests/cogs/sync/test_roles.py index 18682f39f..c561ba447 100644 --- a/tests/cogs/sync/test_roles.py +++ b/tests/cogs/sync/test_roles.py @@ -2,63 +2,102 @@ from bot.cogs.sync.syncers import Role, get_roles_for_sync def test_get_roles_for_sync_empty_return_for_equal_roles(): - api_roles = {Role(id=41, name='name', colour=33, permissions=0x8)} - guild_roles = {Role(id=41, name='name', colour=33, permissions=0x8)} + api_roles = {Role(id=41, name='name', colour=33, permissions=0x8, position=1)} + guild_roles = {Role(id=41, name='name', colour=33, permissions=0x8, position=1)} - assert get_roles_for_sync(guild_roles, api_roles) == (set(), set()) + assert get_roles_for_sync(guild_roles, api_roles) == (set(), set(), set()) def test_get_roles_for_sync_returns_roles_to_update_with_non_id_diff(): - api_roles = {Role(id=41, name='old name', colour=35, permissions=0x8)} - guild_roles = {Role(id=41, name='new name', colour=33, permissions=0x8)} + api_roles = {Role(id=41, name='old name', colour=35, permissions=0x8, position=1)} + guild_roles = {Role(id=41, name='new name', colour=33, permissions=0x8, position=2)} assert get_roles_for_sync(guild_roles, api_roles) == ( set(), - guild_roles + guild_roles, + set(), ) def test_get_roles_only_returns_roles_that_require_update(): api_roles = { - Role(id=41, name='old name', colour=33, permissions=0x8), - Role(id=53, name='other role', colour=55, permissions=0) + Role(id=41, name='old name', colour=33, permissions=0x8, position=1), + Role(id=53, name='other role', colour=55, permissions=0, position=3) } guild_roles = { - Role(id=41, name='new name', colour=35, permissions=0x8), - Role(id=53, name='other role', colour=55, permissions=0) + Role(id=41, name='new name', colour=35, permissions=0x8, position=2), + Role(id=53, name='other role', colour=55, permissions=0, position=3) } assert get_roles_for_sync(guild_roles, api_roles) == ( set(), - {Role(id=41, name='new name', colour=35, permissions=0x8)}, + {Role(id=41, name='new name', colour=35, permissions=0x8, position=2)}, + set(), ) def test_get_roles_returns_new_roles_in_first_tuple_element(): api_roles = { - Role(id=41, name='name', colour=35, permissions=0x8), + Role(id=41, name='name', colour=35, permissions=0x8, position=1), } guild_roles = { - Role(id=41, name='name', colour=35, permissions=0x8), - Role(id=53, name='other role', colour=55, permissions=0) + Role(id=41, name='name', colour=35, permissions=0x8, position=1), + Role(id=53, name='other role', colour=55, permissions=0, position=2) } assert get_roles_for_sync(guild_roles, api_roles) == ( - {Role(id=53, name='other role', colour=55, permissions=0)}, - set() + {Role(id=53, name='other role', colour=55, permissions=0, position=2)}, + set(), + set(), ) def test_get_roles_returns_roles_to_update_and_new_roles(): api_roles = { - Role(id=41, name='old name', colour=35, permissions=0x8), + Role(id=41, name='old name', colour=35, permissions=0x8, position=1), + } + guild_roles = { + Role(id=41, name='new name', colour=40, permissions=0x16, position=2), + Role(id=53, name='other role', colour=55, permissions=0, position=3) + } + + assert get_roles_for_sync(guild_roles, api_roles) == ( + {Role(id=53, name='other role', colour=55, permissions=0, position=3)}, + {Role(id=41, name='new name', colour=40, permissions=0x16, position=2)}, + set(), + ) + + +def test_get_roles_returns_roles_to_delete(): + api_roles = { + Role(id=41, name='name', colour=35, permissions=0x8, position=1), + Role(id=61, name='to delete', colour=99, permissions=0x9, position=2), + } + guild_roles = { + Role(id=41, name='name', colour=35, permissions=0x8, position=1), + } + + assert get_roles_for_sync(guild_roles, api_roles) == ( + set(), + set(), + {Role(id=61, name='to delete', colour=99, permissions=0x9, position=2)}, + ) + + +def test_get_roles_returns_roles_to_delete_update_and_new_roles(): + api_roles = { + Role(id=41, name='not changed', colour=35, permissions=0x8, position=1), + Role(id=61, name='to delete', colour=99, permissions=0x9, position=2), + Role(id=71, name='to update', colour=99, permissions=0x9, position=3), } guild_roles = { - Role(id=41, name='new name', colour=40, permissions=0x16), - Role(id=53, name='other role', colour=55, permissions=0) + Role(id=41, name='not changed', colour=35, permissions=0x8, position=1), + Role(id=81, name='to create', colour=99, permissions=0x9, position=4), + Role(id=71, name='updated', colour=101, permissions=0x5, position=3), } assert get_roles_for_sync(guild_roles, api_roles) == ( - {Role(id=53, name='other role', colour=55, permissions=0)}, - {Role(id=41, name='new name', colour=40, permissions=0x16)} + {Role(id=81, name='to create', colour=99, permissions=0x9, position=4)}, + {Role(id=71, name='updated', colour=101, permissions=0x5, position=3)}, + {Role(id=61, name='to delete', colour=99, permissions=0x9, position=2)}, ) -- cgit v1.2.3