From 7657db4212e84bf27325db847e0830131b07ee1d Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Fri, 4 Jan 2019 11:47:23 +0100
Subject: [PATCH] Fix #648: Improved test suite speed by reducing / disabling
 expensive operations

---
 api/config/settings/common.py        |  4 ++++
 api/config/settings/local.py         | 14 +++++++++++---
 api/funkwhale_api/federation/keys.py |  5 ++++-
 api/funkwhale_api/music/models.py    |  2 +-
 api/funkwhale_api/users/models.py    |  2 +-
 api/setup.cfg                        |  4 ++++
 api/tests/conftest.py                |  6 ++++++
 changes/changelog.d/648.enhancement  |  1 +
 8 files changed, 32 insertions(+), 6 deletions(-)
 create mode 100644 changes/changelog.d/648.enhancement

diff --git a/api/config/settings/common.py b/api/config/settings/common.py
index 97a08833..c0ff1e82 100644
--- a/api/config/settings/common.py
+++ b/api/config/settings/common.py
@@ -592,3 +592,7 @@ VERSATILEIMAGEFIELD_RENDITION_KEY_SETS = {
     ]
 }
 VERSATILEIMAGEFIELD_SETTINGS = {"create_images_on_demand": False}
+RSA_KEY_SIZE = 2048
+# for performance gain in tests, since we don't need to actually create the
+# thumbnails
+CREATE_IMAGE_THUMBNAILS = env.bool("CREATE_IMAGE_THUMBNAILS", default=True)
diff --git a/api/config/settings/local.py b/api/config/settings/local.py
index f639fabd..91a202b6 100644
--- a/api/config/settings/local.py
+++ b/api/config/settings/local.py
@@ -31,7 +31,6 @@ EMAIL_PORT = 1025
 
 # django-debug-toolbar
 # ------------------------------------------------------------------------------
-MIDDLEWARE += ("debug_toolbar.middleware.DebugToolbarMiddleware",)
 
 # INTERNAL_IPS = ('127.0.0.1', '10.0.2.2',)
 
@@ -45,14 +44,18 @@ DEBUG_TOOLBAR_CONFIG = {
 # django-extensions
 # ------------------------------------------------------------------------------
 # INSTALLED_APPS += ('django_extensions', )
-INSTALLED_APPS += ("debug_toolbar",)
+
+# Debug toolbar is slow, we disable it for tests
+DEBUG_TOOLBAR_ENABLED = env.bool("DEBUG_TOOLBAR_ENABLED", default=DEBUG)
+if DEBUG_TOOLBAR_ENABLED:
+    MIDDLEWARE += ("debug_toolbar.middleware.DebugToolbarMiddleware",)
+    INSTALLED_APPS += ("debug_toolbar",)
 
 # TESTING
 # ------------------------------------------------------------------------------
 TEST_RUNNER = "django.test.runner.DiscoverRunner"
 
 # CELERY
-# In development, all tasks will be executed locally by blocking until the task returns
 CELERY_TASK_ALWAYS_EAGER = False
 # END CELERY
 
@@ -72,3 +75,8 @@ LOGGING = {
     },
 }
 CSRF_TRUSTED_ORIGINS = [o for o in ALLOWED_HOSTS]
+
+
+if env.bool("WEAK_PASSWORDS", default=False):
+    # Faster during tests
+    PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)
diff --git a/api/funkwhale_api/federation/keys.py b/api/funkwhale_api/federation/keys.py
index e7c30c50..780e149e 100644
--- a/api/funkwhale_api/federation/keys.py
+++ b/api/funkwhale_api/federation/keys.py
@@ -1,6 +1,8 @@
 import re
 import urllib.parse
 
+from django.conf import settings
+
 from cryptography.hazmat.backends import default_backend as crypto_default_backend
 from cryptography.hazmat.primitives import serialization as crypto_serialization
 from cryptography.hazmat.primitives.asymmetric import rsa
@@ -8,7 +10,8 @@ from cryptography.hazmat.primitives.asymmetric import rsa
 KEY_ID_REGEX = re.compile(r"keyId=\"(?P<id>.*)\"")
 
 
-def get_key_pair(size=2048):
+def get_key_pair(size=None):
+    size = size or settings.RSA_KEY_SIZE
     key = rsa.generate_private_key(
         backend=crypto_default_backend(), public_exponent=65537, key_size=size
     )
diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py
index 03e4a956..325cdace 100644
--- a/api/funkwhale_api/music/models.py
+++ b/api/funkwhale_api/music/models.py
@@ -1107,7 +1107,7 @@ def update_request_status(sender, instance, created, **kwargs):
 
 @receiver(models.signals.post_save, sender=Album)
 def warm_album_covers(sender, instance, **kwargs):
-    if not instance.cover:
+    if not instance.cover or not settings.CREATE_IMAGE_THUMBNAILS:
         return
     album_covers_warmer = VersatileImageFieldWarmer(
         instance_or_queryset=instance, rendition_key_set="square", image_attr="cover"
diff --git a/api/funkwhale_api/users/models.py b/api/funkwhale_api/users/models.py
index 79650301..32e4869a 100644
--- a/api/funkwhale_api/users/models.py
+++ b/api/funkwhale_api/users/models.py
@@ -295,7 +295,7 @@ def init_ldap_user(sender, user, ldap_user, **kwargs):
 
 @receiver(models.signals.post_save, sender=User)
 def warm_user_avatar(sender, instance, **kwargs):
-    if not instance.avatar:
+    if not instance.avatar or not settings.CREATE_IMAGE_THUMBNAILS:
         return
     user_avatar_warmer = VersatileImageFieldWarmer(
         instance_or_queryset=instance, rendition_key_set="square", image_attr="avatar"
diff --git a/api/setup.cfg b/api/setup.cfg
index aa9a57ab..4466d626 100644
--- a/api/setup.cfg
+++ b/api/setup.cfg
@@ -19,3 +19,7 @@ env =
     CELERY_BROKER_URL=memory://
     CELERY_TASK_ALWAYS_EAGER=True
     FEDERATION_HOSTNAME=test.federation
+    DEBUG_TOOLBAR_ENABLED=False
+    DEBUG=False
+    WEAK_PASSWORDS=True
+    CREATE_IMAGE_THUMBNAILS=False
diff --git a/api/tests/conftest.py b/api/tests/conftest.py
index 22d8f7eb..03dbdfa4 100644
--- a/api/tests/conftest.py
+++ b/api/tests/conftest.py
@@ -410,3 +410,9 @@ def no_api_auth(preferences):
 def migrator(transactional_db):
     yield MigrationExecutor(connection)
     call_command("migrate", interactive=False)
+
+
+@pytest.fixture(autouse=True)
+def rsa_small_key(settings):
+    # smaller size for faster generation, since it's CPU hungry
+    settings.RSA_KEY_SIZE = 512
diff --git a/changes/changelog.d/648.enhancement b/changes/changelog.d/648.enhancement
new file mode 100644
index 00000000..acf1fd28
--- /dev/null
+++ b/changes/changelog.d/648.enhancement
@@ -0,0 +1 @@
+Improved test suite speed by reducing / disabling expensive operations (#648)
-- 
GitLab