mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-13 14:30:17 +02:00
[ADD] get_main_character_from_user
This commit is contained in:
parent
6a990c11e6
commit
4cc7135ace
@ -13,3 +13,26 @@ def get_sentinel_user() -> User:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
return User.objects.get_or_create(username="deleted")[0]
|
return User.objects.get_or_create(username="deleted")[0]
|
||||||
|
|
||||||
|
|
||||||
|
def get_main_character_from_user(user: User) -> str:
|
||||||
|
"""
|
||||||
|
Get the main character from a user
|
||||||
|
|
||||||
|
:param user:
|
||||||
|
:type user:
|
||||||
|
:return:
|
||||||
|
:rtype:
|
||||||
|
"""
|
||||||
|
|
||||||
|
if user is None:
|
||||||
|
sentinel_user = get_sentinel_user()
|
||||||
|
|
||||||
|
return sentinel_user.username
|
||||||
|
|
||||||
|
try:
|
||||||
|
return_value = user.profile.main_character.character_name
|
||||||
|
except AttributeError:
|
||||||
|
return str(user)
|
||||||
|
|
||||||
|
return return_value
|
||||||
|
129
allianceauth/framework/tests/test_api_user.py
Normal file
129
allianceauth/framework/tests/test_api_user.py
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
"""
|
||||||
|
Test sentinel user
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
# Django
|
||||||
|
from django.contrib.auth.models import Group, User
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Alliance Auth
|
||||||
|
from allianceauth.framework.api.user import get_sentinel_user, \
|
||||||
|
get_main_character_from_user
|
||||||
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
|
|
||||||
|
class TestSentinelUser(TestCase):
|
||||||
|
"""
|
||||||
|
Tests for the sentinel user
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_should_create_user_when_it_does_not_exist(self) -> None:
|
||||||
|
"""
|
||||||
|
Test should create a sentinel user when it doesn't exist
|
||||||
|
|
||||||
|
:return:
|
||||||
|
:rtype:
|
||||||
|
"""
|
||||||
|
|
||||||
|
# when
|
||||||
|
user = get_sentinel_user()
|
||||||
|
|
||||||
|
# then
|
||||||
|
self.assertEqual(first=user.username, second="deleted")
|
||||||
|
|
||||||
|
def test_should_return_user_when_it_does(self) -> None:
|
||||||
|
"""
|
||||||
|
Test should return sentinel user when it exists
|
||||||
|
|
||||||
|
:return:
|
||||||
|
:rtype:
|
||||||
|
"""
|
||||||
|
|
||||||
|
# given
|
||||||
|
User.objects.create_user(username="deleted")
|
||||||
|
|
||||||
|
# when
|
||||||
|
user = get_sentinel_user()
|
||||||
|
|
||||||
|
# then
|
||||||
|
self.assertEqual(first=user.username, second="deleted")
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetMainForUser(TestCase):
|
||||||
|
"""
|
||||||
|
Tests for get_main_character_from_user
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls) -> None:
|
||||||
|
"""
|
||||||
|
Set up groups and users
|
||||||
|
"""
|
||||||
|
|
||||||
|
super().setUpClass()
|
||||||
|
|
||||||
|
character_name = "William T. Riker"
|
||||||
|
username = re.sub(pattern=r"[^\w\d@\.\+-]", repl="_", string=character_name)
|
||||||
|
|
||||||
|
cls.user = AuthUtils.create_user(username=username)
|
||||||
|
cls.group = Group.objects.create(name="Enterprise Crew")
|
||||||
|
|
||||||
|
AuthUtils.add_main_character_2(
|
||||||
|
user=cls.user, name=character_name, character_id=1001
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_get_main_character_from_user_should_return_character_name(self):
|
||||||
|
"""
|
||||||
|
Test should return the main character name for a regular user
|
||||||
|
|
||||||
|
:return:
|
||||||
|
:rtype:
|
||||||
|
"""
|
||||||
|
|
||||||
|
character_name = get_main_character_from_user(user=self.user)
|
||||||
|
|
||||||
|
self.assertEqual(first=character_name, second="William T. Riker")
|
||||||
|
|
||||||
|
def test_get_main_character_from_user_should_return_user_name(self):
|
||||||
|
"""
|
||||||
|
Test should return just the username for a user without a character
|
||||||
|
|
||||||
|
:return:
|
||||||
|
:rtype:
|
||||||
|
"""
|
||||||
|
|
||||||
|
user = AuthUtils.create_user(username="John Doe")
|
||||||
|
|
||||||
|
character_name = get_main_character_from_user(user=user)
|
||||||
|
|
||||||
|
self.assertEqual(first=character_name, second="John Doe")
|
||||||
|
|
||||||
|
def test_get_main_character_from_user_should_return_sentinel_user(self):
|
||||||
|
"""
|
||||||
|
Test should return "deleted" as username (Sentinel User)
|
||||||
|
|
||||||
|
:return:
|
||||||
|
:rtype:
|
||||||
|
"""
|
||||||
|
|
||||||
|
user = get_sentinel_user()
|
||||||
|
|
||||||
|
character_name = get_main_character_from_user(user=user)
|
||||||
|
|
||||||
|
self.assertEqual(first=character_name, second="deleted")
|
||||||
|
|
||||||
|
def test_get_main_character_from_user_should_return_sentinel_user_for_none(self):
|
||||||
|
"""
|
||||||
|
Test should return "deleted" (Sentinel User) if user is None
|
||||||
|
|
||||||
|
:return:
|
||||||
|
:rtype:
|
||||||
|
"""
|
||||||
|
|
||||||
|
user = None
|
||||||
|
|
||||||
|
character_name = get_main_character_from_user(user=user)
|
||||||
|
|
||||||
|
self.assertEqual(first=character_name, second="deleted")
|
@ -1,47 +0,0 @@
|
|||||||
"""
|
|
||||||
Test sentinel user
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Django
|
|
||||||
from django.contrib.auth.models import User
|
|
||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
# Alliance Auth
|
|
||||||
from allianceauth.framework.api.user import get_sentinel_user
|
|
||||||
|
|
||||||
|
|
||||||
class TestSentinelUser(TestCase):
|
|
||||||
"""
|
|
||||||
Tests for the sentinel user
|
|
||||||
"""
|
|
||||||
|
|
||||||
def test_should_create_user_when_it_does_not_exist(self) -> None:
|
|
||||||
"""
|
|
||||||
Test should create a sentinel user when it doesn't exist
|
|
||||||
|
|
||||||
:return:
|
|
||||||
:rtype:
|
|
||||||
"""
|
|
||||||
|
|
||||||
# when
|
|
||||||
user = get_sentinel_user()
|
|
||||||
|
|
||||||
# then
|
|
||||||
self.assertEqual(first=user.username, second="deleted")
|
|
||||||
|
|
||||||
def test_should_return_user_when_it_does(self) -> None:
|
|
||||||
"""
|
|
||||||
Test should return sentinel user when it exists
|
|
||||||
|
|
||||||
:return:
|
|
||||||
:rtype:
|
|
||||||
"""
|
|
||||||
|
|
||||||
# given
|
|
||||||
User.objects.create_user(username="deleted")
|
|
||||||
|
|
||||||
# when
|
|
||||||
user = get_sentinel_user()
|
|
||||||
|
|
||||||
# then
|
|
||||||
self.assertEqual(first=user.username, second="deleted")
|
|
Loading…
x
Reference in New Issue
Block a user