From 011fb4816fe55bef62e0ae96886a8cb6ceb91536 Mon Sep 17 00:00:00 2001 From: Hostea dashboard Date: Sun, 4 Sep 2022 17:49:53 +0530 Subject: [PATCH 1/2] fix: validate VM names fixes: https://gitea.gna.org/Hostea/dashboard/issues/51 --- accounts/tests.py | 8 ++++++-- dash/utils.py | 7 +++++++ dash/views.py | 5 +---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/accounts/tests.py b/accounts/tests.py index 95d7840..06cd182 100644 --- a/accounts/tests.py +++ b/accounts/tests.py @@ -77,7 +77,9 @@ class LoginTest(TestCase): Tests if login template renders """ resp = self.client.get(reverse("accounts.login")) - self.assertEqual(b"A free forge ecosystem for free developers" in resp.content, True) + self.assertEqual( + b"A free forge ecosystem for free developers" in resp.content, True + ) def test_login_works(self): """ @@ -234,7 +236,9 @@ class RegistrationTest(TestCase): Tests if register template renders """ resp = self.client.get(reverse("accounts.register")) - self.assertEqual(b"A free forge ecosystem for free developers." in resp.content, True) + self.assertEqual( + b"A free forge ecosystem for free developers." in resp.content, True + ) def test_register_works(self): """ diff --git a/dash/utils.py b/dash/utils.py index ac3d7b3..3ba5208 100644 --- a/dash/utils.py +++ b/dash/utils.py @@ -23,6 +23,8 @@ from .models import Instance, InstanceConfiguration @unique class VmErrors(Enum): NAME_EXISTS = "Instance name exists, please try again with a different name" + ILLEGAL_NAME = "Only alphanumeric characters are allowed in instance name" + NAME_TOO_LONG = "Instance name must be less than 20 characters" NO_CONFIG = "Configuration doesn't exist, please try again." def __str__(self) -> str: @@ -46,6 +48,11 @@ def create_instance(vm_name: str, configuration_name: str, user: User) -> Instan Create instance view """ + if len(vm_name) > 20: + raise VmException(code=VmErrors.NAME_TOO_LONG) + if not str.isalnum(vm_name): + raise VmException(code=VmErrors.ILLEGAL_NAME) + if Instance.objects.filter(name=vm_name).exists(): raise VmException(code=VmErrors.NAME_EXISTS) diff --git a/dash/views.py b/dash/views.py index 383a072..28be20d 100644 --- a/dash/views.py +++ b/dash/views.py @@ -89,10 +89,7 @@ def create_instance(request): return redirect(reverse("billing.invoice.generate", args=(instance.name,))) except VmException as e: ctx = get_ctx() - if e.code == VmErrors.NAME_EXISTS: - reason = ("Instance name exists, please try again with a different name",) - elif e.code == VmErrors.NO_CONFIG: - reason = "Configuration doesn't exist, please try again." + reason = e.error ctx["error"] = { "title": "Can't create instance", From bcb2e26f612e3a82651b2c36b6069fa64cd132d7 Mon Sep 17 00:00:00 2001 From: Hostea dashboard Date: Sun, 4 Sep 2022 18:04:37 +0530 Subject: [PATCH 2/2] feat: test vm name validation --- dash/tests.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/dash/tests.py b/dash/tests.py index 899adea..52a9c91 100644 --- a/dash/tests.py +++ b/dash/tests.py @@ -30,6 +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 def create_configurations(t: TestCase): @@ -197,6 +198,27 @@ class CreateInstance(TestCase): register_util(t=self, username="createinstance_user") create_configurations(t=self) + @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): + create_instance( + vm_name="12345452131324234234234234", + configuration_name=configuration, + 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") )