From 4f9b5b90d6d6c8e2f49b16cfef1c5c605af450de Mon Sep 17 00:00:00 2001 From: Tobias Urdin Date: Mon, 17 Jun 2024 14:43:50 +0200 Subject: [PATCH] Delete container show duplicate toast notifications When trying to delete a container with objects in it the Swift rest API rejects the request with 409. In the JS code we catch() the promise from the request and show a toast error that the request failed but we don't throw in that catch() so the then() callback that the callee specified gets called which shows a toast message that the container delete was successful and removes it from the list, if you refresh the page the container is still in the list (as expected). This subtle error probably exists in multiple places where the same kind of logic exists. Change-Id: Iadf1e58f23c935a7dd599b9fd3444a0a22396213 --- .../app/core/openstack-service-api/common-test.mock.js | 9 ++++++++- .../app/core/openstack-service-api/swift.service.js | 1 + .../app/core/openstack-service-api/swift.service.spec.js | 8 ++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/openstack_dashboard/static/app/core/openstack-service-api/common-test.mock.js b/openstack_dashboard/static/app/core/openstack-service-api/common-test.mock.js index b071f67253..e2efd254ea 100644 --- a/openstack_dashboard/static/app/core/openstack-service-api/common-test.mock.js +++ b/openstack_dashboard/static/app/core/openstack-service-api/common-test.mock.js @@ -87,7 +87,14 @@ var innerFunc = promise.catch.calls.argsFor(0)[0]; expect(innerFunc).toBeDefined(); spyOn(toastService, 'add'); - innerFunc({status: 500}); + var response = {status: 500}; + if (angular.isDefined(config.throws) && config.throws) { + expect(function() { + innerFunc(response); + }).toThrow(response); + } else { + innerFunc(response); + } expect(toastService.add).toHaveBeenCalledWith(config.messageType || 'error', config.error); } } diff --git a/openstack_dashboard/static/app/core/openstack-service-api/swift.service.js b/openstack_dashboard/static/app/core/openstack-service-api/swift.service.js index daafd8eadd..d529e605e7 100644 --- a/openstack_dashboard/static/app/core/openstack-service-api/swift.service.js +++ b/openstack_dashboard/static/app/core/openstack-service-api/swift.service.js @@ -186,6 +186,7 @@ } else { toastService.add('error', gettext('Unable to delete the container.')); } + throw response; }); } diff --git a/openstack_dashboard/static/app/core/openstack-service-api/swift.service.spec.js b/openstack_dashboard/static/app/core/openstack-service-api/swift.service.spec.js index 06a64908d8..c5df9d4c37 100644 --- a/openstack_dashboard/static/app/core/openstack-service-api/swift.service.spec.js +++ b/openstack_dashboard/static/app/core/openstack-service-api/swift.service.spec.js @@ -95,7 +95,8 @@ method: 'delete', path: '/api/swift/containers/spam/metadata/', error: 'Unable to delete the container.', - testInput: [ 'spam' ] + testInput: [ 'spam' ], + throws: true }, { func: 'setContainerAccess', @@ -205,7 +206,10 @@ var innerFunc = promise.catch.calls.argsFor(0)[0]; // In the case of 409 var message = 'Unable to delete the container because it is not empty.'; - innerFunc({data: message, status: 409}); + var response = {data: message, status: 409}; + expect(function() { + innerFunc(response); + }).toThrow(response); expect(toastService.add).toHaveBeenCalledWith('error', message); } );