diff --git a/openstack_dashboard/dashboards/project/access_and_security/keypairs/forms.py b/openstack_dashboard/dashboards/project/access_and_security/keypairs/forms.py index 7f9945b5fe..add286d93f 100644 --- a/openstack_dashboard/dashboards/project/access_and_security/keypairs/forms.py +++ b/openstack_dashboard/dashboards/project/access_and_security/keypairs/forms.py @@ -29,10 +29,11 @@ from openstack_dashboard import api NEW_LINES = re.compile(r"\r|\n") -KEYPAIR_NAME_REGEX = re.compile(r"^[\w\- ]+$", re.UNICODE) +KEYPAIR_NAME_REGEX = re.compile(r"^\w+(?:[- ]\w+)*$", re.UNICODE) KEYPAIR_ERROR_MESSAGES = { 'invalid': _('Key pair name may only contain letters, ' - 'numbers, underscores, spaces and hyphens.')} + 'numbers, underscores, spaces, and hyphens ' + 'and may not be white space.')} class CreateKeypair(forms.SelfHandlingForm): diff --git a/openstack_dashboard/dashboards/project/access_and_security/keypairs/tests.py b/openstack_dashboard/dashboards/project/access_and_security/keypairs/tests.py index 5668568063..75e10fcc0b 100644 --- a/openstack_dashboard/dashboards/project/access_and_security/keypairs/tests.py +++ b/openstack_dashboard/dashboards/project/access_and_security/keypairs/tests.py @@ -182,6 +182,19 @@ class KeyPairViewTests(test.TestCase): msg = six.text_type(KEYPAIR_ERROR_MESSAGES['invalid']) self.assertFormErrors(res, count=1, message=msg) + def test_import_keypair_space_key_name(self): + key_name = " " + public_key = "ABCDEF" + + formData = {'method': 'ImportKeypair', + 'name': key_name, + 'public_key': public_key} + url = reverse('horizon:project:access_and_security:keypairs:import') + res = self.client.post(url, formData, follow=True) + self.assertEqual(res.redirect_chain, []) + msg = six.text_type(KEYPAIR_ERROR_MESSAGES['invalid']) + self.assertFormErrors(res, count=1, message=msg) + @test.create_stubs({api.nova: ("keypair_create",)}) def test_generate_keypair_exception(self): keypair = self.keypairs.first()