Fix bug blocking superuser from adding Discord bot

This commit is contained in:
ErikKalkoken 2020-06-25 22:19:48 +02:00
parent 93c89dd7cc
commit 55cc77140e
3 changed files with 53 additions and 2 deletions

View File

@ -171,8 +171,15 @@ class DiscordUserManager(models.Manager):
@classmethod
def server_name(cls):
"""returns the name of the Discord server"""
return cls._bot_client().guild_name(DISCORD_GUILD_ID)
"""returns the name of the current Discord server
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
def _bot_client(is_rate_limited: bool = True):

View File

@ -474,3 +474,24 @@ class TestUserFeatures(WebTest):
expected = [remove_guild_member_request, guild_infos_request]
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)

View File

@ -361,3 +361,26 @@ class TestUserHasAccount(TestCase):
def test_return_false_if_not_called_with_user_object(self):
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(), "")