Repository: comic/grand-challenge.org

Issue:
Add token for retina_demo user to init_gc_demo command
For development purposes, we should create a token for the `retina_demo` user (or another user that is in the `retina_graders` group) in the `init_gc_demo` management command and output it to the console. This makes it easier to copy that token to the `.env` file in the `rse-cirrus-core` repository, so that user is authenticated to make API requests to the grand-challenge dev environment.


Discussion:

Resolving patch:
diff --git a/app/config/settings.py b/app/config/settings.py
--- a/app/config/settings.py
+++ b/app/config/settings.py
@@ -664,6 +664,8 @@ def strtobool(val) -> bool:
     MIDDLEWARE = ("corsheaders.middleware.CorsMiddleware", *MIDDLEWARE)
     CORS_ORIGIN_ALLOW_ALL = True
 
+    LOGGING["loggers"]["grandchallenge"]["level"] = "DEBUG"
+
     if ENABLE_DEBUG_TOOLBAR:
         INSTALLED_APPS += ("debug_toolbar",)
 
diff --git a/app/grandchallenge/core/management/commands/init_gc_demo.py b/app/grandchallenge/core/management/commands/init_gc_demo.py
--- a/app/grandchallenge/core/management/commands/init_gc_demo.py
+++ b/app/grandchallenge/core/management/commands/init_gc_demo.py
@@ -1,10 +1,12 @@
 import base64
+import logging
 
 from django.conf import settings
 from django.contrib.flatpages.models import FlatPage
 from django.contrib.sites.models import Site
 from django.core.files.base import ContentFile
 from django.core.management import BaseCommand
+from rest_framework.authtoken.models import Token
 from userena.models import UserenaSignup
 from django.contrib.auth.models import Group
 
@@ -19,12 +21,19 @@
 from grandchallenge.evaluation.models import Result, Submission, Job, Method
 from grandchallenge.pages.models import Page
 
+logger = logging.getLogger(__name__)
+
 
 class Command(BaseCommand):
     def handle(self, *args, **options):
         """
         Creates the main project, demo user and demo challenge
         """
+        if not settings.DEBUG:
+            raise RuntimeError(
+                "Skipping this command, server is not in DEBUG mode."
+            )
+
         # Set the default domain that is used in RequestFactory
         site = Site.objects.get(pk=settings.SITE_ID)
 
@@ -69,6 +78,11 @@ def handle(self, *args, **options):
         )
         adminuser.is_staff = True
         adminuser.save()
+
+        admintoken, _ = Token.objects.get_or_create(
+            user=adminuser, key="1b9436200001f2eaf57cd77db075cbb60a49a00a"
+        )
+
         demo = Challenge.objects.create(
             short_name="demo",
             description="demo project",
@@ -178,3 +192,14 @@ def handle(self, *args, **options):
             name=settings.RETINA_GRADERS_GROUP_NAME
         )
         retina_demo.groups.add(retina_group)
+
+        retinatoken, _ = Token.objects.get_or_create(
+            user=retina_demo, key="f1f98a1733c05b12118785ffd995c250fe4d90da"
+        )
+
+        logger.debug(
+            f"{'*'*80}\n"
+            f"\tadmin token is: {admintoken}\n"
+            f"\tretina_demo token is: {retinatoken}\n"
+            f"{'*'*80}"
+        )


Test patch:
diff --git a/app/tests/core_tests/test_management_commands.py b/app/tests/core_tests/test_management_commands.py
--- a/app/tests/core_tests/test_management_commands.py
+++ b/app/tests/core_tests/test_management_commands.py
@@ -4,8 +4,15 @@
 
 
 @pytest.mark.django_db
-def test_initdemo():
+def test_initdemo(settings):
     call_command("check_permissions")
+
+    settings.DEBUG = False
+    with pytest.raises(RuntimeError):
+        # It should error out in production mode
+        call_command("init_gc_demo")
+
+    settings.DEBUG = True
     call_command("init_gc_demo")
 
     # It should create a number of users