Merge "Fixes issue where metadata is always removed on edit"

This commit is contained in:
Jenkins 2016-07-19 09:24:25 +00:00 committed by Gerrit Code Review
commit 9e4e8e00d5
2 changed files with 27 additions and 80 deletions

View File

@ -23,6 +23,7 @@
editService.$inject = [
'$q',
'horizon.app.core.images.events',
'horizon.app.core.images.resourceType',
'horizon.app.core.images.actions.editWorkflow',
'horizon.app.core.metadata.service',
@ -42,6 +43,7 @@
*/
function editService(
$q,
events,
imageResourceType,
editWorkflow,
metadataService,
@ -54,10 +56,9 @@
toast
) {
var message = {
success: gettext('Image %s was successfully updated.'),
successMetadata: gettext('Image metadata %s was successfully updated.')
success: gettext('Image %s was successfully updated.')
};
var modifyImagePolicyCheck, scope, saveDeferred;
var modifyImagePolicyCheck, scope;
var model = {
image: {},
@ -76,6 +77,7 @@
function initScope($scope) {
scope = $scope;
$scope.$on(events.IMAGE_METADATA_CHANGED, onMetadataChange);
modifyImagePolicyCheck = policy.ifAllowed({rules: [['image', 'modify_image']]});
}
@ -97,64 +99,56 @@
model.image = localImage;
}
wizardModalService.modal({
return wizardModalService.modal({
scope: scope,
workflow: editWorkflow,
submit: submit
}).result.catch(cancel);
saveDeferred = $q.defer();
return saveDeferred.promise;
}).result;
}
function cancel() {
saveDeferred.reject();
function onMetadataChange(e, metadata) {
model.metadata = metadata;
e.stopPropagation();
}
function submit() {
return saveMetadata().then(onSaveMetadata, onFailMetadata);
return updateMetadata().then(updateImage, onUpdateImageFail);
}
function onFailMetadata() {
glance.updateImage(model.image).then(onUpdateImageSuccess, onUpdateImageFail);
}
function onSaveMetadata() {
toast.add('success', interpolate(message.successMetadata, [model.image.name]));
glance.updateImage(model.image).then(onUpdateImageSuccess, onUpdateImageFail);
function updateImage() {
var finalModel = model.image;
return glance.updateImage(finalModel).then(onUpdateImageSuccess, onUpdateImageFail);
}
function onUpdateImageSuccess() {
toast.add('success', interpolate(message.success, [model.image.name]));
saveDeferred.resolve(actionResultService.getActionResult()
return actionResultService.getActionResult()
.updated(imageResourceType, model.image.id)
.result);
.result;
}
function onUpdateImageFail() {
saveDeferred.reject(actionResultService.getActionResult()
return actionResultService.getActionResult()
.failed(imageResourceType, model.image.id)
.result);
.result;
}
function saveMetadata() {
var imageId = model.image.id;
var deferred = $q.defer();
function updateMetadata() {
metadataService.getMetadata('image', imageId).then(onMetadataGet);
return metadataService
.getMetadata('image', model.image.id)
.then(onMetadataGet);
function onMetadataGet(response) {
var updated = model.metadata;
var removed = angular.copy(response.data);
angular.forEach(model.metadata, function(value, key) {
angular.forEach(updated, function(value, key) {
delete removed[key];
});
deferred.resolve(
metadataService.editMetadata('image', imageId, model.metadata, Object.keys(removed))
);
return metadataService
.editMetadata('image', model.image.id, updated, Object.keys(removed));
}
return deferred.promise;
}
function isActive(image) {

View File

@ -77,7 +77,7 @@
}
};
var service, $scope, $q, toast, deferred, testImage, $timeout;
var service, $scope, $q, deferred, testImage, $timeout;
///////////////////////
@ -96,7 +96,6 @@
$scope = _$rootScope_.$new();
$q = _$q_;
service = $injector.get('horizon.app.core.images.actions.edit.service');
toast = $injector.get('horizon.framework.widgets.toast.service');
service.initScope($scope);
deferred = $q.defer();
$timeout = _$timeout_;
@ -119,42 +118,6 @@
expect(modalArgs.workflow).toBeDefined();
});
it('should raise event even if update meta data fails', function() {
var image = { name: 'Test', id: '2' };
var failedPromise = function() {
return {
then: function(callback, errorCallback) {
errorCallback();
}
};
};
spyOn(wizardModalService, 'modal').and.callThrough();
spyOn(glanceAPI, 'updateImage').and.callThrough();
spyOn(metadataService, 'editMetadata').and.callFake(failedPromise);
spyOn(toast, 'add').and.callThrough();
service.initScope($scope);
service.perform(image);
$scope.$apply();
var modalArgs = wizardModalService.modal.calls.argsFor(0)[0];
modalArgs.submit();
$scope.$apply();
expect(toast.add.calls.count()).toBe(1);
});
});
describe('edit', function() {
it('should allow edit if image can be edited', function() {
var image = {owner: 'project', status: 'active'};
var allowed = service.allowed(image);
permissionShouldPass(allowed);
$scope.$apply();
});
it('should not allow edit if image is not owned by user', function() {
deferred.reject();
var image = {owner: 'doesnt_matter', status: 'active'};
@ -180,16 +143,6 @@
});
}
function permissionShouldPass(permissions) {
permissions.then(
function() {
expect(true).toBe(true);
},
function() {
expect(false).toBe(true);
});
}
});
});