From 2a9981cdb943efee200948fc513a4e8824ed3ef4 Mon Sep 17 00:00:00 2001 From: Dusty Meg Date: Wed, 8 Nov 2023 13:04:11 +0000 Subject: [PATCH] Stop renaming discord nick when setting has it disabled --- .../services/modules/discord/auth_hooks.py | 26 +++++++++++-------- .../modules/discord/tests/test_auth_hooks.py | 7 +++++ .../modules/discord/tests/test_integration.py | 2 ++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/allianceauth/services/modules/discord/auth_hooks.py b/allianceauth/services/modules/discord/auth_hooks.py index 2d6bd204..4f3af11f 100644 --- a/allianceauth/services/modules/discord/auth_hooks.py +++ b/allianceauth/services/modules/discord/auth_hooks.py @@ -11,6 +11,9 @@ from .models import DiscordUser from .urls import urlpatterns from .utils import LoggerAddTag from . import tasks, __title__ +from .app_settings import ( + DISCORD_SYNC_NAMES +) logger = LoggerAddTag(logging.getLogger(__name__), __title__) @@ -99,17 +102,18 @@ class DiscordService(ServicesHook): return has_perms def sync_nickname(self, user): - logger.debug('Syncing %s nickname for user %s', self.name, user) - if self.user_has_account(user): - tasks.update_nickname.apply_async( - kwargs={ - 'user_pk': user.pk, - # since the new nickname is not yet in the DB we need to - # provide it manually to the task - 'nickname': user_formatted_nick(user) - }, - priority=SINGLE_TASK_PRIORITY - ) + if DISCORD_SYNC_NAMES: + logger.debug('Syncing %s nickname for user %s', self.name, user) + if self.user_has_account(user): + tasks.update_nickname.apply_async( + kwargs={ + 'user_pk': user.pk, + # since the new nickname is not yet in the DB we need to + # provide it manually to the task + 'nickname': user_formatted_nick(user) + }, + priority=SINGLE_TASK_PRIORITY + ) def sync_nicknames_bulk(self, users: list): """Sync nickname for a list of users in bulk. diff --git a/allianceauth/services/modules/discord/tests/test_auth_hooks.py b/allianceauth/services/modules/discord/tests/test_auth_hooks.py index 61c07cbf..0ca60ce5 100644 --- a/allianceauth/services/modules/discord/tests/test_auth_hooks.py +++ b/allianceauth/services/modules/discord/tests/test_auth_hooks.py @@ -81,11 +81,18 @@ class TestDiscordService(NoSocketsTestCase): self.assertFalse(DiscordUser.objects.filter(user=self.none_member).exists()) @patch(MODULE_PATH + '.tasks.update_nickname') + @patch(MODULE_PATH + '.auth_hooks.DISCORD_SYNC_NAMES', True) def test_sync_nickname(self, mock_update_nickname): service = self.service() service.sync_nickname(self.member) self.assertTrue(mock_update_nickname.apply_async.called) + @patch(MODULE_PATH + '.tasks.update_nickname') + def test_sync_nickname_no_setting(self, mock_update_nickname): + service = self.service() + service.sync_nickname(self.member) + self.assertFalse(mock_update_nickname.apply_async.called) + @patch(MODULE_PATH + '.tasks.update_nicknames_bulk') def test_sync_nicknames_bulk(self, mock_update_nicknames_bulk): service = self.service() diff --git a/allianceauth/services/modules/discord/tests/test_integration.py b/allianceauth/services/modules/discord/tests/test_integration.py index 2d50db5e..aae213e9 100644 --- a/allianceauth/services/modules/discord/tests/test_integration.py +++ b/allianceauth/services/modules/discord/tests/test_integration.py @@ -164,6 +164,7 @@ class TestServiceFeatures(TransactionTestCase): self.discord_user = DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID) self.assertTrue(DiscordUser.objects.user_has_account(self.user)) + @patch(MODULE_PATH + '.auth_hooks.DISCORD_SYNC_NAMES', True) def test_when_name_of_main_changes_then_discord_nick_is_updated( self, requests_mocker ): @@ -185,6 +186,7 @@ class TestServiceFeatures(TransactionTestCase): self.assertTrue(nick_updated) self.assertTrue(DiscordUser.objects.user_has_account(self.user)) + @patch(MODULE_PATH + '.auth_hooks.DISCORD_SYNC_NAMES', True) def test_when_name_of_main_changes_and_user_deleted_then_account_is_deleted( self, requests_mocker ):