mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-10 13:00:16 +02:00
Fix bug blocking superuser from adding Discord bot
This commit is contained in:
parent
93c89dd7cc
commit
55cc77140e
@ -171,8 +171,15 @@ class DiscordUserManager(models.Manager):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def server_name(cls):
|
def server_name(cls):
|
||||||
"""returns the name of the Discord server"""
|
"""returns the name of the current Discord server
|
||||||
return cls._bot_client().guild_name(DISCORD_GUILD_ID)
|
or an empty string if the name could not be retrieved
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
server_name = cls._bot_client().guild_name(DISCORD_GUILD_ID)
|
||||||
|
except HTTPError:
|
||||||
|
server_name = ""
|
||||||
|
|
||||||
|
return server_name
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _bot_client(is_rate_limited: bool = True):
|
def _bot_client(is_rate_limited: bool = True):
|
||||||
|
@ -474,3 +474,24 @@ class TestUserFeatures(WebTest):
|
|||||||
|
|
||||||
expected = [remove_guild_member_request, guild_infos_request]
|
expected = [remove_guild_member_request, guild_infos_request]
|
||||||
self.assertListEqual(requests_made, expected)
|
self.assertListEqual(requests_made, expected)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.views.messages')
|
||||||
|
def test_user_add_new_server(self, requests_mocker, mock_messages):
|
||||||
|
# guild_infos()
|
||||||
|
mock_exception = HTTPError('can not get guild info from Discord API')
|
||||||
|
mock_exception.response = Mock()
|
||||||
|
mock_exception.response.status_code = 440
|
||||||
|
requests_mocker.get(guild_infos_request.url, exc=mock_exception)
|
||||||
|
|
||||||
|
# login
|
||||||
|
superuser = User.objects.create_superuser(
|
||||||
|
"admin", "admin@example.com", "admin123"
|
||||||
|
)
|
||||||
|
AuthUtils.add_main_character_2(superuser, "my_main", 1099)
|
||||||
|
self.app.set_user(superuser)
|
||||||
|
|
||||||
|
# click deactivate on the service page
|
||||||
|
response = self.app.get(reverse('services:services'))
|
||||||
|
|
||||||
|
# check we got can see the page
|
||||||
|
self.assertEqual(response.status_int, 200)
|
||||||
|
@ -361,3 +361,26 @@ class TestUserHasAccount(TestCase):
|
|||||||
|
|
||||||
def test_return_false_if_not_called_with_user_object(self):
|
def test_return_false_if_not_called_with_user_object(self):
|
||||||
self.assertFalse(DiscordUser.objects.user_has_account('abc'))
|
self.assertFalse(DiscordUser.objects.user_has_account('abc'))
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
class TestServerName(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
|
||||||
|
def test_returns_name_when_api_returns_it(self, mock_DiscordClient):
|
||||||
|
server_name = "El Dorado"
|
||||||
|
mock_DiscordClient.return_value.guild_name.return_value = server_name
|
||||||
|
|
||||||
|
self.assertEqual(DiscordUser.objects.server_name(), server_name)
|
||||||
|
|
||||||
|
def test_returns_empty_string_when_api_throws_http_error(self, mock_DiscordClient):
|
||||||
|
mock_exception = HTTPError('Test exception')
|
||||||
|
mock_exception.response = Mock(**{"status_code": 440})
|
||||||
|
mock_DiscordClient.return_value.guild_name.side_effect = mock_exception
|
||||||
|
|
||||||
|
self.assertEqual(DiscordUser.objects.server_name(), "")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user