From 4cc7135ace02134fb4b9d29e652d86cd8dd68324 Mon Sep 17 00:00:00 2001 From: Peter Pfeufer Date: Fri, 3 Nov 2023 19:40:36 +0100 Subject: [PATCH] [ADD] `get_main_character_from_user` --- allianceauth/framework/api/user.py | 23 ++++ allianceauth/framework/tests/test_api_user.py | 129 ++++++++++++++++++ .../framework/tests/test_sentinel_user.py | 47 ------- 3 files changed, 152 insertions(+), 47 deletions(-) create mode 100644 allianceauth/framework/tests/test_api_user.py delete mode 100644 allianceauth/framework/tests/test_sentinel_user.py diff --git a/allianceauth/framework/api/user.py b/allianceauth/framework/api/user.py index a39b67fb..cada8262 100644 --- a/allianceauth/framework/api/user.py +++ b/allianceauth/framework/api/user.py @@ -13,3 +13,26 @@ def get_sentinel_user() -> User: """ 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 diff --git a/allianceauth/framework/tests/test_api_user.py b/allianceauth/framework/tests/test_api_user.py new file mode 100644 index 00000000..7201f5b0 --- /dev/null +++ b/allianceauth/framework/tests/test_api_user.py @@ -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") diff --git a/allianceauth/framework/tests/test_sentinel_user.py b/allianceauth/framework/tests/test_sentinel_user.py deleted file mode 100644 index 60d9f69e..00000000 --- a/allianceauth/framework/tests/test_sentinel_user.py +++ /dev/null @@ -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")