Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run create_superuser to create super user on application's start #2333

Merged
merged 2 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion amy/workshops/management/commands/create_superuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ class Command(BaseCommand):
help = 'Create a superuser called "admin" with password "admin".'

def handle(self, *args, **options):
username = "admin"

if Person.objects.filter(username=username).exists():
print("Admin user exists, quitting.")
return

try:
admin = Person.objects.create_superuser(
username="admin",
username=username,
personal="admin",
family="admin",
email="[email protected]",
Expand Down
48 changes: 48 additions & 0 deletions amy/workshops/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from datetime import date

from django.core.management.base import BaseCommand
from django.test import TestCase

from communityroles.models import CommunityRole, CommunityRoleConfig
Expand All @@ -13,6 +14,7 @@
from workshops.management.commands.assign_trainer_community_role import (
Command as AssignTrainerCommunityRole,
)
from workshops.management.commands.create_superuser import Command as CreateSuperuser
from workshops.management.commands.migrate_inactive_trainers_to_trainer_badges import (
Command as MigrateInactiveTrainersToTrainerBadges,
)
Expand Down Expand Up @@ -596,3 +598,49 @@ def test_handle(self) -> None:
self.assertEqual(award2.badge, self.trainer_badge) # Changed
self.assertEqual(award2.awarded, date(2021, 1, 1))
self.assertEqual(award1.badge, self.trainer_badge) # Not changed


class TestCreateSuperuserCommand(TestCase):
command: BaseCommand

def setUp(self) -> None:
self.command = CreateSuperuser()
instructor_badge = Badge.objects.create(name="instructor", title="Instructor")
CommunityRoleConfig.objects.create(
name="instructor",
link_to_award=True,
award_badge_limit=instructor_badge,
link_to_membership=False,
additional_url=False,
)

def test_admin_created(self) -> None:
"""When `admin` account doesn't exist, it gets created."""
# Act
self.command.handle()

# Assert
Person.objects.get(username="admin")

def test_admin_not_created(self) -> None:
"""When `admin` account exists, the command doesn't change it or create other
superuser accounts."""
# Arrange
superuser = Person.objects.create_superuser(
username="admin",
personal="admin",
family="admin",
email="[email protected]",
password="admin",
)
superuser.is_active = False
superuser.save()

# Act
self.command.handle()

# Assert
Person.objects.get(username="admin")
self.assertEqual(Person.objects.filter(is_superuser=True).count(), 1)
superuser.refresh_from_db()
self.assertFalse(superuser.is_active)
2 changes: 2 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/venv/amy/bin/python manage.py runscript seed_badges
/venv/amy/bin/python manage.py runscript seed_training_requirements

/venv/amy/bin/python manage.py create_superuser

/venv/amy/bin/gunicorn \
--workers=4 \
--bind=0.0.0.0:80 \
Expand Down