From f514412e448236cb5d171a15263edeb2ca67f448 Mon Sep 17 00:00:00 2001 From: "Leandro I. Costantino" Date: Fri, 22 Nov 2013 02:55:42 -0500 Subject: [PATCH] Heat Stack name is missing form validation. When creating a stack using Horizon, the form is missing name validation, so a generic error is shown whenever the name does not follow the regex used by Heat. ([a-zA-Z][a-zA-Z0-9_.-]*) Add server side validation and the right error message. Change-Id: I7a639bcf96b7c74760123b6106aab7d9311f5e2f Closes-Bug: #1254212 --- .../dashboards/project/stacks/forms.py | 6 +++- .../dashboards/project/stacks/tests.py | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/openstack_dashboard/dashboards/project/stacks/forms.py b/openstack_dashboard/dashboards/project/stacks/forms.py index f25ec533..7b9258f2 100644 --- a/openstack_dashboard/dashboards/project/stacks/forms.py +++ b/openstack_dashboard/dashboards/project/stacks/forms.py @@ -174,10 +174,14 @@ class StackCreateForm(forms.SelfHandlingForm): parameters = forms.CharField( widget=forms.widgets.HiddenInput, required=True) - stack_name = forms.CharField( + stack_name = forms.RegexField( max_length='255', label=_('Stack Name'), help_text=_('Name of the stack to create.'), + regex=r"^[a-zA-Z][a-zA-Z0-9_.-]*$", + error_messages={'invalid': _('Name must start with a letter and may ' + 'only contain letters, numbers, underscores, ' + 'periods and hyphens.')}, required=True) timeout_mins = forms.IntegerField( initial=60, diff --git a/openstack_dashboard/dashboards/project/stacks/tests.py b/openstack_dashboard/dashboards/project/stacks/tests.py index 408d86fc..016e8e7c 100644 --- a/openstack_dashboard/dashboards/project/stacks/tests.py +++ b/openstack_dashboard/dashboards/project/stacks/tests.py @@ -155,6 +155,37 @@ class StackTests(test.TestCase): res = self.client.post(url, form_data) self.assertRedirectsNoFollow(res, INDEX_URL) + def test_launch_stack_form_invalid_names_fail(self): + self._test_launch_stack_invalid_name('2_StartWithDigit') + self._test_launch_stack_invalid_name('_StartWithUnderscore') + self._test_launch_stack_invalid_name('.StartWithPoint') + + def _test_launch_stack_invalid_name(self, name): + template = self.stack_templates.first() + url = reverse('horizon:project:stacks:launch') + form_data = {'template_source': 'raw', + 'template_data': template.data, + 'password': 'password', + 'parameters': template.validate, + 'stack_name': name, + "timeout_mins": 60, + "disable_rollback": True, + "__param_DBUsername": "admin", + "__param_LinuxDistribution": "F17", + "__param_InstanceType": "m1.small", + "__param_KeyName": "test", + "__param_DBPassword": "admin", + "__param_DBRootPassword": "admin", + "__param_DBName": "wordpress", + 'method': forms.StackCreateForm.__name__} + + res = self.client.post(url, form_data) + error = ('Name must start with a letter and may only contain letters, ' + 'numbers, underscores, periods and hyphens.') + + self.assertFormErrors(res, 1) + self.assertFormError(res, "form", 'stack_name', error) + class TemplateFormTests(test.TestCase):