added superuser support in apps.lyric.admin & new manage.py cmd ensure_superuser; .tests.integrated.test_admin & .test_management_commands green
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Token, User
|
||||
|
||||
|
||||
admin.site.register(User)
|
||||
class UserAdmin(admin.ModelAdmin):
|
||||
list_display = ["email"]
|
||||
search_fields = ["email"]
|
||||
|
||||
admin.site.register(User, UserAdmin)
|
||||
admin.site.register(Token)
|
||||
|
||||
0
src/apps/lyric/management/__init__.py
Normal file
0
src/apps/lyric/management/__init__.py
Normal file
0
src/apps/lyric/management/commands/__init__.py
Normal file
0
src/apps/lyric/management/commands/__init__.py
Normal file
21
src/apps/lyric/management/commands/ensure_superuser.py
Normal file
21
src/apps/lyric/management/commands/ensure_superuser.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import os
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from apps.lyric.models import User
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Create a superuser if none exists"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if User.objects.filter(is_superuser=True).exists():
|
||||
self.stdout.write("Superuser already exists!")
|
||||
return
|
||||
email = os.environ.get('DJANGO_SUPERUSER_EMAIL')
|
||||
password = os.environ.get('DJANGO_SUPERUSER_PASSWORD')
|
||||
if not email or not password:
|
||||
self.stdout.write("Superuser credentials not set!—skipping")
|
||||
return
|
||||
User.objects.create_superuser(email=email, password=password)
|
||||
self.stdout.write("Superuser created!")
|
||||
25
src/apps/lyric/tests/integrated/test_admin.py
Normal file
25
src/apps/lyric/tests/integrated/test_admin.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from apps.lyric.models import User
|
||||
|
||||
|
||||
class UserAdminTest(TestCase):
|
||||
def setUp(self):
|
||||
self.superuser = User.objects.create_superuser(
|
||||
email="admin@example.com", password="secret"
|
||||
)
|
||||
self.client.force_login(self.superuser)
|
||||
|
||||
def test_user_changelist_loads(self):
|
||||
response = self.client.get("/admin/lyric/user/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_user_changelist_displays_email(self):
|
||||
response = self.client.get("/admin/lyric/user/")
|
||||
self.assertContains(response, "admin@example.com")
|
||||
|
||||
def test_user_changelist_search_by_email(self):
|
||||
User.objects.create_superuser(email="other@example.com", password="x")
|
||||
response = self.client.get("/admin/lyric/user/?q=admin")
|
||||
self.assertContains(response, "admin@example.com")
|
||||
self.assertNotContains(response, "other@example.com")
|
||||
34
src/apps/lyric/tests/integrated/test_management_commands.py
Normal file
34
src/apps/lyric/tests/integrated/test_management_commands.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import os
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
from unittest.mock import patch
|
||||
|
||||
# from apps.lyric.management.commands.ensure_superuser import EnsureSuperuserCommand
|
||||
from apps.lyric.models import User
|
||||
|
||||
|
||||
FAKE_ENV = {
|
||||
'DJANGO_SUPERUSER_EMAIL': 'admin@example.com',
|
||||
'DJANGO_SUPERUSER_PASSWORD': 'secret',
|
||||
}
|
||||
|
||||
|
||||
class EnsureSuperuserCommandTest(TestCase):
|
||||
def test_creates_superuser_if_none_exists(self):
|
||||
with patch.dict('os.environ', FAKE_ENV):
|
||||
call_command('ensure_superuser')
|
||||
self.assertEqual(User.objects.filter(is_superuser=True).count(), 1)
|
||||
|
||||
def test_does_not_create_duplicate_if_superuser_exists(self):
|
||||
User.objects.create_superuser(email="admin@example.com", password="secret")
|
||||
with patch.dict('os.environ', FAKE_ENV):
|
||||
call_command('ensure_superuser')
|
||||
self.assertEqual(User.objects.filter(is_superuser=True).count(), 1)
|
||||
|
||||
def test_skips_creation_if_credentials_not_set(self):
|
||||
with patch.dict("os.environ", {}):
|
||||
os.environ.pop("DJANGO_SUPERUSER_EMAIL", None)
|
||||
os.environ.pop("DJANGO_SUPERUSER_PASSWORD", None)
|
||||
call_command("ensure_superuser")
|
||||
self.assertEqual(User.objects.filter(is_superuser=True).count(), 0)
|
||||
Reference in New Issue
Block a user