From 7743e6f17ee73e384a1101fc4192cb2b541fbebe Mon Sep 17 00:00:00 2001 From: Julie Pichon Date: Mon, 23 Jul 2012 22:44:11 +0100 Subject: [PATCH] Display form with initial input and error after failure to import keypair Fixes bug 1026783. Change-Id: Iffa255f8bae76bc055127db800ded4c4da19b580 --- .../nova/access_and_security/keypairs/forms.py | 6 +++--- .../nova/access_and_security/keypairs/tests.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/horizon/dashboards/nova/access_and_security/keypairs/forms.py b/horizon/dashboards/nova/access_and_security/keypairs/forms.py index c21bc074b..a71336615 100644 --- a/horizon/dashboards/nova/access_and_security/keypairs/forms.py +++ b/horizon/dashboards/nova/access_and_security/keypairs/forms.py @@ -61,6 +61,6 @@ class ImportKeypair(forms.SelfHandlingForm): % data['name']) return keypair except: - exceptions.handle(request, - _('Unable to import keypair.')) - return shortcuts.redirect(request.build_absolute_uri()) + exceptions.handle(request, ignore=True) + self.api_error(_('Unable to import keypair.')) + return False diff --git a/horizon/dashboards/nova/access_and_security/keypairs/tests.py b/horizon/dashboards/nova/access_and_security/keypairs/tests.py index f50fc1a4d..52e58fb21 100644 --- a/horizon/dashboards/nova/access_and_security/keypairs/tests.py +++ b/horizon/dashboards/nova/access_and_security/keypairs/tests.py @@ -127,6 +127,24 @@ class KeyPairViewTests(test.TestCase): res = self.client.post(url, formData) self.assertMessageCount(res, success=1) + def test_import_keypair_invalid_key(self): + key_name = "new key pair" + public_key = "ABCDEF" + + self.mox.StubOutWithMock(api, 'keypair_import') + api.keypair_import(IsA(http.HttpRequest), key_name, public_key) \ + .AndRaise(self.exceptions.nova) + self.mox.ReplayAll() + + formData = {'method': 'ImportKeypair', + 'name': key_name, + 'public_key': public_key} + url = reverse('horizon:nova:access_and_security:keypairs:import') + res = self.client.post(url, formData, follow=True) + self.assertEqual(res.redirect_chain, []) + msg = 'Unable to import keypair.' + self.assertFormErrors(res, count=1, message=msg) + def test_generate_keypair_exception(self): keypair = self.keypairs.first()