← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/maas-bug-948349 into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/maas-bug-948349 into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #948349 in MaaS: "Deleting a MaaS consumer key in two different tabs gives unhelpful error message"
  https://bugs.launchpad.net/maas/+bug/948349

For more details, see:
https://code.launchpad.net/~rvb/maas/maas-bug-948349/+merge/96326

This branch adds a new error message displayed when on tries to delete an API key that has already been deleted.

Drive-by fix: I've moved the call to addCleanup in the utility method createWidget.
-- 
https://code.launchpad.net/~rvb/maas/maas-bug-948349/+merge/96326
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/maas-bug-948349 into lp:maas.
=== modified file 'src/maasserver/static/js/node_add.js'
--- src/maasserver/static/js/node_add.js	2012-02-28 05:41:45 +0000
+++ src/maasserver/static/js/node_add.js	2012-03-07 11:02:19 +0000
@@ -126,7 +126,7 @@
     },
 
     /**
-     * Display an error message.  The passed-in parameter can be a string or 
+     * Display an error message.  The passed-in parameter can be a string or
      * a Node (in which case it will be appended to the error node).
      *
      * @method displayFormError

=== modified file 'src/maasserver/static/js/prefs.js'
--- src/maasserver/static/js/prefs.js	2012-03-05 09:00:49 +0000
+++ src/maasserver/static/js/prefs.js	2012-03-07 11:02:19 +0000
@@ -102,7 +102,13 @@
                     row.remove();
                  },
                 failure: function(id, out) {
-                    self.displayError('Unable to delete the token.');
+                    Y.log(out);
+                    if (out.status === 404) {
+                        self.displayError("The key has been deleted already.");
+                    }
+                    else {
+                        self.displayError("Unable to delete the key.");
+                    }
                 }
             }
         };

=== modified file 'src/maasserver/static/js/tests/test_prefs.js'
--- src/maasserver/static/js/tests/test_prefs.js	2012-03-05 09:00:49 +0000
+++ src/maasserver/static/js/tests/test_prefs.js	2012-03-07 11:02:19 +0000
@@ -21,6 +21,7 @@
 
     createWidget: function() {
         var widget = new module.TokenWidget({srcNode: '#placeholder'});
+        this.addCleanup(function() { widget.destroy(); });
         this.patchWidgetConfirm(widget, true);
         return widget;
     },
@@ -34,7 +35,6 @@
 
     testInitializer: function() {
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
         widget.render();
         // The "create a new API token" has been created.
         var create_link = widget.get('srcNode').one('#create_token');
@@ -51,7 +51,6 @@
 
     test_nb_tokens: function() {
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
         widget.render();
         Y.Assert.areEqual(2, widget.get('nb_tokens'));
      },
@@ -69,30 +68,56 @@
         };
         this.mockIO(mockXhr, module);
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
-        widget.render();
-        var link = widget.get('srcNode').one('.delete-link');
-        link.simulate('click');
-        Y.Assert.isTrue(fired);
-    },
-
-    testDeleteTokenFail: function() {
+        widget.render();
+        var link = widget.get('srcNode').one('.delete-link');
+        link.simulate('click');
+        Y.Assert.isTrue(fired);
+    },
+
+    testDeleteTokenCallsAPI: function() {
+        var mockXhr = new Y.Base();
+        var fired = false;
+        mockXhr.send = function(url, cfg) {
+            fired = true;
+        };
+        this.mockIO(mockXhr, module);
+        var widget = this.createWidget();
+        widget.render();
+        var link = widget.get('srcNode').one('.delete-link');
+        link.simulate('click');
+        Y.Assert.isTrue(fired);
+    },
+
+    testDeleteTokenFail404DisplaysErrorMessage: function() {
+        // If the API call to delete a token fails with a 404 error,
+        // an error saying that the key has already been deleted is displayed.
+        var mockXhr = new Y.Base();
+        mockXhr.send = function(url, cfg) {
+            cfg.on.failure(3, {status: 404});
+        };
+        this.mockIO(mockXhr, module);
+        var widget = this.createWidget();
+        widget.render();
+        var link = widget.get('srcNode').one('.delete-link');
+        link.simulate('click');
+        Y.Assert.areEqual(
+            "The key has been deleted already.",
+            widget.get('srcNode').one('#create_error').get('text'));
+    },
+
+    testDeleteTokenFailDisplaysErrorMessage: function() {
         // If the API call to delete a token fails, an error is displayed.
         var mockXhr = new Y.Base();
-        var fired = false;
         mockXhr.send = function(url, cfg) {
-            fired = true;
-            cfg.on.failure(3);
+            cfg.on.failure(3, {status: 500});
         };
         this.mockIO(mockXhr, module);
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
         widget.render();
         var link = widget.get('srcNode').one('.delete-link');
         link.simulate('click');
-        Y.Assert.isTrue(fired);
         Y.Assert.areEqual(
-            'Unable to delete the token.',
+            "Unable to delete the key.",
             widget.get('srcNode').one('#create_error').get('text'));
     },
 
@@ -107,7 +132,6 @@
         };
         this.mockIO(mockXhr, module);
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
         widget.render();
         var link = widget.get('srcNode').one('.delete-link');
         Y.Assert.isNotNull(Y.one('#tokenkey1'));
@@ -122,7 +146,6 @@
         var mockXhr = new Y.Base();
         var widget = this.createWidget();
         this.patchWidgetConfirm(widget, false);
-        this.addCleanup(function() { widget.destroy(); });
         widget.render();
         var link = widget.get('srcNode').one('.delete-link');
         Y.Assert.isNotNull(Y.one('#tokenkey1'));
@@ -133,7 +156,6 @@
 
     test_createTokenFromKeys: function() {
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
         var token = widget.createTokenFromKeys(
             'consumer_key', 'token_key', 'token_secret');
         Y.Assert.areEqual('consumer_key:token_key:token_secret', token);
@@ -153,7 +175,6 @@
         };
         this.mockIO(mockXhr, module);
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
         widget.render();
         var create_link = widget.get('srcNode').one('#create_token');
         create_link.simulate('click');
@@ -170,7 +191,6 @@
         };
         this.mockIO(mockXhr, module);
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
         widget.render();
         var create_link = widget.get('srcNode').one('#create_token');
         create_link.simulate('click');
@@ -196,7 +216,6 @@
         };
         this.mockIO(mockXhr, module);
         var widget = this.createWidget();
-        this.addCleanup(function() { widget.destroy(); });
         widget.render();
         var create_link = widget.get('srcNode').one('#create_token');
         create_link.simulate('click');