Repository: carpentries/amy

Issue:
Prepare AMY staging instance for actual use
New test AMY server is running, but it lacks some features from the other server.

- [x] Run fixtures (should be accompanied by #2239)
- [x] Scaffold non-admin users for AMY database
- [ ] Add default admin user


Discussion:

Resolving patch:
diff --git a/amy/workshops/management/commands/create_superuser.py b/amy/workshops/management/commands/create_superuser.py
--- a/amy/workshops/management/commands/create_superuser.py
+++ b/amy/workshops/management/commands/create_superuser.py
@@ -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="admin@example.org",


Test patch:
diff --git a/amy/workshops/tests/test_commands.py b/amy/workshops/tests/test_commands.py
--- a/amy/workshops/tests/test_commands.py
+++ b/amy/workshops/tests/test_commands.py
@@ -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
@@ -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,
 )
@@ -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="admin@example.org",
+            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)