From 2725b9b1f6764f0f8f39531a8a3aa195c33e480b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dachary?= Date: Tue, 4 Oct 2022 09:09:01 +0200 Subject: [PATCH 1/2] avoid crash in CI when STRIPE is not set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Loïc Dachary --- dashboard/local_settings.ci.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dashboard/local_settings.ci.py b/dashboard/local_settings.ci.py index 5bbf9ac..098879f 100644 --- a/dashboard/local_settings.ci.py +++ b/dashboard/local_settings.ci.py @@ -44,8 +44,8 @@ PAYMENT_VARIANTS = { "stripe": ( "payments.stripe.StripeProvider", # please don't change this { - "secret_key": env.get_value("STRIPE_SECRET_KEY"), - "public_key": env.get_value("STRIPE_PUBLIC_KEY"), + "secret_key": env.get_value("STRIPE_SECRET_KEY", default="UNSET"), + "public_key": env.get_value("STRIPE_PUBLIC_KEY", default="UNSET"), }, ) } From ad925cddfc7bb46936b53d0b37518b4946f9a916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dachary?= Date: Tue, 4 Oct 2022 08:50:06 +0200 Subject: [PATCH 2/2] convert instance names to lowercase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://gitea.gna.org/Hostea/dashboard/issues/69 Signed-off-by: Loïc Dachary --- dash/tests.py | 21 ++++++++++++--------- dash/utils.py | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/dash/tests.py b/dash/tests.py index 770d4f5..912c202 100644 --- a/dash/tests.py +++ b/dash/tests.py @@ -30,7 +30,7 @@ from payments import get_payment_model, RedirectNeeded, PaymentStatus from accounts.tests import login_util, register_util from .models import InstanceConfiguration, Instance -from .utils import create_instance, VmErrors, VmException +from .utils import create_instance, sanitize_vm_name, VmErrors, VmException def create_configurations(t: TestCase): @@ -198,11 +198,21 @@ class CreateInstance(TestCase): register_util(t=self, username="createinstance_user") create_configurations(t=self) + + def test_sanitize_vm_name(self): + self.assertEqual(sanitize_vm_name(vm_name="LOWERname"), "lowername") + + with self.assertRaises(VmException): + sanitize_vm_name(vm_name="12345452131324234234234234") + + with self.assertRaises(VmException): + sanitize_vm_name(vm_name="122342$#34234") + + @override_settings( HOSTEA=infra_custom_config(test_name="test_create_instance_util") ) def test_create_instance_util(self): - vm_name = "test_create_instance_renders" configuration = self.instance_config[0].name with self.assertRaises(VmException): @@ -212,13 +222,6 @@ class CreateInstance(TestCase): user=self.user, ) - with self.assertRaises(VmException): - create_instance( - vm_name="122342$#34234", - configuration_name=configuration, - user=self.user, - ) - @override_settings( HOSTEA=infra_custom_config(test_name="test_create_instance_renders") ) diff --git a/dash/utils.py b/dash/utils.py index b4b2652..52e298a 100644 --- a/dash/utils.py +++ b/dash/utils.py @@ -43,11 +43,13 @@ class VmException(Exception): return self.error -def create_instance(vm_name: str, configuration_name: str, user: User) -> Instance: +def sanitize_vm_name(vm_name: str) -> str: """ - Create instance view + Sanity checks and normalization of the vm name """ + vm_name = vm_name.lower() + if len(vm_name) > 20: raise VmException(code=VmErrors.NAME_TOO_LONG) if not str.isalnum(vm_name): @@ -56,6 +58,16 @@ def create_instance(vm_name: str, configuration_name: str, user: User) -> Instan if Instance.objects.filter(name=vm_name).exists(): raise VmException(code=VmErrors.NAME_EXISTS) + return vm_name + + +def create_instance(vm_name: str, configuration_name: str, user: User) -> Instance: + """ + Create instance view + """ + + vm_name = sanitize_vm_name(vm_name) + if not InstanceConfiguration.objects.filter(name=configuration_name).exists(): raise VmException(code=VmErrors.NO_CONFIG)