mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-05 06:36:19 +01:00
Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be720d0e0f | ||
|
|
72bed03244 | ||
|
|
38083ed284 | ||
|
|
53f1b94475 | ||
|
|
ed4270a0e3 | ||
|
|
f1d5cc8903 | ||
|
|
80efdec5d9 | ||
|
|
d49687400a | ||
|
|
e6e03b50da | ||
|
|
543fa3cfa9 | ||
|
|
899988c7c2 | ||
|
|
2f48dd449b | ||
|
|
f70fbbdfee | ||
|
|
2b09ca240d | ||
|
|
0626ff84ad | ||
|
|
62ec746ee3 | ||
|
|
d0f12d7d56 | ||
|
|
b806a69604 | ||
|
|
a609d6360b | ||
|
|
dafbfc8644 | ||
|
|
55413eea19 | ||
|
|
5247c181af | ||
|
|
321af5ec87 | ||
|
|
9ccf340b3d | ||
|
|
d7dcacb899 | ||
|
|
8addd483c2 | ||
|
|
4d27e5ac9b | ||
|
|
31290f6e80 | ||
|
|
c31cc4dbee | ||
|
|
cc1f94cf61 | ||
|
|
a9132b8d50 | ||
|
|
7b4a9891aa | ||
|
|
dcaaf38ecc | ||
|
|
653a8aa850 | ||
|
|
274af11385 | ||
|
|
170b246901 | ||
|
|
9ea79ea389 | ||
|
|
b6fdf840ef | ||
|
|
42948386ec |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -72,3 +72,6 @@ celerybeat-schedule
|
|||||||
|
|
||||||
#transifex
|
#transifex
|
||||||
.tx/
|
.tx/
|
||||||
|
|
||||||
|
#other
|
||||||
|
.flake8
|
||||||
|
|||||||
@@ -6,11 +6,6 @@ before_script:
|
|||||||
- python -V
|
- python -V
|
||||||
- pip install wheel tox
|
- pip install wheel tox
|
||||||
|
|
||||||
test-3.5-core:
|
|
||||||
image: python:3.5-buster
|
|
||||||
script:
|
|
||||||
- tox -e py35-core
|
|
||||||
|
|
||||||
test-3.6-core:
|
test-3.6-core:
|
||||||
image: python:3.6-buster
|
image: python:3.6-buster
|
||||||
script:
|
script:
|
||||||
@@ -26,11 +21,6 @@ test-3.8-core:
|
|||||||
script:
|
script:
|
||||||
- tox -e py38-core
|
- tox -e py38-core
|
||||||
|
|
||||||
test-3.5-all:
|
|
||||||
image: python:3.5-buster
|
|
||||||
script:
|
|
||||||
- tox -e py35-all
|
|
||||||
|
|
||||||
test-3.6-all:
|
test-3.6-all:
|
||||||
image: python:3.6-buster
|
image: python:3.6-buster
|
||||||
script:
|
script:
|
||||||
|
|||||||
14
.pylintrc
Normal file
14
.pylintrc
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
[MASTER]
|
||||||
|
ignore-patterns=test_.*.py,__init__.py,generate_.*.py
|
||||||
|
|
||||||
|
[BASIC]
|
||||||
|
# Good variable names which should always be accepted, separated by a comma
|
||||||
|
good-names=i,j,k,x,f,ex
|
||||||
|
|
||||||
|
[FORMAT]
|
||||||
|
# Maximum number of characters on a single line.
|
||||||
|
max-line-length=100
|
||||||
|
|
||||||
|
[MESSAGES CONTROL]
|
||||||
|
disable=R,C
|
||||||
|
|
||||||
27
.readthedocs.yml
Normal file
27
.readthedocs.yml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# .readthedocs.yml
|
||||||
|
# Read the Docs configuration file
|
||||||
|
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
|
||||||
|
|
||||||
|
# Required
|
||||||
|
version: 2
|
||||||
|
|
||||||
|
# Build documentation in the docs/ directory with Sphinx
|
||||||
|
sphinx:
|
||||||
|
configuration: docs/conf.py
|
||||||
|
|
||||||
|
# Build documentation with MkDocs
|
||||||
|
#mkdocs:
|
||||||
|
# configuration: mkdocs.yml
|
||||||
|
|
||||||
|
# Optionally build your docs in additional formats such as PDF and ePub
|
||||||
|
formats: all
|
||||||
|
|
||||||
|
# Optionally set the version of Python and requirements required to build your docs
|
||||||
|
python:
|
||||||
|
version: 3.7
|
||||||
|
install:
|
||||||
|
- method: pip
|
||||||
|
path: .
|
||||||
|
extra_requirements:
|
||||||
|
- testing
|
||||||
|
system_packages: true
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
# This will make sure the app is always imported when
|
# This will make sure the app is always imported when
|
||||||
# Django starts so that shared_task will use this app.
|
# Django starts so that shared_task will use this app.
|
||||||
|
|
||||||
__version__ = '2.6.3'
|
__version__ = '2.6.6a9'
|
||||||
NAME = 'Alliance Auth v%s' % __version__
|
__title__ = 'Alliance Auth'
|
||||||
|
__url__ = 'https://gitlab.com/allianceauth/allianceauth'
|
||||||
|
NAME = '%s v%s' % (__title__, __version__)
|
||||||
default_app_config = 'allianceauth.apps.AllianceAuthConfig'
|
default_app_config = 'allianceauth.apps.AllianceAuthConfig'
|
||||||
|
|||||||
@@ -37,8 +37,11 @@ def make_service_hooks_update_groups_action(service):
|
|||||||
:return: fn to update services groups for the selected users
|
:return: fn to update services groups for the selected users
|
||||||
"""
|
"""
|
||||||
def update_service_groups(modeladmin, request, queryset):
|
def update_service_groups(modeladmin, request, queryset):
|
||||||
for user in queryset: # queryset filtering doesn't work here?
|
if hasattr(service, 'update_groups_bulk'):
|
||||||
service.update_groups(user)
|
service.update_groups_bulk(queryset)
|
||||||
|
else:
|
||||||
|
for user in queryset: # queryset filtering doesn't work here?
|
||||||
|
service.update_groups(user)
|
||||||
|
|
||||||
update_service_groups.__name__ = str('update_{}_groups'.format(slugify(service.name)))
|
update_service_groups.__name__ = str('update_{}_groups'.format(slugify(service.name)))
|
||||||
update_service_groups.short_description = "Sync groups for selected {} accounts".format(service.title)
|
update_service_groups.short_description = "Sync groups for selected {} accounts".format(service.title)
|
||||||
@@ -52,8 +55,11 @@ def make_service_hooks_sync_nickname_action(service):
|
|||||||
:return: fn to sync nickname for the selected users
|
:return: fn to sync nickname for the selected users
|
||||||
"""
|
"""
|
||||||
def sync_nickname(modeladmin, request, queryset):
|
def sync_nickname(modeladmin, request, queryset):
|
||||||
for user in queryset: # queryset filtering doesn't work here?
|
if hasattr(service, 'sync_nicknames_bulk'):
|
||||||
service.sync_nickname(user)
|
service.sync_nicknames_bulk(queryset)
|
||||||
|
else:
|
||||||
|
for user in queryset: # queryset filtering doesn't work here?
|
||||||
|
service.sync_nickname(user)
|
||||||
|
|
||||||
sync_nickname.__name__ = str('sync_{}_nickname'.format(slugify(service.name)))
|
sync_nickname.__name__ = str('sync_{}_nickname'.format(slugify(service.name)))
|
||||||
sync_nickname.short_description = "Sync nicknames for selected {} accounts".format(service.title)
|
sync_nickname.short_description = "Sync nicknames for selected {} accounts".format(service.title)
|
||||||
|
|||||||
@@ -141,19 +141,17 @@
|
|||||||
</table>
|
</table>
|
||||||
<table class="table table-aa visible-xs-block" style="width: 100%">
|
<table class="table table-aa visible-xs-block" style="width: 100%">
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for ownership in request.user.character_ownerships.all %}
|
{% for char in characters %}
|
||||||
{% with ownership.character as char %}
|
<tr>
|
||||||
<tr>
|
<td class="text-center" style="vertical-align: middle">
|
||||||
<td class="text-center" style="vertical-align: middle">
|
<img class="ra-avatar img-circle" src="{{ char.portrait_url_32 }}">
|
||||||
<img class="ra-avatar img-circle" src="{{ char.portrait_url_32 }}">
|
</td>
|
||||||
</td>
|
<td class="text-center" style="vertical-align: middle; width: 100%">
|
||||||
<td class="text-center" style="vertical-align: middle; width: 100%">
|
<strong>{{ char.character_name }}</strong><br>
|
||||||
<strong>{{ char.character_name }}</strong><br>
|
{{ char.corporation_name }}<br>
|
||||||
{{ char.corporation_name }}<br>
|
{{ char.alliance_name|default:"" }}
|
||||||
{{ char.alliance_name|default:"" }}
|
</td>
|
||||||
</td>
|
</tr>
|
||||||
</tr>
|
|
||||||
{% endwith %}
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -6,6 +6,10 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<meta name="description" content="">
|
<meta name="description" content="">
|
||||||
<meta name="author" content="">
|
<meta name="author" content="">
|
||||||
|
<meta property="og:title" content="{{ SITE_NAME }}">
|
||||||
|
<meta property="og:image" content="{{ request.scheme }}://{{ request.get_host }}{% static 'icons/apple-touch-icon.png' %}">
|
||||||
|
<meta property="og:description" content="Alliance Auth - An auth system for EVE Online to help in-game organizations manage online service access.">
|
||||||
|
|
||||||
{% include 'allianceauth/icons.html' %}
|
{% include 'allianceauth/icons.html' %}
|
||||||
|
|
||||||
<title>{% block title %}{{ SITE_NAME }}{% endblock %}</title>
|
<title>{% block title %}{{ SITE_NAME }}{% endblock %}</title>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch, MagicMock
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
@@ -13,6 +13,7 @@ from allianceauth.authentication.models import (
|
|||||||
from allianceauth.eveonline.models import (
|
from allianceauth.eveonline.models import (
|
||||||
EveCharacter, EveCorporationInfo, EveAllianceInfo
|
EveCharacter, EveCorporationInfo, EveAllianceInfo
|
||||||
)
|
)
|
||||||
|
from allianceauth.services.hooks import ServicesHook
|
||||||
from allianceauth.tests.auth_utils import AuthUtils
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
from ..admin import (
|
from ..admin import (
|
||||||
@@ -28,7 +29,9 @@ from ..admin import (
|
|||||||
user_main_organization,
|
user_main_organization,
|
||||||
user_profile_pic,
|
user_profile_pic,
|
||||||
user_username,
|
user_username,
|
||||||
update_main_character_model
|
update_main_character_model,
|
||||||
|
make_service_hooks_update_groups_action,
|
||||||
|
make_service_hooks_sync_nickname_action
|
||||||
)
|
)
|
||||||
from . import get_admin_change_view_url, get_admin_search_url
|
from . import get_admin_change_view_url, get_admin_search_url
|
||||||
|
|
||||||
@@ -45,136 +48,144 @@ class MockRequest(object):
|
|||||||
def __init__(self, user=None):
|
def __init__(self, user=None):
|
||||||
self.user = user
|
self.user = user
|
||||||
|
|
||||||
|
class TestCaseWithTestData(TestCase):
|
||||||
|
|
||||||
def create_test_data():
|
@classmethod
|
||||||
# groups
|
def setUpClass(cls):
|
||||||
group_1 = Group.objects.create(
|
super().setUpClass()
|
||||||
name='Group 1'
|
|
||||||
)
|
|
||||||
group_2 = Group.objects.create(
|
|
||||||
name='Group 2'
|
|
||||||
)
|
|
||||||
|
|
||||||
# user 1 - corp and alliance, normal user
|
|
||||||
character_1 = EveCharacter.objects.create(
|
|
||||||
character_id='1001',
|
|
||||||
character_name='Bruce Wayne',
|
|
||||||
corporation_id='2001',
|
|
||||||
corporation_name='Wayne Technologies',
|
|
||||||
corporation_ticker='WT',
|
|
||||||
alliance_id='3001',
|
|
||||||
alliance_name='Wayne Enterprises',
|
|
||||||
alliance_ticker='WE',
|
|
||||||
)
|
|
||||||
character_1a = EveCharacter.objects.create(
|
|
||||||
character_id='1002',
|
|
||||||
character_name='Batman',
|
|
||||||
corporation_id='2001',
|
|
||||||
corporation_name='Wayne Technologies',
|
|
||||||
corporation_ticker='WT',
|
|
||||||
alliance_id='3001',
|
|
||||||
alliance_name='Wayne Enterprises',
|
|
||||||
alliance_ticker='WE',
|
|
||||||
)
|
|
||||||
alliance = EveAllianceInfo.objects.create(
|
|
||||||
alliance_id='3001',
|
|
||||||
alliance_name='Wayne Enterprises',
|
|
||||||
alliance_ticker='WE',
|
|
||||||
executor_corp_id='2001'
|
|
||||||
)
|
|
||||||
EveCorporationInfo.objects.create(
|
|
||||||
corporation_id='2001',
|
|
||||||
corporation_name='Wayne Technologies',
|
|
||||||
corporation_ticker='WT',
|
|
||||||
member_count=42,
|
|
||||||
alliance=alliance
|
|
||||||
)
|
|
||||||
user_1 = User.objects.create_user(
|
|
||||||
character_1.character_name.replace(' ', '_'),
|
|
||||||
'abc@example.com',
|
|
||||||
'password'
|
|
||||||
)
|
|
||||||
CharacterOwnership.objects.create(
|
|
||||||
character=character_1,
|
|
||||||
owner_hash='x1' + character_1.character_name,
|
|
||||||
user=user_1
|
|
||||||
)
|
|
||||||
CharacterOwnership.objects.create(
|
|
||||||
character=character_1a,
|
|
||||||
owner_hash='x1' + character_1a.character_name,
|
|
||||||
user=user_1
|
|
||||||
)
|
|
||||||
user_1.profile.main_character = character_1
|
|
||||||
user_1.profile.save()
|
|
||||||
user_1.groups.add(group_1)
|
|
||||||
|
|
||||||
# user 2 - corp only, staff
|
for MyModel in [
|
||||||
character_2 = EveCharacter.objects.create(
|
EveAllianceInfo, EveCorporationInfo, EveCharacter, Group, User
|
||||||
character_id=1003,
|
]:
|
||||||
character_name='Clark Kent',
|
MyModel.objects.all().delete()
|
||||||
corporation_id=2002,
|
|
||||||
corporation_name='Daily Planet',
|
# groups
|
||||||
corporation_ticker='DP',
|
cls.group_1 = Group.objects.create(
|
||||||
alliance_id=None
|
name='Group 1'
|
||||||
)
|
)
|
||||||
EveCorporationInfo.objects.create(
|
cls.group_2 = Group.objects.create(
|
||||||
corporation_id=2002,
|
name='Group 2'
|
||||||
corporation_name='Daily Plane',
|
)
|
||||||
corporation_ticker='DP',
|
|
||||||
member_count=99,
|
# user 1 - corp and alliance, normal user
|
||||||
alliance=None
|
character_1 = EveCharacter.objects.create(
|
||||||
)
|
character_id='1001',
|
||||||
user_2 = User.objects.create_user(
|
character_name='Bruce Wayne',
|
||||||
character_2.character_name.replace(' ', '_'),
|
corporation_id='2001',
|
||||||
'abc@example.com',
|
corporation_name='Wayne Technologies',
|
||||||
'password'
|
corporation_ticker='WT',
|
||||||
)
|
alliance_id='3001',
|
||||||
CharacterOwnership.objects.create(
|
alliance_name='Wayne Enterprises',
|
||||||
character=character_2,
|
alliance_ticker='WE',
|
||||||
owner_hash='x1' + character_2.character_name,
|
)
|
||||||
user=user_2
|
character_1a = EveCharacter.objects.create(
|
||||||
)
|
character_id='1002',
|
||||||
user_2.profile.main_character = character_2
|
character_name='Batman',
|
||||||
user_2.profile.save()
|
corporation_id='2001',
|
||||||
user_2.groups.add(group_2)
|
corporation_name='Wayne Technologies',
|
||||||
user_2.is_staff = True
|
corporation_ticker='WT',
|
||||||
user_2.save()
|
alliance_id='3001',
|
||||||
|
alliance_name='Wayne Enterprises',
|
||||||
# user 3 - no main, no group, superuser
|
alliance_ticker='WE',
|
||||||
character_3 = EveCharacter.objects.create(
|
)
|
||||||
character_id=1101,
|
alliance = EveAllianceInfo.objects.create(
|
||||||
character_name='Lex Luthor',
|
alliance_id='3001',
|
||||||
corporation_id=2101,
|
alliance_name='Wayne Enterprises',
|
||||||
corporation_name='Lex Corp',
|
alliance_ticker='WE',
|
||||||
corporation_ticker='LC',
|
executor_corp_id='2001'
|
||||||
alliance_id=None
|
)
|
||||||
)
|
EveCorporationInfo.objects.create(
|
||||||
EveCorporationInfo.objects.create(
|
corporation_id='2001',
|
||||||
corporation_id=2101,
|
corporation_name='Wayne Technologies',
|
||||||
corporation_name='Lex Corp',
|
corporation_ticker='WT',
|
||||||
corporation_ticker='LC',
|
member_count=42,
|
||||||
member_count=666,
|
alliance=alliance
|
||||||
alliance=None
|
)
|
||||||
)
|
cls.user_1 = User.objects.create_user(
|
||||||
EveAllianceInfo.objects.create(
|
character_1.character_name.replace(' ', '_'),
|
||||||
alliance_id='3101',
|
'abc@example.com',
|
||||||
alliance_name='Lex World Domination',
|
'password'
|
||||||
alliance_ticker='LWD',
|
)
|
||||||
executor_corp_id=''
|
CharacterOwnership.objects.create(
|
||||||
)
|
character=character_1,
|
||||||
user_3 = User.objects.create_user(
|
owner_hash='x1' + character_1.character_name,
|
||||||
character_3.character_name.replace(' ', '_'),
|
user=cls.user_1
|
||||||
'abc@example.com',
|
)
|
||||||
'password'
|
CharacterOwnership.objects.create(
|
||||||
)
|
character=character_1a,
|
||||||
CharacterOwnership.objects.create(
|
owner_hash='x1' + character_1a.character_name,
|
||||||
character=character_3,
|
user=cls.user_1
|
||||||
owner_hash='x1' + character_3.character_name,
|
)
|
||||||
user=user_3
|
cls.user_1.profile.main_character = character_1
|
||||||
)
|
cls.user_1.profile.save()
|
||||||
user_3.is_superuser = True
|
cls.user_1.groups.add(cls.group_1)
|
||||||
user_3.save()
|
|
||||||
return user_1, user_2, user_3, group_1, group_2
|
# user 2 - corp only, staff
|
||||||
|
character_2 = EveCharacter.objects.create(
|
||||||
|
character_id=1003,
|
||||||
|
character_name='Clark Kent',
|
||||||
|
corporation_id=2002,
|
||||||
|
corporation_name='Daily Planet',
|
||||||
|
corporation_ticker='DP',
|
||||||
|
alliance_id=None
|
||||||
|
)
|
||||||
|
EveCorporationInfo.objects.create(
|
||||||
|
corporation_id=2002,
|
||||||
|
corporation_name='Daily Plane',
|
||||||
|
corporation_ticker='DP',
|
||||||
|
member_count=99,
|
||||||
|
alliance=None
|
||||||
|
)
|
||||||
|
cls.user_2 = User.objects.create_user(
|
||||||
|
character_2.character_name.replace(' ', '_'),
|
||||||
|
'abc@example.com',
|
||||||
|
'password'
|
||||||
|
)
|
||||||
|
CharacterOwnership.objects.create(
|
||||||
|
character=character_2,
|
||||||
|
owner_hash='x1' + character_2.character_name,
|
||||||
|
user=cls.user_2
|
||||||
|
)
|
||||||
|
cls.user_2.profile.main_character = character_2
|
||||||
|
cls.user_2.profile.save()
|
||||||
|
cls.user_2.groups.add(cls.group_2)
|
||||||
|
cls.user_2.is_staff = True
|
||||||
|
cls.user_2.save()
|
||||||
|
|
||||||
|
# user 3 - no main, no group, superuser
|
||||||
|
character_3 = EveCharacter.objects.create(
|
||||||
|
character_id=1101,
|
||||||
|
character_name='Lex Luthor',
|
||||||
|
corporation_id=2101,
|
||||||
|
corporation_name='Lex Corp',
|
||||||
|
corporation_ticker='LC',
|
||||||
|
alliance_id=None
|
||||||
|
)
|
||||||
|
EveCorporationInfo.objects.create(
|
||||||
|
corporation_id=2101,
|
||||||
|
corporation_name='Lex Corp',
|
||||||
|
corporation_ticker='LC',
|
||||||
|
member_count=666,
|
||||||
|
alliance=None
|
||||||
|
)
|
||||||
|
EveAllianceInfo.objects.create(
|
||||||
|
alliance_id='3101',
|
||||||
|
alliance_name='Lex World Domination',
|
||||||
|
alliance_ticker='LWD',
|
||||||
|
executor_corp_id=''
|
||||||
|
)
|
||||||
|
cls.user_3 = User.objects.create_user(
|
||||||
|
character_3.character_name.replace(' ', '_'),
|
||||||
|
'abc@example.com',
|
||||||
|
'password'
|
||||||
|
)
|
||||||
|
CharacterOwnership.objects.create(
|
||||||
|
character=character_3,
|
||||||
|
owner_hash='x1' + character_3.character_name,
|
||||||
|
user=cls.user_3
|
||||||
|
)
|
||||||
|
cls.user_3.is_superuser = True
|
||||||
|
cls.user_3.save()
|
||||||
|
|
||||||
|
|
||||||
def make_generic_search_request(ModelClass: type, search_term: str):
|
def make_generic_search_request(ModelClass: type, search_term: str):
|
||||||
@@ -188,12 +199,7 @@ def make_generic_search_request(ModelClass: type, search_term: str):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestCharacterOwnershipAdmin(TestCase):
|
class TestCharacterOwnershipAdmin(TestCaseWithTestData):
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpClass(cls):
|
|
||||||
super().setUpClass()
|
|
||||||
cls.user_1, _, _, _, _ = create_test_data()
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.modeladmin = CharacterOwnershipAdmin(
|
self.modeladmin = CharacterOwnershipAdmin(
|
||||||
@@ -219,12 +225,7 @@ class TestCharacterOwnershipAdmin(TestCase):
|
|||||||
self.assertEqual(response.status_code, expected)
|
self.assertEqual(response.status_code, expected)
|
||||||
|
|
||||||
|
|
||||||
class TestOwnershipRecordAdmin(TestCase):
|
class TestOwnershipRecordAdmin(TestCaseWithTestData):
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpClass(cls):
|
|
||||||
super().setUpClass()
|
|
||||||
cls.user_1, _, _, _, _ = create_test_data()
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.modeladmin = OwnershipRecordAdmin(
|
self.modeladmin = OwnershipRecordAdmin(
|
||||||
@@ -250,13 +251,8 @@ class TestOwnershipRecordAdmin(TestCase):
|
|||||||
self.assertEqual(response.status_code, expected)
|
self.assertEqual(response.status_code, expected)
|
||||||
|
|
||||||
|
|
||||||
class TestStateAdmin(TestCase):
|
class TestStateAdmin(TestCaseWithTestData):
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpClass(cls):
|
|
||||||
super().setUpClass()
|
|
||||||
create_test_data()
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.modeladmin = StateAdmin(
|
self.modeladmin = StateAdmin(
|
||||||
model=User, admin_site=AdminSite()
|
model=User, admin_site=AdminSite()
|
||||||
@@ -283,13 +279,7 @@ class TestStateAdmin(TestCase):
|
|||||||
expected = 200
|
expected = 200
|
||||||
self.assertEqual(response.status_code, expected)
|
self.assertEqual(response.status_code, expected)
|
||||||
|
|
||||||
class TestUserAdmin(TestCase):
|
class TestUserAdmin(TestCaseWithTestData):
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpClass(cls):
|
|
||||||
super().setUpClass()
|
|
||||||
cls.user_1, cls.user_2, cls.user_3, cls.group_1, cls.group_2 = \
|
|
||||||
create_test_data()
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
@@ -578,4 +568,68 @@ class TestUserAdmin(TestCase):
|
|||||||
obj = User.objects.first()
|
obj = User.objects.first()
|
||||||
response = make_generic_search_request(type(obj), obj.username)
|
response = make_generic_search_request(type(obj), obj.username)
|
||||||
expected = 200
|
expected = 200
|
||||||
self.assertEqual(response.status_code, expected)
|
self.assertEqual(response.status_code, expected)
|
||||||
|
|
||||||
|
|
||||||
|
class TestMakeServicesHooksActions(TestCaseWithTestData):
|
||||||
|
|
||||||
|
class MyServicesHookTypeA(ServicesHook):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.name = 'My Service A'
|
||||||
|
|
||||||
|
def update_groups(self, user):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def sync_nicknames(self, user):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MyServicesHookTypeB(ServicesHook):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.name = 'My Service B'
|
||||||
|
|
||||||
|
def update_groups(self, user):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def update_groups_bulk(self, user):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def sync_nicknames(self, user):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def sync_nicknames_bulk(self, user):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_service_has_update_groups_only(self):
|
||||||
|
service = self.MyServicesHookTypeA()
|
||||||
|
mock_service = MagicMock(spec=service)
|
||||||
|
action = make_service_hooks_update_groups_action(mock_service)
|
||||||
|
action(MagicMock(), MagicMock(), [self.user_1])
|
||||||
|
self.assertTrue(mock_service.update_groups.called)
|
||||||
|
|
||||||
|
def test_service_has_update_groups_bulk(self):
|
||||||
|
service = self.MyServicesHookTypeB()
|
||||||
|
mock_service = MagicMock(spec=service)
|
||||||
|
action = make_service_hooks_update_groups_action(mock_service)
|
||||||
|
action(MagicMock(), MagicMock(), [self.user_1])
|
||||||
|
self.assertFalse(mock_service.update_groups.called)
|
||||||
|
self.assertTrue(mock_service.update_groups_bulk.called)
|
||||||
|
|
||||||
|
def test_service_has_sync_nickname_only(self):
|
||||||
|
service = self.MyServicesHookTypeA()
|
||||||
|
mock_service = MagicMock(spec=service)
|
||||||
|
action = make_service_hooks_sync_nickname_action(mock_service)
|
||||||
|
action(MagicMock(), MagicMock(), [self.user_1])
|
||||||
|
self.assertTrue(mock_service.sync_nickname.called)
|
||||||
|
|
||||||
|
def test_service_has_sync_nicknames_bulk(self):
|
||||||
|
service = self.MyServicesHookTypeB()
|
||||||
|
mock_service = MagicMock(spec=service)
|
||||||
|
action = make_service_hooks_sync_nickname_action(mock_service)
|
||||||
|
action(MagicMock(), MagicMock(), [self.user_1])
|
||||||
|
self.assertFalse(mock_service.sync_nickname.called)
|
||||||
|
self.assertTrue(mock_service.sync_nicknames_bulk.called)
|
||||||
|
|||||||
@@ -22,13 +22,6 @@ urlpatterns = [
|
|||||||
r'^account/characters/add/$',
|
r'^account/characters/add/$',
|
||||||
views.add_character,
|
views.add_character,
|
||||||
name='add_character'
|
name='add_character'
|
||||||
),
|
),
|
||||||
url(
|
|
||||||
r'^help/$',
|
|
||||||
login_required(
|
|
||||||
TemplateView.as_view(template_name='allianceauth/help.html')
|
|
||||||
),
|
|
||||||
name='help'
|
|
||||||
),
|
|
||||||
url(r'^dashboard/$', views.dashboard, name='dashboard'),
|
url(r'^dashboard/$', views.dashboard, name='dashboard'),
|
||||||
]
|
]
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -53,6 +53,10 @@
|
|||||||
<a href="{% url "groupmanagement:audit_log" group.id %}" class="btn btn-info" title="{% trans "Audit Members" %}">
|
<a href="{% url "groupmanagement:audit_log" group.id %}" class="btn btn-info" title="{% trans "Audit Members" %}">
|
||||||
<i class="glyphicon glyphicon-list-alt"></i>
|
<i class="glyphicon glyphicon-list-alt"></i>
|
||||||
</a>
|
</a>
|
||||||
|
<a id="clipboard-copy" data-clipboard-text="{{ request.scheme }}://{{request.get_host}}{% url 'groupmanagement:request_add' group.id %}" class="btn btn-warning" title="{% trans "Copy Direct Join Link" %}">
|
||||||
|
<i class="glyphicon glyphicon-copy"></i>
|
||||||
|
</a>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@@ -68,3 +72,9 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
{% block extra_javascript %}
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script>
|
||||||
|
<script>
|
||||||
|
new ClipboardJS('#clipboard-copy');
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
Binary file not shown.
@@ -13,7 +13,7 @@ msgid ""
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: PACKAGE VERSION\n"
|
"Project-Id-Version: PACKAGE VERSION\n"
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2020-04-02 03:23+0000\n"
|
"POT-Creation-Date: 2020-05-08 00:57+0000\n"
|
||||||
"PO-Revision-Date: 2020-02-18 03:14+0000\n"
|
"PO-Revision-Date: 2020-02-18 03:14+0000\n"
|
||||||
"Last-Translator: Rounon Dax <rounon.dax@terra-nanotech.de>, 2020\n"
|
"Last-Translator: Rounon Dax <rounon.dax@terra-nanotech.de>, 2020\n"
|
||||||
"Language-Team: German (https://www.transifex.com/alliance-auth/teams/107430/de/)\n"
|
"Language-Team: German (https://www.transifex.com/alliance-auth/teams/107430/de/)\n"
|
||||||
@@ -657,7 +657,11 @@ msgstr "Mitglieder ansehen"
|
|||||||
msgid "Audit Members"
|
msgid "Audit Members"
|
||||||
msgstr "Mitglieder Protokoll"
|
msgstr "Mitglieder Protokoll"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:64
|
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:56
|
||||||
|
msgid "Copy Direrct Join Link"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:68
|
||||||
msgid "No groups to list."
|
msgid "No groups to list."
|
||||||
msgstr "Keine Gruppen vorhanden."
|
msgstr "Keine Gruppen vorhanden."
|
||||||
|
|
||||||
@@ -1177,7 +1181,7 @@ msgstr "Änderungen für Operation timer %(opname)s gespeichert."
|
|||||||
#: allianceauth/permissions_tool/templates/permissions_tool/audit.html:6
|
#: allianceauth/permissions_tool/templates/permissions_tool/audit.html:6
|
||||||
#: allianceauth/permissions_tool/templates/permissions_tool/audit.html:10
|
#: allianceauth/permissions_tool/templates/permissions_tool/audit.html:10
|
||||||
msgid "Permissions Audit"
|
msgid "Permissions Audit"
|
||||||
msgstr "Berechtigungsprüfung"
|
msgstr "Berechtigungsübersicht"
|
||||||
|
|
||||||
#: allianceauth/permissions_tool/templates/permissions_tool/audit.html:22
|
#: allianceauth/permissions_tool/templates/permissions_tool/audit.html:22
|
||||||
msgid "User / Character"
|
msgid "User / Character"
|
||||||
@@ -1912,12 +1916,6 @@ msgid_plural "%(tasks)s tasks"
|
|||||||
msgstr[0] "%(tasks)sAufgabe"
|
msgstr[0] "%(tasks)sAufgabe"
|
||||||
msgstr[1] "%(tasks)sAufgaben"
|
msgstr[1] "%(tasks)sAufgaben"
|
||||||
|
|
||||||
#: allianceauth/templates/allianceauth/help.html:4
|
|
||||||
#: allianceauth/templates/allianceauth/help.html:9
|
|
||||||
#: allianceauth/templates/allianceauth/side-menu.html:34
|
|
||||||
msgid "Help"
|
|
||||||
msgstr "Hilfe"
|
|
||||||
|
|
||||||
#: allianceauth/templates/allianceauth/night-toggle.html:3
|
#: allianceauth/templates/allianceauth/night-toggle.html:3
|
||||||
msgid "Night"
|
msgid "Night"
|
||||||
msgstr "Nacht"
|
msgstr "Nacht"
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ msgid ""
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: PACKAGE VERSION\n"
|
"Project-Id-Version: PACKAGE VERSION\n"
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2020-04-02 03:23+0000\n"
|
"POT-Creation-Date: 2020-05-08 00:57+0000\n"
|
||||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||||
@@ -642,7 +642,11 @@ msgstr ""
|
|||||||
msgid "Audit Members"
|
msgid "Audit Members"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:64
|
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:56
|
||||||
|
msgid "Copy Direrct Join Link"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:68
|
||||||
msgid "No groups to list."
|
msgid "No groups to list."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -1876,12 +1880,6 @@ msgid_plural "%(tasks)s tasks"
|
|||||||
msgstr[0] ""
|
msgstr[0] ""
|
||||||
msgstr[1] ""
|
msgstr[1] ""
|
||||||
|
|
||||||
#: allianceauth/templates/allianceauth/help.html:4
|
|
||||||
#: allianceauth/templates/allianceauth/help.html:9
|
|
||||||
#: allianceauth/templates/allianceauth/side-menu.html:34
|
|
||||||
msgid "Help"
|
|
||||||
msgstr ""
|
|
||||||
|
|
||||||
#: allianceauth/templates/allianceauth/night-toggle.html:3
|
#: allianceauth/templates/allianceauth/night-toggle.html:3
|
||||||
msgid "Night"
|
msgid "Night"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|||||||
Binary file not shown.
@@ -12,7 +12,7 @@ msgid ""
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: PACKAGE VERSION\n"
|
"Project-Id-Version: PACKAGE VERSION\n"
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2020-03-26 03:07+0000\n"
|
"POT-Creation-Date: 2020-05-08 00:57+0000\n"
|
||||||
"PO-Revision-Date: 2020-02-18 03:14+0000\n"
|
"PO-Revision-Date: 2020-02-18 03:14+0000\n"
|
||||||
"Last-Translator: frank1210 <francolopez_16@hotmail.com>, 2020\n"
|
"Last-Translator: frank1210 <francolopez_16@hotmail.com>, 2020\n"
|
||||||
"Language-Team: Spanish (https://www.transifex.com/alliance-auth/teams/107430/es/)\n"
|
"Language-Team: Spanish (https://www.transifex.com/alliance-auth/teams/107430/es/)\n"
|
||||||
@@ -653,7 +653,11 @@ msgstr "Ver Miembros"
|
|||||||
msgid "Audit Members"
|
msgid "Audit Members"
|
||||||
msgstr "Auditar Miembros"
|
msgstr "Auditar Miembros"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:64
|
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:56
|
||||||
|
msgid "Copy Direrct Join Link"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:68
|
||||||
msgid "No groups to list."
|
msgid "No groups to list."
|
||||||
msgstr "No hay grupos para listar"
|
msgstr "No hay grupos para listar"
|
||||||
|
|
||||||
@@ -785,21 +789,21 @@ msgid "Rejected application from %(mainchar)s to leave %(group)s."
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Se rechazo la solicitud de %(mainchar)s para dejar el grupo %(group)s."
|
"Se rechazo la solicitud de %(mainchar)s para dejar el grupo %(group)s."
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:347
|
#: allianceauth/groupmanagement/views.py:346
|
||||||
#: allianceauth/groupmanagement/views.py:359
|
#: allianceauth/groupmanagement/views.py:358
|
||||||
msgid "You cannot join that group"
|
msgid "You cannot join that group"
|
||||||
msgstr "No puedes unirte a ese grupo"
|
msgstr "No puedes unirte a ese grupo"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:353
|
#: allianceauth/groupmanagement/views.py:352
|
||||||
msgid "You are already a member of that group."
|
msgid "You are already a member of that group."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:368
|
#: allianceauth/groupmanagement/views.py:367
|
||||||
msgid "You already have a pending application for that group."
|
msgid "You already have a pending application for that group."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:371
|
#: allianceauth/groupmanagement/views.py:370
|
||||||
#: allianceauth/groupmanagement/views.py:409
|
#: allianceauth/groupmanagement/views.py:408
|
||||||
#: allianceauth/hrapplications/templates/hrapplications/management.html:37
|
#: allianceauth/hrapplications/templates/hrapplications/management.html:37
|
||||||
#: allianceauth/hrapplications/templates/hrapplications/management.html:72
|
#: allianceauth/hrapplications/templates/hrapplications/management.html:72
|
||||||
#: allianceauth/hrapplications/templates/hrapplications/management.html:99
|
#: allianceauth/hrapplications/templates/hrapplications/management.html:99
|
||||||
@@ -811,24 +815,24 @@ msgstr ""
|
|||||||
msgid "Pending"
|
msgid "Pending"
|
||||||
msgstr "Pendiente"
|
msgstr "Pendiente"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:377
|
#: allianceauth/groupmanagement/views.py:376
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Applied to group %(group)s."
|
msgid "Applied to group %(group)s."
|
||||||
msgstr "Solicitud enviada al grupo %(group)s."
|
msgstr "Solicitud enviada al grupo %(group)s."
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:388
|
#: allianceauth/groupmanagement/views.py:387
|
||||||
msgid "You cannot leave that group"
|
msgid "You cannot leave that group"
|
||||||
msgstr "No puedes dejar el grupos"
|
msgstr "No puedes dejar el grupos"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:393
|
#: allianceauth/groupmanagement/views.py:392
|
||||||
msgid "You are not a member of that group"
|
msgid "You are not a member of that group"
|
||||||
msgstr "No eres miembro de ese grupo"
|
msgstr "No eres miembro de ese grupo"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:402
|
#: allianceauth/groupmanagement/views.py:401
|
||||||
msgid "You already have a pending leave request for that group."
|
msgid "You already have a pending leave request for that group."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:415
|
#: allianceauth/groupmanagement/views.py:414
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Applied to leave group %(group)s."
|
msgid "Applied to leave group %(group)s."
|
||||||
msgstr "Solicitaste dejar el grupo %(group)s."
|
msgstr "Solicitaste dejar el grupo %(group)s."
|
||||||
@@ -1894,12 +1898,6 @@ msgid_plural "%(tasks)s tasks"
|
|||||||
msgstr[0] ""
|
msgstr[0] ""
|
||||||
msgstr[1] ""
|
msgstr[1] ""
|
||||||
|
|
||||||
#: allianceauth/templates/allianceauth/help.html:4
|
|
||||||
#: allianceauth/templates/allianceauth/help.html:9
|
|
||||||
#: allianceauth/templates/allianceauth/side-menu.html:34
|
|
||||||
msgid "Help"
|
|
||||||
msgstr "Ayuda"
|
|
||||||
|
|
||||||
#: allianceauth/templates/allianceauth/night-toggle.html:3
|
#: allianceauth/templates/allianceauth/night-toggle.html:3
|
||||||
msgid "Night"
|
msgid "Night"
|
||||||
msgstr "Noche"
|
msgstr "Noche"
|
||||||
|
|||||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -11,7 +11,7 @@ msgid ""
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: PACKAGE VERSION\n"
|
"Project-Id-Version: PACKAGE VERSION\n"
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"Report-Msgid-Bugs-To: \n"
|
||||||
"POT-Creation-Date: 2020-03-26 03:07+0000\n"
|
"POT-Creation-Date: 2020-05-08 00:57+0000\n"
|
||||||
"PO-Revision-Date: 2020-02-18 03:14+0000\n"
|
"PO-Revision-Date: 2020-02-18 03:14+0000\n"
|
||||||
"Last-Translator: Alexander Gess <de.alex.gess@gmail.com>, 2020\n"
|
"Last-Translator: Alexander Gess <de.alex.gess@gmail.com>, 2020\n"
|
||||||
"Language-Team: Russian (https://www.transifex.com/alliance-auth/teams/107430/ru/)\n"
|
"Language-Team: Russian (https://www.transifex.com/alliance-auth/teams/107430/ru/)\n"
|
||||||
@@ -651,7 +651,11 @@ msgstr "Посмотреть участников"
|
|||||||
msgid "Audit Members"
|
msgid "Audit Members"
|
||||||
msgstr "Проверить участников"
|
msgstr "Проверить участников"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:64
|
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:56
|
||||||
|
msgid "Copy Direrct Join Link"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: allianceauth/groupmanagement/templates/groupmanagement/groupmembership.html:68
|
||||||
msgid "No groups to list."
|
msgid "No groups to list."
|
||||||
msgstr "Нет групп в списке"
|
msgstr "Нет групп в списке"
|
||||||
|
|
||||||
@@ -782,21 +786,21 @@ msgstr ""
|
|||||||
msgid "Rejected application from %(mainchar)s to leave %(group)s."
|
msgid "Rejected application from %(mainchar)s to leave %(group)s."
|
||||||
msgstr "Прошение об исключении %(mainchar)s из %(group)s – отклонено. "
|
msgstr "Прошение об исключении %(mainchar)s из %(group)s – отклонено. "
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:347
|
#: allianceauth/groupmanagement/views.py:346
|
||||||
#: allianceauth/groupmanagement/views.py:359
|
#: allianceauth/groupmanagement/views.py:358
|
||||||
msgid "You cannot join that group"
|
msgid "You cannot join that group"
|
||||||
msgstr "Вы не можете вступить"
|
msgstr "Вы не можете вступить"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:353
|
#: allianceauth/groupmanagement/views.py:352
|
||||||
msgid "You are already a member of that group."
|
msgid "You are already a member of that group."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:368
|
#: allianceauth/groupmanagement/views.py:367
|
||||||
msgid "You already have a pending application for that group."
|
msgid "You already have a pending application for that group."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:371
|
#: allianceauth/groupmanagement/views.py:370
|
||||||
#: allianceauth/groupmanagement/views.py:409
|
#: allianceauth/groupmanagement/views.py:408
|
||||||
#: allianceauth/hrapplications/templates/hrapplications/management.html:37
|
#: allianceauth/hrapplications/templates/hrapplications/management.html:37
|
||||||
#: allianceauth/hrapplications/templates/hrapplications/management.html:72
|
#: allianceauth/hrapplications/templates/hrapplications/management.html:72
|
||||||
#: allianceauth/hrapplications/templates/hrapplications/management.html:99
|
#: allianceauth/hrapplications/templates/hrapplications/management.html:99
|
||||||
@@ -808,24 +812,24 @@ msgstr ""
|
|||||||
msgid "Pending"
|
msgid "Pending"
|
||||||
msgstr "Ожидание"
|
msgstr "Ожидание"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:377
|
#: allianceauth/groupmanagement/views.py:376
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Applied to group %(group)s."
|
msgid "Applied to group %(group)s."
|
||||||
msgstr "Вступить в группу %(group)s."
|
msgstr "Вступить в группу %(group)s."
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:388
|
#: allianceauth/groupmanagement/views.py:387
|
||||||
msgid "You cannot leave that group"
|
msgid "You cannot leave that group"
|
||||||
msgstr "Вы не можете покинуть эту группу"
|
msgstr "Вы не можете покинуть эту группу"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:393
|
#: allianceauth/groupmanagement/views.py:392
|
||||||
msgid "You are not a member of that group"
|
msgid "You are not a member of that group"
|
||||||
msgstr "Вы не участник группыы"
|
msgstr "Вы не участник группыы"
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:402
|
#: allianceauth/groupmanagement/views.py:401
|
||||||
msgid "You already have a pending leave request for that group."
|
msgid "You already have a pending leave request for that group."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: allianceauth/groupmanagement/views.py:415
|
#: allianceauth/groupmanagement/views.py:414
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid "Applied to leave group %(group)s."
|
msgid "Applied to leave group %(group)s."
|
||||||
msgstr "Запрос на выход из группы %(group)s."
|
msgstr "Запрос на выход из группы %(group)s."
|
||||||
@@ -1896,12 +1900,6 @@ msgstr[1] "%(tasks)s задач"
|
|||||||
msgstr[2] "%(tasks)s задач"
|
msgstr[2] "%(tasks)s задач"
|
||||||
msgstr[3] "%(tasks)s задач"
|
msgstr[3] "%(tasks)s задач"
|
||||||
|
|
||||||
#: allianceauth/templates/allianceauth/help.html:4
|
|
||||||
#: allianceauth/templates/allianceauth/help.html:9
|
|
||||||
#: allianceauth/templates/allianceauth/side-menu.html:34
|
|
||||||
msgid "Help"
|
|
||||||
msgstr "Помощь"
|
|
||||||
|
|
||||||
#: allianceauth/templates/allianceauth/night-toggle.html:3
|
#: allianceauth/templates/allianceauth/night-toggle.html:3
|
||||||
msgid "Night"
|
msgid "Night"
|
||||||
msgstr "Ночь"
|
msgstr "Ночь"
|
||||||
|
|||||||
@@ -4,3 +4,8 @@ from allianceauth import urls
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'', include(urls)),
|
url(r'', include(urls)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
handler500 = 'allianceauth.views.Generic500Redirect'
|
||||||
|
handler404 = 'allianceauth.views.Generic404Redirect'
|
||||||
|
handler403 = 'allianceauth.views.Generic403Redirect'
|
||||||
|
handler400 = 'allianceauth.views.Generic400Redirect'
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.db.models.functions import Lower
|
|
||||||
from django.urls import reverse
|
|
||||||
from django.utils.html import format_html
|
|
||||||
|
|
||||||
from allianceauth import hooks
|
from allianceauth import hooks
|
||||||
from allianceauth.eveonline.models import EveCharacter
|
from allianceauth.authentication.admin import (
|
||||||
from allianceauth.authentication.admin import user_profile_pic, \
|
user_profile_pic,
|
||||||
user_username, user_main_organization, MainCorporationsFilter,\
|
user_username,
|
||||||
|
user_main_organization,
|
||||||
|
MainCorporationsFilter,
|
||||||
MainAllianceFilter
|
MainAllianceFilter
|
||||||
|
)
|
||||||
|
|
||||||
from .models import NameFormatConfig
|
from .models import NameFormatConfig
|
||||||
|
|
||||||
@@ -25,16 +25,24 @@ class ServicesUserAdmin(admin.ModelAdmin):
|
|||||||
list_select_related = True
|
list_select_related = True
|
||||||
list_display = (
|
list_display = (
|
||||||
user_profile_pic,
|
user_profile_pic,
|
||||||
user_username,
|
user_username,
|
||||||
|
'_state',
|
||||||
user_main_organization,
|
user_main_organization,
|
||||||
'_date_joined'
|
'_date_joined'
|
||||||
)
|
)
|
||||||
list_filter = (
|
list_filter = (
|
||||||
|
'user__profile__state',
|
||||||
MainCorporationsFilter,
|
MainCorporationsFilter,
|
||||||
MainAllianceFilter,
|
MainAllianceFilter,
|
||||||
'user__date_joined'
|
'user__date_joined',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _state(self, obj):
|
||||||
|
return obj.user.profile.state.name
|
||||||
|
|
||||||
|
_state.short_description = 'state'
|
||||||
|
_state.admin_order_field = 'user__profile__state__name'
|
||||||
|
|
||||||
def _date_joined(self, obj):
|
def _date_joined(self, obj):
|
||||||
return obj.user.date_joined
|
return obj.user.date_joined
|
||||||
|
|
||||||
@@ -45,7 +53,8 @@ class ServicesUserAdmin(admin.ModelAdmin):
|
|||||||
class NameFormatConfigForm(forms.ModelForm):
|
class NameFormatConfigForm(forms.ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(NameFormatConfigForm, self).__init__(*args, **kwargs)
|
super(NameFormatConfigForm, self).__init__(*args, **kwargs)
|
||||||
SERVICE_CHOICES = [(s.name, s.name) for h in hooks.get_hooks('services_hook') for s in [h()]]
|
SERVICE_CHOICES = \
|
||||||
|
[(s.name, s.name) for h in hooks.get_hooks('services_hook') for s in [h()]]
|
||||||
if self.instance.id:
|
if self.instance.id:
|
||||||
current_choice = (self.instance.service_name, self.instance.service_name)
|
current_choice = (self.instance.service_name, self.instance.service_name)
|
||||||
if current_choice not in SERVICE_CHOICES:
|
if current_choice not in SERVICE_CHOICES:
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
default_app_config = 'allianceauth.services.modules.discord.apps.DiscordServiceConfig'
|
default_app_config = 'allianceauth.services.modules.discord.apps.DiscordServiceConfig' # noqa
|
||||||
|
|
||||||
|
__title__ = 'Discord Service'
|
||||||
|
|||||||
@@ -1,13 +1,22 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from .models import DiscordUser
|
from . import __title__
|
||||||
from ...admin import ServicesUserAdmin
|
from ...admin import ServicesUserAdmin
|
||||||
|
from .models import DiscordUser
|
||||||
|
from .utils import LoggerAddTag
|
||||||
|
|
||||||
|
|
||||||
|
logger = LoggerAddTag(logging.getLogger(__name__), __title__)
|
||||||
|
|
||||||
|
|
||||||
@admin.register(DiscordUser)
|
@admin.register(DiscordUser)
|
||||||
class DiscordUserAdmin(ServicesUserAdmin):
|
class DiscordUserAdmin(ServicesUserAdmin):
|
||||||
list_display = ServicesUserAdmin.list_display + ('_uid',)
|
search_fields = ServicesUserAdmin.search_fields + ('uid', 'username')
|
||||||
search_fields = ServicesUserAdmin.search_fields + ('uid', )
|
list_display = ServicesUserAdmin.list_display + ('activated', '_username', '_uid')
|
||||||
|
list_filter = ServicesUserAdmin.list_filter + ('activated',)
|
||||||
|
ordering = ('-activated',)
|
||||||
|
|
||||||
def _uid(self, obj):
|
def _uid(self, obj):
|
||||||
return obj.uid
|
return obj.uid
|
||||||
@@ -15,3 +24,11 @@ class DiscordUserAdmin(ServicesUserAdmin):
|
|||||||
_uid.short_description = 'Discord ID (UID)'
|
_uid.short_description = 'Discord ID (UID)'
|
||||||
_uid.admin_order_field = 'uid'
|
_uid.admin_order_field = 'uid'
|
||||||
|
|
||||||
|
def _username(self, obj):
|
||||||
|
if obj.username and obj.discriminator:
|
||||||
|
return f'{obj.username}#{obj.discriminator}'
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
_username.short_description = 'Discord Username'
|
||||||
|
_username.admin_order_field = 'username'
|
||||||
|
|||||||
17
allianceauth/services/modules/discord/app_settings.py
Normal file
17
allianceauth/services/modules/discord/app_settings.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from .utils import clean_setting
|
||||||
|
|
||||||
|
|
||||||
|
DISCORD_APP_ID = clean_setting('DISCORD_APP_ID', '')
|
||||||
|
DISCORD_APP_SECRET = clean_setting('DISCORD_APP_SECRET', '')
|
||||||
|
DISCORD_BOT_TOKEN = clean_setting('DISCORD_BOT_TOKEN', '')
|
||||||
|
DISCORD_CALLBACK_URL = clean_setting('DISCORD_CALLBACK_URL', '')
|
||||||
|
DISCORD_GUILD_ID = clean_setting('DISCORD_GUILD_ID', '')
|
||||||
|
|
||||||
|
# max retries of tasks after an error occurred
|
||||||
|
DISCORD_TASKS_MAX_RETRIES = clean_setting('DISCORD_TASKS_MAX_RETRIES', 3)
|
||||||
|
|
||||||
|
# Pause in seconds until next retry for tasks after the API returned an error
|
||||||
|
DISCORD_TASKS_RETRY_PAUSE = clean_setting('DISCORD_TASKS_RETRY_PAUSE', 60)
|
||||||
|
|
||||||
|
# automatically sync Discord users names to user's main character name when created
|
||||||
|
DISCORD_SYNC_NAMES = clean_setting('DISCORD_SYNC_NAMES', False)
|
||||||
@@ -1,17 +1,26 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from django.contrib.auth.models import User
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
from allianceauth import hooks
|
from allianceauth import hooks
|
||||||
from allianceauth.services.hooks import ServicesHook
|
from allianceauth.services.hooks import ServicesHook
|
||||||
from .tasks import DiscordTasks
|
|
||||||
from .urls import urlpatterns
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
from .models import DiscordUser
|
||||||
|
from .urls import urlpatterns
|
||||||
|
from .utils import LoggerAddTag
|
||||||
|
from . import tasks, __title__
|
||||||
|
|
||||||
|
|
||||||
|
logger = LoggerAddTag(logging.getLogger(__name__), __title__)
|
||||||
|
|
||||||
|
# Default priority for single tasks like update group and sync nickname
|
||||||
|
SINGLE_TASK_PRIORITY = 3
|
||||||
|
|
||||||
|
|
||||||
class DiscordService(ServicesHook):
|
class DiscordService(ServicesHook):
|
||||||
|
"""Service for managing a Discord server with Auth"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
ServicesHook.__init__(self)
|
ServicesHook.__init__(self)
|
||||||
self.urlpatterns = urlpatterns
|
self.urlpatterns = urlpatterns
|
||||||
@@ -20,36 +29,85 @@ class DiscordService(ServicesHook):
|
|||||||
self.access_perm = 'discord.access_discord'
|
self.access_perm = 'discord.access_discord'
|
||||||
self.name_format = '{character_name}'
|
self.name_format = '{character_name}'
|
||||||
|
|
||||||
def delete_user(self, user, notify_user=False):
|
def delete_user(self, user: User, notify_user: bool = False) -> None:
|
||||||
logger.debug('Deleting user %s %s account' % (user, self.name))
|
if self.user_has_account(user):
|
||||||
return DiscordTasks.delete_user(user, notify_user=notify_user)
|
logger.debug('Deleting user %s %s account', user, self.name)
|
||||||
|
tasks.delete_user.apply_async(
|
||||||
|
kwargs={'user_pk': user.pk}, priority=SINGLE_TASK_PRIORITY
|
||||||
|
)
|
||||||
|
|
||||||
|
def render_services_ctrl(self, request):
|
||||||
|
if self.user_has_account(request.user):
|
||||||
|
user_has_account = True
|
||||||
|
username = request.user.discord.username
|
||||||
|
discriminator = request.user.discord.discriminator
|
||||||
|
if username and discriminator:
|
||||||
|
discord_username = f'{username}#{discriminator}'
|
||||||
|
else:
|
||||||
|
discord_username = ''
|
||||||
|
else:
|
||||||
|
discord_username = ''
|
||||||
|
user_has_account = False
|
||||||
|
|
||||||
def update_groups(self, user):
|
return render_to_string(
|
||||||
logger.debug('Processing %s groups for %s' % (self.name, user))
|
self.service_ctrl_template,
|
||||||
if DiscordTasks.has_account(user):
|
{
|
||||||
DiscordTasks.update_groups.delay(user.pk)
|
'server_name': DiscordUser.objects.server_name(),
|
||||||
|
'user_has_account': user_has_account,
|
||||||
def validate_user(self, user):
|
'discord_username': discord_username
|
||||||
logger.debug('Validating user %s %s account' % (user, self.name))
|
},
|
||||||
if DiscordTasks.has_account(user) and not self.service_active_for_user(user):
|
request=request
|
||||||
self.delete_user(user, notify_user=True)
|
)
|
||||||
|
|
||||||
def sync_nickname(self, user):
|
|
||||||
logger.debug('Syncing %s nickname for user %s' % (self.name, user))
|
|
||||||
DiscordTasks.update_nickname.delay(user.pk)
|
|
||||||
|
|
||||||
def update_all_groups(self):
|
|
||||||
logger.debug('Update all %s groups called' % self.name)
|
|
||||||
DiscordTasks.update_all_groups.delay()
|
|
||||||
|
|
||||||
def service_active_for_user(self, user):
|
def service_active_for_user(self, user):
|
||||||
return user.has_perm(self.access_perm)
|
return user.has_perm(self.access_perm)
|
||||||
|
|
||||||
def render_services_ctrl(self, request):
|
def sync_nickname(self, user):
|
||||||
return render_to_string(self.service_ctrl_template, {
|
logger.debug('Syncing %s nickname for user %s', self.name, user)
|
||||||
'discord_uid': request.user.discord.uid if DiscordTasks.has_account(request.user) else None,
|
if self.user_has_account(user):
|
||||||
'DISCORD_SERVER_ID': getattr(settings, 'DISCORD_GUILD_ID', ''),
|
tasks.update_nickname.apply_async(
|
||||||
}, request=request)
|
kwargs={'user_pk': user.pk}, priority=SINGLE_TASK_PRIORITY
|
||||||
|
)
|
||||||
|
|
||||||
|
def sync_nicknames_bulk(self, users: list):
|
||||||
|
"""Sync nickname for a list of users in bulk.
|
||||||
|
Preferred over sync_nickname(), because it will not break the rate limit
|
||||||
|
"""
|
||||||
|
logger.debug(
|
||||||
|
'Syncing %s nicknames in bulk for %d users', self.name, len(users)
|
||||||
|
)
|
||||||
|
user_pks = [user.pk for user in users]
|
||||||
|
tasks.update_nicknames_bulk.delay(user_pks)
|
||||||
|
|
||||||
|
def update_all_groups(self):
|
||||||
|
logger.debug('Update all %s groups called', self.name)
|
||||||
|
tasks.update_all_groups.delay()
|
||||||
|
|
||||||
|
def update_groups(self, user):
|
||||||
|
logger.debug('Processing %s groups for %s', self.name, user)
|
||||||
|
if self.user_has_account(user):
|
||||||
|
tasks.update_groups.apply_async(
|
||||||
|
kwargs={'user_pk': user.pk}, priority=SINGLE_TASK_PRIORITY
|
||||||
|
)
|
||||||
|
|
||||||
|
def update_groups_bulk(self, users: list):
|
||||||
|
"""Updates groups for a list of users in bulk.
|
||||||
|
Preferred over update_groups(), because it will not break the rate limit
|
||||||
|
"""
|
||||||
|
logger.debug(
|
||||||
|
'Processing %s groups in bulk for %d users', self.name, len(users)
|
||||||
|
)
|
||||||
|
user_pks = [user.pk for user in users]
|
||||||
|
tasks.update_groups_bulk.delay(user_pks)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def user_has_account(user: User) -> bool:
|
||||||
|
return DiscordUser.objects.user_has_account(user)
|
||||||
|
|
||||||
|
def validate_user(self, user):
|
||||||
|
logger.debug('Validating user %s %s account', user, self.name)
|
||||||
|
if self.user_has_account(user) and not self.service_active_for_user(user):
|
||||||
|
self.delete_user(user, notify_user=True)
|
||||||
|
|
||||||
|
|
||||||
@hooks.register('services_hook')
|
@hooks.register('services_hook')
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
from .client import DiscordClient # noqa
|
||||||
|
from .exceptions import DiscordApiBackoff # noqa
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
from ..utils import clean_setting
|
||||||
|
|
||||||
|
|
||||||
|
# Base URL for all API calls. Must end with /.
|
||||||
|
DISCORD_API_BASE_URL = clean_setting(
|
||||||
|
'DISCORD_API_BASE_URL', 'https://discordapp.com/api/'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Low level timeout for requests to the Discord API in ms
|
||||||
|
DISCORD_API_TIMEOUT = clean_setting(
|
||||||
|
'DISCORD_API_TIMEOUT', 5000
|
||||||
|
)
|
||||||
|
|
||||||
|
# Base authorization URL for Discord Oauth
|
||||||
|
DISCORD_OAUTH_BASE_URL = clean_setting(
|
||||||
|
'DISCORD_OAUTH_BASE_URL', 'https://discordapp.com/api/oauth2/authorize'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Base authorization URL for Discord Oauth
|
||||||
|
DISCORD_OAUTH_TOKEN_URL = clean_setting(
|
||||||
|
'DISCORD_OAUTH_TOKEN_URL', 'https://discordapp.com/api/oauth2/token'
|
||||||
|
)
|
||||||
|
|
||||||
|
# How long the Discord guild names retrieved from the server are
|
||||||
|
# caches locally in milliseconds.
|
||||||
|
DISCORD_GUILD_NAME_CACHE_MAX_AGE = clean_setting(
|
||||||
|
'DISCORD_GUILD_NAME_CACHE_MAX_AGE', 3600 * 2 * 1000
|
||||||
|
)
|
||||||
|
|
||||||
|
# How long Discord roles retrieved from the server are caches locally in milliseconds.
|
||||||
|
DISCORD_ROLES_CACHE_MAX_AGE = clean_setting(
|
||||||
|
'DISCORD_ROLES_CACHE_MAX_AGE', 3600 * 2 * 1000
|
||||||
|
)
|
||||||
|
|
||||||
|
# Turns off creation of new roles. In case the rate limit for creating roles is
|
||||||
|
# exhausted, this setting allows the Discord service to continue to function
|
||||||
|
# and wait out the reset. Rate limit is about 250 per 48 hrs.
|
||||||
|
DISCORD_DISABLE_ROLE_CREATION = clean_setting(
|
||||||
|
'DISCORD_DISABLE_ROLE_CREATION', False
|
||||||
|
)
|
||||||
690
allianceauth/services/modules/discord/discord_client/client.py
Normal file
690
allianceauth/services/modules/discord/discord_client/client.py
Normal file
@@ -0,0 +1,690 @@
|
|||||||
|
from hashlib import md5
|
||||||
|
import logging
|
||||||
|
from time import sleep
|
||||||
|
from urllib.parse import urljoin
|
||||||
|
from uuid import uuid1
|
||||||
|
|
||||||
|
from redis import Redis
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from django.core.cache import caches
|
||||||
|
|
||||||
|
from allianceauth import __title__ as AUTH_TITLE, __url__, __version__
|
||||||
|
|
||||||
|
from .. import __title__
|
||||||
|
from .app_settings import (
|
||||||
|
DISCORD_API_BASE_URL,
|
||||||
|
DISCORD_API_TIMEOUT,
|
||||||
|
DISCORD_DISABLE_ROLE_CREATION,
|
||||||
|
DISCORD_GUILD_NAME_CACHE_MAX_AGE,
|
||||||
|
DISCORD_OAUTH_BASE_URL,
|
||||||
|
DISCORD_OAUTH_TOKEN_URL,
|
||||||
|
DISCORD_ROLES_CACHE_MAX_AGE,
|
||||||
|
)
|
||||||
|
from .exceptions import DiscordRateLimitExhausted, DiscordTooManyRequestsError
|
||||||
|
from ..utils import LoggerAddTag
|
||||||
|
|
||||||
|
|
||||||
|
logger = LoggerAddTag(logging.getLogger(__name__), __title__)
|
||||||
|
|
||||||
|
# max requests that can be executed until reset
|
||||||
|
RATE_LIMIT_MAX_REQUESTS = 5
|
||||||
|
|
||||||
|
# Time until remaining requests are reset
|
||||||
|
RATE_LIMIT_RESETS_AFTER = 5000
|
||||||
|
|
||||||
|
# Delay used for API backoff in case no info returned from API on 429s
|
||||||
|
DEFAULT_BACKOFF_DELAY = 5000
|
||||||
|
|
||||||
|
# additional duration to compensate for potential clock discrepancies
|
||||||
|
# with the Discord server
|
||||||
|
DURATION_CONTINGENCY = 500
|
||||||
|
|
||||||
|
# Client will do a blocking wait rather than throwing a backoff exception if the
|
||||||
|
# time until next reset is below this threshold
|
||||||
|
WAIT_THRESHOLD = 250
|
||||||
|
|
||||||
|
# If the rate limit resets soon we will wait it out and then retry to
|
||||||
|
# either get a remaining request from our cached counter
|
||||||
|
# or again wait out a short reset time and retry again.
|
||||||
|
# This could happen several times within a high concurrency situation,
|
||||||
|
# but must fail after x tries to avoid an infinite loop
|
||||||
|
RATE_LIMIT_RETRIES = 1000
|
||||||
|
|
||||||
|
|
||||||
|
class DiscordClient:
|
||||||
|
"""This class provides a web client for interacting with the Discord API
|
||||||
|
|
||||||
|
The client has rate limiting that supports concurrency.
|
||||||
|
This means it is able to ensure the API rate limit is not violated,
|
||||||
|
even when used concurrently, e.g. with multiple parallel celery tasks.
|
||||||
|
|
||||||
|
In addition the client support proper API backoff.
|
||||||
|
|
||||||
|
Synchronization of rate limit infos accross multiple processes
|
||||||
|
is implemented with Redis and thus requires Redis as Django cache backend.
|
||||||
|
|
||||||
|
All durations are in milliseconds.
|
||||||
|
"""
|
||||||
|
OAUTH_BASE_URL = DISCORD_OAUTH_BASE_URL
|
||||||
|
OAUTH_TOKEN_URL = DISCORD_OAUTH_TOKEN_URL
|
||||||
|
|
||||||
|
_KEY_GLOBAL_BACKOFF_UNTIL = 'DISCORD_GLOBAL_BACKOFF_UNTIL'
|
||||||
|
_KEY_GLOBAL_RATE_LIMIT_REMAINING = 'DISCORD_GLOBAL_RATE_LIMIT_REMAINING'
|
||||||
|
_KEYPREFIX_GUILD_NAME = 'DISCORD_GUILD_NAME'
|
||||||
|
_KEYPREFIX_ROLE_NAME = 'DISCORD_ROLE_NAME'
|
||||||
|
_ROLE_NAME_MAX_CHARS = 100
|
||||||
|
_NICK_MAX_CHARS = 32
|
||||||
|
|
||||||
|
_HTTP_STATUS_CODE_NOT_FOUND = 404
|
||||||
|
_HTTP_STATUS_CODE_RATE_LIMITED = 429
|
||||||
|
_DISCORD_STATUS_CODE_UNKNOWN_MEMBER = 10007
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
access_token: str,
|
||||||
|
redis: Redis = None,
|
||||||
|
is_rate_limited: bool = True
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Params:
|
||||||
|
- access_token: Discord access token used to authenticate all calls to the API
|
||||||
|
- redis: Redis instance to be used.
|
||||||
|
- is_rate_limited: Set to False to run of rate limiting (use with care)
|
||||||
|
If not specified will try to use the Redis instance
|
||||||
|
from the default Django cache backend.
|
||||||
|
"""
|
||||||
|
self._access_token = str(access_token)
|
||||||
|
self._is_rate_limited = bool(is_rate_limited)
|
||||||
|
if not redis:
|
||||||
|
default_cache = caches['default']
|
||||||
|
self._redis = default_cache.get_master_client()
|
||||||
|
if not isinstance(self._redis, Redis):
|
||||||
|
raise RuntimeError(
|
||||||
|
'This class requires a Redis client, but none was provided '
|
||||||
|
'and the default Django cache backend is not Redis either.'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._redis = redis
|
||||||
|
|
||||||
|
lua_1 = """
|
||||||
|
if redis.call("exists", KEYS[1]) == 0 then
|
||||||
|
redis.call("set", KEYS[1], ARGV[1], 'px', ARGV[2])
|
||||||
|
end
|
||||||
|
return redis.call("decr", KEYS[1])
|
||||||
|
"""
|
||||||
|
self.__redis_script_decr_or_set = self._redis.register_script(lua_1)
|
||||||
|
|
||||||
|
lua_2 = """
|
||||||
|
local current_px = tonumber(redis.call("pttl", KEYS[1]))
|
||||||
|
if current_px < tonumber(ARGV[2]) then
|
||||||
|
return redis.call("set", KEYS[1], ARGV[1], 'px', ARGV[2])
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
"""
|
||||||
|
self.__redis_script_set_longer = self._redis.register_script(lua_2)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def access_token(self):
|
||||||
|
return self._access_token
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_rate_limited(self):
|
||||||
|
return self._is_rate_limited
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'{type(self).__name__}(access_token=...{self.access_token[-5:]})'
|
||||||
|
|
||||||
|
def _redis_decr_or_set(self, name: str, value: str, px: int) -> bool:
|
||||||
|
"""decreases the key value if it exists and returns the result
|
||||||
|
else sets the key
|
||||||
|
|
||||||
|
Implemented as Lua script to ensure atomicity.
|
||||||
|
"""
|
||||||
|
return self.__redis_script_decr_or_set(
|
||||||
|
keys=[str(name)], args=[str(value), int(px)]
|
||||||
|
)
|
||||||
|
|
||||||
|
def _redis_set_if_longer(self, name: str, value: str, px: int) -> bool:
|
||||||
|
"""like set, but only goes through if either key doesn't exist
|
||||||
|
or px would be extended.
|
||||||
|
|
||||||
|
Implemented as Lua script to ensure atomicity.
|
||||||
|
"""
|
||||||
|
return self.__redis_script_set_longer(
|
||||||
|
keys=[str(name)], args=[str(value), int(px)]
|
||||||
|
)
|
||||||
|
|
||||||
|
# users
|
||||||
|
|
||||||
|
def current_user(self) -> dict:
|
||||||
|
"""returns the user belonging to the current access_token"""
|
||||||
|
authorization = f'Bearer {self.access_token}'
|
||||||
|
r = self._api_request(
|
||||||
|
method='get', route='users/@me', authorization=authorization
|
||||||
|
)
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
# guild roles
|
||||||
|
|
||||||
|
def create_guild_role(self, guild_id: int, role_name: str, **kwargs) -> dict:
|
||||||
|
"""Create a new guild role with the given name.
|
||||||
|
See official documentation for additional optional parameters.
|
||||||
|
|
||||||
|
Note that Discord allows creating multiple roles with the name name,
|
||||||
|
so it's important to check existing roles before creating new one
|
||||||
|
to avoid duplicates.
|
||||||
|
|
||||||
|
return a new role object on success
|
||||||
|
"""
|
||||||
|
route = f"guilds/{guild_id}/roles"
|
||||||
|
data = {'name': self._sanitize_role_name(role_name)}
|
||||||
|
data.update(kwargs)
|
||||||
|
r = self._api_request(method='post', route=route, data=data)
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def guild_infos(self, guild_id: int) -> dict:
|
||||||
|
"""Returns all basic infos about this guild"""
|
||||||
|
route = f"guilds/{guild_id}"
|
||||||
|
r = self._api_request(method='get', route=route)
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def guild_name(self, guild_id: int) -> str:
|
||||||
|
"""returns the name of this guild (cached)
|
||||||
|
or an empty string if something went wrong
|
||||||
|
"""
|
||||||
|
key_name = self._guild_name_cache_key(guild_id)
|
||||||
|
guild_name = self._redis_decode(self._redis.get(key_name))
|
||||||
|
if not guild_name:
|
||||||
|
guild_infos = self.guild_infos(guild_id)
|
||||||
|
if 'name' in guild_infos:
|
||||||
|
guild_name = guild_infos['name']
|
||||||
|
self._redis.set(
|
||||||
|
name=key_name,
|
||||||
|
value=guild_name,
|
||||||
|
px=DISCORD_GUILD_NAME_CACHE_MAX_AGE
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
guild_name = ''
|
||||||
|
|
||||||
|
return guild_name
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _guild_name_cache_key(cls, guild_id: int) -> str:
|
||||||
|
"""Returns key for accessing role given by name in the role cache"""
|
||||||
|
gen_key = DiscordClient._generate_hash(f'{guild_id}')
|
||||||
|
return f'{cls._KEYPREFIX_GUILD_NAME}__{gen_key}'
|
||||||
|
|
||||||
|
def guild_roles(self, guild_id: int) -> list:
|
||||||
|
"""Returns the list of all roles for this guild"""
|
||||||
|
route = f"guilds/{guild_id}/roles"
|
||||||
|
r = self._api_request(method='get', route=route)
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def delete_guild_role(self, guild_id: int, role_id: int) -> bool:
|
||||||
|
"""Deletes a guild role"""
|
||||||
|
route = f"guilds/{guild_id}/roles/{role_id}"
|
||||||
|
r = self._api_request(method='delete', route=route)
|
||||||
|
if r.status_code == 204:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# guild role cache
|
||||||
|
|
||||||
|
def match_guild_roles_to_names(self, guild_id: int, role_names: list) -> list:
|
||||||
|
"""returns Discord roles matching the given names
|
||||||
|
|
||||||
|
Returns as list of tuple of role and created flag
|
||||||
|
|
||||||
|
Will try to match with existing roles names
|
||||||
|
Non-existing roles will be created, then created flag will be True
|
||||||
|
Roles names are cached to improve performance
|
||||||
|
"""
|
||||||
|
roles = list()
|
||||||
|
for role_name in role_names:
|
||||||
|
role, created = self.match_guild_role_to_name(
|
||||||
|
guild_id=guild_id, role_name=self._sanitize_role_name(role_name)
|
||||||
|
)
|
||||||
|
if role:
|
||||||
|
roles.append((role, created))
|
||||||
|
return roles
|
||||||
|
|
||||||
|
def match_guild_role_to_name(self, guild_id: int, role_name: str) -> tuple:
|
||||||
|
"""returns Discord role matching the given name
|
||||||
|
|
||||||
|
Returns as tuple of role and created flag
|
||||||
|
|
||||||
|
Will try to match with existing roles names
|
||||||
|
Non-existing roles will be created, then created flag will be True
|
||||||
|
Roles names are cached to improve performance
|
||||||
|
"""
|
||||||
|
created = False
|
||||||
|
role_name = self._sanitize_role_name(role_name)
|
||||||
|
role_id = self._redis_decode(
|
||||||
|
self._redis.get(name=self._role_cache_key(guild_id, role_name))
|
||||||
|
)
|
||||||
|
if not role_id:
|
||||||
|
role_id = None
|
||||||
|
for role in self.guild_roles(guild_id):
|
||||||
|
self._update_role_cache(guild_id, role)
|
||||||
|
if role['name'] == role_name:
|
||||||
|
role_id = role['id']
|
||||||
|
|
||||||
|
if role_id:
|
||||||
|
role = self._create_role(role_id, role_name)
|
||||||
|
|
||||||
|
else:
|
||||||
|
if not DISCORD_DISABLE_ROLE_CREATION:
|
||||||
|
role_raw = self.create_guild_role(guild_id, role_name)
|
||||||
|
role = self._create_role(role_raw['id'], role_name)
|
||||||
|
self._update_role_cache(guild_id, role)
|
||||||
|
created = True
|
||||||
|
else:
|
||||||
|
role = None
|
||||||
|
else:
|
||||||
|
role = self._create_role(int(role_id), role_name)
|
||||||
|
|
||||||
|
return role, created
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _create_role(role_id: int, role_name: str) -> dict:
|
||||||
|
return {'id': int(role_id), 'name': str(role_name)}
|
||||||
|
|
||||||
|
def _update_role_cache(self, guild_id: int, role: dict) -> bool:
|
||||||
|
"""updates role cache with given role
|
||||||
|
|
||||||
|
Returns True on success, else False or raises exception
|
||||||
|
"""
|
||||||
|
if not isinstance(role, dict):
|
||||||
|
raise TypeError('role must be a dict')
|
||||||
|
|
||||||
|
return self._redis.set(
|
||||||
|
name=self._role_cache_key(guild_id=guild_id, role_name=role['name']),
|
||||||
|
value=role['id'],
|
||||||
|
px=DISCORD_ROLES_CACHE_MAX_AGE
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _role_cache_key(cls, guild_id: int, role_name: str) -> str:
|
||||||
|
"""Returns key for accessing role given by name in the role cache"""
|
||||||
|
gen_key = DiscordClient._generate_hash(f'{guild_id}{role_name}')
|
||||||
|
return f'{cls._KEYPREFIX_ROLE_NAME}__{gen_key}'
|
||||||
|
|
||||||
|
# guild members
|
||||||
|
|
||||||
|
def add_guild_member(
|
||||||
|
self,
|
||||||
|
guild_id: int,
|
||||||
|
user_id: int,
|
||||||
|
access_token: str,
|
||||||
|
role_ids: list = None,
|
||||||
|
nick: str = None
|
||||||
|
) -> bool:
|
||||||
|
"""Adds a user to the guilds.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- True when a new user was added
|
||||||
|
- None if the user already existed
|
||||||
|
- False when something went wrong or raises exception
|
||||||
|
"""
|
||||||
|
route = f"guilds/{guild_id}/members/{user_id}"
|
||||||
|
data = {
|
||||||
|
'access_token': str(access_token)
|
||||||
|
}
|
||||||
|
if role_ids:
|
||||||
|
data['roles'] = self._sanitize_role_ids(role_ids)
|
||||||
|
|
||||||
|
if nick:
|
||||||
|
data['nick'] = str(nick)[:self._NICK_MAX_CHARS]
|
||||||
|
|
||||||
|
r = self._api_request(method='put', route=route, data=data)
|
||||||
|
r.raise_for_status()
|
||||||
|
if r.status_code == 201:
|
||||||
|
return True
|
||||||
|
elif r.status_code == 204:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def guild_member(self, guild_id: int, user_id: int) -> dict:
|
||||||
|
"""returns the user info for a guild member
|
||||||
|
|
||||||
|
or None if the user is not a member of the guild
|
||||||
|
"""
|
||||||
|
route = f'guilds/{guild_id}/members/{user_id}'
|
||||||
|
r = self._api_request(method='get', route=route, raise_for_status=False)
|
||||||
|
if self._is_member_unknown_error(r):
|
||||||
|
logger.warning("Discord user ID %s could not be found on server.", user_id)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
|
def modify_guild_member(
|
||||||
|
self, guild_id: int, user_id: int, role_ids: list = None, nick: str = None
|
||||||
|
) -> bool:
|
||||||
|
"""Modify attributes of a guild member.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
- True when successful
|
||||||
|
- None if user is not a member of this guild
|
||||||
|
- False otherwise
|
||||||
|
"""
|
||||||
|
if not role_ids and not nick:
|
||||||
|
raise ValueError('Must specify role_ids or nick')
|
||||||
|
|
||||||
|
if role_ids and not isinstance(role_ids, list):
|
||||||
|
raise TypeError('role_ids must be a list type')
|
||||||
|
|
||||||
|
data = dict()
|
||||||
|
if role_ids:
|
||||||
|
data['roles'] = self._sanitize_role_ids(role_ids)
|
||||||
|
|
||||||
|
if nick:
|
||||||
|
data['nick'] = self._sanitize_nick(nick)
|
||||||
|
|
||||||
|
route = f"guilds/{guild_id}/members/{user_id}"
|
||||||
|
r = self._api_request(
|
||||||
|
method='patch', route=route, data=data, raise_for_status=False
|
||||||
|
)
|
||||||
|
if self._is_member_unknown_error(r):
|
||||||
|
logger.warning('User ID %s is not a member of this guild', user_id)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
if r.status_code == 204:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def remove_guild_member(self, guild_id: int, user_id: int) -> bool:
|
||||||
|
"""Remove a member from a guild
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- True when successful
|
||||||
|
- None if member does not exist
|
||||||
|
- False otherwise
|
||||||
|
"""
|
||||||
|
route = f"guilds/{guild_id}/members/{user_id}"
|
||||||
|
r = self._api_request(
|
||||||
|
method='delete', route=route, raise_for_status=False
|
||||||
|
)
|
||||||
|
if self._is_member_unknown_error(r):
|
||||||
|
logger.warning('User ID %s is not a member of this guild', user_id)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
if r.status_code == 204:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Guild member roles
|
||||||
|
|
||||||
|
def add_guild_member_role(
|
||||||
|
self, guild_id: int, user_id: int, role_id: int
|
||||||
|
) -> bool:
|
||||||
|
"""Adds a role to a guild member
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- True when successful
|
||||||
|
- None if member does not exist
|
||||||
|
- False otherwise
|
||||||
|
"""
|
||||||
|
route = f"guilds/{guild_id}/members/{user_id}/roles/{role_id}"
|
||||||
|
r = self._api_request(method='put', route=route, raise_for_status=False)
|
||||||
|
if self._is_member_unknown_error(r):
|
||||||
|
logger.warning('User ID %s is not a member of this guild', user_id)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
if r.status_code == 204:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def remove_guild_member_role(
|
||||||
|
self, guild_id: int, user_id: int, role_id: int
|
||||||
|
) -> bool:
|
||||||
|
"""Removes a role to a guild member
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- True when successful
|
||||||
|
- None if member does not exist
|
||||||
|
- False otherwise
|
||||||
|
"""
|
||||||
|
route = f"guilds/{guild_id}/members/{user_id}/roles/{role_id}"
|
||||||
|
r = self._api_request(method='delete', route=route, raise_for_status=False)
|
||||||
|
if self._is_member_unknown_error(r):
|
||||||
|
logger.warning('User ID %s is not a member of this guild', user_id)
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
if r.status_code == 204:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _is_member_unknown_error(cls, r: requests.Response) -> bool:
|
||||||
|
try:
|
||||||
|
result = (
|
||||||
|
r.status_code == cls._HTTP_STATUS_CODE_NOT_FOUND
|
||||||
|
and r.json()['code'] == cls._DISCORD_STATUS_CODE_UNKNOWN_MEMBER
|
||||||
|
)
|
||||||
|
except (ValueError, KeyError):
|
||||||
|
result = False
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Internal methods
|
||||||
|
|
||||||
|
def _api_request(
|
||||||
|
self,
|
||||||
|
method: str,
|
||||||
|
route: str,
|
||||||
|
data: dict = None,
|
||||||
|
authorization: str = None,
|
||||||
|
raise_for_status: bool = True
|
||||||
|
) -> requests.Response:
|
||||||
|
"""Core method for performing all API calls"""
|
||||||
|
uid = uuid1().hex
|
||||||
|
|
||||||
|
if not hasattr(requests, method):
|
||||||
|
raise ValueError('Invalid method: %s' % method)
|
||||||
|
|
||||||
|
if not authorization:
|
||||||
|
authorization = f'Bot {self.access_token}'
|
||||||
|
|
||||||
|
self._handle_ongoing_api_backoff(uid)
|
||||||
|
if self.is_rate_limited:
|
||||||
|
self._ensure_rate_limed_not_exhausted(uid)
|
||||||
|
headers = {
|
||||||
|
'User-Agent': f'{AUTH_TITLE} ({__url__}, {__version__})',
|
||||||
|
'accept': 'application/json',
|
||||||
|
'X-RateLimit-Precision': 'millisecond',
|
||||||
|
'authorization': str(authorization)
|
||||||
|
}
|
||||||
|
if data:
|
||||||
|
headers['content-type'] = 'application/json'
|
||||||
|
|
||||||
|
url = urljoin(DISCORD_API_BASE_URL, route)
|
||||||
|
args = {
|
||||||
|
'url': url,
|
||||||
|
'headers': headers,
|
||||||
|
'timeout': DISCORD_API_TIMEOUT / 1000
|
||||||
|
}
|
||||||
|
if data:
|
||||||
|
args['json'] = data
|
||||||
|
|
||||||
|
logger.info('%s: sending %s request to url \'%s\'', uid, method.upper(), url)
|
||||||
|
logger.debug('%s: request headers:\n%s', uid, headers)
|
||||||
|
r = getattr(requests, method)(**args)
|
||||||
|
logger.debug(
|
||||||
|
'%s: returned status code %d with headers:\n%s',
|
||||||
|
uid,
|
||||||
|
r.status_code,
|
||||||
|
r.headers
|
||||||
|
)
|
||||||
|
logger.debug('%s: response:\n%s', uid, r.text)
|
||||||
|
if not r.ok:
|
||||||
|
logger.warning(
|
||||||
|
'%s: Discord API returned error code %d and this response: %s',
|
||||||
|
uid,
|
||||||
|
r.status_code,
|
||||||
|
r.text
|
||||||
|
)
|
||||||
|
|
||||||
|
if r.status_code == self._HTTP_STATUS_CODE_RATE_LIMITED:
|
||||||
|
self._handle_new_api_backoff(r, uid)
|
||||||
|
|
||||||
|
self._report_rate_limit_from_api(r, uid)
|
||||||
|
|
||||||
|
if raise_for_status:
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
|
def _handle_ongoing_api_backoff(self, uid: str) -> None:
|
||||||
|
"""checks if api is currently on backoff
|
||||||
|
if on backoff: will do a blocking wait if it expires soon,
|
||||||
|
else raises exception
|
||||||
|
"""
|
||||||
|
global_backoff_duration = self._redis.pttl(self._KEY_GLOBAL_BACKOFF_UNTIL)
|
||||||
|
if global_backoff_duration > 0:
|
||||||
|
if global_backoff_duration < WAIT_THRESHOLD:
|
||||||
|
logger.info(
|
||||||
|
'%s: Global API backoff still ongoing for %s ms. Waiting.',
|
||||||
|
uid,
|
||||||
|
global_backoff_duration
|
||||||
|
)
|
||||||
|
sleep(global_backoff_duration / 1000)
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
'%s: Global API backoff still ongoing for %s ms. Re-raising.',
|
||||||
|
uid,
|
||||||
|
global_backoff_duration
|
||||||
|
)
|
||||||
|
raise DiscordTooManyRequestsError(retry_after=global_backoff_duration)
|
||||||
|
|
||||||
|
def _ensure_rate_limed_not_exhausted(self, uid: str) -> int:
|
||||||
|
"""ensures that the rate limit is not exhausted
|
||||||
|
if exhausted: will do a blocking wait if rate limit resets soon,
|
||||||
|
else raises exception
|
||||||
|
|
||||||
|
returns requests remaining on success
|
||||||
|
"""
|
||||||
|
for _ in range(RATE_LIMIT_RETRIES):
|
||||||
|
requests_remaining = self._redis_decr_or_set(
|
||||||
|
name=self._KEY_GLOBAL_RATE_LIMIT_REMAINING,
|
||||||
|
value=RATE_LIMIT_MAX_REQUESTS,
|
||||||
|
px=RATE_LIMIT_RESETS_AFTER + DURATION_CONTINGENCY
|
||||||
|
)
|
||||||
|
resets_in = self._redis.pttl(self._KEY_GLOBAL_RATE_LIMIT_REMAINING)
|
||||||
|
if requests_remaining >= 0:
|
||||||
|
logger.debug(
|
||||||
|
'%s: Got %d remaining requests until reset in %s ms',
|
||||||
|
uid,
|
||||||
|
requests_remaining + 1,
|
||||||
|
resets_in
|
||||||
|
)
|
||||||
|
return requests_remaining
|
||||||
|
|
||||||
|
elif resets_in < WAIT_THRESHOLD:
|
||||||
|
sleep(resets_in / 1000)
|
||||||
|
logger.debug(
|
||||||
|
'%s: No requests remaining until reset in %d ms. '
|
||||||
|
'Waiting for reset.',
|
||||||
|
uid,
|
||||||
|
resets_in
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.debug(
|
||||||
|
'%s: No requests remaining until reset in %d ms. '
|
||||||
|
'Raising exception.',
|
||||||
|
uid,
|
||||||
|
resets_in
|
||||||
|
)
|
||||||
|
raise DiscordRateLimitExhausted(resets_in)
|
||||||
|
|
||||||
|
raise RuntimeError('Failed to handle rate limit after after too tries.')
|
||||||
|
|
||||||
|
def _handle_new_api_backoff(self, r: requests.Response, uid: str) -> None:
|
||||||
|
"""raises exception for new API backoff error"""
|
||||||
|
response = r.json()
|
||||||
|
if 'retry_after' in response:
|
||||||
|
try:
|
||||||
|
retry_after = \
|
||||||
|
int(response['retry_after']) + DURATION_CONTINGENCY
|
||||||
|
except ValueError:
|
||||||
|
retry_after = DEFAULT_BACKOFF_DELAY
|
||||||
|
else:
|
||||||
|
retry_after = DEFAULT_BACKOFF_DELAY
|
||||||
|
self._redis_set_if_longer(
|
||||||
|
name=self._KEY_GLOBAL_BACKOFF_UNTIL,
|
||||||
|
value='GLOBAL_API_BACKOFF',
|
||||||
|
px=retry_after
|
||||||
|
)
|
||||||
|
logger.warning(
|
||||||
|
"%s: Rate limit violated. Need to back off for at least %d ms",
|
||||||
|
uid,
|
||||||
|
retry_after
|
||||||
|
)
|
||||||
|
raise DiscordTooManyRequestsError(retry_after=retry_after)
|
||||||
|
|
||||||
|
def _report_rate_limit_from_api(self, r, uid):
|
||||||
|
"""Tries to log the current rate limit reported from API"""
|
||||||
|
if (
|
||||||
|
logger.getEffectiveLevel() <= logging.DEBUG
|
||||||
|
and 'x-ratelimit-limit' in r.headers
|
||||||
|
and 'x-ratelimit-remaining' in r.headers
|
||||||
|
and 'x-ratelimit-reset-after' in r.headers
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
limit = int(r.headers['x-ratelimit-limit'])
|
||||||
|
remaining = int(r.headers['x-ratelimit-remaining'])
|
||||||
|
reset_after = float(r.headers['x-ratelimit-reset-after']) * 1000
|
||||||
|
if remaining + 1 == limit:
|
||||||
|
logger.debug(
|
||||||
|
'%s: Rate limit reported from API: %d requests per %s ms',
|
||||||
|
uid,
|
||||||
|
limit,
|
||||||
|
reset_after
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _redis_decode(value: str) -> str:
|
||||||
|
"""Decodes a string from Redis and passes through None and Booleans"""
|
||||||
|
if value is not None and not isinstance(value, bool):
|
||||||
|
return value.decode('utf-8')
|
||||||
|
else:
|
||||||
|
return value
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _generate_hash(key: str) -> str:
|
||||||
|
return md5(key.encode('utf-8')).hexdigest()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _sanitize_role_ids(role_ids: list) -> list:
|
||||||
|
"""make sure its a list of integers"""
|
||||||
|
return [int(role_id) for role_id in list(role_ids)]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _sanitize_role_name(cls, role_name: str) -> str:
|
||||||
|
"""shortens too long strings if necessary"""
|
||||||
|
return str(role_name)[:cls._ROLE_NAME_MAX_CHARS]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _sanitize_nick(cls, nick: str) -> str:
|
||||||
|
"""shortens too long strings if necessary"""
|
||||||
|
return str(nick)[:cls._NICK_MAX_CHARS]
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
class DiscordClientException(Exception):
|
||||||
|
"""Base Exception for the Discord client"""
|
||||||
|
|
||||||
|
|
||||||
|
class DiscordApiBackoff(DiscordClientException):
|
||||||
|
"""Exception signaling we need to backoff from sending requests to the API for now
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, retry_after: int):
|
||||||
|
"""
|
||||||
|
:param retry_after: int time to retry after in milliseconds
|
||||||
|
"""
|
||||||
|
super().__init__()
|
||||||
|
self.retry_after = int(retry_after)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def retry_after_seconds(self):
|
||||||
|
return math.ceil(self.retry_after / 1000)
|
||||||
|
|
||||||
|
|
||||||
|
class DiscordRateLimitExhausted(DiscordApiBackoff):
|
||||||
|
"""Exception signaling that the total number of requests allowed under the
|
||||||
|
current rate limit have been exhausted and weed to wait until next reset.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class DiscordTooManyRequestsError(DiscordApiBackoff):
|
||||||
|
"""API has responded with a 429 Too Many Requests Error.
|
||||||
|
Need to backoff for now.
|
||||||
|
"""
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
"""This is script is for concurrency testing the Discord client with a Discord server.
|
||||||
|
|
||||||
|
It will run multiple requests against Discord with multiple workers in parallel.
|
||||||
|
The results can be analysed in a special log file.
|
||||||
|
|
||||||
|
This script is design to be run manually as unit test, e.g. by running the following:
|
||||||
|
|
||||||
|
python manage.py test
|
||||||
|
allianceauth.services.modules.discord.discord_client.tests.piloting_concurrency
|
||||||
|
|
||||||
|
To make it work please set the below mentioned environment variables for your server.
|
||||||
|
Since this may cause lots of 429s we'd recommend NOT to use your
|
||||||
|
alliance Discord server for this.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from random import random
|
||||||
|
import threading
|
||||||
|
from time import sleep
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from .. import DiscordClient, DiscordApiBackoff
|
||||||
|
|
||||||
|
from ...utils import set_logger_to_file
|
||||||
|
|
||||||
|
logger = set_logger_to_file(
|
||||||
|
'allianceauth.services.modules.discord.discord_client.client', __file__
|
||||||
|
)
|
||||||
|
|
||||||
|
# Make sure to set these environnement variables for your Discord server and user
|
||||||
|
DISCORD_GUILD_ID = os.environ['DISCORD_GUILD_ID']
|
||||||
|
DISCORD_BOT_TOKEN = os.environ['DISCORD_BOT_TOKEN']
|
||||||
|
DISCORD_USER_ID = os.environ['DISCORD_USER_ID']
|
||||||
|
NICK = 'Dummy'
|
||||||
|
|
||||||
|
# Configure these settings to adjust the load profile
|
||||||
|
NUMBER_OF_WORKERS = 5
|
||||||
|
NUMBER_OF_RUNS = 10
|
||||||
|
|
||||||
|
# max seconds a worker waits before starting a new run
|
||||||
|
# set to near 0 for max load preassure
|
||||||
|
MAX_JITTER_PER_RUN_SECS = 1.0
|
||||||
|
|
||||||
|
|
||||||
|
def worker(num: int):
|
||||||
|
"""worker function"""
|
||||||
|
worker_info = 'worker %d' % num
|
||||||
|
logger.info('%s: started', worker_info)
|
||||||
|
client = DiscordClient(DISCORD_BOT_TOKEN)
|
||||||
|
try:
|
||||||
|
runs = 0
|
||||||
|
while runs < NUMBER_OF_RUNS:
|
||||||
|
run_info = '%s: run %d' % (worker_info, runs + 1)
|
||||||
|
my_jitter_secs = random() * MAX_JITTER_PER_RUN_SECS
|
||||||
|
logger.info('%s - waiting %s secs', run_info, f'{my_jitter_secs:.3f}')
|
||||||
|
sleep(my_jitter_secs)
|
||||||
|
logger.info('%s - started', run_info)
|
||||||
|
try:
|
||||||
|
client.modify_guild_member(
|
||||||
|
DISCORD_GUILD_ID, DISCORD_USER_ID, nick=NICK
|
||||||
|
)
|
||||||
|
runs += 1
|
||||||
|
except DiscordApiBackoff as bo:
|
||||||
|
message = '%s - waiting out API backoff for %d ms' % (
|
||||||
|
run_info, bo.retry_after
|
||||||
|
)
|
||||||
|
logger.info(message)
|
||||||
|
print()
|
||||||
|
print(message)
|
||||||
|
sleep(bo.retry_after / 1000)
|
||||||
|
|
||||||
|
except Exception as ex:
|
||||||
|
logger.exception('%s: Processing aborted: %s', worker_info, ex)
|
||||||
|
|
||||||
|
logger.info('%s: finished', worker_info)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
class TestMulti(TestCase):
|
||||||
|
|
||||||
|
def test_multi(self):
|
||||||
|
logger.info('Starting multi test')
|
||||||
|
for num in range(NUMBER_OF_WORKERS):
|
||||||
|
x = threading.Thread(target=worker, args=(num + 1,))
|
||||||
|
x.start()
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
"""This script is for functional testing of the Discord client with a Discord server
|
||||||
|
|
||||||
|
It will run single requests of the various functions to validate
|
||||||
|
that they actually work - excluding those that require Oauth, or does not work
|
||||||
|
with a bot token. The results can be also seen in a special log file.
|
||||||
|
|
||||||
|
This script is design to be run manually as unit test, e.g. by running the following:
|
||||||
|
|
||||||
|
python manage.py test
|
||||||
|
allianceauth.services.modules.discord.discord_self.client.tests.piloting_functionality
|
||||||
|
|
||||||
|
To make it work please set the below mentioned environment variables for your server.
|
||||||
|
Since this may cause lots of 429s we'd recommend NOT to use your
|
||||||
|
alliance Discord server for this.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from uuid import uuid1
|
||||||
|
import os
|
||||||
|
from unittest import TestCase
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
from .. import DiscordClient
|
||||||
|
from ...utils import set_logger_to_file
|
||||||
|
|
||||||
|
logger = set_logger_to_file(
|
||||||
|
'allianceauth.services.modules.discord.discord_self.client.client', __file__
|
||||||
|
)
|
||||||
|
|
||||||
|
# Make sure to set these environnement variables for your Discord server and user
|
||||||
|
DISCORD_GUILD_ID = os.environ['DISCORD_GUILD_ID']
|
||||||
|
DISCORD_BOT_TOKEN = os.environ['DISCORD_BOT_TOKEN']
|
||||||
|
DISCORD_USER_ID = os.environ['DISCORD_USER_ID']
|
||||||
|
|
||||||
|
RATE_LIMIT_DELAY_SECS = 1
|
||||||
|
|
||||||
|
|
||||||
|
class TestDiscordApiLive(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
logger.info('Live demo of the Discord API Client')
|
||||||
|
cls.client = DiscordClient(DISCORD_BOT_TOKEN)
|
||||||
|
|
||||||
|
def test_run_other_features(self):
|
||||||
|
"""runs features that have not been run in any of the other tests"""
|
||||||
|
self.client.guild_infos(DISCORD_GUILD_ID)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
|
||||||
|
self.client.guild_name(DISCORD_GUILD_ID)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
|
||||||
|
self.client.match_guild_role_to_name(DISCORD_GUILD_ID, 'Testrole')
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
|
||||||
|
self.client.match_guild_roles_to_names(
|
||||||
|
DISCORD_GUILD_ID, ['Testrole A', 'Testrole B']
|
||||||
|
)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
|
||||||
|
def test_create_and_remove_roles(self):
|
||||||
|
# get base
|
||||||
|
logger.info('guild_roles')
|
||||||
|
expected = {role['id'] for role in self.client.guild_roles(DISCORD_GUILD_ID)}
|
||||||
|
|
||||||
|
# add role
|
||||||
|
role_name = 'my test role 12345678'
|
||||||
|
logger.info('create_guild_role')
|
||||||
|
new_role = self.client.create_guild_role(
|
||||||
|
guild_id=DISCORD_GUILD_ID, role_name=role_name
|
||||||
|
)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
self.assertEqual(new_role['name'], role_name)
|
||||||
|
|
||||||
|
# remove role again
|
||||||
|
logger.info('delete_guild_role')
|
||||||
|
self.client.delete_guild_role(
|
||||||
|
guild_id=DISCORD_GUILD_ID, role_id=new_role['id']
|
||||||
|
)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
|
||||||
|
# verify it worked
|
||||||
|
logger.info('guild_roles')
|
||||||
|
role_ids = {role['id'] for role in self.client.guild_roles(DISCORD_GUILD_ID)}
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
self.assertSetEqual(role_ids, expected)
|
||||||
|
|
||||||
|
def test_change_member_nick(self):
|
||||||
|
# set new nick for user
|
||||||
|
logger.info('modify_guild_member')
|
||||||
|
new_nick = f'Testnick {uuid1().hex}'[:32]
|
||||||
|
self.assertTrue(
|
||||||
|
self.client.modify_guild_member(
|
||||||
|
guild_id=DISCORD_GUILD_ID, user_id=DISCORD_USER_ID, nick=new_nick
|
||||||
|
)
|
||||||
|
)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
|
||||||
|
# verify it is saved
|
||||||
|
logger.info('guild_member')
|
||||||
|
user = self.client.guild_member(DISCORD_GUILD_ID, DISCORD_USER_ID)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
self.assertEqual(user['nick'], new_nick)
|
||||||
|
|
||||||
|
def test_member_add_remove_roles(self):
|
||||||
|
# create new guild role
|
||||||
|
logger.info('create_guild_role')
|
||||||
|
new_role = self.client.create_guild_role(
|
||||||
|
guild_id=DISCORD_GUILD_ID, role_name='Special role 98765'
|
||||||
|
)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
new_role_id = new_role['id']
|
||||||
|
|
||||||
|
# add to member
|
||||||
|
logger.info('add_guild_member_role')
|
||||||
|
self.assertTrue(
|
||||||
|
self.client.add_guild_member_role(
|
||||||
|
guild_id=DISCORD_GUILD_ID, user_id=DISCORD_USER_ID, role_id=new_role_id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
|
|
||||||
|
# remove again
|
||||||
|
logger.info('remove_guild_member_role')
|
||||||
|
self.assertTrue(
|
||||||
|
self.client.remove_guild_member_role(
|
||||||
|
guild_id=DISCORD_GUILD_ID, user_id=DISCORD_USER_ID, role_id=new_role_id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
sleep(RATE_LIMIT_DELAY_SECS)
|
||||||
47
allianceauth/services/modules/discord/discord_client/tests/piloting_tasks.py
Executable file
47
allianceauth/services/modules/discord/discord_client/tests/piloting_tasks.py
Executable file
@@ -0,0 +1,47 @@
|
|||||||
|
"""Load testing Discord services tasks
|
||||||
|
|
||||||
|
This script will load test the Discord service tasks.
|
||||||
|
Note that his will run against your production Auth.
|
||||||
|
To run this test start a bunch of celery workers and then run this script directly.
|
||||||
|
|
||||||
|
This script requires a user with a Discord account setup through Auth.
|
||||||
|
Please provide the respective Discord user ID by setting it as environment variable:
|
||||||
|
|
||||||
|
export DISCORD_USER_ID="123456789"
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
myauth_dir = '/home/erik997/dev/python/aa/allianceauth-dev/myauth'
|
||||||
|
sys.path.insert(0, myauth_dir)
|
||||||
|
|
||||||
|
import django # noqa: E402
|
||||||
|
|
||||||
|
# init and setup django project
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myauth.settings.local")
|
||||||
|
django.setup()
|
||||||
|
|
||||||
|
from uuid import uuid1 # noqa: E402
|
||||||
|
|
||||||
|
from django.contrib.auth.models import User # noqa: E402
|
||||||
|
# from allianceauth.services.modules.discord.tasks import update_groups # noqa: E402
|
||||||
|
|
||||||
|
if 'DISCORD_USER_ID' not in os.environ:
|
||||||
|
print('Please set DISCORD_USER_ID')
|
||||||
|
exit()
|
||||||
|
|
||||||
|
DISCORD_USER_ID = os.environ['DISCORD_USER_ID']
|
||||||
|
|
||||||
|
|
||||||
|
def run_many_updates(runs):
|
||||||
|
user = User.objects.get(discord__uid=DISCORD_USER_ID)
|
||||||
|
for _ in range(runs):
|
||||||
|
new_nick = f'Testnick {uuid1().hex}'[:32]
|
||||||
|
user.profile.main_character.character_name = new_nick
|
||||||
|
user.profile.main_character.save()
|
||||||
|
# update_groups.delay(user_pk=user.pk)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_many_updates(20)
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# Discord rate limits
|
||||||
|
|
||||||
|
The following table shows the rate limit as reported from the API for different routes.
|
||||||
|
|
||||||
|
method | limit | reset | rate / s | bucket
|
||||||
|
-- | -- | -- | -- | --
|
||||||
|
add_guild_member | 10 | 10,000 | 1 | self
|
||||||
|
create_guild_role | 250 | 180,000,000 | 0.001 | self
|
||||||
|
delete_guild_role | g | g | g | g
|
||||||
|
guild_member | 5 | 1,000 | 5 | self
|
||||||
|
guild_roles | g | g | g | g
|
||||||
|
add_guild_member_role | 10 | 10,000 | 1 | B1
|
||||||
|
remove_guild_member_role | 10 | 10,000 | 1 | B1
|
||||||
|
modify_guild_member | 10 | 10,000 | 1 | self
|
||||||
|
remove_guild_member | 5 | 1,000 | 5 | self
|
||||||
|
current_user | g | g | g | g
|
||||||
|
|
||||||
|
Legend:
|
||||||
|
|
||||||
|
- g: global rate limit. API does not provide any rate limit infos for those routes.
|
||||||
|
|
||||||
|
- reset: Values in milliseconds.
|
||||||
|
|
||||||
|
- bucket: "self" means the rate limit is only counted for that route, Bx means the same rate limit is counted for multiple routes.
|
||||||
|
|
||||||
|
- Data was collected on 2020-MAY-07 and is subject to change.
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,33 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from ..exceptions import (
|
||||||
|
DiscordApiBackoff,
|
||||||
|
DiscordClientException,
|
||||||
|
DiscordRateLimitExhausted,
|
||||||
|
DiscordTooManyRequestsError
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestExceptions(TestCase):
|
||||||
|
|
||||||
|
def test_DiscordApiException(self):
|
||||||
|
with self.assertRaises(DiscordClientException):
|
||||||
|
raise DiscordClientException()
|
||||||
|
|
||||||
|
def test_DiscordApiBackoff_raise(self):
|
||||||
|
with self.assertRaises(DiscordApiBackoff):
|
||||||
|
raise DiscordApiBackoff(999)
|
||||||
|
|
||||||
|
def test_DiscordApiBackoff_retry_after_seconds(self):
|
||||||
|
retry_after = 999
|
||||||
|
ex = DiscordApiBackoff(retry_after)
|
||||||
|
self.assertEqual(ex.retry_after, retry_after)
|
||||||
|
self.assertEqual(ex.retry_after_seconds, 1)
|
||||||
|
|
||||||
|
def test_DiscordRateLimitedExhausted_raise(self):
|
||||||
|
with self.assertRaises(DiscordRateLimitExhausted):
|
||||||
|
raise DiscordRateLimitExhausted(999)
|
||||||
|
|
||||||
|
def test_DiscordApiBackoffError_raise(self):
|
||||||
|
with self.assertRaises(DiscordTooManyRequestsError):
|
||||||
|
raise DiscordTooManyRequestsError(999)
|
||||||
@@ -1,333 +0,0 @@
|
|||||||
import requests
|
|
||||||
import math
|
|
||||||
from django.conf import settings
|
|
||||||
from requests_oauthlib import OAuth2Session
|
|
||||||
from functools import wraps
|
|
||||||
import logging
|
|
||||||
import datetime
|
|
||||||
import time
|
|
||||||
from django.core.cache import cache
|
|
||||||
from hashlib import md5
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
DISCORD_URL = "https://discordapp.com/api"
|
|
||||||
|
|
||||||
AUTH_URL = "https://discordapp.com/api/oauth2/authorize"
|
|
||||||
TOKEN_URL = "https://discordapp.com/api/oauth2/token"
|
|
||||||
|
|
||||||
"""
|
|
||||||
Previously all we asked for was permission to kick members, manage roles, and manage nicknames.
|
|
||||||
Users have reported weird unauthorized errors we don't understand. So now we ask for full server admin.
|
|
||||||
It's almost fixed the problem.
|
|
||||||
"""
|
|
||||||
# kick members, manage roles, manage nicknames, create instant invite
|
|
||||||
# BOT_PERMISSIONS = 0x00000002 + 0x10000000 + 0x08000000 + 0x00000001
|
|
||||||
BOT_PERMISSIONS = 0x00000008
|
|
||||||
|
|
||||||
# get user ID, accept invite
|
|
||||||
SCOPES = [
|
|
||||||
'identify',
|
|
||||||
'guilds.join',
|
|
||||||
]
|
|
||||||
|
|
||||||
GROUP_CACHE_MAX_AGE = getattr(settings, 'DISCORD_GROUP_CACHE_MAX_AGE', 2 * 60 * 60) # 2 hours default
|
|
||||||
|
|
||||||
|
|
||||||
class DiscordApiException(Exception):
|
|
||||||
def __init__(self):
|
|
||||||
super(Exception, self).__init__()
|
|
||||||
|
|
||||||
|
|
||||||
class DiscordApiTooBusy(DiscordApiException):
|
|
||||||
def __init__(self):
|
|
||||||
super(DiscordApiException, self).__init__()
|
|
||||||
self.message = "The Discord API is too busy to process this request now, please try again later."
|
|
||||||
|
|
||||||
|
|
||||||
class DiscordApiBackoff(DiscordApiException):
|
|
||||||
def __init__(self, retry_after, global_ratelimit):
|
|
||||||
"""
|
|
||||||
:param retry_after: int time to retry after in milliseconds
|
|
||||||
:param global_ratelimit: bool Is the API under a global backoff
|
|
||||||
"""
|
|
||||||
super(DiscordApiException, self).__init__()
|
|
||||||
self.retry_after = retry_after
|
|
||||||
self.global_ratelimit = global_ratelimit
|
|
||||||
|
|
||||||
@property
|
|
||||||
def retry_after_seconds(self):
|
|
||||||
return math.ceil(self.retry_after / 1000)
|
|
||||||
|
|
||||||
|
|
||||||
cache_time_format = '%Y-%m-%d %H:%M:%S.%f'
|
|
||||||
|
|
||||||
|
|
||||||
def api_backoff(func):
|
|
||||||
"""
|
|
||||||
Decorator, Handles HTTP 429 "Too Many Requests" messages from the Discord API
|
|
||||||
If blocking=True is specified, this function will block and retry
|
|
||||||
the function up to max_retries=n times, or 3 if retries is not specified.
|
|
||||||
If the API call still recieves a backoff timer this function will raise
|
|
||||||
a <DiscordApiTooBusy> exception.
|
|
||||||
If the caller chooses blocking=False, the decorator will raise a DiscordApiBackoff
|
|
||||||
exception and the caller can choose to retry after the given timespan available in
|
|
||||||
the retry_after property in seconds.
|
|
||||||
"""
|
|
||||||
|
|
||||||
class PerformBackoff(Exception):
|
|
||||||
def __init__(self, retry_after, retry_datetime, global_ratelimit):
|
|
||||||
super(Exception, self).__init__()
|
|
||||||
self.retry_after = int(retry_after)
|
|
||||||
self.retry_datetime = retry_datetime
|
|
||||||
self.global_ratelimit = global_ratelimit
|
|
||||||
|
|
||||||
@wraps(func)
|
|
||||||
def decorated(*args, **kwargs):
|
|
||||||
blocking = kwargs.get('blocking', False)
|
|
||||||
retries = kwargs.get('max_retries', 3)
|
|
||||||
|
|
||||||
# Strip our parameters
|
|
||||||
if 'max_retries' in kwargs:
|
|
||||||
del kwargs['max_retries']
|
|
||||||
if 'blocking' in kwargs:
|
|
||||||
del kwargs['blocking']
|
|
||||||
|
|
||||||
cache_key = 'DISCORD_BACKOFF_' + func.__name__
|
|
||||||
cache_global_key = 'DISCORD_BACKOFF_GLOBAL'
|
|
||||||
|
|
||||||
while retries > 0:
|
|
||||||
try:
|
|
||||||
try:
|
|
||||||
# Check global backoff first, then route backoff
|
|
||||||
existing_global_backoff = cache.get(cache_global_key)
|
|
||||||
existing_backoff = existing_global_backoff or cache.get(cache_key)
|
|
||||||
if existing_backoff:
|
|
||||||
backoff_timer = datetime.datetime.strptime(existing_backoff, cache_time_format)
|
|
||||||
if backoff_timer > datetime.datetime.utcnow():
|
|
||||||
backoff_seconds = (backoff_timer - datetime.datetime.utcnow()).total_seconds()
|
|
||||||
logger.debug("Still under backoff for %s seconds, backing off" % backoff_seconds)
|
|
||||||
# Still under backoff
|
|
||||||
raise PerformBackoff(
|
|
||||||
retry_after=backoff_seconds,
|
|
||||||
retry_datetime=backoff_timer,
|
|
||||||
global_ratelimit=bool(existing_global_backoff)
|
|
||||||
)
|
|
||||||
logger.debug("Calling API calling function")
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
except requests.HTTPError as e:
|
|
||||||
if e.response.status_code == 429:
|
|
||||||
try:
|
|
||||||
retry_after = int(e.response.headers['Retry-After'])
|
|
||||||
except (TypeError, KeyError):
|
|
||||||
# Pick some random time
|
|
||||||
retry_after = 5000
|
|
||||||
|
|
||||||
logger.info("Received backoff from API of %s seconds, handling" % retry_after)
|
|
||||||
# Store value in redis
|
|
||||||
backoff_until = (datetime.datetime.utcnow() +
|
|
||||||
datetime.timedelta(milliseconds=retry_after))
|
|
||||||
global_backoff = bool(e.response.headers.get('X-RateLimit-Global', False))
|
|
||||||
if global_backoff:
|
|
||||||
logger.info("Global backoff!!")
|
|
||||||
cache.set(cache_global_key, backoff_until.strftime(cache_time_format), retry_after)
|
|
||||||
else:
|
|
||||||
cache.set(cache_key, backoff_until.strftime(cache_time_format), retry_after)
|
|
||||||
raise PerformBackoff(retry_after=retry_after, retry_datetime=backoff_until,
|
|
||||||
global_ratelimit=global_backoff)
|
|
||||||
else:
|
|
||||||
# Not 429, re-raise
|
|
||||||
raise e
|
|
||||||
except PerformBackoff as bo:
|
|
||||||
# Sleep if we're blocking
|
|
||||||
if blocking:
|
|
||||||
logger.info("Blocking Back off from API calls for %s seconds" % bo.retry_after)
|
|
||||||
time.sleep((10 if bo.retry_after > 10 else bo.retry_after) / 1000)
|
|
||||||
else:
|
|
||||||
# Otherwise raise exception and let caller handle the backoff
|
|
||||||
raise DiscordApiBackoff(retry_after=bo.retry_after, global_ratelimit=bo.global_ratelimit)
|
|
||||||
finally:
|
|
||||||
retries -= 1
|
|
||||||
if retries == 0:
|
|
||||||
raise DiscordApiTooBusy()
|
|
||||||
return decorated
|
|
||||||
|
|
||||||
|
|
||||||
class DiscordOAuthManager:
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _sanitize_name(name):
|
|
||||||
return name[:32]
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _sanitize_group_name(name):
|
|
||||||
return name[:100]
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def generate_bot_add_url():
|
|
||||||
return AUTH_URL + '?client_id=' + settings.DISCORD_APP_ID + '&scope=bot&permissions=' + str(BOT_PERMISSIONS)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def generate_oauth_redirect_url():
|
|
||||||
oauth = OAuth2Session(settings.DISCORD_APP_ID, redirect_uri=settings.DISCORD_CALLBACK_URL, scope=SCOPES)
|
|
||||||
url, state = oauth.authorization_url(AUTH_URL)
|
|
||||||
return url
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _process_callback_code(code):
|
|
||||||
oauth = OAuth2Session(settings.DISCORD_APP_ID, redirect_uri=settings.DISCORD_CALLBACK_URL)
|
|
||||||
token = oauth.fetch_token(TOKEN_URL, client_secret=settings.DISCORD_APP_SECRET, code=code)
|
|
||||||
return token
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def add_user(code, groups, nickname=None):
|
|
||||||
try:
|
|
||||||
token = DiscordOAuthManager._process_callback_code(code)['access_token']
|
|
||||||
logger.debug("Received token from OAuth")
|
|
||||||
|
|
||||||
custom_headers = {'accept': 'application/json', 'authorization': 'Bearer ' + token}
|
|
||||||
path = DISCORD_URL + "/users/@me"
|
|
||||||
r = requests.get(path, headers=custom_headers)
|
|
||||||
logger.debug("Got status code %s after retrieving Discord profile" % r.status_code)
|
|
||||||
r.raise_for_status()
|
|
||||||
|
|
||||||
user_id = r.json()['id']
|
|
||||||
|
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/members/" + str(user_id)
|
|
||||||
group_ids = [DiscordOAuthManager._group_name_to_id(DiscordOAuthManager._sanitize_group_name(g)) for g in
|
|
||||||
groups]
|
|
||||||
data = {
|
|
||||||
'roles': group_ids,
|
|
||||||
'access_token': token,
|
|
||||||
}
|
|
||||||
if nickname:
|
|
||||||
data['nick'] = DiscordOAuthManager._sanitize_name(nickname)
|
|
||||||
custom_headers['authorization'] = 'Bot ' + settings.DISCORD_BOT_TOKEN
|
|
||||||
r = requests.put(path, headers=custom_headers, json=data)
|
|
||||||
logger.debug("Got status code %s after joining Discord server" % r.status_code)
|
|
||||||
r.raise_for_status()
|
|
||||||
|
|
||||||
logger.info("Added Discord user ID %s to server." % user_id)
|
|
||||||
return user_id
|
|
||||||
except:
|
|
||||||
logger.exception("Failed to add Discord user")
|
|
||||||
return None
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@api_backoff
|
|
||||||
def update_nickname(user_id, nickname):
|
|
||||||
nickname = DiscordOAuthManager._sanitize_name(nickname)
|
|
||||||
custom_headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
data = {'nick': nickname}
|
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/members/" + str(user_id)
|
|
||||||
r = requests.patch(path, headers=custom_headers, json=data)
|
|
||||||
logger.debug("Got status code %s after setting nickname for Discord user ID %s (%s)" % (
|
|
||||||
r.status_code, user_id, nickname))
|
|
||||||
if r.status_code == 404:
|
|
||||||
logger.warn("Discord user ID %s could not be found in server." % user_id)
|
|
||||||
return True
|
|
||||||
r.raise_for_status()
|
|
||||||
return True
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def delete_user(user_id):
|
|
||||||
try:
|
|
||||||
custom_headers = {'accept': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/members/" + str(user_id)
|
|
||||||
r = requests.delete(path, headers=custom_headers)
|
|
||||||
logger.debug("Got status code %s after removing Discord user ID %s" % (r.status_code, user_id))
|
|
||||||
if r.status_code == 404:
|
|
||||||
logger.warn("Discord user ID %s already left the server." % user_id)
|
|
||||||
return True
|
|
||||||
r.raise_for_status()
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
logger.exception("Failed to remove Discord user ID %s" % user_id)
|
|
||||||
return False
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _get_groups():
|
|
||||||
custom_headers = {'accept': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/roles"
|
|
||||||
r = requests.get(path, headers=custom_headers)
|
|
||||||
logger.debug("Got status code %s after retrieving Discord roles" % r.status_code)
|
|
||||||
r.raise_for_status()
|
|
||||||
return r.json()
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _generate_cache_role_key(name):
|
|
||||||
return 'DISCORD_ROLE_NAME__%s' % md5(str(name).encode('utf-8')).hexdigest()
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _group_name_to_id(name):
|
|
||||||
name = DiscordOAuthManager._sanitize_group_name(name)
|
|
||||||
|
|
||||||
def get_or_make_role():
|
|
||||||
groups = DiscordOAuthManager._get_groups()
|
|
||||||
for g in groups:
|
|
||||||
if g['name'] == name:
|
|
||||||
return g['id']
|
|
||||||
return DiscordOAuthManager._create_group(name)['id']
|
|
||||||
return cache.get_or_set(DiscordOAuthManager._generate_cache_role_key(name), get_or_make_role, GROUP_CACHE_MAX_AGE)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def __generate_role(name, **kwargs):
|
|
||||||
custom_headers = {'accept': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/roles"
|
|
||||||
data = {'name': name}
|
|
||||||
data.update(kwargs)
|
|
||||||
r = requests.post(path, headers=custom_headers, json=data)
|
|
||||||
logger.debug("Received status code %s after generating new role." % r.status_code)
|
|
||||||
r.raise_for_status()
|
|
||||||
return r.json()
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def __edit_role(role_id, **kwargs):
|
|
||||||
custom_headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/roles/" + str(role_id)
|
|
||||||
r = requests.patch(path, headers=custom_headers, json=kwargs)
|
|
||||||
logger.debug("Received status code %s after editing role id %s" % (r.status_code, role_id))
|
|
||||||
r.raise_for_status()
|
|
||||||
return r.json()
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _create_group(name):
|
|
||||||
return DiscordOAuthManager.__generate_role(name)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _get_user(user_id):
|
|
||||||
custom_headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/members/" + str(user_id)
|
|
||||||
r = requests.get(path, headers=custom_headers)
|
|
||||||
r.raise_for_status()
|
|
||||||
return r.json()
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _get_user_roles(user_id):
|
|
||||||
user = DiscordOAuthManager._get_user(user_id)
|
|
||||||
return user['roles']
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _modify_user_role(user_id, role_id, method):
|
|
||||||
custom_headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/members/" + str(user_id) + "/roles/" + str(
|
|
||||||
role_id)
|
|
||||||
r = getattr(requests, method)(path, headers=custom_headers)
|
|
||||||
r.raise_for_status()
|
|
||||||
logger.debug("%s role %s for user %s" % (method, role_id, user_id))
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@api_backoff
|
|
||||||
def update_groups(user_id, groups):
|
|
||||||
group_ids = [DiscordOAuthManager._group_name_to_id(DiscordOAuthManager._sanitize_group_name(g)) for g in groups]
|
|
||||||
user_group_ids = DiscordOAuthManager._get_user_roles(user_id)
|
|
||||||
for g in group_ids:
|
|
||||||
if g not in user_group_ids:
|
|
||||||
DiscordOAuthManager._modify_user_role(user_id, g, 'put')
|
|
||||||
time.sleep(1) # we're gonna be hammering the API here
|
|
||||||
for g in user_group_ids:
|
|
||||||
if g not in group_ids:
|
|
||||||
DiscordOAuthManager._modify_user_role(user_id, g, 'delete')
|
|
||||||
time.sleep(1)
|
|
||||||
175
allianceauth/services/modules/discord/managers.py
Normal file
175
allianceauth/services/modules/discord/managers.py
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
import logging
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
|
from requests_oauthlib import OAuth2Session
|
||||||
|
from requests.exceptions import HTTPError
|
||||||
|
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from django.db import models
|
||||||
|
from django.utils.timezone import now
|
||||||
|
|
||||||
|
from allianceauth.services.hooks import NameFormatter
|
||||||
|
|
||||||
|
from . import __title__
|
||||||
|
from .app_settings import (
|
||||||
|
DISCORD_APP_ID,
|
||||||
|
DISCORD_APP_SECRET,
|
||||||
|
DISCORD_BOT_TOKEN,
|
||||||
|
DISCORD_CALLBACK_URL,
|
||||||
|
DISCORD_GUILD_ID,
|
||||||
|
DISCORD_SYNC_NAMES
|
||||||
|
)
|
||||||
|
from .discord_client import DiscordClient, DiscordApiBackoff
|
||||||
|
from .utils import LoggerAddTag
|
||||||
|
|
||||||
|
|
||||||
|
logger = LoggerAddTag(logging.getLogger(__name__), __title__)
|
||||||
|
|
||||||
|
|
||||||
|
class DiscordUserManager(models.Manager):
|
||||||
|
"""Manager for DiscordUser"""
|
||||||
|
|
||||||
|
# full server admin
|
||||||
|
BOT_PERMISSIONS = 0x00000008
|
||||||
|
|
||||||
|
# get user ID, accept invite
|
||||||
|
SCOPES = [
|
||||||
|
'identify',
|
||||||
|
'guilds.join',
|
||||||
|
]
|
||||||
|
|
||||||
|
def add_user(
|
||||||
|
self,
|
||||||
|
user: User,
|
||||||
|
authorization_code: str,
|
||||||
|
is_rate_limited: bool = True
|
||||||
|
) -> bool:
|
||||||
|
"""adds a new Discord user
|
||||||
|
|
||||||
|
Params:
|
||||||
|
- user: Auth user to join
|
||||||
|
- authorization_code: authorization code returns from oauth
|
||||||
|
- is_rate_limited: When False will disable default rate limiting (use with care)
|
||||||
|
|
||||||
|
Returns: True on success, else False or raises exception
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
nickname = self.user_formatted_nick(user) if DISCORD_SYNC_NAMES else None
|
||||||
|
group_names = self.user_group_names(user)
|
||||||
|
access_token = self._exchange_auth_code_for_token(authorization_code)
|
||||||
|
user_client = DiscordClient(access_token, is_rate_limited=is_rate_limited)
|
||||||
|
discord_user = user_client.current_user()
|
||||||
|
user_id = discord_user['id']
|
||||||
|
bot_client = self._bot_client(is_rate_limited=is_rate_limited)
|
||||||
|
|
||||||
|
if group_names:
|
||||||
|
role_ids = self.model._guild_get_or_create_role_ids(
|
||||||
|
bot_client, group_names
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
role_ids = None
|
||||||
|
|
||||||
|
created = bot_client.add_guild_member(
|
||||||
|
guild_id=DISCORD_GUILD_ID,
|
||||||
|
user_id=user_id,
|
||||||
|
access_token=access_token,
|
||||||
|
role_ids=role_ids,
|
||||||
|
nick=nickname
|
||||||
|
)
|
||||||
|
if created is not False:
|
||||||
|
if created is None:
|
||||||
|
logger.debug(
|
||||||
|
"User %s with Discord ID %s is already a member.",
|
||||||
|
user,
|
||||||
|
user_id,
|
||||||
|
)
|
||||||
|
self.update_or_create(
|
||||||
|
user=user,
|
||||||
|
defaults={
|
||||||
|
'uid': user_id,
|
||||||
|
'username': discord_user['username'][:32],
|
||||||
|
'discriminator': discord_user['discriminator'][:4],
|
||||||
|
'activated': now()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"Added user %s with Discord ID %s to Discord server", user, user_id
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
"Failed to add user %s with Discord ID %s to Discord server",
|
||||||
|
user,
|
||||||
|
user_id,
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
except (HTTPError, ConnectionError, DiscordApiBackoff) as ex:
|
||||||
|
logger.exception(
|
||||||
|
'Failed to add user %s to Discord server: %s', user, ex
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def user_formatted_nick(user: User) -> str:
|
||||||
|
"""returns the name of the given users main character with name formatting
|
||||||
|
or None if user has no main
|
||||||
|
"""
|
||||||
|
from .auth_hooks import DiscordService
|
||||||
|
|
||||||
|
if user.profile.main_character:
|
||||||
|
return NameFormatter(DiscordService(), user).format_name()
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def user_group_names(user: User) -> list:
|
||||||
|
"""returns list of group names plus state the given user is a member of"""
|
||||||
|
return [group.name for group in user.groups.all()] + [user.profile.state.name]
|
||||||
|
|
||||||
|
def user_has_account(self, user: User) -> bool:
|
||||||
|
"""Returns True if the user has an Discord account, else False
|
||||||
|
|
||||||
|
only checks locally, does not hit the API
|
||||||
|
"""
|
||||||
|
return True if hasattr(user, self.model.USER_RELATED_NAME) else False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def generate_bot_add_url(cls):
|
||||||
|
params = urlencode({
|
||||||
|
'client_id': DISCORD_APP_ID,
|
||||||
|
'scope': 'bot',
|
||||||
|
'permissions': str(cls.BOT_PERMISSIONS)
|
||||||
|
|
||||||
|
})
|
||||||
|
return f'{DiscordClient.OAUTH_BASE_URL}?{params}'
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def generate_oauth_redirect_url(cls):
|
||||||
|
oauth = OAuth2Session(
|
||||||
|
DISCORD_APP_ID, redirect_uri=DISCORD_CALLBACK_URL, scope=cls.SCOPES
|
||||||
|
)
|
||||||
|
url, state = oauth.authorization_url(DiscordClient.OAUTH_BASE_URL)
|
||||||
|
return url
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _exchange_auth_code_for_token(authorization_code: str) -> str:
|
||||||
|
oauth = OAuth2Session(DISCORD_APP_ID, redirect_uri=DISCORD_CALLBACK_URL)
|
||||||
|
token = oauth.fetch_token(
|
||||||
|
DiscordClient.OAUTH_TOKEN_URL,
|
||||||
|
client_secret=DISCORD_APP_SECRET,
|
||||||
|
code=authorization_code
|
||||||
|
)
|
||||||
|
logger.debug("Received token from OAuth")
|
||||||
|
return token['access_token']
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def server_name(cls):
|
||||||
|
"""returns the name of the Discord server"""
|
||||||
|
return cls._bot_client().guild_name(DISCORD_GUILD_ID)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _bot_client(is_rate_limited: bool = True):
|
||||||
|
"""returns a bot client for access to the Discord API"""
|
||||||
|
return DiscordClient(DISCORD_BOT_TOKEN, is_rate_limited=is_rate_limited)
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
# Generated by Django 2.2.12 on 2020-05-10 19:59
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('discord', '0002_service_permissions'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='discorduser',
|
||||||
|
name='activated',
|
||||||
|
field=models.DateTimeField(blank=True, default=None, help_text='Date & time this service account was activated', null=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='discorduser',
|
||||||
|
name='discriminator',
|
||||||
|
field=models.CharField(blank=True, default='', help_text="user's discriminator on Discord", max_length=4),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='discorduser',
|
||||||
|
name='username',
|
||||||
|
field=models.CharField(blank=True, db_index=True, default='', help_text="user's username on Discord", max_length=32),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='discorduser',
|
||||||
|
name='uid',
|
||||||
|
field=models.BigIntegerField(db_index=True, help_text="user's ID on Discord"),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='discorduser',
|
||||||
|
name='user',
|
||||||
|
field=models.OneToOneField(help_text='Auth user owning this Discord account', on_delete=django.db.models.deletion.CASCADE, primary_key=True, related_name='discord', serialize=False, to=settings.AUTH_USER_MODEL),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -1,18 +1,179 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from requests.exceptions import HTTPError
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils.translation import gettext_lazy
|
||||||
|
|
||||||
|
from allianceauth.notifications import notify
|
||||||
|
|
||||||
|
from . import __title__
|
||||||
|
from .app_settings import DISCORD_GUILD_ID
|
||||||
|
from .discord_client import DiscordClient, DiscordApiBackoff
|
||||||
|
from .managers import DiscordUserManager
|
||||||
|
from .utils import LoggerAddTag
|
||||||
|
|
||||||
|
|
||||||
|
logger = LoggerAddTag(logging.getLogger(__name__), __title__)
|
||||||
|
|
||||||
|
|
||||||
class DiscordUser(models.Model):
|
class DiscordUser(models.Model):
|
||||||
user = models.OneToOneField(User,
|
|
||||||
primary_key=True,
|
|
||||||
on_delete=models.CASCADE,
|
|
||||||
related_name='discord')
|
|
||||||
uid = models.CharField(max_length=254)
|
|
||||||
|
|
||||||
def __str__(self):
|
USER_RELATED_NAME = 'discord'
|
||||||
return "{} - {}".format(self.user.username, self.uid)
|
|
||||||
|
user = models.OneToOneField(
|
||||||
|
User,
|
||||||
|
primary_key=True,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name=USER_RELATED_NAME,
|
||||||
|
help_text='Auth user owning this Discord account'
|
||||||
|
)
|
||||||
|
uid = models.BigIntegerField(
|
||||||
|
db_index=True,
|
||||||
|
help_text='user\'s ID on Discord'
|
||||||
|
)
|
||||||
|
username = models.CharField(
|
||||||
|
max_length=32,
|
||||||
|
default='',
|
||||||
|
blank=True,
|
||||||
|
db_index=True,
|
||||||
|
help_text='user\'s username on Discord'
|
||||||
|
)
|
||||||
|
discriminator = models.CharField(
|
||||||
|
max_length=4,
|
||||||
|
default='',
|
||||||
|
blank=True,
|
||||||
|
help_text='user\'s discriminator on Discord'
|
||||||
|
)
|
||||||
|
activated = models.DateTimeField(
|
||||||
|
default=None,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
help_text='Date & time this service account was activated'
|
||||||
|
)
|
||||||
|
|
||||||
|
objects = DiscordUserManager()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
permissions = (
|
permissions = (
|
||||||
("access_discord", u"Can access the Discord service"),
|
("access_discord", "Can access the Discord service"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'{self.user.username} - {self.uid}'
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'{type(self).__name__}(user=\'{self.user}\', uid={self.uid})'
|
||||||
|
|
||||||
|
def update_nickname(self) -> bool:
|
||||||
|
"""Update nickname with formatted name of main character
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- True on success
|
||||||
|
- None if user is no longer a member of the Discord server
|
||||||
|
- False on error or raises exception
|
||||||
|
"""
|
||||||
|
requested_nick = DiscordUser.objects.user_formatted_nick(self.user)
|
||||||
|
if requested_nick:
|
||||||
|
client = DiscordUser.objects._bot_client()
|
||||||
|
success = client.modify_guild_member(
|
||||||
|
guild_id=DISCORD_GUILD_ID,
|
||||||
|
user_id=self.uid,
|
||||||
|
nick=requested_nick
|
||||||
|
)
|
||||||
|
if success:
|
||||||
|
logger.info('Nickname for %s has been updated', self.user)
|
||||||
|
else:
|
||||||
|
logger.warning('Failed to update nickname for %s', self.user)
|
||||||
|
return success
|
||||||
|
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def update_groups(self) -> bool:
|
||||||
|
"""update groups for a user based on his current group memberships.
|
||||||
|
Will add or remove roles of a user as needed.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- True on success
|
||||||
|
- None if user is no longer a member of the Discord server
|
||||||
|
- False on error or raises exception
|
||||||
|
"""
|
||||||
|
role_names = DiscordUser.objects.user_group_names(self.user)
|
||||||
|
client = DiscordUser.objects._bot_client()
|
||||||
|
requested_role_ids = self._guild_get_or_create_role_ids(client, role_names)
|
||||||
|
logger.debug(
|
||||||
|
'Requested to update groups for user %s: %s', self.user, requested_role_ids
|
||||||
|
)
|
||||||
|
success = client.modify_guild_member(
|
||||||
|
guild_id=DISCORD_GUILD_ID,
|
||||||
|
user_id=self.uid,
|
||||||
|
role_ids=requested_role_ids
|
||||||
|
)
|
||||||
|
if success:
|
||||||
|
logger.info('Groups for %s have been updated', self.user)
|
||||||
|
else:
|
||||||
|
logger.warning('Failed to update groups for %s', self.user)
|
||||||
|
return success
|
||||||
|
|
||||||
|
def delete_user(
|
||||||
|
self, notify_user: bool = False, is_rate_limited: bool = True
|
||||||
|
) -> bool:
|
||||||
|
"""Deletes the Discount user both on the server and locally
|
||||||
|
|
||||||
|
Params:
|
||||||
|
- notify_user: When True will sent a notification to the user
|
||||||
|
informing him about the deleting of his account
|
||||||
|
- is_rate_limited: When False will disable default rate limiting (use with care)
|
||||||
|
|
||||||
|
Returns True when successful, otherwise False or raises exceptions
|
||||||
|
Return None if user does no longer exist
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
client = DiscordUser.objects._bot_client(is_rate_limited=is_rate_limited)
|
||||||
|
success = client.remove_guild_member(
|
||||||
|
guild_id=DISCORD_GUILD_ID, user_id=self.uid
|
||||||
|
)
|
||||||
|
if success is not False:
|
||||||
|
deleted_count, _ = self.delete()
|
||||||
|
if deleted_count > 0:
|
||||||
|
if notify_user:
|
||||||
|
notify(
|
||||||
|
user=self.user,
|
||||||
|
title=gettext_lazy('Discord Account Disabled'),
|
||||||
|
message=gettext_lazy(
|
||||||
|
'Your Discord account was disabeled automatically '
|
||||||
|
'by Auth. If you think this was a mistake, '
|
||||||
|
'please contact an admin.'
|
||||||
|
),
|
||||||
|
level='warning'
|
||||||
|
)
|
||||||
|
logger.info('Account for user %s was deleted.', self.user)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logger.debug('Account for user %s was already deleted.', self.user)
|
||||||
|
return None
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
'Failed to remove user %s from the Discord server', self.user
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
except (HTTPError, ConnectionError, DiscordApiBackoff) as ex:
|
||||||
|
logger.exception(
|
||||||
|
'Failed to remove user %s from Discord server: %s', self.user, ex
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _guild_get_or_create_role_ids(client: DiscordClient, role_names: list) -> list:
|
||||||
|
"""wrapper for DiscordClient.match_guild_roles_to_names()
|
||||||
|
that only returns the list of IDs
|
||||||
|
"""
|
||||||
|
return [
|
||||||
|
x[0]['id'] for x in client.match_guild_roles_to_names(
|
||||||
|
guild_id=DISCORD_GUILD_ID, role_names=role_names
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|||||||
@@ -1,148 +1,187 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.conf import settings
|
from celery import shared_task, chain
|
||||||
from django.contrib.auth.models import User
|
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
|
||||||
from allianceauth.notifications import notify
|
|
||||||
from celery import shared_task
|
|
||||||
from requests.exceptions import HTTPError
|
from requests.exceptions import HTTPError
|
||||||
from allianceauth.services.hooks import NameFormatter
|
|
||||||
from .manager import DiscordOAuthManager, DiscordApiBackoff
|
from django.contrib.auth.models import User
|
||||||
from .models import DiscordUser
|
from django.db.models.query import QuerySet
|
||||||
|
|
||||||
from allianceauth.services.tasks import QueueOnce
|
from allianceauth.services.tasks import QueueOnce
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
from . import __title__
|
||||||
|
from .app_settings import (
|
||||||
|
DISCORD_TASKS_MAX_RETRIES, DISCORD_TASKS_RETRY_PAUSE, DISCORD_SYNC_NAMES
|
||||||
|
)
|
||||||
|
from .discord_client import DiscordApiBackoff
|
||||||
|
from .models import DiscordUser
|
||||||
|
from .utils import LoggerAddTag
|
||||||
|
|
||||||
|
|
||||||
class DiscordTasks:
|
logger = LoggerAddTag(logging.getLogger(__name__), __title__)
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@classmethod
|
# task priority of bulk tasks
|
||||||
def add_user(cls, user, code):
|
BULK_TASK_PRIORITY = 6
|
||||||
groups = DiscordTasks.get_groups(user)
|
|
||||||
nickname = None
|
|
||||||
if settings.DISCORD_SYNC_NAMES:
|
|
||||||
nickname = DiscordTasks.get_nickname(user)
|
|
||||||
user_id = DiscordOAuthManager.add_user(code, groups, nickname=nickname)
|
|
||||||
if user_id:
|
|
||||||
discord_user = DiscordUser()
|
|
||||||
discord_user.user = user
|
|
||||||
discord_user.uid = user_id
|
|
||||||
discord_user.save()
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def delete_user(cls, user, notify_user=False):
|
|
||||||
if cls.has_account(user):
|
|
||||||
logger.debug("User %s has discord account %s. Deleting." % (user, user.discord.uid))
|
|
||||||
if DiscordOAuthManager.delete_user(user.discord.uid):
|
|
||||||
user.discord.delete()
|
|
||||||
if notify_user:
|
|
||||||
notify(user, 'Discord Account Disabled', level='danger')
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
@classmethod
|
@shared_task(
|
||||||
def has_account(cls, user):
|
bind=True, name='discord.update_groups', base=QueueOnce, max_retries=None
|
||||||
"""
|
)
|
||||||
Check if the user has an account (has a DiscordUser record)
|
def update_groups(self, user_pk: int) -> None:
|
||||||
:param user: django.contrib.auth.models.User
|
"""Update roles on Discord for given user according to his current groups
|
||||||
:return: bool
|
|
||||||
"""
|
Params:
|
||||||
|
- user_pk: PK of given user
|
||||||
|
"""
|
||||||
|
_task_perform_user_action(self, user_pk, 'update_groups')
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task(
|
||||||
|
bind=True, name='discord.update_nickname', base=QueueOnce, max_retries=None
|
||||||
|
)
|
||||||
|
def update_nickname(self, user_pk: int) -> None:
|
||||||
|
"""Set nickname on Discord for given user to his main character name
|
||||||
|
|
||||||
|
Params:
|
||||||
|
- user_pk: PK of given user
|
||||||
|
"""
|
||||||
|
_task_perform_user_action(self, user_pk, 'update_nickname')
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task(
|
||||||
|
bind=True, name='discord.delete_user', base=QueueOnce, max_retries=None
|
||||||
|
)
|
||||||
|
def delete_user(self, user_pk: int, notify_user: bool = False) -> None:
|
||||||
|
"""Delete Discord user
|
||||||
|
|
||||||
|
Params:
|
||||||
|
- user_pk: PK of given user
|
||||||
|
"""
|
||||||
|
_task_perform_user_action(self, user_pk, 'delete_user', notify_user=notify_user)
|
||||||
|
|
||||||
|
|
||||||
|
def _task_perform_user_action(self, user_pk: int, method: str, **kwargs) -> None:
|
||||||
|
"""perform a user related action incl. managing all exceptions"""
|
||||||
|
logger.debug("Starting %s for user with pk %s", method, user_pk)
|
||||||
|
user = User.objects.get(pk=user_pk)
|
||||||
|
if DiscordUser.objects.user_has_account(user):
|
||||||
|
logger.info("Running %s for user %s", method, user)
|
||||||
try:
|
try:
|
||||||
user.discord
|
success = getattr(user.discord, method)(**kwargs)
|
||||||
except ObjectDoesNotExist:
|
|
||||||
return False
|
except DiscordApiBackoff as bo:
|
||||||
|
logger.info(
|
||||||
|
"API back off for %s wth user %s due to %r, retrying in %s seconds",
|
||||||
|
method,
|
||||||
|
user,
|
||||||
|
bo,
|
||||||
|
bo.retry_after_seconds
|
||||||
|
)
|
||||||
|
raise self.retry(countdown=bo.retry_after_seconds)
|
||||||
|
|
||||||
|
except AttributeError:
|
||||||
|
raise ValueError(f'{method} not a valid method for DiscordUser: %r')
|
||||||
|
|
||||||
|
except (HTTPError, ConnectionError):
|
||||||
|
logger.warning(
|
||||||
|
'%s failed for user %s, retrying in %d secs',
|
||||||
|
method,
|
||||||
|
user,
|
||||||
|
DISCORD_TASKS_RETRY_PAUSE,
|
||||||
|
exc_info=True
|
||||||
|
)
|
||||||
|
if self.request.retries < DISCORD_TASKS_MAX_RETRIES:
|
||||||
|
raise self.retry(countdown=DISCORD_TASKS_RETRY_PAUSE)
|
||||||
|
else:
|
||||||
|
logger.error(
|
||||||
|
'%s failed for user %s after max retries',
|
||||||
|
method,
|
||||||
|
user,
|
||||||
|
exc_info=True
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
logger.error(
|
||||||
|
'%s for %s failed due to unexpected exception',
|
||||||
|
method,
|
||||||
|
user,
|
||||||
|
exc_info=True
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return True
|
if success is None and method != 'delete_user':
|
||||||
|
delete_user.delay(user.pk, notify_user=True)
|
||||||
|
|
||||||
@staticmethod
|
else:
|
||||||
@shared_task(bind=True, name='discord.update_groups', base=QueueOnce)
|
logger.debug(
|
||||||
def update_groups(self, pk):
|
'User %s does not have a discord account, skipping %s', user, method
|
||||||
user = User.objects.get(pk=pk)
|
)
|
||||||
logger.debug("Updating discord groups for user %s" % user)
|
|
||||||
if DiscordTasks.has_account(user):
|
|
||||||
groups = DiscordTasks.get_groups(user)
|
|
||||||
logger.debug("Updating user %s discord groups to %s" % (user, groups))
|
|
||||||
try:
|
|
||||||
DiscordOAuthManager.update_groups(user.discord.uid, groups)
|
|
||||||
except DiscordApiBackoff as bo:
|
|
||||||
logger.info("Discord group sync API back off for %s, "
|
|
||||||
"retrying in %s seconds" % (user, bo.retry_after_seconds))
|
|
||||||
raise self.retry(countdown=bo.retry_after_seconds)
|
|
||||||
except HTTPError as e:
|
|
||||||
if e.response.status_code == 404:
|
|
||||||
try:
|
|
||||||
if e.response.json()['code'] == 10007:
|
|
||||||
# user has left the server
|
|
||||||
DiscordTasks.delete_user(user)
|
|
||||||
return
|
|
||||||
finally:
|
|
||||||
raise e
|
|
||||||
except Exception as e:
|
|
||||||
if self:
|
|
||||||
logger.exception("Discord group sync failed for %s, retrying in 10 mins" % user)
|
|
||||||
raise self.retry(countdown=60 * 10)
|
|
||||||
else:
|
|
||||||
# Rethrow
|
|
||||||
raise e
|
|
||||||
logger.debug("Updated user %s discord groups." % user)
|
|
||||||
else:
|
|
||||||
logger.debug("User does not have a discord account, skipping")
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@shared_task(name='discord.update_all_groups')
|
|
||||||
def update_all_groups():
|
|
||||||
logger.debug("Updating ALL discord groups")
|
|
||||||
for discord_user in DiscordUser.objects.exclude(uid__exact=''):
|
|
||||||
DiscordTasks.update_groups.delay(discord_user.user.pk)
|
|
||||||
|
|
||||||
@staticmethod
|
@shared_task(name='discord.update_all_groups')
|
||||||
@shared_task(bind=True, name='discord.update_nickname', base=QueueOnce)
|
def update_all_groups() -> None:
|
||||||
def update_nickname(self, pk):
|
"""Update roles for all known users with a Discord account."""
|
||||||
user = User.objects.get(pk=pk)
|
discord_users_qs = DiscordUser.objects.all()
|
||||||
logger.debug("Updating discord nickname for user %s" % user)
|
_bulk_update_groups_for_users(discord_users_qs)
|
||||||
if DiscordTasks.has_account(user):
|
|
||||||
if user.profile.main_character:
|
|
||||||
character = user.profile.main_character
|
|
||||||
logger.debug("Updating user %s discord nickname to %s" % (user, character.character_name))
|
|
||||||
try:
|
|
||||||
DiscordOAuthManager.update_nickname(user.discord.uid, DiscordTasks.get_nickname(user))
|
|
||||||
except DiscordApiBackoff as bo:
|
|
||||||
logger.info("Discord nickname update API back off for %s, "
|
|
||||||
"retrying in %s seconds" % (user, bo.retry_after_seconds))
|
|
||||||
raise self.retry(countdown=bo.retry_after_seconds)
|
|
||||||
except Exception as e:
|
|
||||||
if self:
|
|
||||||
logger.exception("Discord nickname sync failed for %s, retrying in 10 mins" % user)
|
|
||||||
raise self.retry(countdown=60 * 10)
|
|
||||||
else:
|
|
||||||
# Rethrow
|
|
||||||
raise e
|
|
||||||
logger.debug("Updated user %s discord nickname." % user)
|
|
||||||
else:
|
|
||||||
logger.debug("User %s does not have a main character" % user)
|
|
||||||
else:
|
|
||||||
logger.debug("User %s does not have a discord account" % user)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@shared_task(name='discord.update_all_nicknames')
|
|
||||||
def update_all_nicknames():
|
|
||||||
logger.debug("Updating ALL discord nicknames")
|
|
||||||
for discord_user in DiscordUser.objects.exclude(uid__exact=''):
|
|
||||||
DiscordTasks.update_nickname.delay(discord_user.user.pk)
|
|
||||||
|
|
||||||
@classmethod
|
@shared_task(name='discord.update_groups_bulk')
|
||||||
def disable(cls):
|
def update_groups_bulk(user_pks: list) -> None:
|
||||||
DiscordUser.objects.all().delete()
|
"""Update roles for list of users with a Discord account in bulk."""
|
||||||
|
discord_users_qs = DiscordUser.objects\
|
||||||
|
.filter(user__pk__in=user_pks)\
|
||||||
|
.select_related()
|
||||||
|
_bulk_update_groups_for_users(discord_users_qs)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_nickname(user):
|
|
||||||
from .auth_hooks import DiscordService
|
|
||||||
return NameFormatter(DiscordService(), user).format_name()
|
|
||||||
|
|
||||||
@staticmethod
|
def _bulk_update_groups_for_users(discord_users_qs: QuerySet) -> None:
|
||||||
def get_groups(user):
|
logger.info(
|
||||||
return [g.name for g in user.groups.all()] + [user.profile.state.name]
|
"Starting to bulk update discord roles for %d users", discord_users_qs.count()
|
||||||
|
)
|
||||||
|
update_groups_chain = list()
|
||||||
|
for discord_user in discord_users_qs:
|
||||||
|
update_groups_chain.append(update_groups.si(discord_user.user.pk))
|
||||||
|
|
||||||
|
chain(update_groups_chain).apply_async(priority=BULK_TASK_PRIORITY)
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task(name='discord.update_all_nicknames')
|
||||||
|
def update_all_nicknames() -> None:
|
||||||
|
"""Update nicknames for all known users with a Discord account."""
|
||||||
|
discord_users_qs = DiscordUser.objects.all()
|
||||||
|
_bulk_update_nicknames_for_users(discord_users_qs)
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task(name='discord.update_nicknames_bulk')
|
||||||
|
def update_nicknames_bulk(user_pks: list) -> None:
|
||||||
|
"""Update nicknames for list of users with a Discord account in bulk."""
|
||||||
|
discord_users_qs = DiscordUser.objects\
|
||||||
|
.filter(user__pk__in=user_pks)\
|
||||||
|
.select_related()
|
||||||
|
_bulk_update_nicknames_for_users(discord_users_qs)
|
||||||
|
|
||||||
|
|
||||||
|
def _bulk_update_nicknames_for_users(discord_users_qs: QuerySet) -> None:
|
||||||
|
logger.info(
|
||||||
|
"Starting to bulk update discord nicknames for %d users",
|
||||||
|
discord_users_qs.count()
|
||||||
|
)
|
||||||
|
update_nicknames_chain = list()
|
||||||
|
for discord_user in discord_users_qs:
|
||||||
|
update_nicknames_chain.append(update_nickname.si(discord_user.user.pk))
|
||||||
|
|
||||||
|
chain(update_nicknames_chain).apply_async(priority=BULK_TASK_PRIORITY)
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task(name='discord.update_all')
|
||||||
|
def update_all() -> None:
|
||||||
|
"""Updates groups and nicknames (when activated) for all users."""
|
||||||
|
discord_users_qs = DiscordUser.objects.all()
|
||||||
|
logger.info(
|
||||||
|
'Starting to bulk update all %s Discord users', discord_users_qs.count()
|
||||||
|
)
|
||||||
|
update_all_chain = list()
|
||||||
|
for discord_user in discord_users_qs:
|
||||||
|
update_all_chain.append(update_groups.si(discord_user.user.pk))
|
||||||
|
if DISCORD_SYNC_NAMES:
|
||||||
|
update_all_chain.append(update_nickname.si(discord_user.user.pk))
|
||||||
|
|
||||||
|
chain(update_all_chain).apply_async(priority=BULK_TASK_PRIORITY)
|
||||||
|
|||||||
@@ -3,10 +3,18 @@
|
|||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td class="text-center">Discord</td>
|
<td class="text-center">Discord</td>
|
||||||
<td class="text-center"></td>
|
|
||||||
<td class="text-center"><a href="https://discordapp.com/channels/{{ DISCORD_SERVER_ID }}/{{ DISCORD_SERVER_ID}}">https://discordapp.com</a></td>
|
|
||||||
<td class="text-center">
|
<td class="text-center">
|
||||||
{% if not discord_uid %}
|
{% if not user_has_account %}
|
||||||
|
(not activated)
|
||||||
|
{% else %}
|
||||||
|
{{discord_username}}
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
<td class="text-center">
|
||||||
|
{{server_name}}
|
||||||
|
</td>
|
||||||
|
<td class="text-center">
|
||||||
|
{% if not user_has_account %}
|
||||||
<a href="{% url 'discord:activate' %}" title="Activate" class="btn btn-warning">
|
<a href="{% url 'discord:activate' %}" title="Activate" class="btn btn-warning">
|
||||||
<span class="glyphicon glyphicon-ok"></span>
|
<span class="glyphicon glyphicon-ok"></span>
|
||||||
</a>
|
</a>
|
||||||
@@ -20,7 +28,9 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
{% if request.user.is_superuser %}
|
{% if request.user.is_superuser %}
|
||||||
<div class="text-center" style="padding-top:5px;">
|
<div class="text-center" style="padding-top:5px;">
|
||||||
<a type="button" class="btn btn-success" href="{% url 'discord:add_bot' %}">{% trans "Link Discord Server" %}</a>
|
<a type="button" class="btn btn-success" href="{% url 'discord:add_bot' %}">
|
||||||
|
{% trans "Link Discord Server" %}
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
from django.contrib.auth.models import User, Group, Permission
|
from django.contrib.auth.models import Group, Permission
|
||||||
from allianceauth.tests.auth_utils import AuthUtils
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
DEFAULT_AUTH_GROUP = 'Member'
|
DEFAULT_AUTH_GROUP = 'Member'
|
||||||
MODULE_PATH = 'allianceauth.services.modules.discord'
|
MODULE_PATH = 'allianceauth.services.modules.discord'
|
||||||
|
|
||||||
def add_permissions():
|
TEST_GUILD_ID = 123456789012345678
|
||||||
|
TEST_USER_ID = 198765432012345678
|
||||||
|
TEST_USER_NAME = 'Peter Parker'
|
||||||
|
TEST_MAIN_NAME = 'Spiderman'
|
||||||
|
TEST_MAIN_ID = 1005
|
||||||
|
|
||||||
|
|
||||||
|
def add_permissions_to_members():
|
||||||
permission = Permission.objects.get(codename='access_discord')
|
permission = Permission.objects.get(codename='access_discord')
|
||||||
members = Group.objects.get_or_create(name=DEFAULT_AUTH_GROUP)[0]
|
members = Group.objects.get_or_create(name=DEFAULT_AUTH_GROUP)[0]
|
||||||
AuthUtils.add_permissions_to_groups([permission], [members])
|
AuthUtils.add_permissions_to_groups([permission], [members])
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
from django.test import TestCase, RequestFactory
|
from django.test import TestCase, RequestFactory
|
||||||
from django.contrib import admin
|
|
||||||
from django.contrib.admin.sites import AdminSite
|
from django.contrib.admin.sites import AdminSite
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.utils.timezone import now
|
||||||
|
|
||||||
from allianceauth.authentication.models import CharacterOwnership
|
from allianceauth.authentication.models import CharacterOwnership
|
||||||
from allianceauth.eveonline.models import (
|
from allianceauth.eveonline.models import (
|
||||||
@@ -18,17 +16,21 @@ from ....admin import (
|
|||||||
MainCorporationsFilter,
|
MainCorporationsFilter,
|
||||||
MainAllianceFilter
|
MainAllianceFilter
|
||||||
)
|
)
|
||||||
from ..admin import (
|
from ..admin import DiscordUserAdmin
|
||||||
DiscordUser,
|
from ..models import DiscordUser
|
||||||
DiscordUserAdmin
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestDiscordUserAdmin(TestCase):
|
class TestDataMixin(TestCase):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
super().setUpClass()
|
super().setUpClass()
|
||||||
|
|
||||||
|
EveCharacter.objects.all().delete()
|
||||||
|
EveCorporationInfo.objects.all().delete()
|
||||||
|
EveAllianceInfo.objects.all().delete()
|
||||||
|
User.objects.all().delete()
|
||||||
|
DiscordUser.objects.all().delete()
|
||||||
|
|
||||||
# user 1 - corp and alliance, normal user
|
# user 1 - corp and alliance, normal user
|
||||||
cls.character_1 = EveCharacter.objects.create(
|
cls.character_1 = EveCharacter.objects.create(
|
||||||
@@ -83,7 +85,10 @@ class TestDiscordUserAdmin(TestCase):
|
|||||||
cls.user_1.profile.save()
|
cls.user_1.profile.save()
|
||||||
DiscordUser.objects.create(
|
DiscordUser.objects.create(
|
||||||
user=cls.user_1,
|
user=cls.user_1,
|
||||||
uid=1001
|
uid=1001,
|
||||||
|
username='Bruce Wayne',
|
||||||
|
discriminator='1234',
|
||||||
|
activated=now()
|
||||||
)
|
)
|
||||||
|
|
||||||
# user 2 - corp only, staff
|
# user 2 - corp only, staff
|
||||||
@@ -156,18 +161,20 @@ class TestDiscordUserAdmin(TestCase):
|
|||||||
uid=1003
|
uid=1003
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
self.modeladmin = DiscordUserAdmin(
|
self.modeladmin = DiscordUserAdmin(
|
||||||
model=DiscordUser, admin_site=AdminSite()
|
model=DiscordUser, admin_site=AdminSite()
|
||||||
)
|
)
|
||||||
|
|
||||||
# column rendering
|
|
||||||
|
class TestColumnRendering(TestDataMixin, TestCase):
|
||||||
|
|
||||||
def test_user_profile_pic_u1(self):
|
def test_user_profile_pic_u1(self):
|
||||||
expected = ('<img src="https://images.evetech.net/characters/1001/'
|
expected = (
|
||||||
'portrait?size=32" class="img-circle">')
|
'<img src="https://images.evetech.net/characters/1001/'
|
||||||
|
'portrait?size=32" class="img-circle">'
|
||||||
|
)
|
||||||
self.assertEqual(user_profile_pic(self.user_1.discord), expected)
|
self.assertEqual(user_profile_pic(self.user_1.discord), expected)
|
||||||
|
|
||||||
def test_user_profile_pic_u3(self):
|
def test_user_profile_pic_u3(self):
|
||||||
@@ -204,9 +211,26 @@ class TestDiscordUserAdmin(TestCase):
|
|||||||
result = user_main_organization(self.user_3.discord)
|
result = user_main_organization(self.user_3.discord)
|
||||||
self.assertEqual(result, expected)
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
|
def test_uid(self):
|
||||||
|
expected = 1001
|
||||||
|
result = self.modeladmin._uid(self.user_1.discord)
|
||||||
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
|
def test_username_when_defined(self):
|
||||||
|
expected = 'Bruce Wayne#1234'
|
||||||
|
result = self.modeladmin._username(self.user_1.discord)
|
||||||
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
|
def test_username_when_not_defined(self):
|
||||||
|
expected = ''
|
||||||
|
result = self.modeladmin._username(self.user_2.discord)
|
||||||
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
# actions
|
# actions
|
||||||
|
|
||||||
# filters
|
|
||||||
|
class TestFilters(TestDataMixin, TestCase):
|
||||||
|
|
||||||
def test_filter_main_corporations(self):
|
def test_filter_main_corporations(self):
|
||||||
|
|
||||||
class DiscordUserAdminTest(ServicesUserAdmin):
|
class DiscordUserAdminTest(ServicesUserAdmin):
|
||||||
@@ -228,8 +252,7 @@ class TestDiscordUserAdmin(TestCase):
|
|||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
request = self.factory.get(
|
request = self.factory.get(
|
||||||
'/',
|
'/', {'main_corporation_id__exact': self.character_1.corporation_id}
|
||||||
{'main_corporation_id__exact': self.character_1.corporation_id}
|
|
||||||
)
|
)
|
||||||
request.user = self.user_1
|
request.user = self.user_1
|
||||||
changelist = my_modeladmin.get_changelist_instance(request)
|
changelist = my_modeladmin.get_changelist_instance(request)
|
||||||
@@ -250,19 +273,17 @@ class TestDiscordUserAdmin(TestCase):
|
|||||||
changelist = my_modeladmin.get_changelist_instance(request)
|
changelist = my_modeladmin.get_changelist_instance(request)
|
||||||
filters = changelist.get_filters(request)
|
filters = changelist.get_filters(request)
|
||||||
filterspec = filters[0][0]
|
filterspec = filters[0][0]
|
||||||
expected = [
|
expected = [
|
||||||
('3001', 'Wayne Enterprises'),
|
('3001', 'Wayne Enterprises'),
|
||||||
]
|
]
|
||||||
self.assertEqual(filterspec.lookup_choices, expected)
|
self.assertEqual(filterspec.lookup_choices, expected)
|
||||||
|
|
||||||
# Make sure the correct queryset is returned
|
# Make sure the correct queryset is returned
|
||||||
request = self.factory.get(
|
request = self.factory.get(
|
||||||
'/',
|
'/', {'main_alliance_id__exact': self.character_1.alliance_id}
|
||||||
{'main_alliance_id__exact': self.character_1.alliance_id}
|
|
||||||
)
|
)
|
||||||
request.user = self.user_1
|
request.user = self.user_1
|
||||||
changelist = my_modeladmin.get_changelist_instance(request)
|
changelist = my_modeladmin.get_changelist_instance(request)
|
||||||
queryset = changelist.get_queryset(request)
|
queryset = changelist.get_queryset(request)
|
||||||
expected = [self.user_1.discord]
|
expected = [self.user_1.discord]
|
||||||
self.assertSetEqual(set(queryset), set(expected))
|
self.assertSetEqual(set(queryset), set(expected))
|
||||||
|
|
||||||
140
allianceauth/services/modules/discord/tests/test_auth_hooks.py
Normal file
140
allianceauth/services/modules/discord/tests/test_auth_hooks.py
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from django.test import TestCase, RequestFactory
|
||||||
|
from django.test.utils import override_settings
|
||||||
|
|
||||||
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
|
from . import TEST_USER_NAME, TEST_USER_ID, add_permissions_to_members, MODULE_PATH
|
||||||
|
from ..auth_hooks import DiscordService
|
||||||
|
from ..models import DiscordUser, DiscordClient
|
||||||
|
from ..utils import set_logger_to_file
|
||||||
|
|
||||||
|
|
||||||
|
logger = set_logger_to_file(MODULE_PATH + '.auth_hooks', __file__)
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(CELERY_ALWAYS_EAGER=True)
|
||||||
|
class TestDiscordService(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.member = AuthUtils.create_member(TEST_USER_NAME)
|
||||||
|
DiscordUser.objects.create(
|
||||||
|
user=self.member,
|
||||||
|
uid=TEST_USER_ID,
|
||||||
|
username=TEST_USER_NAME,
|
||||||
|
discriminator='1234'
|
||||||
|
)
|
||||||
|
self.none_member = AuthUtils.create_user('Lex Luther')
|
||||||
|
self.service = DiscordService
|
||||||
|
add_permissions_to_members()
|
||||||
|
self.factory = RequestFactory()
|
||||||
|
|
||||||
|
def test_service_enabled(self):
|
||||||
|
service = self.service()
|
||||||
|
self.assertTrue(service.service_active_for_user(self.member))
|
||||||
|
self.assertFalse(service.service_active_for_user(self.none_member))
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.tasks.update_all_groups')
|
||||||
|
def test_update_all_groups(self, mock_update_all_groups):
|
||||||
|
service = self.service()
|
||||||
|
service.update_all_groups()
|
||||||
|
self.assertTrue(mock_update_all_groups.delay.called)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.tasks.update_groups_bulk')
|
||||||
|
def test_update_groups_bulk(self, mock_update_groups_bulk):
|
||||||
|
service = self.service()
|
||||||
|
service.update_groups_bulk([self.member])
|
||||||
|
self.assertTrue(mock_update_groups_bulk.delay.called)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.tasks.update_groups')
|
||||||
|
def test_update_groups_for_member(self, mock_update_groups):
|
||||||
|
service = self.service()
|
||||||
|
service.update_groups(self.member)
|
||||||
|
self.assertTrue(mock_update_groups.apply_async.called)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.tasks.update_groups')
|
||||||
|
def test_update_groups_for_none_member(self, mock_update_groups):
|
||||||
|
service = self.service()
|
||||||
|
service.update_groups(self.none_member)
|
||||||
|
self.assertFalse(mock_update_groups.apply_async.called)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.models.notify')
|
||||||
|
@patch(MODULE_PATH + '.tasks.DiscordUser')
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
def test_validate_user(
|
||||||
|
self, mock_DiscordClient, mock_DiscordUser, mock_notify
|
||||||
|
):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = True
|
||||||
|
|
||||||
|
# Test member is not deleted
|
||||||
|
service = self.service()
|
||||||
|
service.validate_user(self.member)
|
||||||
|
self.assertTrue(DiscordUser.objects.filter(user=self.member).exists())
|
||||||
|
|
||||||
|
# Test none member is deleted
|
||||||
|
DiscordUser.objects.create(user=self.none_member, uid=TEST_USER_ID)
|
||||||
|
service.validate_user(self.none_member)
|
||||||
|
self.assertFalse(DiscordUser.objects.filter(user=self.none_member).exists())
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.tasks.update_nickname')
|
||||||
|
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_nicknames_bulk')
|
||||||
|
def test_sync_nicknames_bulk(self, mock_update_nicknames_bulk):
|
||||||
|
service = self.service()
|
||||||
|
service.sync_nicknames_bulk([self.member])
|
||||||
|
self.assertTrue(mock_update_nicknames_bulk.delay.called)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
def test_delete_user_is_member(self, mock_DiscordClient):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = True
|
||||||
|
|
||||||
|
service = self.service()
|
||||||
|
service.delete_user(self.member)
|
||||||
|
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.remove_guild_member.called)
|
||||||
|
self.assertFalse(DiscordUser.objects.filter(user=self.member).exists())
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
def test_delete_user_is_not_member(self, mock_DiscordClient):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = True
|
||||||
|
|
||||||
|
service = self.service()
|
||||||
|
service.delete_user(self.none_member)
|
||||||
|
|
||||||
|
self.assertFalse(mock_DiscordClient.return_value.remove_guild_member.called)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
def test_render_services_ctrl_with_username(self, mock_DiscordClient):
|
||||||
|
service = self.service()
|
||||||
|
request = self.factory.get('/services/')
|
||||||
|
request.user = self.member
|
||||||
|
|
||||||
|
response = service.render_services_ctrl(request)
|
||||||
|
self.assertTemplateUsed(service.service_ctrl_template)
|
||||||
|
self.assertIn('/discord/reset/', response)
|
||||||
|
self.assertIn('/discord/deactivate/', response)
|
||||||
|
|
||||||
|
# Test register becomes available
|
||||||
|
self.member.discord.delete()
|
||||||
|
self.member.refresh_from_db()
|
||||||
|
request.user = self.member
|
||||||
|
response = service.render_services_ctrl(request)
|
||||||
|
self.assertIn('/discord/activate/', response)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
def test_render_services_ctrl_wo_username(self, mock_DiscordClient):
|
||||||
|
my_member = AuthUtils.create_member('John Doe')
|
||||||
|
DiscordUser.objects.create(user=my_member, uid=111222333)
|
||||||
|
service = self.service()
|
||||||
|
request = self.factory.get('/services/')
|
||||||
|
request.user = my_member
|
||||||
|
|
||||||
|
response = service.render_services_ctrl(request)
|
||||||
|
self.assertTemplateUsed(service.service_ctrl_template)
|
||||||
|
self.assertIn('/discord/reset/', response)
|
||||||
|
self.assertIn('/discord/deactivate/', response)
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
from unittest import mock
|
|
||||||
|
|
||||||
from django.test import TestCase, RequestFactory
|
|
||||||
from django.contrib.auth.models import User
|
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
|
||||||
from allianceauth.tests.auth_utils import AuthUtils
|
|
||||||
|
|
||||||
from ..auth_hooks import DiscordService
|
|
||||||
from ..models import DiscordUser
|
|
||||||
from ..tasks import DiscordTasks
|
|
||||||
from ..manager import DiscordOAuthManager
|
|
||||||
|
|
||||||
from . import DEFAULT_AUTH_GROUP, add_permissions, MODULE_PATH
|
|
||||||
|
|
||||||
|
|
||||||
class DiscordHooksTestCase(TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
self.member = 'member_user'
|
|
||||||
member = AuthUtils.create_member(self.member)
|
|
||||||
DiscordUser.objects.create(user=member, uid='12345')
|
|
||||||
self.none_user = 'none_user'
|
|
||||||
none_user = AuthUtils.create_user(self.none_user)
|
|
||||||
self.service = DiscordService
|
|
||||||
add_permissions()
|
|
||||||
|
|
||||||
def test_has_account(self):
|
|
||||||
member = User.objects.get(username=self.member)
|
|
||||||
none_user = User.objects.get(username=self.none_user)
|
|
||||||
self.assertTrue(DiscordTasks.has_account(member))
|
|
||||||
self.assertFalse(DiscordTasks.has_account(none_user))
|
|
||||||
|
|
||||||
def test_service_enabled(self):
|
|
||||||
service = self.service()
|
|
||||||
member = User.objects.get(username=self.member)
|
|
||||||
none_user = User.objects.get(username=self.none_user)
|
|
||||||
|
|
||||||
self.assertTrue(service.service_active_for_user(member))
|
|
||||||
self.assertFalse(service.service_active_for_user(none_user))
|
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager')
|
|
||||||
def test_update_all_groups(self, manager):
|
|
||||||
service = self.service()
|
|
||||||
service.update_all_groups()
|
|
||||||
# Check member and blue user have groups updated
|
|
||||||
self.assertTrue(manager.update_groups.called)
|
|
||||||
self.assertEqual(manager.update_groups.call_count, 1)
|
|
||||||
|
|
||||||
def test_update_groups(self):
|
|
||||||
# Check member has Member group updated
|
|
||||||
with mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager') as manager:
|
|
||||||
service = self.service()
|
|
||||||
member = User.objects.get(username=self.member)
|
|
||||||
AuthUtils.disconnect_signals()
|
|
||||||
service.update_groups(member)
|
|
||||||
self.assertTrue(manager.update_groups.called)
|
|
||||||
args, kwargs = manager.update_groups.call_args
|
|
||||||
user_id, groups = args
|
|
||||||
self.assertIn(DEFAULT_AUTH_GROUP, groups)
|
|
||||||
self.assertEqual(user_id, member.discord.uid)
|
|
||||||
|
|
||||||
# Check none user does not have groups updated
|
|
||||||
with mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager') as manager:
|
|
||||||
service = self.service()
|
|
||||||
none_user = User.objects.get(username=self.none_user)
|
|
||||||
service.update_groups(none_user)
|
|
||||||
self.assertFalse(manager.update_groups.called)
|
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager')
|
|
||||||
def test_validate_user(self, manager):
|
|
||||||
service = self.service()
|
|
||||||
# Test member is not deleted
|
|
||||||
member = User.objects.get(username=self.member)
|
|
||||||
service.validate_user(member)
|
|
||||||
self.assertTrue(member.discord)
|
|
||||||
|
|
||||||
# Test none user is deleted
|
|
||||||
none_user = User.objects.get(username=self.none_user)
|
|
||||||
DiscordUser.objects.create(user=none_user, uid='abc123')
|
|
||||||
service.validate_user(none_user)
|
|
||||||
self.assertTrue(manager.delete_user.called)
|
|
||||||
with self.assertRaises(ObjectDoesNotExist):
|
|
||||||
none_discord = User.objects.get(username=self.none_user).discord
|
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager')
|
|
||||||
def test_sync_nickname(self, manager):
|
|
||||||
service = self.service()
|
|
||||||
member = User.objects.get(username=self.member)
|
|
||||||
AuthUtils.add_main_character(member, 'test user', '12345', corp_ticker='AAUTH')
|
|
||||||
|
|
||||||
service.sync_nickname(member)
|
|
||||||
|
|
||||||
self.assertTrue(manager.update_nickname.called)
|
|
||||||
args, kwargs = manager.update_nickname.call_args
|
|
||||||
self.assertEqual(args[0], member.discord.uid)
|
|
||||||
self.assertEqual(args[1], 'test user')
|
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager')
|
|
||||||
def test_delete_user(self, manager):
|
|
||||||
member = User.objects.get(username=self.member)
|
|
||||||
|
|
||||||
service = self.service()
|
|
||||||
result = service.delete_user(member)
|
|
||||||
|
|
||||||
self.assertTrue(result)
|
|
||||||
self.assertTrue(manager.delete_user.called)
|
|
||||||
with self.assertRaises(ObjectDoesNotExist):
|
|
||||||
discord_user = User.objects.get(username=self.member).discord
|
|
||||||
|
|
||||||
def test_render_services_ctrl(self):
|
|
||||||
service = self.service()
|
|
||||||
member = User.objects.get(username=self.member)
|
|
||||||
request = RequestFactory().get('/services/')
|
|
||||||
request.user = member
|
|
||||||
|
|
||||||
response = service.render_services_ctrl(request)
|
|
||||||
self.assertTemplateUsed(service.service_ctrl_template)
|
|
||||||
self.assertIn('/discord/reset/', response)
|
|
||||||
self.assertIn('/discord/deactivate/', response)
|
|
||||||
|
|
||||||
# Test register becomes available
|
|
||||||
member.discord.delete()
|
|
||||||
member = User.objects.get(username=self.member)
|
|
||||||
request.user = member
|
|
||||||
response = service.render_services_ctrl(request)
|
|
||||||
self.assertIn('/discord/activate/', response)
|
|
||||||
|
|
||||||
# TODO: Test update nicknames
|
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
from django_webtest import WebTest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from django.shortcuts import reverse
|
||||||
|
|
||||||
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
|
from . import (
|
||||||
|
add_permissions_to_members,
|
||||||
|
MODULE_PATH,
|
||||||
|
TEST_USER_NAME,
|
||||||
|
TEST_MAIN_NAME,
|
||||||
|
TEST_MAIN_ID
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestServiceUserActivation(WebTest):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.member = AuthUtils.create_member(TEST_USER_NAME)
|
||||||
|
AuthUtils.add_main_character_2(
|
||||||
|
self.member,
|
||||||
|
TEST_MAIN_NAME,
|
||||||
|
TEST_MAIN_ID,
|
||||||
|
disconnect_signals=True
|
||||||
|
)
|
||||||
|
add_permissions_to_members()
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.views.messages')
|
||||||
|
@patch(MODULE_PATH + '.models.DiscordUser.objects.add_user')
|
||||||
|
@patch(MODULE_PATH + '.managers.OAuth2Session')
|
||||||
|
def test_user_activation(
|
||||||
|
self, mock_OAuth2Session, mock_add_user, mock_messages
|
||||||
|
):
|
||||||
|
authentication_code = 'auth_code'
|
||||||
|
mock_add_user.return_value = True
|
||||||
|
oauth_url = 'https://www.example.com/oauth'
|
||||||
|
state = ''
|
||||||
|
mock_OAuth2Session.return_value.authorization_url.return_value = \
|
||||||
|
oauth_url, state
|
||||||
|
|
||||||
|
# login
|
||||||
|
self.app.set_user(self.member)
|
||||||
|
|
||||||
|
# click activate on the service page
|
||||||
|
response = self.app.get(reverse('discord:activate'))
|
||||||
|
|
||||||
|
# check we got a redirect to Discord OAuth
|
||||||
|
self.assertRedirects(
|
||||||
|
response, expected_url=oauth_url, fetch_redirect_response=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# simulate Discord callback
|
||||||
|
response = self.app.get(
|
||||||
|
reverse('discord:callback'), params={'code': authentication_code}
|
||||||
|
)
|
||||||
|
|
||||||
|
# user was added to Discord
|
||||||
|
self.assertTrue(mock_add_user.called)
|
||||||
|
|
||||||
|
# user got a success message
|
||||||
|
self.assertTrue(mock_messages.success.called)
|
||||||
@@ -1,244 +1,356 @@
|
|||||||
import json
|
from unittest.mock import patch, Mock
|
||||||
import urllib
|
import urllib
|
||||||
import datetime
|
|
||||||
import requests_mock
|
|
||||||
from unittest import mock
|
|
||||||
|
|
||||||
|
from requests.exceptions import HTTPError
|
||||||
|
|
||||||
|
from django.contrib.auth.models import Group, User
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
from ..manager import DiscordOAuthManager
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
from .. import manager
|
|
||||||
|
|
||||||
from . import DEFAULT_AUTH_GROUP, add_permissions, MODULE_PATH
|
from . import (
|
||||||
|
TEST_GUILD_ID,
|
||||||
|
TEST_USER_NAME,
|
||||||
|
TEST_USER_ID,
|
||||||
|
TEST_MAIN_NAME,
|
||||||
|
TEST_MAIN_ID,
|
||||||
|
MODULE_PATH
|
||||||
|
)
|
||||||
|
from ..app_settings import (
|
||||||
|
DISCORD_APP_ID,
|
||||||
|
DISCORD_APP_SECRET,
|
||||||
|
DISCORD_CALLBACK_URL,
|
||||||
|
)
|
||||||
|
from ..discord_client import DiscordClient, DiscordApiBackoff
|
||||||
|
from ..models import DiscordUser
|
||||||
|
from ..utils import set_logger_to_file
|
||||||
|
|
||||||
|
|
||||||
class DiscordManagerTestCase(TestCase):
|
logger = set_logger_to_file(MODULE_PATH + '.managers', __file__)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DISCORD_GUILD_ID', TEST_GUILD_ID)
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
@patch(MODULE_PATH + '.models.DiscordUser.objects._exchange_auth_code_for_token')
|
||||||
|
@patch(MODULE_PATH + '.models.DiscordUser.objects.model._guild_get_or_create_role_ids')
|
||||||
|
@patch(MODULE_PATH + '.models.DiscordUser.objects.user_group_names')
|
||||||
|
@patch(MODULE_PATH + '.models.DiscordUser.objects.user_formatted_nick')
|
||||||
|
class TestAddUser(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
pass
|
self.user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
self.user_info = {
|
||||||
|
'id': TEST_USER_ID,
|
||||||
|
'name': TEST_USER_NAME,
|
||||||
|
'username': TEST_USER_NAME,
|
||||||
|
'discriminator': '1234',
|
||||||
|
}
|
||||||
|
self.access_token = 'accesstoken'
|
||||||
|
|
||||||
|
def test_can_create_user_no_roles_no_nick(
|
||||||
|
self,
|
||||||
|
mock_user_formatted_nick,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_exchange_auth_code_for_token,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
mock_user_formatted_nick.return_value = None
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = None
|
||||||
|
mock_exchange_auth_code_for_token.return_value = self.access_token
|
||||||
|
mock_DiscordClient.return_value.current_user.return_value = self.user_info
|
||||||
|
mock_DiscordClient.return_value.add_guild_member.return_value = True
|
||||||
|
|
||||||
|
result = DiscordUser.objects.add_user(self.user, authorization_code='abcdef')
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertTrue(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.add_guild_member.called)
|
||||||
|
args, kwargs = mock_DiscordClient.return_value.add_guild_member.call_args
|
||||||
|
self.assertEqual(kwargs['guild_id'], TEST_GUILD_ID)
|
||||||
|
self.assertEqual(kwargs['user_id'], TEST_USER_ID)
|
||||||
|
self.assertEqual(kwargs['access_token'], self.access_token)
|
||||||
|
self.assertIsNone(kwargs['role_ids'])
|
||||||
|
self.assertIsNone(kwargs['nick'])
|
||||||
|
|
||||||
def test__sanitize_group_name(self):
|
def test_can_create_user_with_roles_no_nick(
|
||||||
test_group_name = str(10**103)
|
self,
|
||||||
group_name = DiscordOAuthManager._sanitize_group_name(test_group_name)
|
mock_user_formatted_nick,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_exchange_auth_code_for_token,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
role_ids = [1, 2, 3]
|
||||||
|
mock_user_formatted_nick.return_value = None
|
||||||
|
mock_user_group_names.return_value = ['a', 'b', 'c']
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = role_ids
|
||||||
|
mock_exchange_auth_code_for_token.return_value = self.access_token
|
||||||
|
mock_DiscordClient.return_value.current_user.return_value = self.user_info
|
||||||
|
mock_DiscordClient.return_value.add_guild_member.return_value = True
|
||||||
|
|
||||||
|
result = DiscordUser.objects.add_user(self.user, authorization_code='abcdef')
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertTrue(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.add_guild_member.called)
|
||||||
|
args, kwargs = mock_DiscordClient.return_value.add_guild_member.call_args
|
||||||
|
self.assertEqual(kwargs['guild_id'], TEST_GUILD_ID)
|
||||||
|
self.assertEqual(kwargs['user_id'], TEST_USER_ID)
|
||||||
|
self.assertEqual(kwargs['access_token'], self.access_token)
|
||||||
|
self.assertEqual(kwargs['role_ids'], role_ids)
|
||||||
|
self.assertIsNone(kwargs['nick'])
|
||||||
|
|
||||||
self.assertEqual(group_name, test_group_name[:100])
|
@patch(MODULE_PATH + '.managers.DISCORD_SYNC_NAMES', True)
|
||||||
|
def test_can_create_user_no_roles_with_nick(
|
||||||
|
self,
|
||||||
|
mock_user_formatted_nick,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_exchange_auth_code_for_token,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
mock_user_formatted_nick.return_value = TEST_MAIN_NAME
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = []
|
||||||
|
mock_exchange_auth_code_for_token.return_value = self.access_token
|
||||||
|
mock_DiscordClient.return_value.current_user.return_value = self.user_info
|
||||||
|
mock_DiscordClient.return_value.add_guild_member.return_value = True
|
||||||
|
|
||||||
|
result = DiscordUser.objects.add_user(self.user, authorization_code='abcdef')
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertTrue(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.add_guild_member.called)
|
||||||
|
args, kwargs = mock_DiscordClient.return_value.add_guild_member.call_args
|
||||||
|
self.assertEqual(kwargs['guild_id'], TEST_GUILD_ID)
|
||||||
|
self.assertEqual(kwargs['user_id'], TEST_USER_ID)
|
||||||
|
self.assertEqual(kwargs['access_token'], self.access_token)
|
||||||
|
self.assertIsNone(kwargs['role_ids'])
|
||||||
|
self.assertEqual(kwargs['nick'], TEST_MAIN_NAME)
|
||||||
|
|
||||||
def test_generate_Bot_add_url(self):
|
@patch(MODULE_PATH + '.managers.DISCORD_SYNC_NAMES', False)
|
||||||
bot_add_url = DiscordOAuthManager.generate_bot_add_url()
|
def test_can_create_user_no_roles_and_without_nick_if_turned_off(
|
||||||
|
self,
|
||||||
|
mock_user_formatted_nick,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_exchange_auth_code_for_token,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
mock_user_formatted_nick.return_value = TEST_MAIN_NAME
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = []
|
||||||
|
mock_exchange_auth_code_for_token.return_value = self.access_token
|
||||||
|
mock_DiscordClient.return_value.current_user.return_value = self.user_info
|
||||||
|
mock_DiscordClient.return_value.add_guild_member.return_value = True
|
||||||
|
|
||||||
|
result = DiscordUser.objects.add_user(self.user, authorization_code='abcdef')
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertTrue(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.add_guild_member.called)
|
||||||
|
args, kwargs = mock_DiscordClient.return_value.add_guild_member.call_args
|
||||||
|
self.assertEqual(kwargs['guild_id'], TEST_GUILD_ID)
|
||||||
|
self.assertEqual(kwargs['user_id'], TEST_USER_ID)
|
||||||
|
self.assertEqual(kwargs['access_token'], self.access_token)
|
||||||
|
self.assertIsNone(kwargs['role_ids'])
|
||||||
|
self.assertIsNone(kwargs['nick'])
|
||||||
|
|
||||||
|
def test_can_activate_existing_guild_member(
|
||||||
|
self,
|
||||||
|
mock_user_formatted_nick,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_exchange_auth_code_for_token,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
mock_user_formatted_nick.return_value = None
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = None
|
||||||
|
mock_exchange_auth_code_for_token.return_value = self.access_token
|
||||||
|
mock_DiscordClient.return_value.current_user.return_value = self.user_info
|
||||||
|
mock_DiscordClient.return_value.add_guild_member.return_value = None
|
||||||
|
|
||||||
|
result = DiscordUser.objects.add_user(self.user, authorization_code='abcdef')
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertTrue(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.add_guild_member.called)
|
||||||
|
|
||||||
|
def test_return_false_when_user_creation_fails(
|
||||||
|
self,
|
||||||
|
mock_user_formatted_nick,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_exchange_auth_code_for_token,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
mock_user_formatted_nick.return_value = None
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = None
|
||||||
|
mock_exchange_auth_code_for_token.return_value = self.access_token
|
||||||
|
mock_DiscordClient.return_value.current_user.return_value = self.user_info
|
||||||
|
mock_DiscordClient.return_value.add_guild_member.return_value = False
|
||||||
|
|
||||||
|
result = DiscordUser.objects.add_user(self.user, authorization_code='abcdef')
|
||||||
|
self.assertFalse(result)
|
||||||
|
self.assertFalse(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.add_guild_member.called)
|
||||||
|
|
||||||
auth_url = manager.AUTH_URL
|
def test_return_false_when_on_api_backoff(
|
||||||
real_bot_add_url = '{}?client_id=appid&scope=bot&permissions={}'.format(auth_url, manager.BOT_PERMISSIONS)
|
self,
|
||||||
|
mock_user_formatted_nick,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_exchange_auth_code_for_token,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
mock_user_formatted_nick.return_value = None
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = None
|
||||||
|
mock_exchange_auth_code_for_token.return_value = self.access_token
|
||||||
|
mock_DiscordClient.return_value.current_user.return_value = self.user_info
|
||||||
|
mock_DiscordClient.return_value.add_guild_member.side_effect = \
|
||||||
|
DiscordApiBackoff(999)
|
||||||
|
|
||||||
|
result = DiscordUser.objects.add_user(self.user, authorization_code='abcdef')
|
||||||
|
self.assertFalse(result)
|
||||||
|
self.assertFalse(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.add_guild_member.called)
|
||||||
|
|
||||||
|
def test_return_false_on_http_error(
|
||||||
|
self,
|
||||||
|
mock_user_formatted_nick,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_exchange_auth_code_for_token,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
mock_user_formatted_nick.return_value = None
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = None
|
||||||
|
mock_exchange_auth_code_for_token.return_value = self.access_token
|
||||||
|
mock_DiscordClient.return_value.current_user.return_value = self.user_info
|
||||||
|
mock_exception = HTTPError('error')
|
||||||
|
mock_exception.response = Mock()
|
||||||
|
mock_exception.response.status_code = 500
|
||||||
|
mock_DiscordClient.return_value.add_guild_member.side_effect = mock_exception
|
||||||
|
|
||||||
|
result = DiscordUser.objects.add_user(self.user, authorization_code='abcdef')
|
||||||
|
self.assertFalse(result)
|
||||||
|
self.assertFalse(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.add_guild_member.called)
|
||||||
|
|
||||||
|
|
||||||
|
class TestOauthHelpers(TestCase):
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DISCORD_APP_ID', '123456')
|
||||||
|
def test_generate_bot_add_url(self):
|
||||||
|
bot_add_url = DiscordUser.objects.generate_bot_add_url()
|
||||||
|
|
||||||
|
auth_url = DiscordClient.OAUTH_BASE_URL
|
||||||
|
real_bot_add_url = (
|
||||||
|
f'{auth_url}?client_id=123456&scope=bot'
|
||||||
|
f'&permissions={DiscordUser.objects.BOT_PERMISSIONS}'
|
||||||
|
)
|
||||||
self.assertEqual(bot_add_url, real_bot_add_url)
|
self.assertEqual(bot_add_url, real_bot_add_url)
|
||||||
|
|
||||||
def test_generate_oauth_redirect_url(self):
|
def test_generate_oauth_redirect_url(self):
|
||||||
oauth_url = DiscordOAuthManager.generate_oauth_redirect_url()
|
oauth_url = DiscordUser.objects.generate_oauth_redirect_url()
|
||||||
|
|
||||||
self.assertIn(manager.AUTH_URL, oauth_url)
|
self.assertIn(DiscordClient.OAUTH_BASE_URL, oauth_url)
|
||||||
self.assertIn('+'.join(manager.SCOPES), oauth_url)
|
self.assertIn('+'.join(DiscordUser.objects.SCOPES), oauth_url)
|
||||||
self.assertIn(settings.DISCORD_APP_ID, oauth_url)
|
self.assertIn(DISCORD_APP_ID, oauth_url)
|
||||||
self.assertIn(urllib.parse.quote_plus(settings.DISCORD_CALLBACK_URL), oauth_url)
|
self.assertIn(urllib.parse.quote_plus(DISCORD_CALLBACK_URL), oauth_url)
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.OAuth2Session')
|
@patch(MODULE_PATH + '.managers.OAuth2Session')
|
||||||
def test__process_callback_code(self, oauth):
|
def test_process_callback_code(self, oauth):
|
||||||
instance = oauth.return_value
|
instance = oauth.return_value
|
||||||
instance.fetch_token.return_value = {'access_token': 'mywonderfultoken'}
|
instance.fetch_token.return_value = {'access_token': 'mywonderfultoken'}
|
||||||
|
|
||||||
token = DiscordOAuthManager._process_callback_code('12345')
|
token = DiscordUser.objects._exchange_auth_code_for_token('12345')
|
||||||
|
|
||||||
self.assertTrue(oauth.called)
|
self.assertTrue(oauth.called)
|
||||||
args, kwargs = oauth.call_args
|
args, kwargs = oauth.call_args
|
||||||
self.assertEqual(args[0], settings.DISCORD_APP_ID)
|
self.assertEqual(args[0], DISCORD_APP_ID)
|
||||||
self.assertEqual(kwargs['redirect_uri'], settings.DISCORD_CALLBACK_URL)
|
self.assertEqual(kwargs['redirect_uri'], DISCORD_CALLBACK_URL)
|
||||||
self.assertTrue(instance.fetch_token.called)
|
self.assertTrue(instance.fetch_token.called)
|
||||||
args, kwargs = instance.fetch_token.call_args
|
args, kwargs = instance.fetch_token.call_args
|
||||||
self.assertEqual(args[0], manager.TOKEN_URL)
|
self.assertEqual(args[0], DiscordClient.OAUTH_TOKEN_URL)
|
||||||
self.assertEqual(kwargs['client_secret'], settings.DISCORD_APP_SECRET)
|
self.assertEqual(kwargs['client_secret'], DISCORD_APP_SECRET)
|
||||||
self.assertEqual(kwargs['code'], '12345')
|
self.assertEqual(kwargs['code'], '12345')
|
||||||
self.assertEqual(token['access_token'], 'mywonderfultoken')
|
self.assertEqual(token, 'mywonderfultoken')
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.DiscordOAuthManager._process_callback_code')
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_add_user(self, oauth_token, m):
|
|
||||||
# Arrange
|
|
||||||
oauth_token.return_value = {'access_token': 'accesstoken'}
|
|
||||||
|
|
||||||
headers = {'accept': 'application/json', 'authorization': 'Bearer accesstoken'}
|
class TestUserFormattedNick(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
|
||||||
|
def test_return_nick_when_user_has_main(self):
|
||||||
|
AuthUtils.add_main_character_2(self.user, TEST_MAIN_NAME, TEST_MAIN_ID)
|
||||||
|
result = DiscordUser.objects.user_formatted_nick(self.user)
|
||||||
|
expected = TEST_MAIN_NAME
|
||||||
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
m.register_uri('GET',
|
def test_return_none_if_user_has_no_main(self):
|
||||||
manager.DISCORD_URL + "/users/@me",
|
result = DiscordUser.objects.user_formatted_nick(self.user)
|
||||||
request_headers=headers,
|
self.assertIsNone(result)
|
||||||
text=json.dumps({'id': "123456"}))
|
|
||||||
|
|
||||||
headers = {'accept': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
|
|
||||||
m.register_uri('PUT',
|
class TestUserGroupNames(TestCase):
|
||||||
manager.DISCORD_URL + '/guilds/' + str(settings.DISCORD_GUILD_ID) + '/members/123456',
|
|
||||||
request_headers=headers,
|
|
||||||
text='{}')
|
|
||||||
|
|
||||||
# Act
|
@classmethod
|
||||||
return_value = DiscordOAuthManager.add_user('abcdef', [])
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.group_1 = Group.objects.create(name='Group 1')
|
||||||
|
cls.group_2 = Group.objects.create(name='Group 2')
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.user = AuthUtils.create_member(TEST_USER_NAME)
|
||||||
|
|
||||||
# Assert
|
def test_return_groups_and_state_names_for_user(self):
|
||||||
self.assertEqual(return_value, '123456')
|
self.user.groups.add(self.group_1)
|
||||||
self.assertEqual(m.call_count, 2)
|
result = DiscordUser.objects.user_group_names(self.user)
|
||||||
|
expected = ['Group 1', 'Member']
|
||||||
|
self.assertSetEqual(set(result), set(expected))
|
||||||
|
|
||||||
|
def test_return_state_only_if_user_has_no_groups(self):
|
||||||
|
result = DiscordUser.objects.user_group_names(self.user)
|
||||||
|
expected = ['Member']
|
||||||
|
self.assertSetEqual(set(result), set(expected))
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_delete_user(self, m):
|
|
||||||
# Arrange
|
|
||||||
headers = {'accept': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
|
|
||||||
user_id = 12345
|
class TestUserHasAccount(TestCase):
|
||||||
request_url = '{}/guilds/{}/members/{}'.format(manager.DISCORD_URL, settings.DISCORD_GUILD_ID, user_id)
|
|
||||||
m.register_uri('DELETE',
|
|
||||||
request_url,
|
|
||||||
request_headers=headers,
|
|
||||||
text=json.dumps({}))
|
|
||||||
|
|
||||||
# Act
|
@classmethod
|
||||||
result = DiscordOAuthManager.delete_user(user_id)
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
|
||||||
# Assert
|
def test_return_true_if_user_has_account(self):
|
||||||
self.assertTrue(result)
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
self.assertTrue(DiscordUser.objects.user_has_account(self.user))
|
||||||
|
|
||||||
###
|
def test_return_false_if_user_has_no_account(self):
|
||||||
# Test 404 (already deleted)
|
self.assertFalse(DiscordUser.objects.user_has_account(self.user))
|
||||||
# Arrange
|
|
||||||
m.register_uri('DELETE',
|
|
||||||
request_url,
|
|
||||||
request_headers=headers,
|
|
||||||
status_code=404)
|
|
||||||
|
|
||||||
# Act
|
def test_return_false_if_user_does_not_exist(self):
|
||||||
result = DiscordOAuthManager.delete_user(user_id)
|
my_user = User(username='Dummy')
|
||||||
|
self.assertFalse(DiscordUser.objects.user_has_account(my_user))
|
||||||
|
|
||||||
# Assert
|
def test_return_false_if_not_called_with_user_object(self):
|
||||||
self.assertTrue(result)
|
self.assertFalse(DiscordUser.objects.user_has_account('abc'))
|
||||||
|
|
||||||
###
|
|
||||||
# Test 500 (some random API error)
|
|
||||||
# Arrange
|
|
||||||
m.register_uri('DELETE',
|
|
||||||
request_url,
|
|
||||||
request_headers=headers,
|
|
||||||
status_code=500)
|
|
||||||
|
|
||||||
# Act
|
|
||||||
result = DiscordOAuthManager.delete_user(user_id)
|
|
||||||
|
|
||||||
# Assert
|
|
||||||
self.assertFalse(result)
|
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_update_nickname(self, m):
|
|
||||||
# Arrange
|
|
||||||
headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
|
|
||||||
user_id = 12345
|
|
||||||
request_url = '{}/guilds/{}/members/{}'.format(manager.DISCORD_URL, settings.DISCORD_GUILD_ID, user_id)
|
|
||||||
m.patch(request_url,
|
|
||||||
request_headers=headers)
|
|
||||||
|
|
||||||
# Act
|
|
||||||
result = DiscordOAuthManager.update_nickname(user_id, 'somenick')
|
|
||||||
|
|
||||||
# Assert
|
|
||||||
self.assertTrue(result)
|
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.DiscordOAuthManager._get_user_roles')
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.DiscordOAuthManager._get_groups')
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_update_groups(self, group_cache, user_roles, m):
|
|
||||||
# Arrange
|
|
||||||
groups = ['Member', 'Blue', 'SpecialGroup']
|
|
||||||
|
|
||||||
group_cache.return_value = [{'id': '111', 'name': 'Member'},
|
|
||||||
{'id': '222', 'name': 'Blue'},
|
|
||||||
{'id': '333', 'name': 'SpecialGroup'},
|
|
||||||
{'id': '444', 'name': 'NotYourGroup'}]
|
|
||||||
user_roles.return_value = ['444']
|
|
||||||
|
|
||||||
headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
user_id = 12345
|
|
||||||
user_request_url = '{}/guilds/{}/members/{}'.format(manager.DISCORD_URL, settings.DISCORD_GUILD_ID, user_id)
|
|
||||||
group_request_urls = ['{}/guilds/{}/members/{}/roles/{}'.format(manager.DISCORD_URL, settings.DISCORD_GUILD_ID, user_id, g['id']) for g in group_cache.return_value]
|
|
||||||
|
|
||||||
m.patch(user_request_url, request_headers=headers)
|
|
||||||
[m.put(url, request_headers=headers) for url in group_request_urls[:-1]]
|
|
||||||
m.delete(group_request_urls[-1], request_headers=headers)
|
|
||||||
|
|
||||||
# Act
|
|
||||||
DiscordOAuthManager.update_groups(user_id, groups)
|
|
||||||
|
|
||||||
# Assert
|
|
||||||
self.assertEqual(len(m.request_history), 4, 'Must be 4 HTTP calls made')
|
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.cache')
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.DiscordOAuthManager._get_user_roles')
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.DiscordOAuthManager._group_name_to_id')
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_update_groups_backoff(self, name_to_id, user_groups, djcache, m):
|
|
||||||
# Arrange
|
|
||||||
groups = ['Member']
|
|
||||||
user_groups.return_value = []
|
|
||||||
name_to_id.return_value = '111'
|
|
||||||
|
|
||||||
headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
user_id = 12345
|
|
||||||
request_url = '{}/guilds/{}/members/{}/roles/{}'.format(manager.DISCORD_URL, settings.DISCORD_GUILD_ID, user_id, name_to_id.return_value)
|
|
||||||
|
|
||||||
djcache.get.return_value = None # No existing backoffs in cache
|
|
||||||
|
|
||||||
m.put(request_url,
|
|
||||||
request_headers=headers,
|
|
||||||
headers={'Retry-After': '200000'},
|
|
||||||
status_code=429)
|
|
||||||
|
|
||||||
# Act & Assert
|
|
||||||
with self.assertRaises(manager.DiscordApiBackoff) as bo:
|
|
||||||
try:
|
|
||||||
DiscordOAuthManager.update_groups(user_id, groups, blocking=False)
|
|
||||||
except manager.DiscordApiBackoff as bo:
|
|
||||||
self.assertEqual(bo.retry_after, 200000, 'Retry-After time must be equal to Retry-After set in header')
|
|
||||||
self.assertFalse(bo.global_ratelimit, 'global_ratelimit must be False')
|
|
||||||
raise bo
|
|
||||||
|
|
||||||
self.assertTrue(djcache.set.called)
|
|
||||||
args, kwargs = djcache.set.call_args
|
|
||||||
self.assertEqual(args[0], 'DISCORD_BACKOFF_update_groups')
|
|
||||||
self.assertTrue(datetime.datetime.strptime(args[1], manager.cache_time_format) > datetime.datetime.now())
|
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.cache')
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.DiscordOAuthManager._get_user_roles')
|
|
||||||
@mock.patch(MODULE_PATH + '.manager.DiscordOAuthManager._group_name_to_id')
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_update_groups_global_backoff(self, name_to_id, user_groups, djcache, m):
|
|
||||||
# Arrange
|
|
||||||
groups = ['Member']
|
|
||||||
user_groups.return_value = []
|
|
||||||
name_to_id.return_value = '111'
|
|
||||||
|
|
||||||
headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
|
|
||||||
user_id = 12345
|
|
||||||
request_url = '{}/guilds/{}/members/{}/roles/{}'.format(manager.DISCORD_URL, settings.DISCORD_GUILD_ID, user_id, name_to_id.return_value)
|
|
||||||
|
|
||||||
djcache.get.return_value = None # No existing backoffs in cache
|
|
||||||
|
|
||||||
m.put(request_url,
|
|
||||||
request_headers=headers,
|
|
||||||
headers={'Retry-After': '200000', 'X-RateLimit-Global': 'true'},
|
|
||||||
status_code=429)
|
|
||||||
|
|
||||||
# Act & Assert
|
|
||||||
with self.assertRaises(manager.DiscordApiBackoff) as bo:
|
|
||||||
try:
|
|
||||||
DiscordOAuthManager.update_groups(user_id, groups, blocking=False)
|
|
||||||
except manager.DiscordApiBackoff as bo:
|
|
||||||
self.assertEqual(bo.retry_after, 200000, 'Retry-After time must be equal to Retry-After set in header')
|
|
||||||
self.assertTrue(bo.global_ratelimit, 'global_ratelimit must be True')
|
|
||||||
raise bo
|
|
||||||
|
|
||||||
self.assertTrue(djcache.set.called)
|
|
||||||
args, kwargs = djcache.set.call_args
|
|
||||||
self.assertEqual(args[0], 'DISCORD_BACKOFF_GLOBAL')
|
|
||||||
self.assertTrue(datetime.datetime.strptime(args[1], manager.cache_time_format) > datetime.datetime.now())
|
|
||||||
|
|||||||
222
allianceauth/services/modules/discord/tests/test_models.py
Normal file
222
allianceauth/services/modules/discord/tests/test_models.py
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
from unittest.mock import patch, Mock
|
||||||
|
|
||||||
|
from requests.exceptions import HTTPError
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
|
from . import TEST_USER_NAME, TEST_USER_ID, TEST_MAIN_NAME, TEST_MAIN_ID, MODULE_PATH
|
||||||
|
from ..discord_client import DiscordClient, DiscordApiBackoff
|
||||||
|
from ..models import DiscordUser
|
||||||
|
from ..utils import set_logger_to_file
|
||||||
|
|
||||||
|
|
||||||
|
logger = set_logger_to_file(MODULE_PATH + '.models', __file__)
|
||||||
|
|
||||||
|
|
||||||
|
class TestBasicsAndHelpers(TestCase):
|
||||||
|
|
||||||
|
def test_str(self):
|
||||||
|
user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
discord_user = DiscordUser.objects.create(user=user, uid=TEST_USER_ID)
|
||||||
|
expected = 'Peter Parker - 198765432012345678'
|
||||||
|
self.assertEqual(str(discord_user), expected)
|
||||||
|
|
||||||
|
def test_repr(self):
|
||||||
|
user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
discord_user = DiscordUser.objects.create(user=user, uid=TEST_USER_ID)
|
||||||
|
expected = 'DiscordUser(user=\'Peter Parker\', uid=198765432012345678)'
|
||||||
|
self.assertEqual(repr(discord_user), expected)
|
||||||
|
|
||||||
|
def test_guild_get_or_create_role_ids(self):
|
||||||
|
mock_client = Mock(spec=DiscordClient)
|
||||||
|
mock_client.match_guild_roles_to_names.return_value = \
|
||||||
|
[({'id': 1, 'name': 'alpha'}, True), ({'id': 2, 'name': 'bravo'}, True)]
|
||||||
|
|
||||||
|
result = DiscordUser._guild_get_or_create_role_ids(mock_client, [])
|
||||||
|
excepted = [1, 2]
|
||||||
|
self.assertEqual(set(result), set(excepted))
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
class TestUpdateNick(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
self.discord_user = DiscordUser.objects.create(
|
||||||
|
user=self.user, uid=TEST_USER_ID
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def user_info(nick):
|
||||||
|
return {
|
||||||
|
'user': {
|
||||||
|
'id': TEST_USER_ID,
|
||||||
|
'username': TEST_USER_NAME
|
||||||
|
},
|
||||||
|
'nick': nick,
|
||||||
|
'roles': [1, 2, 3]
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_can_update(self, mock_DiscordClient):
|
||||||
|
AuthUtils.add_main_character_2(self.user, TEST_MAIN_NAME, TEST_MAIN_ID)
|
||||||
|
mock_DiscordClient.return_value.modify_guild_member.return_value = True
|
||||||
|
|
||||||
|
result = self.discord_user.update_nickname()
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.modify_guild_member.called)
|
||||||
|
|
||||||
|
def test_dont_update_if_user_has_no_main(self, mock_DiscordClient):
|
||||||
|
mock_DiscordClient.return_value.modify_guild_member.return_value = False
|
||||||
|
|
||||||
|
result = self.discord_user.update_nickname()
|
||||||
|
self.assertFalse(result)
|
||||||
|
self.assertFalse(mock_DiscordClient.return_value.modify_guild_member.called)
|
||||||
|
|
||||||
|
def test_return_none_if_user_no_longer_a_member(
|
||||||
|
self, mock_DiscordClient
|
||||||
|
):
|
||||||
|
AuthUtils.add_main_character_2(self.user, TEST_MAIN_NAME, TEST_MAIN_ID)
|
||||||
|
mock_DiscordClient.return_value.modify_guild_member.return_value = None
|
||||||
|
|
||||||
|
result = self.discord_user.update_nickname()
|
||||||
|
self.assertIsNone(result)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.modify_guild_member.called)
|
||||||
|
|
||||||
|
def test_return_false_if_api_returns_false(self, mock_DiscordClient):
|
||||||
|
AuthUtils.add_main_character_2(self.user, TEST_MAIN_NAME, TEST_MAIN_ID)
|
||||||
|
mock_DiscordClient.return_value.modify_guild_member.return_value = False
|
||||||
|
|
||||||
|
result = self.discord_user.update_nickname()
|
||||||
|
self.assertFalse(result)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.modify_guild_member.called)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.models.notify')
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
class TestDeleteUser(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
self.discord_user = DiscordUser.objects.create(
|
||||||
|
user=self.user, uid=TEST_USER_ID
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_can_delete_user(self, mock_DiscordClient, mock_notify):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = True
|
||||||
|
result = self.discord_user.delete_user()
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertFalse(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.remove_guild_member.called)
|
||||||
|
self.assertFalse(mock_notify.called)
|
||||||
|
|
||||||
|
def test_can_delete_user_and_notify_user(self, mock_DiscordClient, mock_notify):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = True
|
||||||
|
result = self.discord_user.delete_user(notify_user=True)
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertTrue(mock_notify.called)
|
||||||
|
|
||||||
|
def test_can_delete_user_when_member_is_unknown(
|
||||||
|
self, mock_DiscordClient, mock_notify
|
||||||
|
):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = None
|
||||||
|
result = self.discord_user.delete_user()
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertFalse(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.remove_guild_member.called)
|
||||||
|
self.assertFalse(mock_notify.called)
|
||||||
|
|
||||||
|
def test_return_false_when_api_fails(self, mock_DiscordClient, mock_notify):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = False
|
||||||
|
result = self.discord_user.delete_user()
|
||||||
|
self.assertFalse(result)
|
||||||
|
|
||||||
|
def test_dont_notify_if_user_was_already_deleted_and_return_none(
|
||||||
|
self, mock_DiscordClient, mock_notify
|
||||||
|
):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = None
|
||||||
|
DiscordUser.objects.get(pk=self.discord_user.pk).delete()
|
||||||
|
result = self.discord_user.delete_user()
|
||||||
|
self.assertIsNone(result)
|
||||||
|
self.assertFalse(
|
||||||
|
DiscordUser.objects.filter(user=self.user, uid=TEST_USER_ID).exists()
|
||||||
|
)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.remove_guild_member.called)
|
||||||
|
self.assertFalse(mock_notify.called)
|
||||||
|
|
||||||
|
def test_return_false_on_api_backoff(self, mock_DiscordClient, mock_notify):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.side_effect = \
|
||||||
|
DiscordApiBackoff(999)
|
||||||
|
result = self.discord_user.delete_user()
|
||||||
|
self.assertFalse(result)
|
||||||
|
|
||||||
|
def test_return_false_on_http_error(self, mock_DiscordClient, mock_notify):
|
||||||
|
mock_exception = HTTPError('error')
|
||||||
|
mock_exception.response = Mock()
|
||||||
|
mock_exception.response.status_code = 500
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.side_effect = \
|
||||||
|
mock_exception
|
||||||
|
result = self.discord_user.delete_user()
|
||||||
|
self.assertFalse(result)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
@patch(MODULE_PATH + '.models.DiscordUser._guild_get_or_create_role_ids')
|
||||||
|
@patch(MODULE_PATH + '.models.DiscordUser.objects.user_group_names')
|
||||||
|
class TestUpdateGroups(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.user = AuthUtils.create_user(TEST_USER_NAME)
|
||||||
|
self.discord_user = DiscordUser.objects.create(
|
||||||
|
user=self.user, uid=TEST_USER_ID
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_can_update(
|
||||||
|
self,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
roles_requested = [1, 2, 3]
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = roles_requested
|
||||||
|
mock_DiscordClient.return_value.modify_guild_member.return_value = True
|
||||||
|
|
||||||
|
result = self.discord_user.update_groups()
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.modify_guild_member.called)
|
||||||
|
|
||||||
|
def test_return_none_if_user_no_longer_a_member(
|
||||||
|
self,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
roles_requested = [1, 2, 3]
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = roles_requested
|
||||||
|
mock_DiscordClient.return_value.modify_guild_member.return_value = None
|
||||||
|
|
||||||
|
result = self.discord_user.update_groups()
|
||||||
|
self.assertIsNone(result)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.modify_guild_member.called)
|
||||||
|
|
||||||
|
def test_return_false_if_api_returns_false(
|
||||||
|
self,
|
||||||
|
mock_user_group_names,
|
||||||
|
mock_guild_get_or_create_role_ids,
|
||||||
|
mock_DiscordClient
|
||||||
|
):
|
||||||
|
roles_requested = [1, 2, 3]
|
||||||
|
mock_user_group_names.return_value = []
|
||||||
|
mock_guild_get_or_create_role_ids.return_value = roles_requested
|
||||||
|
mock_DiscordClient.return_value.modify_guild_member.return_value = False
|
||||||
|
|
||||||
|
result = self.discord_user.update_groups()
|
||||||
|
self.assertFalse(result)
|
||||||
|
self.assertTrue(mock_DiscordClient.return_value.modify_guild_member.called)
|
||||||
310
allianceauth/services/modules/discord/tests/test_tasks.py
Normal file
310
allianceauth/services/modules/discord/tests/test_tasks.py
Normal file
@@ -0,0 +1,310 @@
|
|||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from celery.exceptions import Retry
|
||||||
|
from requests.exceptions import HTTPError
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.contrib.auth.models import Group
|
||||||
|
from django.test.utils import override_settings
|
||||||
|
|
||||||
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
|
from . import TEST_USER_NAME, TEST_USER_ID, TEST_MAIN_NAME, TEST_MAIN_ID
|
||||||
|
from ..models import DiscordUser
|
||||||
|
from ..discord_client import DiscordApiBackoff
|
||||||
|
from .. import tasks
|
||||||
|
from ..utils import set_logger_to_file
|
||||||
|
|
||||||
|
|
||||||
|
MODULE_PATH = 'allianceauth.services.modules.discord.tasks'
|
||||||
|
logger = set_logger_to_file(MODULE_PATH, __file__)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.DiscordUser.update_groups')
|
||||||
|
class TestUpdateGroups(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.user = AuthUtils.create_member(TEST_USER_NAME)
|
||||||
|
cls.group_1 = Group.objects.create(name='Group 1')
|
||||||
|
cls.group_2 = Group.objects.create(name='Group 2')
|
||||||
|
cls.group_1.user_set.add(cls.user)
|
||||||
|
cls.group_2.user_set.add(cls.user)
|
||||||
|
|
||||||
|
def test_can_update_groups(self, mock_update_groups):
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
tasks.update_groups(self.user.pk)
|
||||||
|
self.assertTrue(mock_update_groups.called)
|
||||||
|
|
||||||
|
def test_no_action_if_user_has_no_discord_account(self, mock_update_groups):
|
||||||
|
tasks.update_groups(self.user.pk)
|
||||||
|
self.assertFalse(mock_update_groups.called)
|
||||||
|
|
||||||
|
def test_retries_on_api_backoff(self, mock_update_groups):
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
mock_exception = DiscordApiBackoff(999)
|
||||||
|
mock_update_groups.side_effect = mock_exception
|
||||||
|
|
||||||
|
with self.assertRaises(Retry):
|
||||||
|
tasks.update_groups(self.user.pk)
|
||||||
|
|
||||||
|
def test_retry_on_http_error_except_404(self, mock_update_groups):
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
mock_exception = HTTPError('error')
|
||||||
|
mock_exception.response = MagicMock()
|
||||||
|
mock_exception.response.status_code = 500
|
||||||
|
mock_update_groups.side_effect = mock_exception
|
||||||
|
|
||||||
|
with self.assertRaises(Retry):
|
||||||
|
tasks.update_groups(self.user.pk)
|
||||||
|
|
||||||
|
def test_retry_on_http_error_404_when_user_not_deleted(self, mock_update_groups):
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
mock_exception = HTTPError('error')
|
||||||
|
mock_exception.response = MagicMock()
|
||||||
|
mock_exception.response.status_code = 404
|
||||||
|
mock_update_groups.side_effect = mock_exception
|
||||||
|
|
||||||
|
with self.assertRaises(Retry):
|
||||||
|
tasks.update_groups(self.user.pk)
|
||||||
|
|
||||||
|
def test_retry_on_non_http_error(self, mock_update_groups):
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
mock_update_groups.side_effect = ConnectionError
|
||||||
|
|
||||||
|
with self.assertRaises(Retry):
|
||||||
|
tasks.update_groups(self.user.pk)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.DISCORD_TASKS_MAX_RETRIES', 3)
|
||||||
|
def test_log_error_if_retries_exhausted(self, mock_update_groups):
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
mock_task = MagicMock(**{'request.retries': 3})
|
||||||
|
mock_update_groups.side_effect = ConnectionError
|
||||||
|
update_groups_inner = tasks.update_groups.__wrapped__.__func__
|
||||||
|
|
||||||
|
update_groups_inner(mock_task, self.user.pk)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.delete_user.delay')
|
||||||
|
def test_delete_user_if_user_is_no_longer_member_of_discord_server(
|
||||||
|
self, mock_delete_user, mock_update_groups
|
||||||
|
):
|
||||||
|
mock_update_groups.return_value = None
|
||||||
|
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
tasks.update_groups(self.user.pk)
|
||||||
|
self.assertTrue(mock_update_groups.called)
|
||||||
|
self.assertTrue(mock_delete_user.called)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.DiscordUser.update_nickname')
|
||||||
|
class TestUpdateNickname(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.user = AuthUtils.create_member(TEST_USER_NAME)
|
||||||
|
AuthUtils.add_main_character_2(
|
||||||
|
cls.user,
|
||||||
|
TEST_MAIN_NAME,
|
||||||
|
TEST_MAIN_ID,
|
||||||
|
corp_id='2',
|
||||||
|
corp_name='test_corp',
|
||||||
|
corp_ticker='TEST',
|
||||||
|
disconnect_signals=True
|
||||||
|
)
|
||||||
|
cls.discord_user = DiscordUser.objects.create(user=cls.user, uid=TEST_USER_ID)
|
||||||
|
|
||||||
|
def test_can_update_nickname(self, mock_update_nickname):
|
||||||
|
mock_update_nickname.return_value = True
|
||||||
|
|
||||||
|
tasks.update_nickname(self.user.pk)
|
||||||
|
self.assertTrue(mock_update_nickname.called)
|
||||||
|
|
||||||
|
def test_no_action_when_user_had_no_account(self, mock_update_nickname):
|
||||||
|
my_user = AuthUtils.create_user('Dummy User')
|
||||||
|
mock_update_nickname.return_value = False
|
||||||
|
|
||||||
|
tasks.update_nickname(my_user.pk)
|
||||||
|
self.assertFalse(mock_update_nickname.called)
|
||||||
|
|
||||||
|
def test_retries_on_api_backoff(self, mock_update_nickname):
|
||||||
|
mock_exception = DiscordApiBackoff(999)
|
||||||
|
mock_update_nickname.side_effect = mock_exception
|
||||||
|
|
||||||
|
with self.assertRaises(Retry):
|
||||||
|
tasks.update_nickname(self.user.pk)
|
||||||
|
|
||||||
|
def test_retries_on_general_exception(self, mock_update_nickname):
|
||||||
|
mock_update_nickname.side_effect = ConnectionError
|
||||||
|
|
||||||
|
with self.assertRaises(Retry):
|
||||||
|
tasks.update_nickname(self.user.pk)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.DISCORD_TASKS_MAX_RETRIES', 3)
|
||||||
|
def test_log_error_if_retries_exhausted(self, mock_update_nickname):
|
||||||
|
mock_task = MagicMock(**{'request.retries': 3})
|
||||||
|
mock_update_nickname.side_effect = ConnectionError
|
||||||
|
update_nickname_inner = tasks.update_nickname.__wrapped__.__func__
|
||||||
|
|
||||||
|
update_nickname_inner(mock_task, self.user.pk)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.DiscordUser.delete_user')
|
||||||
|
class TestDeleteUser(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.user = AuthUtils.create_member('Peter Parker')
|
||||||
|
cls.discord_user = DiscordUser.objects.create(user=cls.user, uid=TEST_USER_ID)
|
||||||
|
|
||||||
|
def test_can_delete_user(self, mock_delete_user):
|
||||||
|
mock_delete_user.return_value = True
|
||||||
|
|
||||||
|
tasks.delete_user(self.user.pk)
|
||||||
|
self.assertTrue(mock_delete_user.called)
|
||||||
|
|
||||||
|
def test_can_delete_user_with_notify(self, mock_delete_user):
|
||||||
|
mock_delete_user.return_value = True
|
||||||
|
|
||||||
|
tasks.delete_user(self.user.pk, notify_user=True)
|
||||||
|
self.assertTrue(mock_delete_user.called)
|
||||||
|
args, kwargs = mock_delete_user.call_args
|
||||||
|
self.assertTrue(kwargs['notify_user'])
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.delete_user.delay')
|
||||||
|
def test_dont_retry_delete_user_if_user_is_no_longer_member_of_discord_server(
|
||||||
|
self, mock_delete_user_delay, mock_delete_user
|
||||||
|
):
|
||||||
|
mock_delete_user.return_value = None
|
||||||
|
|
||||||
|
tasks.delete_user(self.user.pk)
|
||||||
|
self.assertTrue(mock_delete_user.called)
|
||||||
|
self.assertFalse(mock_delete_user_delay.called)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.DiscordUser.update_groups')
|
||||||
|
class TestTaskPerformUserAction(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.user = AuthUtils.create_member('Peter Parker')
|
||||||
|
cls.discord_user = DiscordUser.objects.create(user=cls.user, uid=TEST_USER_ID)
|
||||||
|
|
||||||
|
def test_raise_value_error_on_unknown_method(self, mock_update_groups):
|
||||||
|
mock_task = MagicMock(**{'request.retries': 0})
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
tasks._task_perform_user_action(mock_task, self.user.pk, 'invalid_method')
|
||||||
|
|
||||||
|
def test_catch_and_log_unexpected_exceptions(self, mock_update_groups):
|
||||||
|
mock_task = MagicMock(**{'request.retries': 0})
|
||||||
|
mock_update_groups.side_effect = RuntimeError
|
||||||
|
|
||||||
|
tasks._task_perform_user_action(mock_task, self.user.pk, 'update_groups')
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(CELERY_ALWAYS_EAGER=True)
|
||||||
|
class TestBulkTasks(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.user_1 = AuthUtils.create_user('Peter Parker')
|
||||||
|
cls.user_2 = AuthUtils.create_user('Kara Danvers')
|
||||||
|
cls.user_3 = AuthUtils.create_user('Clark Kent')
|
||||||
|
DiscordUser.objects.all().delete()
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.update_groups.si')
|
||||||
|
def test_can_update_groups_for_multiple_users(self, mock_update_groups):
|
||||||
|
du_1 = DiscordUser.objects.create(user=self.user_1, uid=123)
|
||||||
|
du_2 = DiscordUser.objects.create(user=self.user_2, uid=456)
|
||||||
|
DiscordUser.objects.create(user=self.user_3, uid=789)
|
||||||
|
expected_pks = [du_1.pk, du_2.pk]
|
||||||
|
|
||||||
|
tasks.update_groups_bulk(expected_pks)
|
||||||
|
self.assertEqual(mock_update_groups.call_count, 2)
|
||||||
|
current_pks = [args[0][0] for args in mock_update_groups.call_args_list]
|
||||||
|
|
||||||
|
self.assertSetEqual(set(current_pks), set(expected_pks))
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.update_groups.si')
|
||||||
|
def test_can_update_all_groups(self, mock_update_groups):
|
||||||
|
du_1 = DiscordUser.objects.create(user=self.user_1, uid=123)
|
||||||
|
du_2 = DiscordUser.objects.create(user=self.user_2, uid=456)
|
||||||
|
du_3 = DiscordUser.objects.create(user=self.user_3, uid=789)
|
||||||
|
|
||||||
|
tasks.update_all_groups()
|
||||||
|
self.assertEqual(mock_update_groups.call_count, 3)
|
||||||
|
current_pks = [args[0][0] for args in mock_update_groups.call_args_list]
|
||||||
|
expected_pks = [du_1.pk, du_2.pk, du_3.pk]
|
||||||
|
self.assertSetEqual(set(current_pks), set(expected_pks))
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.update_nickname.si')
|
||||||
|
def test_can_update_nicknames_for_multiple_users(self, mock_update_nickname):
|
||||||
|
du_1 = DiscordUser.objects.create(user=self.user_1, uid=123)
|
||||||
|
du_2 = DiscordUser.objects.create(user=self.user_2, uid=456)
|
||||||
|
DiscordUser.objects.create(user=self.user_3, uid=789)
|
||||||
|
expected_pks = [du_1.pk, du_2.pk]
|
||||||
|
|
||||||
|
tasks.update_nicknames_bulk(expected_pks)
|
||||||
|
self.assertEqual(mock_update_nickname.call_count, 2)
|
||||||
|
current_pks = [
|
||||||
|
args[0][0] for args in mock_update_nickname.call_args_list
|
||||||
|
]
|
||||||
|
self.assertSetEqual(set(current_pks), set(expected_pks))
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.update_nickname.si')
|
||||||
|
def test_can_update_nicknames_for_all_users(self, mock_update_nickname):
|
||||||
|
du_1 = DiscordUser.objects.create(user=self.user_1, uid='123')
|
||||||
|
du_2 = DiscordUser.objects.create(user=self.user_2, uid='456')
|
||||||
|
du_3 = DiscordUser.objects.create(user=self.user_3, uid='789')
|
||||||
|
|
||||||
|
tasks.update_all_nicknames()
|
||||||
|
self.assertEqual(mock_update_nickname.call_count, 3)
|
||||||
|
current_pks = [
|
||||||
|
args[0][0] for args in mock_update_nickname.call_args_list
|
||||||
|
]
|
||||||
|
expected_pks = [du_1.pk, du_2.pk, du_3.pk]
|
||||||
|
self.assertSetEqual(set(current_pks), set(expected_pks))
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.DISCORD_SYNC_NAMES', True)
|
||||||
|
@patch(MODULE_PATH + '.update_nickname')
|
||||||
|
@patch(MODULE_PATH + '.update_groups')
|
||||||
|
def test_can_update_all_incl_nicknames(
|
||||||
|
self, mock_update_groups, mock_update_nickname
|
||||||
|
):
|
||||||
|
du_1 = DiscordUser.objects.create(user=self.user_1, uid=123)
|
||||||
|
du_2 = DiscordUser.objects.create(user=self.user_2, uid=456)
|
||||||
|
du_3 = DiscordUser.objects.create(user=self.user_3, uid=789)
|
||||||
|
|
||||||
|
tasks.update_all()
|
||||||
|
self.assertEqual(mock_update_groups.si.call_count, 3)
|
||||||
|
current_pks = [args[0][0] for args in mock_update_groups.si.call_args_list]
|
||||||
|
expected_pks = [du_1.pk, du_2.pk, du_3.pk]
|
||||||
|
self.assertSetEqual(set(current_pks), set(expected_pks))
|
||||||
|
|
||||||
|
self.assertEqual(mock_update_nickname.si.call_count, 3)
|
||||||
|
current_pks = [args[0][0] for args in mock_update_nickname.si.call_args_list]
|
||||||
|
expected_pks = [du_1.pk, du_2.pk, du_3.pk]
|
||||||
|
self.assertSetEqual(set(current_pks), set(expected_pks))
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.DISCORD_SYNC_NAMES', False)
|
||||||
|
@patch(MODULE_PATH + '.update_nickname')
|
||||||
|
@patch(MODULE_PATH + '.update_groups')
|
||||||
|
def test_can_update_all_excl_nicknames(
|
||||||
|
self, mock_update_groups, mock_update_nickname
|
||||||
|
):
|
||||||
|
du_1 = DiscordUser.objects.create(user=self.user_1, uid=123)
|
||||||
|
du_2 = DiscordUser.objects.create(user=self.user_2, uid=456)
|
||||||
|
du_3 = DiscordUser.objects.create(user=self.user_3, uid=789)
|
||||||
|
|
||||||
|
tasks.update_all()
|
||||||
|
self.assertEqual(mock_update_groups.si.call_count, 3)
|
||||||
|
current_pks = [args[0][0] for args in mock_update_groups.si.call_args_list]
|
||||||
|
expected_pks = [du_1.pk, du_2.pk, du_3.pk]
|
||||||
|
self.assertSetEqual(set(current_pks), set(expected_pks))
|
||||||
|
|
||||||
|
self.assertEqual(mock_update_nickname.si.call_count, 0)
|
||||||
102
allianceauth/services/modules/discord/tests/test_utils.py
Normal file
102
allianceauth/services/modules/discord/tests/test_utils.py
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
from unittest.mock import Mock, patch
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from ..utils import clean_setting
|
||||||
|
|
||||||
|
MODULE_PATH = 'allianceauth.services.modules.discord.utils'
|
||||||
|
|
||||||
|
|
||||||
|
class TestCleanSetting(TestCase):
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_default_if_not_set(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = Mock(spec=None)
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
False,
|
||||||
|
)
|
||||||
|
self.assertEqual(result, False)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_default_if_not_set_for_none(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = Mock(spec=None)
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
None,
|
||||||
|
required_type=int
|
||||||
|
)
|
||||||
|
self.assertEqual(result, None)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_true_stays_true(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = True
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
False,
|
||||||
|
)
|
||||||
|
self.assertEqual(result, True)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_false_stays_false(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = False
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
False
|
||||||
|
)
|
||||||
|
self.assertEqual(result, False)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_default_for_invalid_type_bool(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = 'invalid type'
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
False
|
||||||
|
)
|
||||||
|
self.assertEqual(result, False)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_default_for_invalid_type_int(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = 'invalid type'
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
50
|
||||||
|
)
|
||||||
|
self.assertEqual(result, 50)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_default_if_below_minimum_1(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = -5
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
default_value=50
|
||||||
|
)
|
||||||
|
self.assertEqual(result, 50)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_default_if_below_minimum_2(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = -50
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
default_value=50,
|
||||||
|
min_value=-10
|
||||||
|
)
|
||||||
|
self.assertEqual(result, 50)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_default_for_invalid_type_int_2(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = 1000
|
||||||
|
result = clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
default_value=50,
|
||||||
|
max_value=100
|
||||||
|
)
|
||||||
|
self.assertEqual(result, 50)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.settings')
|
||||||
|
def test_default_is_none_needs_required_type(self, mock_settings):
|
||||||
|
mock_settings.TEST_SETTING_DUMMY = 'invalid type'
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
clean_setting(
|
||||||
|
'TEST_SETTING_DUMMY',
|
||||||
|
default_value=None
|
||||||
|
)
|
||||||
@@ -1,66 +1,167 @@
|
|||||||
from django_webtest import WebTest
|
from unittest.mock import patch
|
||||||
from unittest import mock
|
|
||||||
|
|
||||||
from django.test import TestCase
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.test import TestCase, RequestFactory
|
||||||
from django.conf import settings
|
from django.urls import reverse
|
||||||
|
|
||||||
from allianceauth.tests.auth_utils import AuthUtils
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
from ..models import DiscordUser
|
from . import MODULE_PATH, add_permissions_to_members, TEST_USER_NAME, TEST_USER_ID
|
||||||
from ..manager import DiscordOAuthManager
|
from ..models import DiscordUser, DiscordClient
|
||||||
|
from ..utils import set_logger_to_file
|
||||||
from . import DEFAULT_AUTH_GROUP, add_permissions, MODULE_PATH
|
from ..views import (
|
||||||
|
discord_callback,
|
||||||
|
reset_discord,
|
||||||
|
deactivate_discord,
|
||||||
|
discord_add_bot,
|
||||||
|
activate_discord
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class DiscordViewsTestCase(WebTest):
|
logger = set_logger_to_file(MODULE_PATH + '.views', __file__)
|
||||||
|
|
||||||
|
|
||||||
|
class SetupClassMixin(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
super().setUpClass()
|
||||||
|
cls.factory = RequestFactory()
|
||||||
|
cls.user = AuthUtils.create_member(TEST_USER_NAME)
|
||||||
|
add_permissions_to_members()
|
||||||
|
cls.services_url = reverse('services:services')
|
||||||
|
|
||||||
|
|
||||||
|
class TestActivateDiscord(SetupClassMixin, TestCase):
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.views.DiscordUser.objects.generate_oauth_redirect_url')
|
||||||
|
def test_redirects_to_correct_url(self, mock_generate_oauth_redirect_url):
|
||||||
|
expected_url = '/example.com/oauth/'
|
||||||
|
mock_generate_oauth_redirect_url.return_value = expected_url
|
||||||
|
request = self.factory.get(reverse('discord:activate'))
|
||||||
|
request.user = self.user
|
||||||
|
response = activate_discord(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, expected_url)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.views.messages')
|
||||||
|
@patch(MODULE_PATH + '.managers.DiscordClient', spec=DiscordClient)
|
||||||
|
class TestDeactivateDiscord(SetupClassMixin, TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.member = AuthUtils.create_member('auth_member')
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
AuthUtils.add_main_character(self.member, 'test character', '1234', '2345', 'test corp', 'testc')
|
|
||||||
add_permissions()
|
def test_when_successful_show_success_message(
|
||||||
|
self, mock_DiscordClient, mock_messages
|
||||||
|
):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = True
|
||||||
|
request = self.factory.get(reverse('discord:deactivate'))
|
||||||
|
request.user = self.user
|
||||||
|
response = deactivate_discord(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, self.services_url)
|
||||||
|
self.assertTrue(mock_messages.success.called)
|
||||||
|
self.assertFalse(mock_messages.error.called)
|
||||||
|
|
||||||
def login(self):
|
def test_when_unsuccessful_show_error_message(
|
||||||
self.app.set_user(self.member)
|
self, mock_DiscordClient, mock_messages
|
||||||
|
):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = False
|
||||||
|
request = self.factory.get(reverse('discord:deactivate'))
|
||||||
|
request.user = self.user
|
||||||
|
response = deactivate_discord(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, self.services_url)
|
||||||
|
self.assertFalse(mock_messages.success.called)
|
||||||
|
self.assertTrue(mock_messages.error.called)
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.views.DiscordOAuthManager')
|
|
||||||
def test_activate(self, manager):
|
|
||||||
self.login()
|
|
||||||
manager.generate_oauth_redirect_url.return_value = '/example.com/oauth/'
|
|
||||||
response = self.app.get('/discord/activate/', auto_follow=False)
|
|
||||||
self.assertRedirects(response, expected_url='/example.com/oauth/', target_status_code=404)
|
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager')
|
@patch(MODULE_PATH + '.views.messages')
|
||||||
def test_callback(self, manager):
|
@patch(MODULE_PATH + '.managers.DiscordClient')
|
||||||
self.login()
|
class TestResetDiscord(SetupClassMixin, TestCase):
|
||||||
manager.add_user.return_value = '1234'
|
|
||||||
response = self.app.get('/discord/callback/', params={'code': '1234'})
|
def setUp(self):
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
|
||||||
self.member = User.objects.get(pk=self.member.pk)
|
def test_when_successful_redirect_to_activate(
|
||||||
|
self, mock_DiscordClient, mock_messages
|
||||||
|
):
|
||||||
|
mock_DiscordClient.return_value.remove_guild_member.return_value = True
|
||||||
|
request = self.factory.get(reverse('discord:reset'))
|
||||||
|
request.user = self.user
|
||||||
|
response = reset_discord(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, reverse("discord:activate"))
|
||||||
|
self.assertFalse(mock_messages.error.called)
|
||||||
|
|
||||||
self.assertTrue(manager.add_user.called)
|
def test_when_unsuccessful_message_error_and_redirect_to_service(
|
||||||
self.assertEqual(manager.update_nickname.called, settings.DISCORD_SYNC_NAMES)
|
self, mock_DiscordClient, mock_messages
|
||||||
self.assertEqual(self.member.discord.uid, '1234')
|
):
|
||||||
self.assertRedirects(response, expected_url='/services/', target_status_code=200)
|
mock_DiscordClient.return_value.remove_guild_member.return_value = False
|
||||||
|
request = self.factory.get(reverse('discord:reset'))
|
||||||
|
request.user = self.user
|
||||||
|
response = reset_discord(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, self.services_url)
|
||||||
|
self.assertTrue(mock_messages.error.called)
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager')
|
|
||||||
def test_reset(self, manager):
|
|
||||||
self.login()
|
|
||||||
DiscordUser.objects.create(user=self.member, uid='12345')
|
|
||||||
manager.delete_user.return_value = True
|
|
||||||
|
|
||||||
response = self.app.get('/discord/reset/')
|
@patch(MODULE_PATH + '.views.messages')
|
||||||
|
@patch(MODULE_PATH + '.views.DiscordUser.objects.add_user')
|
||||||
|
class TestDiscordCallback(SetupClassMixin, TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
DiscordUser.objects.create(user=self.user, uid=TEST_USER_ID)
|
||||||
|
|
||||||
self.assertRedirects(response, expected_url='/discord/activate/', target_status_code=302)
|
def test_success_message_when_ok(self, mock_add_user, mock_messages):
|
||||||
|
mock_add_user.return_value = True
|
||||||
|
request = self.factory.get(
|
||||||
|
reverse('discord:callback'), data={'code': '1234'}
|
||||||
|
)
|
||||||
|
request.user = self.user
|
||||||
|
response = discord_callback(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, self.services_url)
|
||||||
|
self.assertTrue(mock_messages.success.called)
|
||||||
|
self.assertFalse(mock_messages.error.called)
|
||||||
|
|
||||||
|
def test_handle_no_code(self, mock_add_user, mock_messages):
|
||||||
|
mock_add_user.return_value = True
|
||||||
|
request = self.factory.get(
|
||||||
|
reverse('discord:callback'), data={}
|
||||||
|
)
|
||||||
|
request.user = self.user
|
||||||
|
response = discord_callback(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, self.services_url)
|
||||||
|
self.assertFalse(mock_messages.success.called)
|
||||||
|
self.assertTrue(mock_messages.error.called)
|
||||||
|
|
||||||
|
def test_error_message_when_user_creation_failed(
|
||||||
|
self, mock_add_user, mock_messages
|
||||||
|
):
|
||||||
|
mock_add_user.return_value = False
|
||||||
|
request = self.factory.get(
|
||||||
|
reverse('discord:callback'), data={'code': '1234'}
|
||||||
|
)
|
||||||
|
request.user = self.user
|
||||||
|
response = discord_callback(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, self.services_url)
|
||||||
|
self.assertFalse(mock_messages.success.called)
|
||||||
|
self.assertTrue(mock_messages.error.called)
|
||||||
|
|
||||||
@mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager')
|
|
||||||
def test_deactivate(self, manager):
|
|
||||||
self.login()
|
|
||||||
DiscordUser.objects.create(user=self.member, uid='12345')
|
|
||||||
manager.delete_user.return_value = True
|
|
||||||
|
|
||||||
response = self.app.get('/discord/deactivate/')
|
@patch(MODULE_PATH + '.views.DiscordUser.objects.generate_bot_add_url')
|
||||||
|
class TestDiscordAddBot(TestCase):
|
||||||
self.assertTrue(manager.delete_user.called)
|
|
||||||
self.assertRedirects(response, expected_url='/services/', target_status_code=200)
|
def test_add_bot(self, mock_generate_bot_add_url):
|
||||||
with self.assertRaises(ObjectDoesNotExist):
|
bot_url = 'https://www.example.com/bot'
|
||||||
discord_user = User.objects.get(pk=self.member.pk).discord
|
mock_generate_bot_add_url.return_value = bot_url
|
||||||
|
my_user = User.objects.create_superuser('Lex Luthor', 'abc', 'def')
|
||||||
|
request = RequestFactory().get(reverse('discord:add_bot'))
|
||||||
|
request.user = my_user
|
||||||
|
response = discord_add_bot(request)
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(response.url, bot_url)
|
||||||
|
|||||||
89
allianceauth/services/modules/discord/utils.py
Normal file
89
allianceauth/services/modules/discord/utils.py
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class LoggerAddTag(logging.LoggerAdapter):
|
||||||
|
"""add custom tag to a logger"""
|
||||||
|
def __init__(self, logger, prefix):
|
||||||
|
super(LoggerAddTag, self).__init__(logger, {})
|
||||||
|
self.prefix = prefix
|
||||||
|
|
||||||
|
def process(self, msg, kwargs):
|
||||||
|
return '[%s] %s' % (self.prefix, msg), kwargs
|
||||||
|
|
||||||
|
|
||||||
|
def clean_setting(
|
||||||
|
name: str,
|
||||||
|
default_value: object,
|
||||||
|
min_value: int = None,
|
||||||
|
max_value: int = None,
|
||||||
|
required_type: type = None
|
||||||
|
):
|
||||||
|
"""cleans the input for a custom setting
|
||||||
|
|
||||||
|
Will use `default_value` if settings does not exit or has the wrong type
|
||||||
|
or is outside define boundaries (for int only)
|
||||||
|
|
||||||
|
Need to define `required_type` if `default_value` is `None`
|
||||||
|
|
||||||
|
Will assume `min_value` of 0 for int (can be overriden)
|
||||||
|
|
||||||
|
Returns cleaned value for setting
|
||||||
|
"""
|
||||||
|
if default_value is None and not required_type:
|
||||||
|
raise ValueError('You must specify a required_type for None defaults')
|
||||||
|
|
||||||
|
if not required_type:
|
||||||
|
required_type = type(default_value)
|
||||||
|
|
||||||
|
if min_value is None and required_type == int:
|
||||||
|
min_value = 0
|
||||||
|
|
||||||
|
if not hasattr(settings, name):
|
||||||
|
cleaned_value = default_value
|
||||||
|
else:
|
||||||
|
if (
|
||||||
|
isinstance(getattr(settings, name), required_type)
|
||||||
|
and (min_value is None or getattr(settings, name) >= min_value)
|
||||||
|
and (max_value is None or getattr(settings, name) <= max_value)
|
||||||
|
):
|
||||||
|
cleaned_value = getattr(settings, name)
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
'You setting for %s it not valid. Please correct it. '
|
||||||
|
'Using default for now: %s',
|
||||||
|
name,
|
||||||
|
default_value
|
||||||
|
)
|
||||||
|
cleaned_value = default_value
|
||||||
|
return cleaned_value
|
||||||
|
|
||||||
|
|
||||||
|
def set_logger_to_file(logger_name: str, name: str) -> object:
|
||||||
|
"""set logger for current module to log into a file. Useful for tests.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- logger: current logger object
|
||||||
|
- name: name of current module, e.g. __file__
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- amended logger
|
||||||
|
"""
|
||||||
|
|
||||||
|
# reconfigure logger so we get logging from tested module
|
||||||
|
f_format = logging.Formatter(
|
||||||
|
'%(asctime)s - %(levelname)s - %(module)s:%(funcName)s - %(message)s'
|
||||||
|
)
|
||||||
|
path = os.path.splitext(name)[0]
|
||||||
|
f_handler = logging.FileHandler('{}.log'.format(path), 'w+')
|
||||||
|
f_handler.setFormatter(f_format)
|
||||||
|
logger = logging.getLogger(logger_name)
|
||||||
|
logger.level = logging.DEBUG
|
||||||
|
logger.addHandler(f_handler)
|
||||||
|
logger.propagate = False
|
||||||
|
return logger
|
||||||
@@ -9,10 +9,12 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
|
|
||||||
from allianceauth.services.views import superuser_test
|
from allianceauth.services.views import superuser_test
|
||||||
|
|
||||||
from .manager import DiscordOAuthManager
|
from . import __title__
|
||||||
from .tasks import DiscordTasks
|
from .models import DiscordUser
|
||||||
|
from .utils import LoggerAddTag
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
logger = LoggerAddTag(logging.getLogger(__name__), __title__)
|
||||||
|
|
||||||
ACCESS_PERM = 'discord.access_discord'
|
ACCESS_PERM = 'discord.access_discord'
|
||||||
|
|
||||||
@@ -20,53 +22,94 @@ ACCESS_PERM = 'discord.access_discord'
|
|||||||
@login_required
|
@login_required
|
||||||
@permission_required(ACCESS_PERM)
|
@permission_required(ACCESS_PERM)
|
||||||
def deactivate_discord(request):
|
def deactivate_discord(request):
|
||||||
logger.debug("deactivate_discord called by user %s" % request.user)
|
logger.debug("deactivate_discord called by user %s", request.user)
|
||||||
if DiscordTasks.delete_user(request.user):
|
if request.user.discord.delete_user(is_rate_limited=False):
|
||||||
logger.info("Successfully deactivated discord for user %s" % request.user)
|
logger.info("Successfully deactivated discord for user %s", request.user)
|
||||||
messages.success(request, _('Deactivated Discord account.'))
|
messages.success(request, _('Deactivated Discord account.'))
|
||||||
else:
|
else:
|
||||||
logger.error("Unsuccessful attempt to deactivate discord for user %s" % request.user)
|
logger.error(
|
||||||
messages.error(request, _('An error occurred while processing your Discord account.'))
|
"Unsuccessful attempt to deactivate discord for user %s", request.user
|
||||||
|
)
|
||||||
|
messages.error(
|
||||||
|
request, _('An error occurred while processing your Discord account.')
|
||||||
|
)
|
||||||
return redirect("services:services")
|
return redirect("services:services")
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@permission_required(ACCESS_PERM)
|
@permission_required(ACCESS_PERM)
|
||||||
def reset_discord(request):
|
def reset_discord(request):
|
||||||
logger.debug("reset_discord called by user %s" % request.user)
|
logger.debug("reset_discord called by user %s", request.user)
|
||||||
if DiscordTasks.delete_user(request.user):
|
if request.user.discord.delete_user(is_rate_limited=False):
|
||||||
logger.info("Successfully deleted discord user for user %s - forwarding to discord activation." % request.user)
|
logger.info(
|
||||||
|
"Successfully deleted discord user for user %s - "
|
||||||
|
"forwarding to discord activation.",
|
||||||
|
request.user
|
||||||
|
)
|
||||||
return redirect("discord:activate")
|
return redirect("discord:activate")
|
||||||
logger.error("Unsuccessful attempt to reset discord for user %s" % request.user)
|
|
||||||
messages.error(request, _('An error occurred while processing your Discord account.'))
|
logger.error(
|
||||||
|
"Unsuccessful attempt to reset discord for user %s", request.user
|
||||||
|
)
|
||||||
|
messages.error(
|
||||||
|
request, _('An error occurred while processing your Discord account.')
|
||||||
|
)
|
||||||
return redirect("services:services")
|
return redirect("services:services")
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@permission_required(ACCESS_PERM)
|
@permission_required(ACCESS_PERM)
|
||||||
def activate_discord(request):
|
def activate_discord(request):
|
||||||
logger.debug("activate_discord called by user %s" % request.user)
|
logger.debug("activate_discord called by user %s", request.user)
|
||||||
return redirect(DiscordOAuthManager.generate_oauth_redirect_url())
|
return redirect(DiscordUser.objects.generate_oauth_redirect_url())
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@permission_required(ACCESS_PERM)
|
@permission_required(ACCESS_PERM)
|
||||||
def discord_callback(request):
|
def discord_callback(request):
|
||||||
logger.debug("Received Discord callback for activation of user %s" % request.user)
|
logger.debug(
|
||||||
code = request.GET.get('code', None)
|
"Received Discord callback for activation of user %s", request.user
|
||||||
if not code:
|
)
|
||||||
logger.warn("Did not receive OAuth code from callback of user %s" % request.user)
|
authorization_code = request.GET.get('code', None)
|
||||||
return redirect("services:services")
|
if not authorization_code:
|
||||||
if DiscordTasks.add_user(request.user, code):
|
logger.warning(
|
||||||
logger.info("Successfully activated Discord for user %s" % request.user)
|
"Did not receive OAuth code from callback for user %s", request.user
|
||||||
messages.success(request, _('Activated Discord account.'))
|
)
|
||||||
|
success = False
|
||||||
else:
|
else:
|
||||||
logger.error("Failed to activate Discord for user %s" % request.user)
|
if DiscordUser.objects.add_user(
|
||||||
messages.error(request, _('An error occurred while processing your Discord account.'))
|
user=request.user,
|
||||||
|
authorization_code=authorization_code,
|
||||||
|
is_rate_limited=False
|
||||||
|
):
|
||||||
|
logger.info(
|
||||||
|
"Successfully activated Discord account for user %s", request.user
|
||||||
|
)
|
||||||
|
success = True
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.error(
|
||||||
|
"Failed to activate Discord account for user %s", request.user
|
||||||
|
)
|
||||||
|
success = False
|
||||||
|
|
||||||
|
if success:
|
||||||
|
messages.success(
|
||||||
|
request, _('Your Discord account has been successfully activated.')
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
messages.error(
|
||||||
|
request,
|
||||||
|
_(
|
||||||
|
'An error occurred while trying to activate your Discord account. '
|
||||||
|
'Please try again.'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return redirect("services:services")
|
return redirect("services:services")
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@user_passes_test(superuser_test)
|
@user_passes_test(superuser_test)
|
||||||
def discord_add_bot(request):
|
def discord_add_bot(request):
|
||||||
return redirect(DiscordOAuthManager.generate_bot_add_url())
|
return redirect(DiscordUser.objects.generate_bot_add_url())
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ class DiscourseManager:
|
|||||||
for arg in kwargs:
|
for arg in kwargs:
|
||||||
if arg not in endpoint['args']['required'] and arg not in endpoint['args']['optional'] and not silent:
|
if arg not in endpoint['args']['required'] and arg not in endpoint['args']['optional'] and not silent:
|
||||||
logger.warn("Received unrecognized kwarg %s for endpoint %s" % (arg, endpoint))
|
logger.warn("Received unrecognized kwarg %s for endpoint %s" % (arg, endpoint))
|
||||||
r = getattr(requests, endpoint['method'])(settings.DISCOURSE_URL + endpoint['parsed_url'], params=params,
|
r = getattr(requests, endpoint['method'])(settings.DISCOURSE_URL + endpoint['parsed_url'], headers=params,
|
||||||
json=data)
|
json=data)
|
||||||
try:
|
try:
|
||||||
if 'errors' in r.json() and not silent:
|
if 'errors' in r.json() and not silent:
|
||||||
@@ -185,6 +185,7 @@ class DiscourseManager:
|
|||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except requests.exceptions.HTTPError as e:
|
except requests.exceptions.HTTPError as e:
|
||||||
raise DiscourseError(endpoint, e.response.status_code)
|
raise DiscourseError(endpoint, e.response.status_code)
|
||||||
|
logger.debug("Discourse API output:\n{}".format(out)) # this is spamy as hell remove before release
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ class MumbleService(ServicesHook):
|
|||||||
if MumbleTasks.has_account(user):
|
if MumbleTasks.has_account(user):
|
||||||
MumbleTasks.update_groups.delay(user.pk)
|
MumbleTasks.update_groups.delay(user.pk)
|
||||||
|
|
||||||
|
def sync_nickname(self, user):
|
||||||
|
logger.debug("Updating %s nickname for %s" % (self.name, user))
|
||||||
|
if MumbleTasks.has_account(user):
|
||||||
|
MumbleTasks.update_display_name.apply_async(args=[user.pk], countdown=5) # cooldown on this task to ensure DB clean when syncing
|
||||||
|
|
||||||
def validate_user(self, user):
|
def validate_user(self, user):
|
||||||
if MumbleTasks.has_account(user) and not self.service_active_for_user(user):
|
if MumbleTasks.has_account(user) and not self.service_active_for_user(user):
|
||||||
self.delete_user(user, notify_user=True)
|
self.delete_user(user, notify_user=True)
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
# Generated by Django 2.2.9 on 2020-03-16 07:49
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mumble', '0007_not_null_user'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='mumbleuser',
|
||||||
|
name='display_name',
|
||||||
|
field=models.CharField(max_length=254, null=True),
|
||||||
|
)
|
||||||
|
]
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
from django.db import migrations, models
|
||||||
|
from ..auth_hooks import MumbleService
|
||||||
|
from allianceauth.services.hooks import NameFormatter
|
||||||
|
|
||||||
|
def fwd_func(apps, schema_editor):
|
||||||
|
MumbleUser = apps.get_model("mumble", "MumbleUser")
|
||||||
|
db_alias = schema_editor.connection.alias
|
||||||
|
all_users = MumbleUser.objects.using(db_alias).all()
|
||||||
|
for user in all_users:
|
||||||
|
display_name = NameFormatter(MumbleService(), user.user).format_name()
|
||||||
|
user.display_name = display_name
|
||||||
|
user.save()
|
||||||
|
|
||||||
|
def rev_func(apps, schema_editor):
|
||||||
|
MumbleUser = apps.get_model("mumble", "MumbleUser")
|
||||||
|
db_alias = schema_editor.connection.alias
|
||||||
|
all_users = MumbleUser.objects.using(db_alias).all()
|
||||||
|
for user in all_users:
|
||||||
|
user.display_name = None
|
||||||
|
user.save()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mumble', '0008_mumbleuser_display_name'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(fwd_func, rev_func),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='mumbleuser',
|
||||||
|
name='display_name',
|
||||||
|
field=models.CharField(max_length=254, unique=True),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -15,10 +15,14 @@ class MumbleManager(models.Manager):
|
|||||||
HASH_FN = 'bcrypt-sha256'
|
HASH_FN = 'bcrypt-sha256'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_username(user):
|
def get_display_name(user):
|
||||||
from .auth_hooks import MumbleService
|
from .auth_hooks import MumbleService
|
||||||
return NameFormatter(MumbleService(), user).format_name()
|
return NameFormatter(MumbleService(), user).format_name()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_username(user):
|
||||||
|
return user.profile.main_character.character_name # main character as the user.username may be incorect
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def sanitise_username(username):
|
def sanitise_username(username):
|
||||||
return username.replace(" ", "_")
|
return username.replace(" ", "_")
|
||||||
@@ -32,20 +36,26 @@ class MumbleManager(models.Manager):
|
|||||||
return bcrypt_sha256.encrypt(password.encode('utf-8'))
|
return bcrypt_sha256.encrypt(password.encode('utf-8'))
|
||||||
|
|
||||||
def create(self, user):
|
def create(self, user):
|
||||||
username = self.get_username(user)
|
try:
|
||||||
logger.debug("Creating mumble user with username {}".format(username))
|
username = self.get_username(user)
|
||||||
username_clean = self.sanitise_username(username)
|
logger.debug("Creating mumble user with username {}".format(username))
|
||||||
password = self.generate_random_pass()
|
username_clean = self.sanitise_username(username)
|
||||||
pwhash = self.gen_pwhash(password)
|
display_name = self.get_display_name(user)
|
||||||
logger.debug("Proceeding with mumble user creation: clean username {}, pwhash starts with {}".format(
|
password = self.generate_random_pass()
|
||||||
username_clean, pwhash[0:5]))
|
pwhash = self.gen_pwhash(password)
|
||||||
logger.info("Creating mumble user {}".format(username_clean))
|
logger.debug("Proceeding with mumble user creation: clean username {}, pwhash starts with {}".format(
|
||||||
|
username_clean, pwhash[0:5]))
|
||||||
|
logger.info("Creating mumble user {}".format(username_clean))
|
||||||
|
|
||||||
result = super(MumbleManager, self).create(user=user, username=username_clean,
|
result = super(MumbleManager, self).create(user=user, username=username_clean,
|
||||||
pwhash=pwhash, hashfn=self.HASH_FN)
|
pwhash=pwhash, hashfn=self.HASH_FN,
|
||||||
result.update_groups()
|
display_name=display_name)
|
||||||
result.credentials.update({'username': result.username, 'password': password})
|
result.update_groups()
|
||||||
return result
|
result.credentials.update({'username': result.username, 'password': password})
|
||||||
|
return result
|
||||||
|
except AttributeError: # No Main or similar errors
|
||||||
|
return False
|
||||||
|
return False
|
||||||
|
|
||||||
def user_exists(self, username):
|
def user_exists(self, username):
|
||||||
return self.filter(username=username).exists()
|
return self.filter(username=username).exists()
|
||||||
@@ -59,6 +69,8 @@ class MumbleUser(AbstractServiceModel):
|
|||||||
|
|
||||||
objects = MumbleManager()
|
objects = MumbleManager()
|
||||||
|
|
||||||
|
display_name = models.CharField(max_length=254, unique=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.username
|
return self.username
|
||||||
|
|
||||||
@@ -91,6 +103,12 @@ class MumbleUser(AbstractServiceModel):
|
|||||||
self.save()
|
self.save()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def update_display_name(self):
|
||||||
|
logger.info("Updating mumble user {} display name".format(self.user))
|
||||||
|
self.display_name = MumbleManager.get_display_name(self.user)
|
||||||
|
self.save()
|
||||||
|
return True
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
permissions = (
|
permissions = (
|
||||||
("access_mumble", u"Can access the Mumble service"),
|
("access_mumble", u"Can access the Mumble service"),
|
||||||
|
|||||||
@@ -45,9 +45,37 @@ class MumbleTasks:
|
|||||||
logger.debug("User %s does not have a mumble account, skipping" % user)
|
logger.debug("User %s does not have a mumble account, skipping" % user)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@shared_task(bind=True, name="mumble.update_display_name", base=QueueOnce)
|
||||||
|
def update_display_name(self, pk):
|
||||||
|
user = User.objects.get(pk=pk)
|
||||||
|
logger.debug("Updating mumble groups for user %s" % user)
|
||||||
|
if MumbleTasks.has_account(user):
|
||||||
|
try:
|
||||||
|
if not user.mumble.update_display_name():
|
||||||
|
raise Exception("Display Name Sync failed")
|
||||||
|
logger.debug("Updated user %s mumble display name." % user)
|
||||||
|
return True
|
||||||
|
except MumbleUser.DoesNotExist:
|
||||||
|
logger.info("Mumble display name sync failed for {}, user does not have a mumble account".format(user))
|
||||||
|
except:
|
||||||
|
logger.exception("Mumble display name sync failed for %s, retrying in 10 mins" % user)
|
||||||
|
raise self.retry(countdown=60 * 10)
|
||||||
|
else:
|
||||||
|
logger.debug("User %s does not have a mumble account, skipping" % user)
|
||||||
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@shared_task(name="mumble.update_all_groups")
|
@shared_task(name="mumble.update_all_groups")
|
||||||
def update_all_groups():
|
def update_all_groups():
|
||||||
logger.debug("Updating ALL mumble groups")
|
logger.debug("Updating ALL mumble groups")
|
||||||
for mumble_user in MumbleUser.objects.exclude(username__exact=''):
|
for mumble_user in MumbleUser.objects.exclude(username__exact=''):
|
||||||
MumbleTasks.update_groups.delay(mumble_user.user.pk)
|
MumbleTasks.update_groups.delay(mumble_user.user.pk)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@shared_task(name="mumble.update_all_display_names")
|
||||||
|
def update_all_display_names():
|
||||||
|
logger.debug("Updating ALL mumble display names")
|
||||||
|
for mumble_user in MumbleUser.objects.exclude(username__exact=''):
|
||||||
|
MumbleTasks.update_display_name.delay(mumble_user.user.pk)
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ class MumbleHooksTestCase(TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.member = 'member_user'
|
self.member = 'member_user'
|
||||||
member = AuthUtils.create_member(self.member)
|
member = AuthUtils.create_member(self.member)
|
||||||
|
AuthUtils.add_main_character(member, 'auth_member', '12345', corp_id='111', corp_name='Test Corporation',
|
||||||
|
corp_ticker='TESTR')
|
||||||
|
member = User.objects.get(pk=member.pk)
|
||||||
MumbleUser.objects.create(user=member)
|
MumbleUser.objects.create(user=member)
|
||||||
self.none_user = 'none_user'
|
self.none_user = 'none_user'
|
||||||
none_user = AuthUtils.create_user(self.none_user)
|
none_user = AuthUtils.create_user(self.none_user)
|
||||||
@@ -122,23 +125,45 @@ class MumbleViewsTestCase(TestCase):
|
|||||||
self.member.save()
|
self.member.save()
|
||||||
AuthUtils.add_main_character(self.member, 'auth_member', '12345', corp_id='111', corp_name='Test Corporation',
|
AuthUtils.add_main_character(self.member, 'auth_member', '12345', corp_id='111', corp_name='Test Corporation',
|
||||||
corp_ticker='TESTR')
|
corp_ticker='TESTR')
|
||||||
|
self.member = User.objects.get(pk=self.member.pk)
|
||||||
add_permissions()
|
add_permissions()
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
self.client.force_login(self.member)
|
self.client.force_login(self.member)
|
||||||
|
|
||||||
def test_activate(self):
|
def test_activate_update(self):
|
||||||
self.login()
|
self.login()
|
||||||
expected_username = '[TESTR]auth_member'
|
expected_username = 'auth_member'
|
||||||
|
expected_displayname = '[TESTR]auth_member'
|
||||||
response = self.client.get(urls.reverse('mumble:activate'), follow=False)
|
response = self.client.get(urls.reverse('mumble:activate'), follow=False)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertContains(response, expected_username)
|
self.assertContains(response, expected_username)
|
||||||
|
# create
|
||||||
mumble_user = MumbleUser.objects.get(user=self.member)
|
mumble_user = MumbleUser.objects.get(user=self.member)
|
||||||
self.assertEqual(mumble_user.username, expected_username)
|
self.assertEqual(mumble_user.username, expected_username)
|
||||||
|
self.assertTrue(MumbleUser.objects.user_exists(expected_username))
|
||||||
|
self.assertEqual(str(mumble_user), expected_username)
|
||||||
|
self.assertEqual(mumble_user.display_name, expected_displayname)
|
||||||
self.assertTrue(mumble_user.pwhash)
|
self.assertTrue(mumble_user.pwhash)
|
||||||
self.assertIn('Guest', mumble_user.groups)
|
self.assertIn('Guest', mumble_user.groups)
|
||||||
self.assertIn('Member', mumble_user.groups)
|
self.assertIn('Member', mumble_user.groups)
|
||||||
self.assertIn(',', mumble_user.groups)
|
self.assertIn(',', mumble_user.groups)
|
||||||
|
# test update
|
||||||
|
self.member.profile.main_character.character_name = "auth_member_updated"
|
||||||
|
self.member.profile.main_character.corporation_ticker = "TESTU"
|
||||||
|
self.member.profile.main_character.save()
|
||||||
|
mumble_user.update_display_name()
|
||||||
|
mumble_user = MumbleUser.objects.get(user=self.member)
|
||||||
|
expected_displayname = '[TESTU]auth_member_updated'
|
||||||
|
self.assertEqual(mumble_user.username, expected_username)
|
||||||
|
self.assertTrue(MumbleUser.objects.user_exists(expected_username))
|
||||||
|
self.assertEqual(str(mumble_user), expected_username)
|
||||||
|
self.assertEqual(mumble_user.display_name, expected_displayname)
|
||||||
|
self.assertTrue(mumble_user.pwhash)
|
||||||
|
self.assertIn('Guest', mumble_user.groups)
|
||||||
|
self.assertIn('Member', mumble_user.groups)
|
||||||
|
self.assertIn(',', mumble_user.groups)
|
||||||
|
|
||||||
|
|
||||||
def test_deactivate_post(self):
|
def test_deactivate_post(self):
|
||||||
self.login()
|
self.login()
|
||||||
@@ -171,7 +196,6 @@ class MumbleViewsTestCase(TestCase):
|
|||||||
self.assertTemplateUsed(response, 'services/service_credentials.html')
|
self.assertTemplateUsed(response, 'services/service_credentials.html')
|
||||||
self.assertContains(response, 'auth_member')
|
self.assertContains(response, 'auth_member')
|
||||||
|
|
||||||
|
|
||||||
class MumbleManagerTestCase(TestCase):
|
class MumbleManagerTestCase(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
from .models import MumbleManager
|
from .models import MumbleManager
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.contrib.auth.models import User, Group, Permission
|
from django.contrib.auth.models import User, Group, Permission
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models.signals import m2m_changed
|
from django.db.models.signals import m2m_changed
|
||||||
from django.db.models.signals import pre_delete
|
from django.db.models.signals import pre_delete
|
||||||
@@ -11,6 +12,7 @@ from .tasks import disable_user
|
|||||||
|
|
||||||
from allianceauth.authentication.models import State, UserProfile
|
from allianceauth.authentication.models import State, UserProfile
|
||||||
from allianceauth.authentication.signals import state_changed
|
from allianceauth.authentication.signals import state_changed
|
||||||
|
from allianceauth.eveonline.models import EveCharacter
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -157,14 +159,45 @@ def disable_services_on_inactive(sender, instance, *args, **kwargs):
|
|||||||
|
|
||||||
|
|
||||||
@receiver(pre_save, sender=UserProfile)
|
@receiver(pre_save, sender=UserProfile)
|
||||||
def disable_services_on_no_main(sender, instance, *args, **kwargs):
|
def process_main_character_change(sender, instance, *args, **kwargs):
|
||||||
if not instance.pk:
|
|
||||||
|
if not instance.pk: # ignore
|
||||||
# new model being created
|
# new model being created
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
old_instance = UserProfile.objects.get(pk=instance.pk)
|
old_instance = UserProfile.objects.get(pk=instance.pk)
|
||||||
if old_instance.main_character and not instance.main_character:
|
if old_instance.main_character and not instance.main_character: # lost main char disable services
|
||||||
logger.info("Disabling services due to loss of main character for user {0}".format(instance.user))
|
logger.info("Disabling services due to loss of main character for user {0}".format(instance.user))
|
||||||
disable_user(instance.user)
|
disable_user(instance.user)
|
||||||
|
elif old_instance.main_character is not instance.main_character: # swapping/changing main character
|
||||||
|
logger.info("Updating Names due to change of main character for user {0}".format(instance.user))
|
||||||
|
for svc in ServicesHook.get_services():
|
||||||
|
try:
|
||||||
|
svc.validate_user(instance.user)
|
||||||
|
svc.sync_nickname(instance.user)
|
||||||
|
except:
|
||||||
|
logger.exception('Exception running sync_nickname for services module %s on user %s' % (svc, instance))
|
||||||
|
|
||||||
except UserProfile.DoesNotExist:
|
except UserProfile.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(pre_save, sender=EveCharacter)
|
||||||
|
def process_main_character_update(sender, instance, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
if instance.userprofile:
|
||||||
|
old_instance = EveCharacter.objects.get(pk=instance.pk)
|
||||||
|
if not instance.character_name == old_instance.character_name or \
|
||||||
|
not instance.corporation_name == old_instance.corporation_name or \
|
||||||
|
not instance.alliance_name == old_instance.alliance_name:
|
||||||
|
logger.info("syncing service nickname for user {0}".format(instance.userprofile.user))
|
||||||
|
|
||||||
|
for svc in ServicesHook.get_services():
|
||||||
|
try:
|
||||||
|
svc.validate_user(instance.userprofile.user)
|
||||||
|
svc.sync_nickname(instance.userprofile.user)
|
||||||
|
except:
|
||||||
|
logger.exception('Exception running sync_nickname for services module %s on user %s' % (svc, instance))
|
||||||
|
|
||||||
|
except ObjectDoesNotExist: # not a main char ignore
|
||||||
|
pass
|
||||||
|
|||||||
@@ -1,21 +1,17 @@
|
|||||||
from allianceauth import NAME
|
|
||||||
from esi.clients import esi_client_factory
|
|
||||||
import requests
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from allianceauth import NAME
|
||||||
|
from allianceauth.eveonline.providers import provider
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
SWAGGER_SPEC_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'swagger.json')
|
|
||||||
"""
|
|
||||||
Swagger Operations:
|
|
||||||
get_killmails_killmail_id_killmail_hash
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class SRPManager:
|
class SRPManager:
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_kill_id(killboard_link):
|
def get_kill_id(killboard_link):
|
||||||
num_set = '0123456789'
|
num_set = '0123456789'
|
||||||
@@ -34,18 +30,23 @@ class SRPManager:
|
|||||||
if result:
|
if result:
|
||||||
killmail_id = result['killmail_id']
|
killmail_id = result['killmail_id']
|
||||||
killmail_hash = result['zkb']['hash']
|
killmail_hash = result['zkb']['hash']
|
||||||
c = esi_client_factory(spec_file=SWAGGER_SPEC_PATH)
|
c = provider.client
|
||||||
km = c.Killmails.get_killmails_killmail_id_killmail_hash(killmail_id=killmail_id,
|
km = c.Killmails.get_killmails_killmail_id_killmail_hash(
|
||||||
killmail_hash=killmail_hash).result()
|
killmail_id=killmail_id,
|
||||||
|
killmail_hash=killmail_hash
|
||||||
|
).result()
|
||||||
else:
|
else:
|
||||||
raise ValueError("Invalid Kill ID")
|
raise ValueError("Invalid Kill ID")
|
||||||
if km:
|
if km:
|
||||||
ship_type = km['victim']['ship_type_id']
|
ship_type = km['victim']['ship_type_id']
|
||||||
logger.debug("Ship type for kill ID %s is %s" % (kill_id, ship_type))
|
logger.debug(
|
||||||
|
"Ship type for kill ID %s is %s" % (kill_id, ship_type)
|
||||||
|
)
|
||||||
ship_value = result['zkb']['totalValue']
|
ship_value = result['zkb']['totalValue']
|
||||||
logger.debug("Total loss value for kill id %s is %s" % (kill_id, ship_value))
|
logger.debug(
|
||||||
|
"Total loss value for kill id %s is %s" % (kill_id, ship_value)
|
||||||
|
)
|
||||||
victim_id = km['victim']['character_id']
|
victim_id = km['victim']['character_id']
|
||||||
return ship_type, ship_value, victim_id
|
return ship_type, ship_value, victim_id
|
||||||
else:
|
else:
|
||||||
raise ValueError("Invalid Kill ID or Hash.")
|
raise ValueError("Invalid Kill ID or Hash.")
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
|||||||
# Create your tests here.
|
|
||||||
0
allianceauth/srp/tests/__init__.py
Executable file
0
allianceauth/srp/tests/__init__.py
Executable file
72
allianceauth/srp/tests/test_managers.py
Executable file
72
allianceauth/srp/tests/test_managers.py
Executable file
@@ -0,0 +1,72 @@
|
|||||||
|
import inspect
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from unittest.mock import patch, Mock
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from ..managers import SRPManager
|
||||||
|
|
||||||
|
MODULE_PATH = 'allianceauth.srp.managers'
|
||||||
|
|
||||||
|
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(
|
||||||
|
inspect.currentframe()
|
||||||
|
)))
|
||||||
|
|
||||||
|
def load_data(filename):
|
||||||
|
"""loads given JSON file from `testdata` sub folder and returns content"""
|
||||||
|
with open(
|
||||||
|
currentdir + '/testdata/%s.json' % filename, 'r', encoding='utf-8'
|
||||||
|
) as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
class TestSrpManager(TestCase):
|
||||||
|
|
||||||
|
def test_can_extract_kill_id(self):
|
||||||
|
link = 'https://zkillboard.com/kill/81973979/'
|
||||||
|
expected = 81973979
|
||||||
|
self.assertEqual(int(SRPManager.get_kill_id(link)), expected)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.provider')
|
||||||
|
@patch(MODULE_PATH + '.requests.get')
|
||||||
|
def test_can_get_kill_data(self, mock_get, mock_provider):
|
||||||
|
mock_get.return_value.json.return_value = load_data(
|
||||||
|
'zkillboard_killmail_api_81973979'
|
||||||
|
)
|
||||||
|
mock_provider.client.Killmails.\
|
||||||
|
get_killmails_killmail_id_killmail_hash.return_value.\
|
||||||
|
result.return_value = load_data(
|
||||||
|
'get_killmails_killmail_id_killmail_hash_81973979'
|
||||||
|
)
|
||||||
|
|
||||||
|
ship_type, ship_value, victim_id = SRPManager.get_kill_data(81973979)
|
||||||
|
self.assertEqual(ship_type, 19720)
|
||||||
|
self.assertEqual(ship_value, 3177859026.86)
|
||||||
|
self.assertEqual(victim_id, 93330670)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.requests.get')
|
||||||
|
def test_invalid_id_for_zkb_raises_exception(self, mock_get):
|
||||||
|
mock_get.return_value.json.return_value = ['']
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
ship_type, ship_value, victim_id = SRPManager.get_kill_data(81973979)
|
||||||
|
|
||||||
|
@patch(MODULE_PATH + '.provider')
|
||||||
|
@patch(MODULE_PATH + '.requests.get')
|
||||||
|
def test_invalid_id_for_esi_raises_exception(
|
||||||
|
self, mock_get, mock_provider
|
||||||
|
):
|
||||||
|
mock_get.return_value.json.return_value = load_data(
|
||||||
|
'zkillboard_killmail_api_81973979'
|
||||||
|
)
|
||||||
|
mock_provider.client.Killmails.\
|
||||||
|
get_killmails_killmail_id_killmail_hash.return_value.\
|
||||||
|
result.return_value = None
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
ship_type, ship_value, victim_id = SRPManager.get_kill_data(81973979)
|
||||||
|
|
||||||
|
|
||||||
953
allianceauth/srp/tests/testdata/get_killmails_killmail_id_killmail_hash_81973979.json
vendored
Normal file
953
allianceauth/srp/tests/testdata/get_killmails_killmail_id_killmail_hash_81973979.json
vendored
Normal file
@@ -0,0 +1,953 @@
|
|||||||
|
{
|
||||||
|
"attackers": [
|
||||||
|
{
|
||||||
|
"alliance_id": 99009221,
|
||||||
|
"character_id": 92606407,
|
||||||
|
"corporation_id": 98343297,
|
||||||
|
"damage_done": 65236,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -6.4,
|
||||||
|
"ship_type_id": 47271,
|
||||||
|
"weapon_type_id": 47271
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 95104060,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 56425,
|
||||||
|
"final_blow": true,
|
||||||
|
"security_status": -1.1,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 2929
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 1220922756,
|
||||||
|
"character_id": 92793488,
|
||||||
|
"corporation_id": 679468421,
|
||||||
|
"damage_done": 55225,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -3.4,
|
||||||
|
"ship_type_id": 47271,
|
||||||
|
"weapon_type_id": 47271
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 90376343,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 51941,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": 0.6,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 28215
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 676848606,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 45906,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.6,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 31894
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 96692394,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 44900,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.9,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 31894
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 96624133,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 44146,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -9.1,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 2929
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"character_id": 95050100,
|
||||||
|
"corporation_id": 98497860,
|
||||||
|
"damage_done": 41517,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -3,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 458944878,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 39888,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -6.5,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 28215
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 96029663,
|
||||||
|
"corporation_id": 98433294,
|
||||||
|
"damage_done": 39406,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -6.7,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 28215
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 90626300,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 37808,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -0.6,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 31894
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 90740848,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 36342,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.8,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 2929
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 1105550086,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 35971,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -2.6,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 28215
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99003581,
|
||||||
|
"character_id": 94727582,
|
||||||
|
"corporation_id": 98514029,
|
||||||
|
"damage_done": 33501,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": 3.2,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 31894
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 90368224,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 32116,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -2.4,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 2929
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 90001595,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 31387,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -0.8,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 2456
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 95278082,
|
||||||
|
"corporation_id": 98418839,
|
||||||
|
"damage_done": 31250,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": 1.8,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 28215
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 91971344,
|
||||||
|
"corporation_id": 98217414,
|
||||||
|
"damage_done": 31247,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1,
|
||||||
|
"ship_type_id": 29986,
|
||||||
|
"weapon_type_id": 29986
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2113448089,
|
||||||
|
"corporation_id": 98418839,
|
||||||
|
"damage_done": 30174,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -0.4,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2115912819,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 29242,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -2.1,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2115885290,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 28009,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -2,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 95746094,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 27565,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -7.8,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99003581,
|
||||||
|
"character_id": 90345487,
|
||||||
|
"corporation_id": 98514029,
|
||||||
|
"damage_done": 26016,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": 0.5,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2115874625,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 25679,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.9,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2115880975,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 23320,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -3.5,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 96667534,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 21699,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -0.6,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2115866658,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 20506,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.3,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 97105982,
|
||||||
|
"corporation_id": 98217414,
|
||||||
|
"damage_done": 19400,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": 0,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99008704,
|
||||||
|
"character_id": 96110151,
|
||||||
|
"corporation_id": 98614116,
|
||||||
|
"damage_done": 17547,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -5.5,
|
||||||
|
"ship_type_id": 17740,
|
||||||
|
"weapon_type_id": 17740
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99009221,
|
||||||
|
"character_id": 90526637,
|
||||||
|
"corporation_id": 98343297,
|
||||||
|
"damage_done": 16791,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.9,
|
||||||
|
"ship_type_id": 33157,
|
||||||
|
"weapon_type_id": 21640
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2112972140,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 16749,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.2,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2115879470,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 14402,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -3.9,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 95698217,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 11546,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -5.2,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 353190170,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 10896,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -4.7,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 28215
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 91546798,
|
||||||
|
"corporation_id": 98217414,
|
||||||
|
"damage_done": 9872,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -0.7,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99009221,
|
||||||
|
"character_id": 91578428,
|
||||||
|
"corporation_id": 302750157,
|
||||||
|
"damage_done": 7699,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -8.7,
|
||||||
|
"ship_type_id": 17920,
|
||||||
|
"weapon_type_id": 2185
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 94105463,
|
||||||
|
"corporation_id": 98418839,
|
||||||
|
"damage_done": 5265,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -3.3,
|
||||||
|
"ship_type_id": 49713,
|
||||||
|
"weapon_type_id": 2488
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 95526304,
|
||||||
|
"corporation_id": 98418839,
|
||||||
|
"damage_done": 3967,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -8.7,
|
||||||
|
"ship_type_id": 22474,
|
||||||
|
"weapon_type_id": 2488
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 90331727,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 2940,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.2,
|
||||||
|
"ship_type_id": 49713,
|
||||||
|
"weapon_type_id": 2185
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2115880459,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 2301,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": 4.1,
|
||||||
|
"ship_type_id": 17738,
|
||||||
|
"weapon_type_id": 17738
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99009547,
|
||||||
|
"character_id": 1832436128,
|
||||||
|
"corporation_id": 98618666,
|
||||||
|
"damage_done": 937,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -8.1,
|
||||||
|
"ship_type_id": 17740,
|
||||||
|
"weapon_type_id": 3186
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99009547,
|
||||||
|
"character_id": 96632877,
|
||||||
|
"corporation_id": 98618666,
|
||||||
|
"damage_done": 430,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -5.4,
|
||||||
|
"ship_type_id": 17740,
|
||||||
|
"weapon_type_id": 3186
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 96146444,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 126,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.2,
|
||||||
|
"ship_type_id": 49713,
|
||||||
|
"weapon_type_id": 49713
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"character_id": 2116393370,
|
||||||
|
"corporation_id": 98593091,
|
||||||
|
"damage_done": 111,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": 0,
|
||||||
|
"ship_type_id": 602,
|
||||||
|
"weapon_type_id": 27321
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 93745147,
|
||||||
|
"corporation_id": 98418839,
|
||||||
|
"damage_done": 6,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.6,
|
||||||
|
"ship_type_id": 12021,
|
||||||
|
"weapon_type_id": 2873
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 95610468,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 4,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -2.2,
|
||||||
|
"ship_type_id": 12017,
|
||||||
|
"weapon_type_id": 484
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99009221,
|
||||||
|
"character_id": 92304254,
|
||||||
|
"corporation_id": 98343297,
|
||||||
|
"damage_done": 1,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -9.3,
|
||||||
|
"ship_type_id": 22474,
|
||||||
|
"weapon_type_id": 22474
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 96624034,
|
||||||
|
"corporation_id": 98493618,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -2.1,
|
||||||
|
"ship_type_id": 12017,
|
||||||
|
"weapon_type_id": 37611
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 95388762,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": 0.4,
|
||||||
|
"ship_type_id": 12017,
|
||||||
|
"weapon_type_id": 3001
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 1290463210,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.9,
|
||||||
|
"ship_type_id": 49713,
|
||||||
|
"weapon_type_id": 23707
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99009547,
|
||||||
|
"character_id": 95748579,
|
||||||
|
"corporation_id": 98618666,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -7.9,
|
||||||
|
"ship_type_id": 643,
|
||||||
|
"weapon_type_id": 16497
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2114899882,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -1.7,
|
||||||
|
"ship_type_id": 12017,
|
||||||
|
"weapon_type_id": 484
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 95624225,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -9.2,
|
||||||
|
"ship_type_id": 12017,
|
||||||
|
"weapon_type_id": 37608
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 93452185,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -2.1,
|
||||||
|
"ship_type_id": 22474,
|
||||||
|
"weapon_type_id": 7537
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2114109824,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -2.1,
|
||||||
|
"ship_type_id": 49713,
|
||||||
|
"weapon_type_id": 484
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alliance_id": 99006941,
|
||||||
|
"character_id": 2113100583,
|
||||||
|
"corporation_id": 98416134,
|
||||||
|
"damage_done": 0,
|
||||||
|
"final_blow": false,
|
||||||
|
"security_status": -0.2,
|
||||||
|
"ship_type_id": 49713,
|
||||||
|
"weapon_type_id": 484
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"killmail_id": 81973979,
|
||||||
|
"killmail_time": "2020-03-01T13:10:55Z",
|
||||||
|
"solar_system_id": 30002537,
|
||||||
|
"victim": {
|
||||||
|
"alliance_id": 99009333,
|
||||||
|
"character_id": 93330670,
|
||||||
|
"corporation_id": 98267621,
|
||||||
|
"damage_taken": 1127412,
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 41332,
|
||||||
|
"quantity_destroyed": 3,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 41332,
|
||||||
|
"quantity_dropped": 3,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 27,
|
||||||
|
"item_type_id": 20847,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 155,
|
||||||
|
"item_type_id": 33474,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 18,
|
||||||
|
"item_type_id": 2048,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 28,
|
||||||
|
"item_type_id": 4292,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 29001,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 22,
|
||||||
|
"item_type_id": 41218,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 133,
|
||||||
|
"item_type_id": 16275,
|
||||||
|
"quantity_destroyed": 1125,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 41330,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 41330,
|
||||||
|
"quantity_dropped": 4,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 94,
|
||||||
|
"item_type_id": 31452,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20028,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20028,
|
||||||
|
"quantity_dropped": 2,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 155,
|
||||||
|
"item_type_id": 41489,
|
||||||
|
"quantity_dropped": 48,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 13,
|
||||||
|
"item_type_id": 18708,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 21,
|
||||||
|
"item_type_id": 1978,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20026,
|
||||||
|
"quantity_destroyed": 3,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 29,
|
||||||
|
"item_type_id": 20847,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 21,
|
||||||
|
"item_type_id": 29001,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 155,
|
||||||
|
"item_type_id": 16275,
|
||||||
|
"quantity_dropped": 1250,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 155,
|
||||||
|
"item_type_id": 16299,
|
||||||
|
"quantity_destroyed": 6,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 155,
|
||||||
|
"item_type_id": 16299,
|
||||||
|
"quantity_dropped": 2,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 41489,
|
||||||
|
"quantity_dropped": 12,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 29,
|
||||||
|
"item_type_id": 37298,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 17,
|
||||||
|
"item_type_id": 40351,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 19,
|
||||||
|
"item_type_id": 41491,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 12,
|
||||||
|
"item_type_id": 2364,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20030,
|
||||||
|
"quantity_dropped": 3,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 14,
|
||||||
|
"item_type_id": 18708,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 28999,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 93,
|
||||||
|
"item_type_id": 30993,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 11,
|
||||||
|
"item_type_id": 2364,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 20,
|
||||||
|
"item_type_id": 29001,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 15,
|
||||||
|
"item_type_id": 40351,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 19,
|
||||||
|
"item_type_id": 41489,
|
||||||
|
"quantity_dropped": 4,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 21254,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 21254,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20032,
|
||||||
|
"quantity_destroyed": 3,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20032,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 16,
|
||||||
|
"item_type_id": 40351,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20843,
|
||||||
|
"quantity_dropped": 3,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 31,
|
||||||
|
"item_type_id": 37298,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20845,
|
||||||
|
"quantity_destroyed": 3,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 92,
|
||||||
|
"item_type_id": 30993,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 31,
|
||||||
|
"item_type_id": 20847,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 133,
|
||||||
|
"item_type_id": 16274,
|
||||||
|
"quantity_dropped": 141666,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 20,
|
||||||
|
"item_type_id": 1978,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20847,
|
||||||
|
"quantity_destroyed": 6,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 20847,
|
||||||
|
"quantity_dropped": 3,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 5,
|
||||||
|
"item_type_id": 21246,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 27,
|
||||||
|
"item_type_id": 37298,
|
||||||
|
"quantity_dropped": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 90,
|
||||||
|
"item_type_id": 585,
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"flag": 94,
|
||||||
|
"item_type_id": 31159,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 20,
|
||||||
|
"item_type_id": 3831,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 93,
|
||||||
|
"item_type_id": 31159,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 22,
|
||||||
|
"item_type_id": 9568,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 12,
|
||||||
|
"item_type_id": 1405,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 92,
|
||||||
|
"item_type_id": 31159,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 21,
|
||||||
|
"item_type_id": 2553,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 11,
|
||||||
|
"item_type_id": 1405,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"flag": 19,
|
||||||
|
"item_type_id": 5971,
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"quantity_destroyed": 1,
|
||||||
|
"singleton": 0
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"position": {
|
||||||
|
"x": 55026869426.47358,
|
||||||
|
"y": 7310382040.828209,
|
||||||
|
"z": -163690355689.8978
|
||||||
|
},
|
||||||
|
"ship_type_id": 19720
|
||||||
|
}
|
||||||
|
}
|
||||||
15
allianceauth/srp/tests/testdata/zkillboard_killmail_api_81973979.json
vendored
Normal file
15
allianceauth/srp/tests/testdata/zkillboard_killmail_api_81973979.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"killmail_id": 81973979,
|
||||||
|
"zkb": {
|
||||||
|
"locationID": 60004816,
|
||||||
|
"hash": "e88a5fa7f342fa658ebe74a055b7679e28b05628",
|
||||||
|
"fittedValue": 1532365686.21,
|
||||||
|
"totalValue": 3177859026.86,
|
||||||
|
"points": 1,
|
||||||
|
"npc": false,
|
||||||
|
"solo": false,
|
||||||
|
"awox": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
{% extends "allianceauth/base.html" %}
|
|
||||||
{% load i18n %}
|
|
||||||
|
|
||||||
{% block page_title %}{% trans "Help" %}{% endblock page_title %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
<div class="col-lg-12">
|
|
||||||
|
|
||||||
<h1 class="page-header text-center">{% trans "Help" %}</h1>
|
|
||||||
|
|
||||||
<div class="container-fluid">
|
|
||||||
<div class="embed-responsive embed-responsive-16by9">
|
|
||||||
<iframe class="embed-responsive-item" src="https://allianceauth.readthedocs.io/en/latest/features/"></iframe>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{% endblock content %}
|
|
||||||
|
|
||||||
@@ -26,15 +26,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% menu_items %}
|
{% menu_items %}
|
||||||
|
|
||||||
{% if user.is_superuser %}
|
|
||||||
<li>
|
|
||||||
<a class="{% navactive request 'authentication:help' %}"
|
|
||||||
href="{% url 'authentication:help' %}">
|
|
||||||
<i class="fa fa-question fa-fw"></i> {% trans "Help" %}
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
{% endif %}
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -39,6 +39,13 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
<li><a href="{% url 'authentication:login' %}">{% trans "Login" %}</a></li>
|
<li><a href="{% url 'authentication:login' %}">{% trans "Login" %}</a></li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if user.is_superuser %}
|
||||||
|
<li>
|
||||||
|
<a class="navbar-brand" style="margin-left: -4px;" href="https://allianceauth.readthedocs.io/" target="_blank">
|
||||||
|
<i class="fa fa-question-circle fa-fw"></i>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
<form id="f-lang-select" class="navbar-form navbar-right" action="{% url 'set_language' %}" method="post">
|
<form id="f-lang-select" class="navbar-form navbar-right" action="{% url 'set_language' %}" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ from allianceauth.services.signals import (
|
|||||||
m2m_changed_group_permissions,
|
m2m_changed_group_permissions,
|
||||||
m2m_changed_user_permissions,
|
m2m_changed_user_permissions,
|
||||||
m2m_changed_state_permissions,
|
m2m_changed_state_permissions,
|
||||||
m2m_changed_user_groups, disable_services_on_inactive
|
m2m_changed_user_groups, disable_services_on_inactive,
|
||||||
|
process_main_character_change,
|
||||||
|
process_main_character_update
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -115,6 +117,8 @@ class AuthUtils:
|
|||||||
post_save.disconnect(state_saved, sender=State)
|
post_save.disconnect(state_saved, sender=State)
|
||||||
post_save.disconnect(reassess_on_profile_save, sender=UserProfile)
|
post_save.disconnect(reassess_on_profile_save, sender=UserProfile)
|
||||||
pre_save.disconnect(assign_state_on_active_change, sender=User)
|
pre_save.disconnect(assign_state_on_active_change, sender=User)
|
||||||
|
pre_save.disconnect(process_main_character_change, sender=UserProfile)
|
||||||
|
pre_save.disconnect(process_main_character_update, sender=EveCharacter)
|
||||||
post_save.disconnect(
|
post_save.disconnect(
|
||||||
check_state_on_character_update, sender=EveCharacter
|
check_state_on_character_update, sender=EveCharacter
|
||||||
)
|
)
|
||||||
@@ -131,7 +135,9 @@ class AuthUtils:
|
|||||||
m2m_changed.connect(state_member_alliances_changed, sender=State.member_alliances.through)
|
m2m_changed.connect(state_member_alliances_changed, sender=State.member_alliances.through)
|
||||||
post_save.connect(state_saved, sender=State)
|
post_save.connect(state_saved, sender=State)
|
||||||
post_save.connect(reassess_on_profile_save, sender=UserProfile)
|
post_save.connect(reassess_on_profile_save, sender=UserProfile)
|
||||||
pre_save.connect(assign_state_on_active_change, sender=User)
|
pre_save.connect(assign_state_on_active_change, sender=User)
|
||||||
|
pre_save.connect(process_main_character_change, sender=UserProfile)
|
||||||
|
pre_save.connect(process_main_character_update, sender=EveCharacter)
|
||||||
post_save.connect(check_state_on_character_update, sender=EveCharacter)
|
post_save.connect(check_state_on_character_update, sender=EveCharacter)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
from django.views.generic.base import View
|
from django.views.generic.base import View
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
|
from django.shortcuts import redirect
|
||||||
|
from django.contrib import messages
|
||||||
|
|
||||||
|
|
||||||
class NightModeRedirectView(View):
|
class NightModeRedirectView(View):
|
||||||
SESSION_VAR = 'NIGHT_MODE'
|
SESSION_VAR = "NIGHT_MODE"
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
request.session[self.SESSION_VAR] = not self.night_mode_state(request)
|
request.session[self.SESSION_VAR] = not self.night_mode_state(request)
|
||||||
return HttpResponseRedirect(request.GET.get('next', '/'))
|
return HttpResponseRedirect(request.GET.get("next", "/"))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def night_mode_state(cls, request):
|
def night_mode_state(cls, request):
|
||||||
@@ -17,3 +19,39 @@ class NightModeRedirectView(View):
|
|||||||
# Session is middleware
|
# Session is middleware
|
||||||
# Sometimes request wont have a session attribute
|
# Sometimes request wont have a session attribute
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def Generic500Redirect(request):
|
||||||
|
messages.error(
|
||||||
|
request,
|
||||||
|
"Auth encountered an error processing your request, please try again. "
|
||||||
|
"If the error persists, please contact the administrators. (500 Internal Server Error)",
|
||||||
|
)
|
||||||
|
return redirect("authentication:dashboard")
|
||||||
|
|
||||||
|
|
||||||
|
def Generic404Redirect(request, exception):
|
||||||
|
messages.error(
|
||||||
|
request,
|
||||||
|
"Page does not exist. If you believe this is in error please contact the administrators. "
|
||||||
|
"(404 Page Not Found)",
|
||||||
|
)
|
||||||
|
return redirect("authentication:dashboard")
|
||||||
|
|
||||||
|
|
||||||
|
def Generic403Redirect(request, exception):
|
||||||
|
messages.error(
|
||||||
|
request,
|
||||||
|
"You do not have permission to access the requested page. "
|
||||||
|
"If you believe this is in error please contact the administrators. (403 Permission Denied)",
|
||||||
|
)
|
||||||
|
return redirect("authentication:dashboard")
|
||||||
|
|
||||||
|
|
||||||
|
def Generic400Redirect(request, exception):
|
||||||
|
messages.error(
|
||||||
|
request,
|
||||||
|
"Auth encountered an error processing your request, please try again. "
|
||||||
|
"If the error persists, please contact the administrators. (400 Bad Request)",
|
||||||
|
)
|
||||||
|
return redirect("authentication:dashboard")
|
||||||
|
|||||||
BIN
docs/_static/images/development/aa_core.png
vendored
Normal file
BIN
docs/_static/images/development/aa_core.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 193 KiB |
BIN
docs/_static/images/features/apps/corpstats.png
vendored
Normal file
BIN
docs/_static/images/features/apps/corpstats.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
17
docs/conf.py
17
docs/conf.py
@@ -18,7 +18,10 @@
|
|||||||
#
|
#
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
sys.path.insert(0, os.path.abspath('.'))
|
import django
|
||||||
|
sys.path.insert(0, os.path.abspath('..'))
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings_all'
|
||||||
|
django.setup()
|
||||||
|
|
||||||
# on_rtd is whether we are on readthedocs.org, this line of code grabbed from docs.readthedocs.org
|
# on_rtd is whether we are on readthedocs.org, this line of code grabbed from docs.readthedocs.org
|
||||||
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
|
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
|
||||||
@@ -38,7 +41,9 @@ from recommonmark.transform import AutoStructify
|
|||||||
# Add any Sphinx extension module names here, as strings. They can be
|
# Add any Sphinx extension module names here, as strings. They can be
|
||||||
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
||||||
# ones.
|
# ones.
|
||||||
extensions = []
|
extensions = [
|
||||||
|
'sphinx.ext.autodoc',
|
||||||
|
]
|
||||||
|
|
||||||
# Add any paths that contain templates here, relative to this directory.
|
# Add any paths that contain templates here, relative to this directory.
|
||||||
templates_path = ['_templates']
|
templates_path = ['_templates']
|
||||||
@@ -96,7 +101,10 @@ html_theme = 'sphinx_rtd_theme'
|
|||||||
# further. For a list of options available for each theme, see the
|
# further. For a list of options available for each theme, see the
|
||||||
# documentation.
|
# documentation.
|
||||||
#
|
#
|
||||||
# html_theme_options = {}
|
|
||||||
|
html_theme_options = {
|
||||||
|
'navigation_depth': 4,
|
||||||
|
}
|
||||||
|
|
||||||
# Add any paths that contain custom static files (such as style sheets) here,
|
# Add any paths that contain custom static files (such as style sheets) here,
|
||||||
# relative to this directory. They are copied after the builtin static files,
|
# relative to this directory. They are copied after the builtin static files,
|
||||||
@@ -148,6 +156,9 @@ man_pages = [
|
|||||||
[author], 1)
|
[author], 1)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# -- Options for autodoc -------------------------------------------------
|
||||||
|
|
||||||
|
add_module_names = False
|
||||||
|
|
||||||
# -- Options for Texinfo output -------------------------------------------
|
# -- Options for Texinfo output -------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ It is possible to customize your **Alliance Auth** instance.
|
|||||||
|
|
||||||
```eval_rst
|
```eval_rst
|
||||||
.. warning::
|
.. warning::
|
||||||
Keep in mind that you may need to update some of your customizations manually after new release (e.g. when replacing AA templates).
|
Keep in mind that you may need to update some of your customizations manually after new Auth releases (e.g. when replacing templates).
|
||||||
```
|
```
|
||||||
|
|
||||||
## Site name
|
## Site name
|
||||||
@@ -46,13 +46,11 @@ In order to integrate with Alliance Auth service modules must provide a `service
|
|||||||
|
|
||||||
This would register the ExampleService class which would need to be a subclass of `services.hooks.ServiceHook`.
|
This would register the ExampleService class which would need to be a subclass of `services.hooks.ServiceHook`.
|
||||||
|
|
||||||
|
|
||||||
```eval_rst
|
```eval_rst
|
||||||
.. important::
|
.. important::
|
||||||
The hook **MUST** be registered in ``yourservice.auth_hooks`` along with any other hooks you are registering for Alliance Auth.
|
The hook **MUST** be registered in ``yourservice.auth_hooks`` along with any other hooks you are registering for Alliance Auth.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
A subclassed `ServiceHook` might look like this:
|
A subclassed `ServiceHook` might look like this:
|
||||||
|
|
||||||
class ExampleService(ServicesHook):
|
class ExampleService(ServicesHook):
|
||||||
@@ -65,7 +63,6 @@ A subclassed `ServiceHook` might look like this:
|
|||||||
Overload base methods here to implement functionality
|
Overload base methods here to implement functionality
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
### The ServiceHook class
|
### The ServiceHook class
|
||||||
|
|
||||||
The base `ServiceHook` class defines function signatures that Alliance Auth will call under certain conditions in order to trigger some action in the service.
|
The base `ServiceHook` class defines function signatures that Alliance Auth will call under certain conditions in order to trigger some action in the service.
|
||||||
@@ -73,31 +70,37 @@ The base `ServiceHook` class defines function signatures that Alliance Auth will
|
|||||||
You will need to subclass `services.hooks.ServiceHook` in order to provide implementation of the functions so that Alliance Auth can interact with the service correctly. All of the functions are optional, so its up to you to define what you need.
|
You will need to subclass `services.hooks.ServiceHook` in order to provide implementation of the functions so that Alliance Auth can interact with the service correctly. All of the functions are optional, so its up to you to define what you need.
|
||||||
|
|
||||||
Instance Variables:
|
Instance Variables:
|
||||||
|
|
||||||
- [self.name](#self-name)
|
- [self.name](#self-name)
|
||||||
- [self.urlpatterns](#self-url-patterns)
|
- [self.urlpatterns](#self-url-patterns)
|
||||||
- [self.service_ctrl_template](#self-service-ctrl-template)
|
- [self.service_ctrl_template](#self-service-ctrl-template)
|
||||||
|
|
||||||
Properties:
|
Properties:
|
||||||
|
|
||||||
- [title](#title)
|
- [title](#title)
|
||||||
|
|
||||||
Functions:
|
Functions:
|
||||||
- [delete_user](#delete-user)
|
|
||||||
- [validate_user](#validate-user)
|
|
||||||
- [sync_nickname](#sync-nickname)
|
|
||||||
- [update_groups](#update-groups)
|
|
||||||
- [update_all_groups](#update-all-groups)
|
|
||||||
- [service_enabled_members](#service-enabled-members)
|
|
||||||
- [service_enabled_blues](#service-enabled-blues)
|
|
||||||
- [service_active_for_user](#service-active-for-user)
|
|
||||||
- [show_service_ctrl](#show-service-ctrl)
|
|
||||||
- [render_service_ctrl](#render-service-ctrl)
|
|
||||||
|
|
||||||
|
- [delete_user](#delete_user)
|
||||||
|
- [validate_user](#validate_user)
|
||||||
|
- [sync_nickname](#sync_nickname)
|
||||||
|
- [sync_nicknames_bulk](#sync_nicknames_bulk)
|
||||||
|
- [update_groups](#update_groups)
|
||||||
|
- [update_groups_bulk](#update_groups_bulk)
|
||||||
|
- [update_all_groups](#update_all_groups)
|
||||||
|
- [service_enabled_members](#service_enabled_members)
|
||||||
|
- [service_enabled_blues](#service_enabled_blues)
|
||||||
|
- [service_active_for_user](#service_active_for_user)
|
||||||
|
- [show_service_ctrl](#show_service_ctrl)
|
||||||
|
- [render_service_ctrl](#render_service_ctrl)
|
||||||
|
|
||||||
#### self.name
|
#### self.name
|
||||||
|
|
||||||
Internal name of the module, should be unique amongst modules.
|
Internal name of the module, should be unique amongst modules.
|
||||||
|
|
||||||
#### self.urlpatterns
|
#### self.urlpatterns
|
||||||
You should define all of your service URLs internally, usually in `urls.py`. Then you can import them and set `self.urlpatterns` to your defined urlpatterns.
|
|
||||||
|
You should define all of your service URLs internally, usually in `urls.py`. Then you can import them and set `self.urlpatterns` to your defined urlpatterns.
|
||||||
|
|
||||||
from . import urls
|
from . import urls
|
||||||
...
|
...
|
||||||
@@ -109,12 +112,15 @@ You should define all of your service URLs internally, usually in `urls.py`. The
|
|||||||
All of your apps defined urlpatterns will then be included in the `URLconf` when the core application starts.
|
All of your apps defined urlpatterns will then be included in the `URLconf` when the core application starts.
|
||||||
|
|
||||||
#### self.service_ctrl_template
|
#### self.service_ctrl_template
|
||||||
|
|
||||||
This is provided as a courtesy and defines the default template to be used with [render_service_ctrl](#render-service-ctrl). You are free to redefine or not use this variable at all.
|
This is provided as a courtesy and defines the default template to be used with [render_service_ctrl](#render-service-ctrl). You are free to redefine or not use this variable at all.
|
||||||
|
|
||||||
#### title
|
#### title
|
||||||
This is a property which provides a user friendly display of your service's name. It will usually do a reasonably good job unless your service name has punctuation or odd capitalisation. If this is the case you should override this method and return a string.
|
|
||||||
|
This is a property which provides a user friendly display of your service's name. It will usually do a reasonably good job unless your service name has punctuation or odd capitalization. If this is the case you should override this method and return a string.
|
||||||
|
|
||||||
#### delete_user
|
#### delete_user
|
||||||
|
|
||||||
`def delete_user(self, user, notify_user=False):`
|
`def delete_user(self, user, notify_user=False):`
|
||||||
|
|
||||||
Delete the users service account, optionally notify them that the service has been disabled. The `user` parameter should be a Django User object. If notify_user is set to `True` a message should be set to the user via the `notifications` module to alert them that their service account has been disabled.
|
Delete the users service account, optionally notify them that the service has been disabled. The `user` parameter should be a Django User object. If notify_user is set to `True` a message should be set to the user via the `notifications` module to alert them that their service account has been disabled.
|
||||||
@@ -122,6 +128,7 @@ Delete the users service account, optionally notify them that the service has be
|
|||||||
The function should return a boolean, `True` if successfully disabled, `False` otherwise.
|
The function should return a boolean, `True` if successfully disabled, `False` otherwise.
|
||||||
|
|
||||||
#### validate_user
|
#### validate_user
|
||||||
|
|
||||||
`def validate_user(self, user):`
|
`def validate_user(self, user):`
|
||||||
|
|
||||||
Validate the users service account, deleting it if they should no longer have access. The `user` parameter should be a Django User object.
|
Validate the users service account, deleting it if they should no longer have access. The `user` parameter should be a Django User object.
|
||||||
@@ -138,30 +145,54 @@ No return value is expected.
|
|||||||
This function will be called periodically on all users to validate that the given user should have their current service accounts.
|
This function will be called periodically on all users to validate that the given user should have their current service accounts.
|
||||||
|
|
||||||
#### sync_nickname
|
#### sync_nickname
|
||||||
|
|
||||||
`def sync_nickname(self, user):`
|
`def sync_nickname(self, user):`
|
||||||
|
|
||||||
Very optional. As of writing only one service defines this. The `user` parameter should be a Django User object. When called, the given users nickname for the service should be updated and synchronised with the service.
|
Very optional. As of writing only one service defines this. The `user` parameter should be a Django User object. When called, the given users nickname for the service should be updated and synchronized with the service.
|
||||||
|
|
||||||
If this function is defined, an admin action will be registered on the Django Users view, allowing admins to manually trigger this action for one or many users. The hook will trigger this action user by user, so you won't have to manage a list of users.
|
If this function is defined, an admin action will be registered on the Django Users view, allowing admins to manually trigger this action for one or many users. The hook will trigger this action user by user, so you won't have to manage a list of users.
|
||||||
|
|
||||||
|
#### sync_nicknames_bulk
|
||||||
|
|
||||||
|
`def sync_nicknames_bulk(self, users):`
|
||||||
|
|
||||||
|
Updates the nickname for a list of users. The `users` parameter must be a list of Django User objects.
|
||||||
|
|
||||||
|
If this method is defined, the admin action for updating service related nicknames for users will call this bulk method instead of sync_nickname. This gives you more control over how mass updates are executed, e.g. ensuring updates do not run in parallel to avoid causing rate limit violations from an external API.
|
||||||
|
|
||||||
|
This is an optional method.
|
||||||
|
|
||||||
#### update_groups
|
#### update_groups
|
||||||
|
|
||||||
`def update_groups(self, user):`
|
`def update_groups(self, user):`
|
||||||
|
|
||||||
Update the users group membership. The `user` parameter should be a Django User object.
|
Update the users group membership. The `user` parameter should be a Django User object.
|
||||||
When this is called the service should determine the groups the user is a member of and synchronise the group membership with the external service. If you service does not support groups then you are not required to define this.
|
When this is called the service should determine the groups the user is a member of and synchronize the group membership with the external service. If you service does not support groups then you are not required to define this.
|
||||||
|
|
||||||
If this function is defined, an admin action will be registered on the Django Users view, allowing admins to manually trigger this action for one or many users. The hook will trigger this action user by user, so you won't have to manage a list of users.
|
If this function is defined, an admin action will be registered on the Django Users view, allowing admins to manually trigger this action for one or many users. The hook will trigger this action user by user, so you won't have to manage a list of users.
|
||||||
|
|
||||||
This action is usually called via a signal when a users group membership changes (joins or leaves a group).
|
This action is usually called via a signal when a users group membership changes (joins or leaves a group).
|
||||||
|
|
||||||
|
#### update_groups_bulk
|
||||||
|
|
||||||
|
`def update_groups_bulk(self, users):`
|
||||||
|
|
||||||
|
Updates the group memberships for a list of users. The `users` parameter must be a list of Django User objects.
|
||||||
|
|
||||||
|
If this method is defined, the admin action for updating service related groups for users will call this bulk method instead of update_groups. This gives you more control over how mass updates are executed, e.g. ensuring updates do not run in parallel to avoid causing rate limit violations from an external API.
|
||||||
|
|
||||||
|
This is an optional method.
|
||||||
|
|
||||||
#### update_all_groups
|
#### update_all_groups
|
||||||
|
|
||||||
`def update_all_groups(self):`
|
`def update_all_groups(self):`
|
||||||
|
|
||||||
The service should iterate through all of its recorded users and update their groups.
|
The service should iterate through all of its recorded users and update their groups.
|
||||||
|
|
||||||
I'm really not sure when this is called, it may have been a hold over from before signals started to be used. Regardless, it can be useful to server admins who may call this from a Django shell to force a synchronisation of all user groups for a specific service.
|
I'm really not sure when this is called, it may have been a hold over from before signals started to be used. Regardless, it can be useful to server admins who may call this from a Django shell to force a synchronization of all user groups for a specific service.
|
||||||
|
|
||||||
#### service_active_for_user
|
#### service_active_for_user
|
||||||
|
|
||||||
`def service_active_for_user(self, user):`
|
`def service_active_for_user(self, user):`
|
||||||
|
|
||||||
Is this service active for the given user? The `user` parameter should be a Django User object.
|
Is this service active for the given user? The `user` parameter should be a Django User object.
|
||||||
@@ -169,6 +200,7 @@ Is this service active for the given user? The `user` parameter should be a Djan
|
|||||||
Usually you wont need to override this as it calls `service_enabled_members` or `service_enabled_blues` depending on the users state.
|
Usually you wont need to override this as it calls `service_enabled_members` or `service_enabled_blues` depending on the users state.
|
||||||
|
|
||||||
#### show_service_ctrl
|
#### show_service_ctrl
|
||||||
|
|
||||||
`def show_service_ctrl(self, user, state):`
|
`def show_service_ctrl(self, user, state):`
|
||||||
|
|
||||||
Should the service be shown for the given `user` with the given `state`? The `user` parameter should be a Django User object, and the `state` parameter should be a valid state from `authentication.states`.
|
Should the service be shown for the given `user` with the given `state`? The `user` parameter should be a Django User object, and the `state` parameter should be a valid state from `authentication.states`.
|
||||||
@@ -178,6 +210,7 @@ Usually you wont need to override this function.
|
|||||||
For more information see the [render_service_ctrl](#render-service-ctrl) section.
|
For more information see the [render_service_ctrl](#render-service-ctrl) section.
|
||||||
|
|
||||||
#### render_service_ctrl
|
#### render_service_ctrl
|
||||||
|
|
||||||
`def render_services_ctrl(self, request):`
|
`def render_services_ctrl(self, request):`
|
||||||
|
|
||||||
Render the services control row. This will be called for all active services when a user visits the `/services/` page and [show_service_ctrl](#show-service-ctrl) returns `True` for the given user.
|
Render the services control row. This will be called for all active services when a user visits the `/services/` page and [show_service_ctrl](#show-service-ctrl) returns `True` for the given user.
|
||||||
@@ -242,7 +275,6 @@ Typically most traditional username/password services define four views.
|
|||||||
|
|
||||||
These views should interact with the service via the Tasks, though in some instances may bypass the Tasks and access the manager directly where necessary, for example OAuth functionality.
|
These views should interact with the service via the Tasks, though in some instances may bypass the Tasks and access the manager directly where necessary, for example OAuth functionality.
|
||||||
|
|
||||||
|
|
||||||
### The Tasks
|
### The Tasks
|
||||||
|
|
||||||
The tasks component is the glue that holds all of the other components of the service module together. It provides the function implementation to handle things like adding and deleting users, updating groups, validating the existence of a users account. Whatever tasks `auth_hooks` and `views` have with interacting with the service will probably live here.
|
The tasks component is the glue that holds all of the other components of the service module together. It provides the function implementation to handle things like adding and deleting users, updating groups, validating the existence of a users account. Whatever tasks `auth_hooks` and `views` have with interacting with the service will probably live here.
|
||||||
@@ -253,13 +285,12 @@ Its very likely that you'll need to store data about a users remote service acco
|
|||||||
|
|
||||||
If you create models you should create the migrations that go along with these inside of your module/app.
|
If you create models you should create the migrations that go along with these inside of your module/app.
|
||||||
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
There is a bare bones example service included in `services.modules.example`, you may like to use this as the base for your new service.
|
There is a bare bones example service included in `services.modules.example`, you may like to use this as the base for your new service.
|
||||||
|
|
||||||
You should have a look through some of the other service modules before you get started to get an idea of the general structure of one. A lot of them aren't perfect so don't feel like you have to rigidly follow the structure of the existing services if you think its sub-optimal or doesn't suit the external service you're integrating.
|
You should have a look through some of the other service modules before you get started to get an idea of the general structure of one. A lot of them aren't perfect so don't feel like you have to rigidly follow the structure of the existing services if you think its sub-optimal or doesn't suit the external service you're integrating.
|
||||||
|
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
You will need to add unit tests for all aspects of your service module before it is accepted. Be mindful that you don't actually want to make external calls to the service so you should mock the appropriate components to prevent this behaviour.
|
|
||||||
|
You will need to add unit tests for all aspects of your service module before it is accepted. Be mindful that you don't actually want to make external calls to the service so you should mock the appropriate components to prevent this behavior.
|
||||||
|
|||||||
463
docs/development/dev_setup/aa-dev-setup-wsl-vsc-v2.md
Normal file
463
docs/development/dev_setup/aa-dev-setup-wsl-vsc-v2.md
Normal file
@@ -0,0 +1,463 @@
|
|||||||
|
# Development on Windows 10 with WSL and Visual Studio Code
|
||||||
|
|
||||||
|
This document describes step-by-step how to setup a complete development environment for Alliance Auth apps on Windows 10 with Windows Subsystem for Linux (WSL) and Visual Studio Code.
|
||||||
|
|
||||||
|
The main benefit of this setup is that it runs all services and code in the native Linux environment (WSL) and at the same time can be full controlled from within a comfortable Windows IDE (Visual Studio Code) including code debugging.
|
||||||
|
|
||||||
|
In addition all tools described in this guide are open source or free software.
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. hint::
|
||||||
|
This guide is meant for development purposes only and not for installing AA in a production environment. For production installation please see chapter **Installation**.
|
||||||
|
```
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The development environment consists of the following components:
|
||||||
|
|
||||||
|
- Visual Studio Code with Remote WSL and Python extension
|
||||||
|
- WSL with Ubunutu 18.04. LTS
|
||||||
|
- Python 3.6 environment on WSL
|
||||||
|
- MySQL server on WSL
|
||||||
|
- Redis on WSL
|
||||||
|
- Alliance Auth on WSL
|
||||||
|
- Celery on WSL
|
||||||
|
|
||||||
|
We will use the build-in Django development webserver, so we don't need to setup a WSGI server or a web server.
|
||||||
|
|
||||||
|
## Requirement
|
||||||
|
|
||||||
|
The only requirement is a PC with Windows 10 and Internet connection in order to download the additional software components.
|
||||||
|
|
||||||
|
## Windows installation
|
||||||
|
|
||||||
|
### Windows Subsystem for Linux
|
||||||
|
|
||||||
|
- Install from here: [Microsoft docs](https://docs.microsoft.com/en-us/windows/wsl/install-win10)
|
||||||
|
|
||||||
|
- Choose Ubuntu 18.04. LTS
|
||||||
|
|
||||||
|
### Visual Studio Code
|
||||||
|
|
||||||
|
- Install from here: [VSC Download](https://code.visualstudio.com/Download)
|
||||||
|
|
||||||
|
- Open the app and install the following VSC extensions:
|
||||||
|
|
||||||
|
- Remote WSL
|
||||||
|
|
||||||
|
- Connect to WSL. This will automatically install the VSC server on the VSC server for WSL
|
||||||
|
|
||||||
|
- Once connected to WSL install the Python extension on the WSL side
|
||||||
|
|
||||||
|
## WSL Installation
|
||||||
|
|
||||||
|
Open a WSL bash and update all software packets:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt update && sudo apt upgrade -y
|
||||||
|
```
|
||||||
|
|
||||||
|
### Install Tools
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt-get install build-essential
|
||||||
|
sudo apt-get install gettext
|
||||||
|
```
|
||||||
|
|
||||||
|
### Install Python
|
||||||
|
|
||||||
|
For AA we want to develop with Python 3.6, because that provides the maximum compatibility with today's AA installations. This also happens to be the default Python 3 version for Ubuntu 18.04. at the point of this writing.
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. hint::
|
||||||
|
To check your system's Python 3 version you can enter: ``python3 --version``
|
||||||
|
```
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. note::
|
||||||
|
Should your Ubuntu come with a newer version of Python we recommend to still setup your dev environment with the oldest Python 3 version supported by AA, e.g Python 3.6
|
||||||
|
You an check out this `page <https://askubuntu.com/questions/682869/how-do-i-install-a-different-python-version-using-apt-get/1195153>`_ on how to install additional Python versions on Ubuntu.
|
||||||
|
```
|
||||||
|
|
||||||
|
Use the following command to install Python 3 with all required libraries with the default version:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt-get install python3 python3-dev python3-venv python3-setuptools python3-pip python-pip
|
||||||
|
```
|
||||||
|
|
||||||
|
### Installing the DBMS
|
||||||
|
|
||||||
|
Install MySQL and required libraries with the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
|
||||||
|
```
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. note::
|
||||||
|
We chose to use MySQL instead of MariaDB, because the standard version of MariaDB that comes with this Ubuntu distribution will not work with AA.
|
||||||
|
```
|
||||||
|
|
||||||
|
We need to apply a permission fix to mysql or you will get a warning with every startup:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo usermod -d /var/lib/mysql/ mysql
|
||||||
|
```
|
||||||
|
|
||||||
|
Start the mysql server
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo service mysql start
|
||||||
|
```
|
||||||
|
|
||||||
|
Create database and user for AA
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo mysql -u root
|
||||||
|
```
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE USER 'aa_dev'@'localhost' IDENTIFIED BY 'PASSWORD';
|
||||||
|
CREATE DATABASE aa_dev CHARACTER SET utf8mb4;
|
||||||
|
GRANT ALL PRIVILEGES ON aa_dev . * TO 'aa_dev'@'localhost';
|
||||||
|
CREATE DATABASE test_aa_dev CHARACTER SET utf8mb4;
|
||||||
|
GRANT ALL PRIVILEGES ON test_aa_dev . * TO 'aa_dev'@'localhost';
|
||||||
|
exit;
|
||||||
|
```
|
||||||
|
|
||||||
|
Add timezone info to mysql
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo mysql_tzinfo_to_sql /usr/share/zoneinfo | sudo mysql -u root mysql
|
||||||
|
```
|
||||||
|
|
||||||
|
### Install redis and other tools
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt-get install unzip git redis-server curl libssl-dev libbz2-dev libffi-dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Start redis
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo redis-server --daemonize yes
|
||||||
|
```
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. note::
|
||||||
|
WSL does not have an init.d service, so it will not automatically start your services such as MySQL and Redis when you boot your Windows machine. For convenience we recommend putting the commands for starting these services in a bash script. Here is an example: ::
|
||||||
|
|
||||||
|
#/bin/bash
|
||||||
|
# start services for AA dev
|
||||||
|
sudo service mysql start
|
||||||
|
sudo redis-server --daemonize yes
|
||||||
|
|
||||||
|
In addition it is possible to configure Windows to automatically start WSL services, but that procedure goes beyond the scopes of this guide.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Setup dev folder on WSL
|
||||||
|
|
||||||
|
Setup your folders on WSL bash for your dev project. Our approach will setup one AA project with one venv and multiple apps running under the same AA project, but each in their own folder and git.
|
||||||
|
|
||||||
|
A good location for setting up this folder structure is your home folder or a subfolder of your home:
|
||||||
|
|
||||||
|
```text
|
||||||
|
~/aa-dev
|
||||||
|
|- venv
|
||||||
|
|- myauth
|
||||||
|
|- my_app_1
|
||||||
|
|- my_app_2
|
||||||
|
|- ...
|
||||||
|
```
|
||||||
|
|
||||||
|
Following this approach you can also setup additional AA projects, e.g. aa-dev-2, aa-dev-3 if needed.
|
||||||
|
|
||||||
|
Create the root folder aa-dev.
|
||||||
|
|
||||||
|
### setup virtual Python environment for aa-dev
|
||||||
|
|
||||||
|
Create the virtual environment. Run this in your aa-dev folder:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m venv venv
|
||||||
|
```
|
||||||
|
|
||||||
|
And activate your venv:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
source venv/bin/activate
|
||||||
|
```
|
||||||
|
|
||||||
|
### install Python packages
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install --upgrade pip
|
||||||
|
pip install wheel
|
||||||
|
```
|
||||||
|
|
||||||
|
## Alliance Auth installation
|
||||||
|
|
||||||
|
## Install and create AA instance
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install allianceauth
|
||||||
|
```
|
||||||
|
|
||||||
|
Now we are ready to setup our AA instance. Make sure to run this command in your aa-dev folder:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
allianceauth start myauth
|
||||||
|
```
|
||||||
|
|
||||||
|
Next we will setup our VSC project for aa-dev by starting it directly from the WSL bash:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
code .
|
||||||
|
```
|
||||||
|
|
||||||
|
First you want to make sure exclude the venv folder from VSC as follows:
|
||||||
|
Open settings and go to Files:Exclude
|
||||||
|
Add pattern: `**/venv`
|
||||||
|
|
||||||
|
### Update settings
|
||||||
|
|
||||||
|
Open the settings file with VSC. Its under `myauth/myauth/settings/local.py`
|
||||||
|
|
||||||
|
Make sure to have the settings of your Eve Online app ready.
|
||||||
|
|
||||||
|
Turn on DEBUG mode to ensure your static files get served by Django:
|
||||||
|
|
||||||
|
```python
|
||||||
|
DEBUG = True
|
||||||
|
```
|
||||||
|
|
||||||
|
Update name, user and password of your DATABASE configuration.
|
||||||
|
|
||||||
|
```python
|
||||||
|
DATABASES['default'] = {
|
||||||
|
'ENGINE': 'django.db.backends.mysql',
|
||||||
|
'NAME': 'aa_dev',
|
||||||
|
'USER': 'aa_dev',
|
||||||
|
'PASSWORD': 'PASSWORD',
|
||||||
|
'HOST': '127.0.0.1',
|
||||||
|
'PORT': '3306',
|
||||||
|
'OPTIONS': {'charset': 'utf8mb4'},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For the Eve Online related setup you need to create a SSO app on the developer site:
|
||||||
|
|
||||||
|
- Create your Eve Online SSO App on the [Eve Online developer site](https://developers.eveonline.com/)
|
||||||
|
- Add all ESI scopes
|
||||||
|
- Set callback URL to: `http://localhost:8000/sso/callback`
|
||||||
|
|
||||||
|
Then update local.py with your settings:
|
||||||
|
|
||||||
|
```python
|
||||||
|
ESI_SSO_CLIENT_ID = 'YOUR-ID'
|
||||||
|
ESI_SSO_CLIENT_SECRET = 'YOUR_SECRET'
|
||||||
|
ESI_SSO_CALLBACK_URL = 'http://localhost:8000/sso/callback'
|
||||||
|
```
|
||||||
|
|
||||||
|
Disable email registration:
|
||||||
|
|
||||||
|
```python
|
||||||
|
REGISTRATION_VERIFY_EMAIL = False
|
||||||
|
```
|
||||||
|
|
||||||
|
### Migrations and superuser
|
||||||
|
|
||||||
|
Before we can start AA we need to run migrations:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd myauth
|
||||||
|
python manage.py migrate
|
||||||
|
```
|
||||||
|
|
||||||
|
We also need to create a superuser for our AA installation:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python /home/allianceserver/myauth/manage.py createsuperuser
|
||||||
|
```
|
||||||
|
|
||||||
|
## Running Alliance Auth
|
||||||
|
|
||||||
|
## AA instance
|
||||||
|
|
||||||
|
We are now ready to run out AA instance with the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python manage.py runserver
|
||||||
|
```
|
||||||
|
|
||||||
|
Once running you can access your auth site on the browser under `http://localhost:8000`. Or the admin site under `http://localhost:8000/admin`
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. hint::
|
||||||
|
You can start your AA server directly from a terminal window in VSC or with a VSC debug config (see chapter about debugging for details).
|
||||||
|
```
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. note::
|
||||||
|
**Debug vs. Non-Debug mode**
|
||||||
|
Usually it is best to run your dev AA instance in debug mode, so you get all the detailed error messages that helps a lot for finding errors. But there might be cases where you want to test features that do not exist in debug mode (e.g. error pages) or just want to see how your app behaves in non-debug / production mode.
|
||||||
|
|
||||||
|
When you turn off debug mode you will see a problem though: Your pages will not render correctly. The reason is that Django will stop serving your static files in production mode and expect you to serve them from a real web server. Luckily, there is an option that forces Django to continue serving your static files directly even when not in debug mode. Just start your server with the following option: ``python manage.py runserver --insecure``
|
||||||
|
```
|
||||||
|
|
||||||
|
### Celery
|
||||||
|
|
||||||
|
In addition you can start a celery worker instance for myauth. For development purposed it makes sense to only start one instance and add some additional logging.
|
||||||
|
|
||||||
|
This can be done from the command line with the following command in the myauth folder (where manage.py is located):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
celery -E -A myauth worker -l info -P solo
|
||||||
|
```
|
||||||
|
|
||||||
|
Same as AA itself you can start Celery from any terminal session, from a terminal window within VSC or as a debug config in VSC (see chapter about debugging for details). For convenience we recommend starting Celery as debug config.
|
||||||
|
|
||||||
|
## Debugging setup
|
||||||
|
|
||||||
|
To be able to debug your code you need to add debugging configuration to VSC. At least one for AA and one for celery.
|
||||||
|
|
||||||
|
### Breakpoints
|
||||||
|
|
||||||
|
By default VSC will break on any uncaught exception. Since every error raised by your tests will cause an uncaught exception we recommend to deactivate this feature.
|
||||||
|
|
||||||
|
To deactivate open click on the debug icon to switch to the debug view. Then un-check "Uncaught Exceptions" on the breakpoints window.
|
||||||
|
|
||||||
|
### AA debug config
|
||||||
|
|
||||||
|
In VSC click on Debug / Add Configuration and choose "Django". Should Django not appear as option make sure to first open a Django file (e.g. the local.py settings) to help VSC detect that you are using Django.
|
||||||
|
|
||||||
|
The result should look something like this:
|
||||||
|
|
||||||
|
```python
|
||||||
|
{
|
||||||
|
"name": "Python: Django",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/myauth/manage.py",
|
||||||
|
"args": [
|
||||||
|
"runserver",
|
||||||
|
"--noreload"
|
||||||
|
],
|
||||||
|
"django": true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Debug celery
|
||||||
|
|
||||||
|
For celery we need another debug config, so that we can run it in parallel to our AA instance.
|
||||||
|
|
||||||
|
Here is an example debug config for Celery:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
{
|
||||||
|
"name": "Python: Celery",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"module": "celery",
|
||||||
|
"cwd": "${workspaceFolder}/myauth",
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"args": [
|
||||||
|
"-E",
|
||||||
|
"-A",
|
||||||
|
"myauth",
|
||||||
|
"worker",
|
||||||
|
"-l",
|
||||||
|
"info",
|
||||||
|
"-P",
|
||||||
|
"solo",
|
||||||
|
],
|
||||||
|
"django": true
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
### Debug config for unit tests
|
||||||
|
|
||||||
|
Finally it makes sense to have a dedicated debug config for running unit tests. Here is an example config for running all tests of the app `example`.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
{
|
||||||
|
"name": "Python: myauth unit tests",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/myauth/manage.py",
|
||||||
|
"args": [
|
||||||
|
"test",
|
||||||
|
"-v 2",
|
||||||
|
"--keepdb",
|
||||||
|
"--debug-mode",
|
||||||
|
"--failfast",
|
||||||
|
"example",
|
||||||
|
],
|
||||||
|
|
||||||
|
"django": true
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also specify to run just a part of your test suite down to a test method. Just give the full path to the test you want to run, e.g. `example.test.test_models.TestDemoModel.test_this_method`
|
||||||
|
|
||||||
|
### Debugging normal python scripts
|
||||||
|
|
||||||
|
Finally you may also want to have a debug config to debug a non-Django Python script:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
{
|
||||||
|
"name": "Python: Current File",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${file}",
|
||||||
|
"console": "integratedTerminal"
|
||||||
|
},
|
||||||
|
```
|
||||||
|
|
||||||
|
## Additional tools
|
||||||
|
|
||||||
|
The following additional tools are very helpful when developing for AA.
|
||||||
|
|
||||||
|
### Code Spell Checker
|
||||||
|
|
||||||
|
Typos in your user facing comments can be quite embarrassing. This spell checker helps you avoid them.
|
||||||
|
|
||||||
|
Install from here: [Code Spell Checker](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker)
|
||||||
|
|
||||||
|
### DBeaver
|
||||||
|
|
||||||
|
DBeaver is a free universal database tool and works with many different kinds of databases include MySQL. It can be installed on Windows 10 and will be able to help manage your MySQL databases running on WSL.
|
||||||
|
|
||||||
|
Install from here. [DBeaver](https://dbeaver.io/)
|
||||||
|
|
||||||
|
### django-extensions
|
||||||
|
|
||||||
|
[django-extensions](https://django-extensions.readthedocs.io/en/latest/) is a swiss army knife for django developers with adds a lot of very useful features to your Django site. Here are a few highlights:
|
||||||
|
|
||||||
|
- shell_plus - An enhanced version of the Django shell. It will auto-load all your models at startup so you don't have to import anything and can use them right away.
|
||||||
|
- graph_models - Creates a dependency graph of Django models. Visualizing a model dependency structure can be very useful for trying to understand how an existing Django app works, or e.g. how all the AA models work together.
|
||||||
|
- runserver_plus - The standard runserver stuff but with the Werkzeug debugger baked in. This is a must have for any serious debugging.
|
||||||
|
|
||||||
|
## Adding apps for development
|
||||||
|
|
||||||
|
The idea behind the particular folder structure of aa-dev is to have each and every app in its own folder and git repo. To integrate them with the AA instance they need to be installed once using the -e option that enabled editing of the package. And then added to the INSTALLED_APPS settings.
|
||||||
|
|
||||||
|
To demonstrate let's add the example plugin to our environment.
|
||||||
|
|
||||||
|
Open a WSL bash and navigate to the aa-dev folder. Make sure you have activate your virtual environment. (`source venv/bin/activate`)
|
||||||
|
|
||||||
|
Run these commands:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://gitlab.com/ErikKalkoken/allianceauth-example-plugin.git
|
||||||
|
pip install -e allianceauth-example-plugin
|
||||||
|
```
|
||||||
|
|
||||||
|
Add `'example'` to INSTALLED_APPS in your `local.py` settings.
|
||||||
|
|
||||||
|
Run migrations and restart your AA server, e.g.:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd myauth
|
||||||
|
python manage.py migrate
|
||||||
|
```
|
||||||
10
docs/development/dev_setup/index.md
Normal file
10
docs/development/dev_setup/index.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Setup dev environment for AA
|
||||||
|
|
||||||
|
Here you find guides on how to setup your development environment for AA.
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 1
|
||||||
|
|
||||||
|
aa-dev-setup-wsl-vsc-v2
|
||||||
|
```
|
||||||
@@ -8,4 +8,6 @@
|
|||||||
|
|
||||||
custom/index
|
custom/index
|
||||||
aa_core/index
|
aa_core/index
|
||||||
|
dev_setup/index
|
||||||
|
tech_docu/index
|
||||||
```
|
```
|
||||||
|
|||||||
39
docs/development/tech_docu/api/esi.rst
Normal file
39
docs/development/tech_docu/api/esi.rst
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
======================
|
||||||
|
django-esi
|
||||||
|
======================
|
||||||
|
|
||||||
|
The django-esi package provides an interface for easy access to the ESI.
|
||||||
|
|
||||||
|
Location: ``esi``
|
||||||
|
|
||||||
|
This is an external package. Please also see `here <https://gitlab.com/allianceauth/django-esi>`_ for it's official documentation.
|
||||||
|
|
||||||
|
clients
|
||||||
|
===========
|
||||||
|
|
||||||
|
.. automodule:: esi.clients
|
||||||
|
:members: esi_client_factory
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
decorators
|
||||||
|
===========
|
||||||
|
|
||||||
|
.. automodule:: esi.decorators
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
errors
|
||||||
|
===========
|
||||||
|
|
||||||
|
.. automodule:: esi.errors
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
|
||||||
|
models
|
||||||
|
===========
|
||||||
|
|
||||||
|
.. automodule:: esi.models
|
||||||
|
:members: Scope, Token
|
||||||
|
:exclude-members: objects, provider
|
||||||
|
:undoc-members:
|
||||||
30
docs/development/tech_docu/api/evelinks.rst
Normal file
30
docs/development/tech_docu/api/evelinks.rst
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
===============================
|
||||||
|
evelinks
|
||||||
|
===============================
|
||||||
|
|
||||||
|
This package generates profile URLs for eve entities on 3rd party websites like evewho and zKillboard.
|
||||||
|
|
||||||
|
Location: ``allianceauth.eveonline.evelinks``
|
||||||
|
|
||||||
|
dotlan
|
||||||
|
===============
|
||||||
|
|
||||||
|
.. automodule:: allianceauth.eveonline.evelinks.dotlan
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
eveho
|
||||||
|
==============
|
||||||
|
|
||||||
|
.. automodule:: allianceauth.eveonline.evelinks.evewho
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
|
|
||||||
|
zkillboard
|
||||||
|
===================
|
||||||
|
|
||||||
|
.. automodule:: allianceauth.eveonline.evelinks.zkillboard
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
||||||
15
docs/development/tech_docu/api/eveonline.rst
Normal file
15
docs/development/tech_docu/api/eveonline.rst
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
======================
|
||||||
|
eveonline
|
||||||
|
======================
|
||||||
|
|
||||||
|
The eveonline package provides models for commonly used Eve Online entities like characters, corporations and alliances. All models have the ability to be loaded from ESI.
|
||||||
|
|
||||||
|
Location: ``allianceauth.eveonline``
|
||||||
|
|
||||||
|
models
|
||||||
|
======
|
||||||
|
|
||||||
|
.. automodule:: allianceauth.eveonline.models
|
||||||
|
:members:
|
||||||
|
:exclude-members: objects, provider
|
||||||
|
:undoc-members:
|
||||||
13
docs/development/tech_docu/api/index.md
Normal file
13
docs/development/tech_docu/api/index.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# API
|
||||||
|
|
||||||
|
To reduce redundancy and help speed up development we encourage developers to utilize the following packages when developing apps for Alliance Auth.
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 1
|
||||||
|
|
||||||
|
esi
|
||||||
|
evelinks
|
||||||
|
eveonline
|
||||||
|
testutils
|
||||||
|
```
|
||||||
14
docs/development/tech_docu/api/testutils.rst
Normal file
14
docs/development/tech_docu/api/testutils.rst
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
=============================
|
||||||
|
tests
|
||||||
|
=============================
|
||||||
|
|
||||||
|
Here you find utility functions and classes, which can help speed up writing test cases for AA.
|
||||||
|
|
||||||
|
Location: ``allianceauth.tests.auth_utils``
|
||||||
|
|
||||||
|
auth_utils
|
||||||
|
===========
|
||||||
|
|
||||||
|
.. automodule:: allianceauth.tests.auth_utils
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
204
docs/development/tech_docu/celery.md
Normal file
204
docs/development/tech_docu/celery.md
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
# Celery FAQ
|
||||||
|
|
||||||
|
**Alliance Auth** uses Celery for asynchronous task management. This page aims to give developers some guidance on how to use Celery when developing apps for Alliance Auth.
|
||||||
|
|
||||||
|
For a complete documentation of Celery please refer to the [official Celery documentation](http://docs.celeryproject.org/en/latest/index.html).
|
||||||
|
|
||||||
|
## When should I use Celery in my app?
|
||||||
|
|
||||||
|
There are two main cases for using celery. Long duration of a process and recurrence of a process.
|
||||||
|
|
||||||
|
### Duration
|
||||||
|
|
||||||
|
Alliance Auth is an online web application and as such the user expects fast and immediate responses to any of his clicks or actions. Same as with any other good web site. Good response times are measures in ms and a user will perceive everything that takes longer than 1 sec as an interruption of his flow of thought (see also [Response Times: The 3 Important Limits](https://www.nngroup.com/articles/response-times-3-important-limits/)).
|
||||||
|
|
||||||
|
As a rule of thumb we therefore recommend to use celery tasks for every process that can take longer than 1 sec to complete (also think about how long your process might take with large amounts of data).
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. note::
|
||||||
|
Another solution for dealing with long response time in particular when loading pages is to load parts of a page asynchronously, for example with AJAX.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Recurrence
|
||||||
|
|
||||||
|
Another case for using celery tasks is when you need recurring execution of tasks. For example you may want to update the list of characters in a corporation from ESI every hour.
|
||||||
|
|
||||||
|
These are called periodic tasks and Alliance Auth uses [celery beat](https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html) to implement them.
|
||||||
|
|
||||||
|
## What is a celery task?
|
||||||
|
|
||||||
|
For the most part a celery task is a Python functions that is configured to be executed asynchronously and controlled by Celery. Celery tasks can be automatically retried, executed periodically, executed in work flows and much more. See the [celery docs](https://docs.celeryproject.org/en/latest/userguide/tasks.html) for a more detailed description.
|
||||||
|
|
||||||
|
## How should I use Celery in my app?
|
||||||
|
|
||||||
|
Please use the following approach to ensure your tasks are working properly with Alliance Auth:
|
||||||
|
|
||||||
|
- All tasks should be defined in a module of your app's package called `tasks.py`
|
||||||
|
- Every task is a Python function with has the `@shared_task` decorator.
|
||||||
|
- Task functions and the tasks module should be kept slim, just like views by mostly utilizing business logic defined in your models/managers.
|
||||||
|
- Tasks should always have logging, so their function and potential errors can be monitored properly
|
||||||
|
|
||||||
|
Here is an example implementation of a task:
|
||||||
|
|
||||||
|
```Python
|
||||||
|
import logging
|
||||||
|
from celery import shared_task
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def example():
|
||||||
|
logger.info('example task started')
|
||||||
|
```
|
||||||
|
|
||||||
|
This task can then be started from any another Python module like so:
|
||||||
|
|
||||||
|
```Python
|
||||||
|
from .tasks import example
|
||||||
|
|
||||||
|
example.delay()
|
||||||
|
```
|
||||||
|
|
||||||
|
## How should I use celery tasks in the UI?
|
||||||
|
|
||||||
|
There is a well established pattern for integrating asynchronous processes in the UI, for example when the user asks your app to perform a longer running action:
|
||||||
|
|
||||||
|
1. Notify the user immediately (with a Django message) that the process for completing the action has been started and that he will receive a report once completed.
|
||||||
|
|
||||||
|
2. Start the celery task
|
||||||
|
|
||||||
|
3. Once the celery task is completed it should send a notification containing the result of the action to the user. It's important to send that notification also in case of errors.
|
||||||
|
|
||||||
|
## Can I use long running tasks?
|
||||||
|
|
||||||
|
Long running tasks are possible, but in general Celery works best with short running tasks. Therefore we strongly recommend to try and break down long running tasks into smaller tasks if possible.
|
||||||
|
|
||||||
|
If contextually possible try to break down your long running task in shorter tasks that can run in parallel.
|
||||||
|
|
||||||
|
However, many long running tasks consist of several smaller processes that need to run one after the other. For example you may have a loop where you perform the same action on hundreds of objects. In those cases you can define each of the smaller processes as it's own task and then link them together, so that they are run one after the other. That is called chaining in Celery and is the preferred approach for implementing long running processes.
|
||||||
|
|
||||||
|
Example implementation for a celery chain:
|
||||||
|
|
||||||
|
```Python
|
||||||
|
import logging
|
||||||
|
from celery import shared_task, chain
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def example():
|
||||||
|
logger.info('example task')
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def long_runner():
|
||||||
|
logger.info('started long runner')
|
||||||
|
my_tasks = list()
|
||||||
|
for _ in range(10):
|
||||||
|
task_signature = example.si()
|
||||||
|
my_task.append(task_signature)
|
||||||
|
|
||||||
|
chain(my_tasks).delay()
|
||||||
|
```
|
||||||
|
|
||||||
|
In this example we fist add 10 example tasks that need to run one after the other to a list. This can be done by creating a so called signature for a task. Those signature are a kind of wrapper for tasks and can be used in various ways to compose work flow for tasks.
|
||||||
|
|
||||||
|
The list of task signatures is then converted to a chain and started asynchronously.
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. hint::
|
||||||
|
In our example we use ``si()``, which is a shortcut for "immutable signatures" and prevents us from having to deal with result sharing between tasks.
|
||||||
|
|
||||||
|
For more information on signature and work flows see the official documentation on `Canvas <https://docs.celeryproject.org/en/latest/userguide/canvas.html>`_.
|
||||||
|
|
||||||
|
In this context please note that Alliance Auth currently only supports chaining, because all other variants require a so called results back, which Alliance Auth does not have.
|
||||||
|
```
|
||||||
|
|
||||||
|
## How can I define periodic tasks for my app?
|
||||||
|
|
||||||
|
Periodic tasks are normal celery tasks which are added the scheduler for periodic execution. The convention for defining periodic tasks for an app is to define them in the local settings. So user will need to add those settings manually to his local settings during the installation process.
|
||||||
|
|
||||||
|
Example setting:
|
||||||
|
|
||||||
|
```Python
|
||||||
|
CELERYBEAT_SCHEDULE['structures_update_all_structures'] = {
|
||||||
|
'task': 'structures.tasks.update_all_structures',
|
||||||
|
'schedule': crontab(minute='*/30'),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `structures_update_all_structures` is the name of the scheduling entry. You can chose any name, but the convention is name of your app plus name of the task.
|
||||||
|
|
||||||
|
- `'task'`: Name of your task (full path)
|
||||||
|
- `'schedule'`: Schedule definition (see Celery documentation on [Periodic Tasks](https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html) for details)
|
||||||
|
|
||||||
|
## How can I use priorities for tasks?
|
||||||
|
|
||||||
|
In Alliance Auth we have defined task priorities from 0 - 9 as follows:
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
====== ========= ===========
|
||||||
|
Number Priority Description
|
||||||
|
====== ========= ===========
|
||||||
|
0 Reserved Reserved for Auth and may not be used by apps
|
||||||
|
1, 2 Highest Needs to run right now
|
||||||
|
3, 4 High needs to run as soon as practical
|
||||||
|
5 Normal default priority for most tasks
|
||||||
|
6, 7 Low needs to run soonish, but is less urgent than most tasks
|
||||||
|
8, 9 Lowest not urgent, can be run whenever there is time
|
||||||
|
====== ========= ===========
|
||||||
|
```
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. warning::
|
||||||
|
Please make sure to use task priorities with care and especially do not use higher priorities without a good reason. All apps including Alliance Auth share the same task queues, so using higher task priorities excessively can potentially prevent more important tasks (of other apps) from completing on time.
|
||||||
|
|
||||||
|
You also want to make sure to run use lower priorities if you have a large amount of tasks or long running tasks, which are not super urgent. (e.g. the regular update of all Eve characters from ESI runs with priority 7)
|
||||||
|
```
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. hint::
|
||||||
|
If no priority is specified all tasks will be started with the default priority, which is 5.
|
||||||
|
```
|
||||||
|
|
||||||
|
To run a task with a different priority you need to specify it when starting it.
|
||||||
|
|
||||||
|
Example for starting a task with priority 3:
|
||||||
|
|
||||||
|
```Python
|
||||||
|
example.apply_async(priority=3)
|
||||||
|
```
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. hint::
|
||||||
|
For defining a priority to tasks you can not use the convenient shortcut ``delay()``, but instead need to start a task with ``apply_async()``, which also requires you to pass parameters to your task function differently. Please check out the `official docs <https://docs.celeryproject.org/en/stable/reference/celery.app.task.html#celery.app.task.Task.apply_async>`_ for details.
|
||||||
|
```
|
||||||
|
|
||||||
|
## What special features should I be aware of?
|
||||||
|
|
||||||
|
Every Alliance Auth installation will come with a couple of special celery related features "out-of-the-box" that you can make use of in your apps.
|
||||||
|
|
||||||
|
### celery-once
|
||||||
|
|
||||||
|
Celery-once is a celery extension "that allows you to prevent multiple execution and queuing of celery tasks". What that means is that you can ensure that only one instance of a celery task runs at any given time. This can be useful for example if you do not want multiple instances of you task to talk to the same external service at the same time.
|
||||||
|
|
||||||
|
We use a custom backend for celery_once in Alliance Auth defined [here](https://gitlab.com/allianceauth/allianceauth/-/blob/master/allianceauth/services/tasks.py#L14)
|
||||||
|
You can import it for use like so:
|
||||||
|
|
||||||
|
```Python
|
||||||
|
from allianceauth.services.tasks import QueueOnce
|
||||||
|
```
|
||||||
|
|
||||||
|
An example of AllianceAuth's use within the `@sharedtask` decorator, can be seen [here](https://gitlab.com/allianceauth/allianceauth/-/blob/master/allianceauth/services/modules/discord/tasks.py#L62) in the discord module
|
||||||
|
You can use it like so:
|
||||||
|
|
||||||
|
```Python
|
||||||
|
@shared_task(bind=True, name='your_modules.update_task', base=QueueOnce)
|
||||||
|
```
|
||||||
|
|
||||||
|
Please see the [official documentation](hhttps://pypi.org/project/celery_once/) of celery-once for details.
|
||||||
|
|
||||||
|
### task priorities
|
||||||
|
|
||||||
|
Alliance Auth is using task priorities to enable priority based scheduling of task execution. Please see [How can I use priorities for tasks?](#how-can-i-use-priorities-for-tasks) for details.
|
||||||
5
docs/development/tech_docu/core_models.md
Normal file
5
docs/development/tech_docu/core_models.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Core models
|
||||||
|
|
||||||
|
The following diagram shows the core models of AA and Django and their relationships:
|
||||||
|
|
||||||
|

|
||||||
13
docs/development/tech_docu/index.md
Normal file
13
docs/development/tech_docu/index.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# Developing apps
|
||||||
|
|
||||||
|
In this section you find topics useful for app developers.
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 1
|
||||||
|
|
||||||
|
api/index
|
||||||
|
celery
|
||||||
|
core_models
|
||||||
|
templatetags
|
||||||
|
```
|
||||||
16
docs/development/tech_docu/templatetags.rst
Normal file
16
docs/development/tech_docu/templatetags.rst
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
=============
|
||||||
|
Template Tags
|
||||||
|
=============
|
||||||
|
|
||||||
|
The following template tags are available to be used by all apps. To use them just load the respeetive template tag in your template like so:
|
||||||
|
|
||||||
|
.. code-block:: html
|
||||||
|
|
||||||
|
{% load evelinks %}
|
||||||
|
|
||||||
|
evelinks
|
||||||
|
========
|
||||||
|
|
||||||
|
.. automodule:: allianceauth.eveonline.templatetags.evelinks
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
This module is used to check the registration status of Corp members and to determine character relationships, being mains or alts.
|
This module is used to check the registration status of Corp members and to determine character relationships, being mains or alts.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|||||||
@@ -8,11 +8,6 @@ Discord is very popular amongst ad-hoc small groups and larger organizations see
|
|||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
```eval_rst
|
|
||||||
.. warning::
|
|
||||||
Do not run the `discord.update_*` periodic tasks on a regular schedule, doing so can cause your discord service to stop syncing completely.
|
|
||||||
```
|
|
||||||
|
|
||||||
### Prepare Your Settings File
|
### Prepare Your Settings File
|
||||||
|
|
||||||
In your auth project's settings file, do the following:
|
In your auth project's settings file, do the following:
|
||||||
@@ -81,11 +76,27 @@ Instead of the usual account creation procedure, for Discord to work we need to
|
|||||||
|
|
||||||
### Syncing Nicknames
|
### Syncing Nicknames
|
||||||
|
|
||||||
If you want users to have their Discord nickname changed to their in-game character name, set `DISCORD_SYNC_NAMES` to `True`
|
If you want users to have their Discord nickname changed to their in-game character name, set `DISCORD_SYNC_NAMES` to `True`.
|
||||||
|
|
||||||
## Managing Roles
|
## Managing Roles
|
||||||
|
|
||||||
Once users link their accounts you’ll notice Roles get populated on Discord. These are the equivalent to Groups on every other service. The default permissions should be enough for members to use text and audio communications. Add more permissions to the roles as desired through the server management window.
|
Once users link their accounts you’ll notice Roles get populated on Discord. These are the equivalent to groups on every other service. The default permissions should be enough for members to use text and audio communications. Add more permissions to the roles as desired through the server management window.
|
||||||
|
|
||||||
|
## Settings
|
||||||
|
|
||||||
|
You can configure your Discord services with the following settings:
|
||||||
|
|
||||||
|
Name | Description | Default
|
||||||
|
-- | -- | --
|
||||||
|
`DISCORD_APP_ID` | Oauth client ID for the Discord Auth app | `''`
|
||||||
|
`DISCORD_APP_SECRET` | Oauth client secret for the Discord Auth app | `''`
|
||||||
|
`DISCORD_BOT_TOKEN` | Generated bot token for the Discord Auth app | `''`
|
||||||
|
`DISCORD_CALLBACK_URL` | Oauth callback URL | `''`
|
||||||
|
`DISCORD_GUILD_ID` | Discord ID of your Discord server | `''`
|
||||||
|
`DISCORD_ROLES_CACHE_MAX_AGE` | How long roles retrieved from the Discord server are caches locally in milliseconds | `7200000`
|
||||||
|
`DISCORD_SYNC_NAMES` | When set to True the nicknames of Discord users will automatically be set to the user's main character name when a new user joins Discord | `False`
|
||||||
|
`DISCORD_TASKS_RETRY_PAUSE` | Pause in seconds until next retry for tasks after an error occurred | `60`
|
||||||
|
`DISCORD_TASKS_MAX_RETRIES` | max retries of tasks after an error occurred | `3`
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
|
|||||||
@@ -1,50 +1,93 @@
|
|||||||
# Mumble
|
# Mumble
|
||||||
|
|
||||||
## Prepare Your Settings
|
|
||||||
In your auth project's settings file, do the following:
|
|
||||||
- Add `'allianceauth.services.modules.mumble',` to your `INSTALLED_APPS` list
|
|
||||||
- Append the following to your local.py settings file:
|
|
||||||
|
|
||||||
```python
|
|
||||||
# Mumble Configuration
|
|
||||||
MUMBLE_URL = ""
|
|
||||||
```
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
Mumble is a free voice chat server. While not as flashy as TeamSpeak, it has all the functionality and is easier to customize. And is better. I may be slightly biased.
|
Mumble is a free voice chat server. While not as flashy as TeamSpeak, it has all the functionality and is easier to customize. And is better. I may be slightly biased.
|
||||||
|
|
||||||
## Dependencies
|
```eval_rst
|
||||||
The mumble server package can be retrieved from a repository we need to add, mumble/release.
|
.. note::
|
||||||
|
Note that this guide assumes that you have installed Auth with the official :doc:`/installation/allianceauth` guide under ``/home/allianceserver`` and that it is called ``myauth``. Accordingly it assumes that you have a service user called ``allianceserver`` that is used to run all Auth services under supervisor.
|
||||||
|
```
|
||||||
|
|
||||||
apt-add-repository ppa:mumble/release
|
```eval_rst
|
||||||
apt-get update
|
.. note::
|
||||||
|
Same as the official installation guide this guide is assuming you are performing all steps as ``root`` user.
|
||||||
|
```
|
||||||
|
|
||||||
Now two packages need to be installed:
|
```eval_rst
|
||||||
|
.. warning::
|
||||||
|
This guide is currently for Ubuntu only.
|
||||||
|
```
|
||||||
|
|
||||||
apt-get install python-software-properties mumble-server
|
## Installations
|
||||||
|
|
||||||
Download the appropriate authenticator release from [the authenticator repository](https://gitlab.com/allianceauth/mumble-authenticator) and install the python dependencies for it:
|
### Installing Mumble Server
|
||||||
|
|
||||||
pip install -r requirements.txt
|
The mumble server package can be retrieved from a repository, which we need to add:
|
||||||
|
|
||||||
## Configuring Mumble
|
```bash
|
||||||
Mumble ships with a configuration file that needs customization. By default it’s located at /etc/mumble-server.ini. Open it with your favourite text editor:
|
apt-add-repository ppa:mumble/release
|
||||||
|
```
|
||||||
|
|
||||||
nano /etc/mumble-server.ini
|
```bash
|
||||||
|
apt-get update
|
||||||
|
```
|
||||||
|
|
||||||
REQUIRED: To enable the ICE authenticator, edit the following:
|
Now three packages need to be installed:
|
||||||
|
|
||||||
- `icesecretwrite=MY_CLEVER_PASSWORD`, obviously choosing a secure password
|
```bash
|
||||||
- ensure the line containing `Ice="tcp -h 127.0.0.1 -p 6502"` is uncommented
|
apt-get install python-software-properties mumble-server libqt5sql5-mysql
|
||||||
|
```
|
||||||
|
|
||||||
By default mumble operates on SQLite which is fine, but slower than a dedicated MySQL server. To customize the database, edit the following:
|
### Installing Mumble Authenticator
|
||||||
|
|
||||||
- uncomment the database line, and change it to `database=alliance_mumble`
|
Next, we need to download the latest authenticator release from the [authenticator repository](https://gitlab.com/allianceauth/mumble-authenticator).
|
||||||
- `dbDriver=QMYSQL`
|
|
||||||
- `dbUsername=allianceserver` or whatever you called the Alliance Auth MySQL user
|
```bash
|
||||||
- `dbPassword=` that user’s password
|
git clone https://gitlab.com/allianceauth/mumble-authenticator /home/allianceserver/mumble-authenticator
|
||||||
- `dbPort=3306`
|
```
|
||||||
- `dbPrefix=murmur_`
|
|
||||||
|
We will now install the authenticator into your Auth virtual environment. Please make sure to activate it first:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
source /home/allianceserver/venv/auth/bin/activate
|
||||||
|
```
|
||||||
|
|
||||||
|
Install the python dependencies for the mumble authenticator. Note that this process can take a couple minutes to complete.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuring Mumble Server
|
||||||
|
|
||||||
|
The mumble server needs it's own database. Open an SQL shell with `mysql -u root -p` and execute the SQL commands to create it:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE DATABASE alliance_mumble CHARACTER SET utf8mb4;
|
||||||
|
```
|
||||||
|
|
||||||
|
```sql
|
||||||
|
GRANT ALL PRIVILEGES ON alliance_mumble . * TO 'allianceserver'@'localhost';
|
||||||
|
```
|
||||||
|
|
||||||
|
Mumble ships with a configuration file that needs customization. By default it’s located at `/etc/mumble-server.ini`. Open it with your favorite text editor:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
nano /etc/mumble-server.ini
|
||||||
|
```
|
||||||
|
|
||||||
|
We need to enable the ICE authenticator. Edit the following:
|
||||||
|
|
||||||
|
- `icesecretwrite=MY_CLEVER_PASSWORD`, obviously choosing a secure password
|
||||||
|
- ensure the line containing `Ice="tcp -h 127.0.0.1 -p 6502"` is uncommented
|
||||||
|
|
||||||
|
We also want to enable Mumble to use the previously created MySQL / MariaDB database, edit the following:
|
||||||
|
|
||||||
|
- uncomment the database line, and change it to `database=alliance_mumble`
|
||||||
|
- `dbDriver=QMYSQL`
|
||||||
|
- `dbUsername=allianceserver` or whatever you called the Alliance Auth MySQL user
|
||||||
|
- `dbPassword=` that user’s password
|
||||||
|
- `dbPort=3306`
|
||||||
|
- `dbPrefix=murmur_`
|
||||||
|
|
||||||
To name your root channel, uncomment and set `registerName=` to whatever cool name you want
|
To name your root channel, uncomment and set `registerName=` to whatever cool name you want
|
||||||
|
|
||||||
@@ -52,55 +95,123 @@ Save and close the file.
|
|||||||
|
|
||||||
To get Mumble superuser account credentials, run the following:
|
To get Mumble superuser account credentials, run the following:
|
||||||
|
|
||||||
dpkg-reconfigure mumble-server
|
```bash
|
||||||
|
dpkg-reconfigure mumble-server
|
||||||
|
```
|
||||||
|
|
||||||
Set the password to something you’ll remember and write it down. This is needed to manage ACLs.
|
Set the password to something you’ll remember and write it down. This is your superuser password and later needed to manage ACLs.
|
||||||
|
|
||||||
Now restart the server to see the changes reflected.
|
Now restart the server to see the changes reflected.
|
||||||
|
|
||||||
service mumble-server restart
|
```bash
|
||||||
|
service mumble-server restart
|
||||||
|
```
|
||||||
|
|
||||||
That’s it! Your server is ready to be connected to at example.com:64738
|
That’s it! Your server is ready to be connected to at example.com:64738
|
||||||
|
|
||||||
## Configuring the Authenticator
|
## Configuring Mumble Authenticator
|
||||||
|
|
||||||
The ICE authenticator lives in the mumble-authenticator repository, cd to the directory where you cloned it.
|
The ICE authenticator lives in the mumble-authenticator repository, cd to the directory where you cloned it.
|
||||||
|
|
||||||
Make a copy of the default config:
|
Make a copy of the default config:
|
||||||
|
|
||||||
cp authenticator.ini.example authenticator.ini
|
```bash
|
||||||
|
cp authenticator.ini.example authenticator.ini
|
||||||
|
```
|
||||||
|
|
||||||
Edit `authenticator.ini` and change these values:
|
Edit `authenticator.ini` and change these values:
|
||||||
|
|
||||||
- `[database]`
|
- `[database]`
|
||||||
- `user = ` your allianceserver MySQL user
|
- `user =` your allianceserver MySQL user
|
||||||
- `password = ` your allianceserver MySQL user's password
|
- `password =` your allianceserver MySQL user's password
|
||||||
- `[ice]`
|
- `[ice]`
|
||||||
- `secret = ` the `icewritesecret` password set earlier
|
- `secret =` the `icewritesecret` password set earlier
|
||||||
|
|
||||||
Test your configuration by starting it: `python authenticator.py`
|
Test your configuration by starting it:
|
||||||
|
|
||||||
## Running the Authenticator
|
```bash
|
||||||
|
python /home/allianceserver/mumble-authenticator/authenticator.py
|
||||||
|
```
|
||||||
|
|
||||||
|
And finally ensure the allianceserver user has read/write permissions to the mumble authenticator files before proceeding:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
chown -R allianceserver:allianceserver /home/allianceserver/mumble-authenticator
|
||||||
|
```
|
||||||
|
|
||||||
The authenticator needs to be running 24/7 to validate users on Mumble. This can be achieved by adding a section to your auth project's supervisor config file like the following example:
|
The authenticator needs to be running 24/7 to validate users on Mumble. This can be achieved by adding a section to your auth project's supervisor config file like the following example:
|
||||||
|
|
||||||
```
|
```text
|
||||||
[program:authenticator]
|
[program:authenticator]
|
||||||
command=/path/to/venv/bin/python authenticator.py
|
command=/home/allianceserver/venv/auth/bin/python authenticator.py
|
||||||
directory=/path/to/authenticator/directory/
|
directory=/home/allianceserver/mumble-authenticator
|
||||||
user=allianceserver
|
user=allianceserver
|
||||||
stdout_logfile=/path/to/authenticator/directory/authenticator.log
|
stdout_logfile=/home/allianceserver/myauth/log/authenticator.log
|
||||||
stderr_logfile=/path/to/authenticator/directory/authenticator.log
|
stderr_logfile=/home/allianceserver/myauth/log/authenticator.log
|
||||||
autostart=true
|
autostart=true
|
||||||
autorestart=true
|
autorestart=true
|
||||||
startsecs=10
|
startsecs=10
|
||||||
priority=998
|
priority=996
|
||||||
```
|
```
|
||||||
|
|
||||||
|
In addition we'd recommend to add the authenticator to Auth's restart group in your supervisor conf. For that you need to add it to the group line as shown in the following example:
|
||||||
|
|
||||||
Note that groups will only be created on Mumble automatically when a user joins who is in the group.
|
```text
|
||||||
|
[group:myauth]
|
||||||
|
programs=beat,worker,gunicorn,authenticator
|
||||||
|
priority=999
|
||||||
|
```
|
||||||
|
|
||||||
## Prepare Auth
|
To enable the changes in your supervisor configuration you need to restart the supervisor process itself. And before we do that we are shutting down the current Auth supervisors gracefully:
|
||||||
In your project's settings file, set `MUMBLE_URL` to the public address of your mumble server. Do not include any leading `http://` or `mumble://`.
|
|
||||||
|
|
||||||
Run migrations and restart Gunicorn and Celery to complete setup.
|
```bash
|
||||||
|
supervisor stop myauth:
|
||||||
|
systemctl restart supervisor
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuring Auth
|
||||||
|
|
||||||
|
In your auth project's settings file (`myauth/settings/local.py`), do the following:
|
||||||
|
|
||||||
|
- Add `'allianceauth.services.modules.mumble',` to your `INSTALLED_APPS` list
|
||||||
|
- set `MUMBLE_URL` to the public address of your mumble server. Do not include any leading `http://` or `mumble://`.
|
||||||
|
|
||||||
|
Example config:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Installed apps
|
||||||
|
INSTALLED_APPS += [
|
||||||
|
# ...
|
||||||
|
'allianceauth.services.modules.mumble'
|
||||||
|
# ...
|
||||||
|
]
|
||||||
|
|
||||||
|
# Mumble Configuration
|
||||||
|
MUMBLE_URL = "mumble.example.com"
|
||||||
|
```
|
||||||
|
|
||||||
|
Finally, run migrations and restart your supervisor to complete the setup:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python /home/allianceserver/myauth/manage.py migrate
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
supervisorctl restart myauth:
|
||||||
|
```
|
||||||
|
|
||||||
|
## Permissions on Auth
|
||||||
|
|
||||||
|
To enable the mumble service for users on Auth you need to give them the `access_mumble` permission. This permission is often added to the `Member` state.
|
||||||
|
|
||||||
|
```eval_rst
|
||||||
|
.. note::
|
||||||
|
Note that groups will only be created on Mumble automatically when a user joins who is in the group.
|
||||||
|
```
|
||||||
|
|
||||||
|
## ACL configuration
|
||||||
|
|
||||||
|
On a freshly installed mumble server only your superuser has the right to configure ACLs and create channels. The credentials for logging in with your superuser are:
|
||||||
|
|
||||||
|
- user: `SuperUser`
|
||||||
|
- password: *what you defined when configuring your mumble server*
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ Welcome to the official documentation for **Alliance Auth**!
|
|||||||
installation/index
|
installation/index
|
||||||
features/index
|
features/index
|
||||||
maintenance/index
|
maintenance/index
|
||||||
development/index
|
|
||||||
support/index
|
support/index
|
||||||
|
customizing/index
|
||||||
|
development/index
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ This document describes how to install **Alliance Auth** from scratch.
|
|||||||
|
|
||||||
```eval_rst
|
```eval_rst
|
||||||
.. note::
|
.. note::
|
||||||
There are additional installation steps for activating services and apps that come with **Alliance Auth**. Please see the page for the respective service or apps in chapter **Features** for details.
|
There are additional installation steps for activating services and apps that come with **Alliance Auth**. Please see the page for the respective service or apps in chapter :doc:`/features/index` for details.
|
||||||
```
|
```
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
@@ -28,7 +28,7 @@ Alliance Auth can be installed on any Unix like operating system. Dependencies a
|
|||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
Alliance Auth requires python3.5 or higher. Ensure it is installed on your server before proceeding.
|
Alliance Auth requires Python 3.6 or higher. Ensure it is installed on your server before proceeding.
|
||||||
|
|
||||||
Ubuntu:
|
Ubuntu:
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
|
|||||||
```eval_rst
|
```eval_rst
|
||||||
.. note::
|
.. note::
|
||||||
You may see errors when you add the timezone tables. To make sure that they were correctly added run the following commands and check for the ``time_zone`` tables::
|
You may see errors when you add the timezone tables. To make sure that they were correctly added run the following commands and check for the ``time_zone`` tables::
|
||||||
|
|
||||||
mysql -u root -p
|
mysql -u root -p
|
||||||
use mysql;
|
use mysql;
|
||||||
show tables;
|
show tables;
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user