diff --git a/allianceauth/fleetactivitytracking/migrations/0007_sentinel_user.py b/allianceauth/fleetactivitytracking/migrations/0007_sentinel_user.py new file mode 100644 index 00000000..0ac26d3e --- /dev/null +++ b/allianceauth/fleetactivitytracking/migrations/0007_sentinel_user.py @@ -0,0 +1,26 @@ +""" +Migration to AA Framework API method +""" + +from django.conf import settings +from django.db import migrations, models + +import allianceauth.framework.api.user + + +class Migration(migrations.Migration): + + dependencies = [ + ("fleetactivitytracking", "0006_auto_20180803_0430"), + ] + + operations = [ + migrations.AlterField( + model_name="fatlink", + name="creator", + field=models.ForeignKey( + on_delete=models.SET(allianceauth.framework.api.user.get_sentinel_user), + to=settings.AUTH_USER_MODEL + ), + ), + ] diff --git a/allianceauth/fleetactivitytracking/models.py b/allianceauth/fleetactivitytracking/models.py index e772cdc4..3cbd3d63 100644 --- a/allianceauth/fleetactivitytracking/models.py +++ b/allianceauth/fleetactivitytracking/models.py @@ -3,10 +3,7 @@ from django.db import models from django.utils import timezone from allianceauth.eveonline.models import EveCharacter - - -def get_sentinel_user(): - return User.objects.get_or_create(username='deleted')[0] +from allianceauth.framework.api.user import get_sentinel_user class Fatlink(models.Model): diff --git a/allianceauth/framework/api/user.py b/allianceauth/framework/api/user.py new file mode 100644 index 00000000..a39b67fb --- /dev/null +++ b/allianceauth/framework/api/user.py @@ -0,0 +1,15 @@ +""" +Alliance Auth User API +""" + +from django.contrib.auth.models import User + + +def get_sentinel_user() -> User: + """ + Get the sentinel user or create one + + :return: + """ + + return User.objects.get_or_create(username="deleted")[0] diff --git a/allianceauth/framework/tests/test_sentinel_user.py b/allianceauth/framework/tests/test_sentinel_user.py new file mode 100644 index 00000000..60d9f69e --- /dev/null +++ b/allianceauth/framework/tests/test_sentinel_user.py @@ -0,0 +1,47 @@ +""" +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")