mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-15 07:20:17 +02:00
parent
2b2f367c30
commit
8ce8789631
@ -163,12 +163,11 @@ class DiscordOAuthManager:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _sanitize_name(name):
|
def _sanitize_name(name):
|
||||||
return re.sub('[^\w.-]', '', name)[:32]
|
return name[:32]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _sanitize_groupname(name):
|
def _sanitize_group_name(name):
|
||||||
name = name.strip(' _')
|
return name[:100]
|
||||||
return DiscordOAuthManager._sanitize_name(name)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def generate_bot_add_url():
|
def generate_bot_add_url():
|
||||||
@ -260,7 +259,7 @@ class DiscordOAuthManager:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _group_name_to_id(name):
|
def _group_name_to_id(name):
|
||||||
name = DiscordOAuthManager._sanitize_groupname(name)
|
name = DiscordOAuthManager._sanitize_group_name(name)
|
||||||
|
|
||||||
def get_or_make_role():
|
def get_or_make_role():
|
||||||
groups = DiscordOAuthManager._get_groups()
|
groups = DiscordOAuthManager._get_groups()
|
||||||
@ -303,10 +302,9 @@ class DiscordOAuthManager:
|
|||||||
@api_backoff
|
@api_backoff
|
||||||
def update_groups(user_id, groups):
|
def update_groups(user_id, groups):
|
||||||
custom_headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
custom_headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
||||||
group_ids = [DiscordOAuthManager._group_name_to_id(DiscordOAuthManager._sanitize_groupname(g)) for g in groups]
|
group_ids = [DiscordOAuthManager._group_name_to_id(DiscordOAuthManager._sanitize_group_name(g)) for g in groups]
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/members/" + str(user_id)
|
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/members/" + str(user_id)
|
||||||
data = {'roles': group_ids}
|
data = {'roles': group_ids}
|
||||||
r = requests.patch(path, headers=custom_headers, json=data)
|
r = requests.patch(path, headers=custom_headers, json=data)
|
||||||
logger.debug("Received status code %s after setting user roles" % r.status_code)
|
logger.debug("Received status code %s after setting user roles" % r.status_code)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
|
@ -198,11 +198,11 @@ class DiscordManagerTestCase(TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test__sanitize_groupname(self):
|
def test__sanitize_group_name(self):
|
||||||
test_group_name = ' Group Name_Test_'
|
test_group_name = str(10**103)
|
||||||
group_name = DiscordOAuthManager._sanitize_groupname(test_group_name)
|
group_name = DiscordOAuthManager._sanitize_group_name(test_group_name)
|
||||||
|
|
||||||
self.assertEqual(group_name, 'GroupName_Test')
|
self.assertEqual(group_name, test_group_name[:100])
|
||||||
|
|
||||||
def test_generate_Bot_add_url(self):
|
def test_generate_Bot_add_url(self):
|
||||||
bot_add_url = DiscordOAuthManager.generate_bot_add_url()
|
bot_add_url = DiscordOAuthManager.generate_bot_add_url()
|
||||||
@ -328,7 +328,7 @@ class DiscordManagerTestCase(TestCase):
|
|||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
def test_update_groups(self, group_cache, m):
|
def test_update_groups(self, group_cache, m):
|
||||||
# Arrange
|
# Arrange
|
||||||
groups = ['Member', 'Blue', 'Special Group']
|
groups = ['Member', 'Blue', 'SpecialGroup']
|
||||||
|
|
||||||
group_cache.return_value = [{'id': 111, 'name': 'Member'},
|
group_cache.return_value = [{'id': 111, 'name': 'Member'},
|
||||||
{'id': 222, 'name': 'Blue'},
|
{'id': 222, 'name': 'Blue'},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user