mirror of
https://github.com/nasa/openmct.git
synced 2024-12-21 06:03:08 +00:00
Merge pull request #1487 from nasa/open1483
[Persistence] Prevent editing of objects that cannot be saved. Fixes #1483
This commit is contained in:
commit
7dd5da8993
@ -2261,7 +2261,7 @@ The platform understands the following policy categories (specifiable as the
|
|||||||
|
|
||||||
* `action`: Determines whether or not a given action is allowable. The candidate
|
* `action`: Determines whether or not a given action is allowable. The candidate
|
||||||
argument here is an Action; the context is its action context object.
|
argument here is an Action; the context is its action context object.
|
||||||
* `composition`: Determines whether or not domain objects of a given type (first argument, `parentType`) can contain a given object (second argument, `child`).
|
* `composition`: Determines whether or not a given domain object(first argument, `parent`) can contain a candidate child object (second argument, `child`).
|
||||||
* `view`: Determines whether or not a view is applicable for a domain object.
|
* `view`: Determines whether or not a view is applicable for a domain object.
|
||||||
The candidate argument is the view's extension definition; the context argument
|
The candidate argument is the view's extension definition; the context argument
|
||||||
is the `DomainObject` to be viewed.
|
is the `DomainObject` to be viewed.
|
||||||
|
@ -34,6 +34,7 @@ define([
|
|||||||
"./src/actions/SaveAsAction",
|
"./src/actions/SaveAsAction",
|
||||||
"./src/actions/CancelAction",
|
"./src/actions/CancelAction",
|
||||||
"./src/policies/EditActionPolicy",
|
"./src/policies/EditActionPolicy",
|
||||||
|
"./src/policies/EditPersistableObjectsPolicy",
|
||||||
"./src/policies/EditableLinkPolicy",
|
"./src/policies/EditableLinkPolicy",
|
||||||
"./src/policies/EditableMovePolicy",
|
"./src/policies/EditableMovePolicy",
|
||||||
"./src/policies/EditContextualActionPolicy",
|
"./src/policies/EditContextualActionPolicy",
|
||||||
@ -72,6 +73,7 @@ define([
|
|||||||
SaveAsAction,
|
SaveAsAction,
|
||||||
CancelAction,
|
CancelAction,
|
||||||
EditActionPolicy,
|
EditActionPolicy,
|
||||||
|
EditPersistableObjectsPolicy,
|
||||||
EditableLinkPolicy,
|
EditableLinkPolicy,
|
||||||
EditableMovePolicy,
|
EditableMovePolicy,
|
||||||
EditContextualActionPolicy,
|
EditContextualActionPolicy,
|
||||||
@ -247,6 +249,11 @@ define([
|
|||||||
"category": "action",
|
"category": "action",
|
||||||
"implementation": EditActionPolicy
|
"implementation": EditActionPolicy
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"category": "action",
|
||||||
|
"implementation": EditPersistableObjectsPolicy,
|
||||||
|
"depends": ["openmct"]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"category": "action",
|
"category": "action",
|
||||||
"implementation": EditContextualActionPolicy,
|
"implementation": EditContextualActionPolicy,
|
||||||
|
@ -60,11 +60,9 @@ define(
|
|||||||
policyService = this.policyService;
|
policyService = this.policyService;
|
||||||
|
|
||||||
function validateLocation(parent) {
|
function validateLocation(parent) {
|
||||||
var parentType = parent &&
|
return parent && policyService.allow(
|
||||||
parent.getCapability('type');
|
|
||||||
return parentType && policyService.allow(
|
|
||||||
"composition",
|
"composition",
|
||||||
parentType,
|
parent,
|
||||||
domainObject
|
domainObject
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* Open MCT, Copyright (c) 2014-2016, United States Government
|
||||||
|
* as represented by the Administrator of the National Aeronautics and Space
|
||||||
|
* Administration. All rights reserved.
|
||||||
|
*
|
||||||
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
* Open MCT includes source code licensed under additional open source
|
||||||
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||||
|
* this source code distribution or the Licensing information page available
|
||||||
|
* at runtime from the About dialog for additional information.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
define(
|
||||||
|
['../../../../../src/api/objects/object-utils'],
|
||||||
|
function (objectUtils) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Policy that prevents editing of any object from a provider that does not
|
||||||
|
* support persistence (ie. the 'save' operation). Editing is prevented
|
||||||
|
* as a subsequent save would fail, causing the loss of a user's changes.
|
||||||
|
* @param openmct
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
function EditPersistableObjectsPolicy(openmct) {
|
||||||
|
this.openmct = openmct;
|
||||||
|
}
|
||||||
|
|
||||||
|
EditPersistableObjectsPolicy.prototype.allow = function (action, context) {
|
||||||
|
var identifier;
|
||||||
|
var provider;
|
||||||
|
var domainObject = context.domainObject;
|
||||||
|
var key = action.getMetadata().key;
|
||||||
|
var category = (context || {}).category;
|
||||||
|
|
||||||
|
// Use category to selectively block edit from the view. Edit action
|
||||||
|
// is also invoked during the create process which should be allowed,
|
||||||
|
// because it may be saved elsewhere
|
||||||
|
if ((key === 'edit' && category === 'view-control') || key === 'properties') {
|
||||||
|
identifier = objectUtils.parseKeyString(domainObject.getId());
|
||||||
|
provider = this.openmct.objects.getProvider(identifier);
|
||||||
|
return provider.save !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
return EditPersistableObjectsPolicy;
|
||||||
|
}
|
||||||
|
);
|
@ -161,6 +161,7 @@ define(
|
|||||||
'otherType',
|
'otherType',
|
||||||
['getKey']
|
['getKey']
|
||||||
),
|
),
|
||||||
|
|
||||||
//Create a form structure with location
|
//Create a form structure with location
|
||||||
structure = wizard.getFormStructure(true),
|
structure = wizard.getFormStructure(true),
|
||||||
sections = structure.sections,
|
sections = structure.sections,
|
||||||
@ -174,7 +175,7 @@ define(
|
|||||||
// can actually contain objects of this type
|
// can actually contain objects of this type
|
||||||
expect(mockPolicyService.allow).toHaveBeenCalledWith(
|
expect(mockPolicyService.allow).toHaveBeenCalledWith(
|
||||||
'composition',
|
'composition',
|
||||||
mockOtherType,
|
mockDomainObj,
|
||||||
mockDomainObject
|
mockDomainObject
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -0,0 +1,104 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* Open MCT, Copyright (c) 2014-2016, United States Government
|
||||||
|
* as represented by the Administrator of the National Aeronautics and Space
|
||||||
|
* Administration. All rights reserved.
|
||||||
|
*
|
||||||
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
* Open MCT includes source code licensed under additional open source
|
||||||
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||||
|
* this source code distribution or the Licensing information page available
|
||||||
|
* at runtime from the About dialog for additional information.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
define(
|
||||||
|
["../../src/policies/EditPersistableObjectsPolicy"],
|
||||||
|
function (EditPersistableObjectsPolicy) {
|
||||||
|
|
||||||
|
describe("The Edit persistable objects policy", function () {
|
||||||
|
var mockDomainObject,
|
||||||
|
mockEditAction,
|
||||||
|
mockPropertiesAction,
|
||||||
|
mockOtherAction,
|
||||||
|
mockAPI,
|
||||||
|
mockObjectAPI,
|
||||||
|
testContext,
|
||||||
|
policy;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
mockDomainObject = jasmine.createSpyObj(
|
||||||
|
'domainObject',
|
||||||
|
[
|
||||||
|
'getId'
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
mockObjectAPI = jasmine.createSpyObj('objectAPI', [
|
||||||
|
'getProvider'
|
||||||
|
]);
|
||||||
|
|
||||||
|
mockAPI = {
|
||||||
|
objects: mockObjectAPI
|
||||||
|
};
|
||||||
|
|
||||||
|
mockEditAction = jasmine.createSpyObj('edit', ['getMetadata']);
|
||||||
|
mockPropertiesAction = jasmine.createSpyObj('properties', ['getMetadata']);
|
||||||
|
mockOtherAction = jasmine.createSpyObj('other', ['getMetadata']);
|
||||||
|
|
||||||
|
mockEditAction.getMetadata.andReturn({ key: 'edit' });
|
||||||
|
mockPropertiesAction.getMetadata.andReturn({ key: 'properties' });
|
||||||
|
mockOtherAction.getMetadata.andReturn({key: 'other'});
|
||||||
|
|
||||||
|
mockDomainObject.getId.andReturn('test:testId');
|
||||||
|
|
||||||
|
testContext = {
|
||||||
|
domainObject: mockDomainObject,
|
||||||
|
category: 'view-control'
|
||||||
|
};
|
||||||
|
|
||||||
|
policy = new EditPersistableObjectsPolicy(mockAPI);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Applies to edit action", function () {
|
||||||
|
mockObjectAPI.getProvider.andReturn({});
|
||||||
|
expect(mockObjectAPI.getProvider).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
policy.allow(mockEditAction, testContext);
|
||||||
|
expect(mockObjectAPI.getProvider).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Applies to properties action", function () {
|
||||||
|
mockObjectAPI.getProvider.andReturn({});
|
||||||
|
expect(mockObjectAPI.getProvider).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
policy.allow(mockPropertiesAction, testContext);
|
||||||
|
expect(mockObjectAPI.getProvider).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not apply to other actions", function () {
|
||||||
|
mockObjectAPI.getProvider.andReturn({});
|
||||||
|
expect(mockObjectAPI.getProvider).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
policy.allow(mockOtherAction, testContext);
|
||||||
|
expect(mockObjectAPI.getProvider).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Tests object provider for editability", function () {
|
||||||
|
mockObjectAPI.getProvider.andReturn({});
|
||||||
|
expect(policy.allow(mockEditAction, testContext)).toBe(false);
|
||||||
|
expect(mockObjectAPI.getProvider).toHaveBeenCalled();
|
||||||
|
mockObjectAPI.getProvider.andReturn({save: function () {}});
|
||||||
|
expect(policy.allow(mockEditAction, testContext)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
@ -25,12 +25,14 @@ define([
|
|||||||
"./src/CompositionMutabilityPolicy",
|
"./src/CompositionMutabilityPolicy",
|
||||||
"./src/CompositionModelPolicy",
|
"./src/CompositionModelPolicy",
|
||||||
"./src/ComposeActionPolicy",
|
"./src/ComposeActionPolicy",
|
||||||
|
"./src/PersistableCompositionPolicy",
|
||||||
'legacyRegistry'
|
'legacyRegistry'
|
||||||
], function (
|
], function (
|
||||||
CompositionPolicy,
|
CompositionPolicy,
|
||||||
CompositionMutabilityPolicy,
|
CompositionMutabilityPolicy,
|
||||||
CompositionModelPolicy,
|
CompositionModelPolicy,
|
||||||
ComposeActionPolicy,
|
ComposeActionPolicy,
|
||||||
|
PersistableCompositionPolicy,
|
||||||
legacyRegistry
|
legacyRegistry
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@ -59,6 +61,12 @@ define([
|
|||||||
"$injector"
|
"$injector"
|
||||||
],
|
],
|
||||||
"message": "Objects of this type cannot contain objects of that type."
|
"message": "Objects of this type cannot contain objects of that type."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"category": "composition",
|
||||||
|
"implementation": PersistableCompositionPolicy,
|
||||||
|
"depends": ["openmct"],
|
||||||
|
"message": "Change cannot be made to composition of non-persistable object"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -43,9 +43,6 @@ define(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ComposeActionPolicy.prototype.allowComposition = function (containerObject, selectedObject) {
|
ComposeActionPolicy.prototype.allowComposition = function (containerObject, selectedObject) {
|
||||||
// Get the object types involved in the compose action
|
|
||||||
var containerType = containerObject &&
|
|
||||||
containerObject.getCapability('type');
|
|
||||||
|
|
||||||
// Get a reference to the policy service if needed...
|
// Get a reference to the policy service if needed...
|
||||||
this.policyService = this.policyService || this.getPolicyService();
|
this.policyService = this.policyService || this.getPolicyService();
|
||||||
@ -54,7 +51,7 @@ define(
|
|||||||
return containerObject.getId() !== selectedObject.getId() &&
|
return containerObject.getId() !== selectedObject.getId() &&
|
||||||
this.policyService.allow(
|
this.policyService.allow(
|
||||||
'composition',
|
'composition',
|
||||||
containerType,
|
containerObject,
|
||||||
selectedObject
|
selectedObject
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -14,8 +14,9 @@ define(
|
|||||||
}
|
}
|
||||||
|
|
||||||
CompositionModelPolicy.prototype.allow = function (candidate) {
|
CompositionModelPolicy.prototype.allow = function (candidate) {
|
||||||
|
var candidateType = candidate.getCapability('type');
|
||||||
return Array.isArray(
|
return Array.isArray(
|
||||||
(candidate.getInitialModel() || {}).composition
|
(candidateType.getInitialModel() || {}).composition
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ define(
|
|||||||
// Equate creatability with mutability; that is, users
|
// Equate creatability with mutability; that is, users
|
||||||
// can only modify objects of types they can create, and
|
// can only modify objects of types they can create, and
|
||||||
// vice versa.
|
// vice versa.
|
||||||
return candidate.hasFeature('creation');
|
return candidate.getCapability('type').hasFeature('creation');
|
||||||
};
|
};
|
||||||
|
|
||||||
return CompositionMutabilityPolicy;
|
return CompositionMutabilityPolicy;
|
||||||
|
@ -30,16 +30,16 @@ define(
|
|||||||
function () {
|
function () {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines composition policy as driven by type metadata.
|
* Determines whether a given object can contain a candidate child object.
|
||||||
* @constructor
|
* @constructor
|
||||||
* @memberof platform/containment
|
* @memberof platform/containment
|
||||||
* @implements {Policy.<Type, Type>}
|
* @implements {Policy.<DomainObjectImpl, DomainObjectImpl>}
|
||||||
*/
|
*/
|
||||||
function CompositionPolicy() {
|
function CompositionPolicy() {
|
||||||
}
|
}
|
||||||
|
|
||||||
CompositionPolicy.prototype.allow = function (parentType, child) {
|
CompositionPolicy.prototype.allow = function (parent, child) {
|
||||||
var parentDef = parentType.getDefinition();
|
var parentDef = parent.getCapability('type').getDefinition();
|
||||||
|
|
||||||
// A parent without containment rules can contain anything.
|
// A parent without containment rules can contain anything.
|
||||||
if (!parentDef.contains) {
|
if (!parentDef.contains) {
|
||||||
|
60
platform/containment/src/PersistableCompositionPolicy.js
Normal file
60
platform/containment/src/PersistableCompositionPolicy.js
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* Open MCT, Copyright (c) 2014-2016, United States Government
|
||||||
|
* as represented by the Administrator of the National Aeronautics and Space
|
||||||
|
* Administration. All rights reserved.
|
||||||
|
*
|
||||||
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
* Open MCT includes source code licensed under additional open source
|
||||||
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||||
|
* this source code distribution or the Licensing information page available
|
||||||
|
* at runtime from the About dialog for additional information.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This bundle implements "containment" rules, which determine which objects
|
||||||
|
* can be contained within which other objects.
|
||||||
|
* @namespace platform/containment
|
||||||
|
*/
|
||||||
|
define(
|
||||||
|
['../../../src/api/objects/object-utils'],
|
||||||
|
function (objectUtils) {
|
||||||
|
|
||||||
|
function PersistableCompositionPolicy(openmct) {
|
||||||
|
this.openmct = openmct;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only allow changes to composition if the changes can be saved. This in
|
||||||
|
* effect prevents selection of objects from the locator that do not
|
||||||
|
* support persistence.
|
||||||
|
* @param parent
|
||||||
|
* @param child
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
PersistableCompositionPolicy.prototype.allow = function (parent) {
|
||||||
|
// If object is in edit mode, allow composition because it is
|
||||||
|
// part of object creation, and the object may be saved to another
|
||||||
|
// namespace that does support persistence. The EditPersistableObjectsPolicy
|
||||||
|
// prevents editing of objects that cannot be persisted, so we can assume that this
|
||||||
|
// is a new object.
|
||||||
|
if (!(parent.hasCapability('editor') && parent.getCapability('editor').isEditContextRoot())) {
|
||||||
|
var identifier = objectUtils.parseKeyString(parent.getId());
|
||||||
|
var provider = this.openmct.objects.getProvider(identifier);
|
||||||
|
return provider.save !== undefined;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
return PersistableCompositionPolicy;
|
||||||
|
}
|
||||||
|
);
|
@ -78,7 +78,7 @@ define(
|
|||||||
|
|
||||||
expect(mockPolicyService.allow).toHaveBeenCalledWith(
|
expect(mockPolicyService.allow).toHaveBeenCalledWith(
|
||||||
'composition',
|
'composition',
|
||||||
mockTypes[0],
|
mockDomainObjects[0],
|
||||||
mockDomainObjects[1]
|
mockDomainObjects[1]
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -4,19 +4,25 @@ define(
|
|||||||
function (CompositionModelPolicy) {
|
function (CompositionModelPolicy) {
|
||||||
|
|
||||||
describe("The composition model policy", function () {
|
describe("The composition model policy", function () {
|
||||||
var mockType,
|
var mockObject,
|
||||||
|
mockType,
|
||||||
policy;
|
policy;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
mockType = jasmine.createSpyObj('type', ['getInitialModel']);
|
mockType = jasmine.createSpyObj('type', ['getInitialModel']);
|
||||||
|
mockObject = {
|
||||||
|
getCapability: function () {
|
||||||
|
return mockType;
|
||||||
|
}
|
||||||
|
};
|
||||||
policy = new CompositionModelPolicy();
|
policy = new CompositionModelPolicy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("only allows composition for types which will have a composition property", function () {
|
it("only allows composition for types which will have a composition property", function () {
|
||||||
mockType.getInitialModel.andReturn({});
|
mockType.getInitialModel.andReturn({});
|
||||||
expect(policy.allow(mockType)).toBeFalsy();
|
expect(policy.allow(mockObject)).toBeFalsy();
|
||||||
mockType.getInitialModel.andReturn({ composition: [] });
|
mockType.getInitialModel.andReturn({ composition: [] });
|
||||||
expect(policy.allow(mockType)).toBeTruthy();
|
expect(policy.allow(mockObject)).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -25,18 +25,24 @@ define(
|
|||||||
function (CompositionMutabilityPolicy) {
|
function (CompositionMutabilityPolicy) {
|
||||||
|
|
||||||
describe("The composition mutability policy", function () {
|
describe("The composition mutability policy", function () {
|
||||||
var mockType,
|
var mockObject,
|
||||||
|
mockType,
|
||||||
policy;
|
policy;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
mockType = jasmine.createSpyObj('type', ['hasFeature']);
|
mockType = jasmine.createSpyObj('type', ['hasFeature']);
|
||||||
|
mockObject = {
|
||||||
|
getCapability: function () {
|
||||||
|
return mockType;
|
||||||
|
}
|
||||||
|
};
|
||||||
policy = new CompositionMutabilityPolicy();
|
policy = new CompositionMutabilityPolicy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("only allows composition for types which can be created/modified", function () {
|
it("only allows composition for types which can be created/modified", function () {
|
||||||
expect(policy.allow(mockType)).toBeFalsy();
|
expect(policy.allow(mockObject)).toBeFalsy();
|
||||||
mockType.hasFeature.andReturn(true);
|
mockType.hasFeature.andReturn(true);
|
||||||
expect(policy.allow(mockType)).toBeTruthy();
|
expect(policy.allow(mockObject)).toBeTruthy();
|
||||||
expect(mockType.hasFeature).toHaveBeenCalledWith('creation');
|
expect(mockType.hasFeature).toHaveBeenCalledWith('creation');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -24,13 +24,18 @@ define(
|
|||||||
["../src/CompositionPolicy"],
|
["../src/CompositionPolicy"],
|
||||||
function (CompositionPolicy) {
|
function (CompositionPolicy) {
|
||||||
describe("Composition policy", function () {
|
describe("Composition policy", function () {
|
||||||
var typeA,
|
var mockParentObject,
|
||||||
|
typeA,
|
||||||
typeB,
|
typeB,
|
||||||
typeC,
|
typeC,
|
||||||
mockChildObject,
|
mockChildObject,
|
||||||
policy;
|
policy;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
mockParentObject = jasmine.createSpyObj('domainObject', [
|
||||||
|
'getCapability'
|
||||||
|
]);
|
||||||
|
|
||||||
typeA = jasmine.createSpyObj(
|
typeA = jasmine.createSpyObj(
|
||||||
'type A-- the particular kind',
|
'type A-- the particular kind',
|
||||||
['getKey', 'getDefinition']
|
['getKey', 'getDefinition']
|
||||||
@ -70,27 +75,31 @@ define(
|
|||||||
describe('enforces simple containment rules', function () {
|
describe('enforces simple containment rules', function () {
|
||||||
|
|
||||||
it('allows when type matches', function () {
|
it('allows when type matches', function () {
|
||||||
|
mockParentObject.getCapability.andReturn(typeA);
|
||||||
|
|
||||||
mockChildObject.getCapability.andReturn(typeA);
|
mockChildObject.getCapability.andReturn(typeA);
|
||||||
expect(policy.allow(typeA, mockChildObject))
|
expect(policy.allow(mockParentObject, mockChildObject))
|
||||||
.toBeTruthy();
|
.toBeTruthy();
|
||||||
|
|
||||||
expect(policy.allow(typeB, mockChildObject))
|
mockParentObject.getCapability.andReturn(typeB);
|
||||||
|
expect(policy.allow(mockParentObject, mockChildObject))
|
||||||
.toBeTruthy();
|
.toBeTruthy();
|
||||||
|
|
||||||
mockChildObject.getCapability.andReturn(typeB);
|
mockChildObject.getCapability.andReturn(typeB);
|
||||||
expect(policy.allow(typeB, mockChildObject))
|
expect(policy.allow(mockParentObject, mockChildObject))
|
||||||
.toBeTruthy();
|
.toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('disallows when type doesn\'t match', function () {
|
it('disallows when type doesn\'t match', function () {
|
||||||
|
|
||||||
|
mockParentObject.getCapability.andReturn(typeA);
|
||||||
mockChildObject.getCapability.andReturn(typeB);
|
mockChildObject.getCapability.andReturn(typeB);
|
||||||
expect(policy.allow(typeA, mockChildObject))
|
expect(policy.allow(mockParentObject, mockChildObject))
|
||||||
.toBeFalsy();
|
.toBeFalsy();
|
||||||
|
|
||||||
mockChildObject.getCapability.andReturn(typeC);
|
mockChildObject.getCapability.andReturn(typeC);
|
||||||
expect(policy.allow(typeA, mockChildObject))
|
expect(policy.allow(mockParentObject, mockChildObject))
|
||||||
.toBeFalsy();
|
.toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -98,8 +107,10 @@ define(
|
|||||||
|
|
||||||
describe('enforces capability-based containment rules', function () {
|
describe('enforces capability-based containment rules', function () {
|
||||||
it('allows when object has capability', function () {
|
it('allows when object has capability', function () {
|
||||||
|
mockParentObject.getCapability.andReturn(typeC);
|
||||||
|
|
||||||
mockChildObject.hasCapability.andReturn(true);
|
mockChildObject.hasCapability.andReturn(true);
|
||||||
expect(policy.allow(typeC, mockChildObject))
|
expect(policy.allow(mockParentObject, mockChildObject))
|
||||||
.toBeTruthy();
|
.toBeTruthy();
|
||||||
expect(mockChildObject.hasCapability)
|
expect(mockChildObject.hasCapability)
|
||||||
.toHaveBeenCalledWith('telemetry');
|
.toHaveBeenCalledWith('telemetry');
|
||||||
@ -107,7 +118,10 @@ define(
|
|||||||
|
|
||||||
it('skips when object doesn\'t have capability', function () {
|
it('skips when object doesn\'t have capability', function () {
|
||||||
mockChildObject.hasCapability.andReturn(false);
|
mockChildObject.hasCapability.andReturn(false);
|
||||||
expect(policy.allow(typeC, mockChildObject))
|
|
||||||
|
mockParentObject.getCapability.andReturn(typeC);
|
||||||
|
|
||||||
|
expect(policy.allow(mockParentObject, mockChildObject))
|
||||||
.toBeFalsy();
|
.toBeFalsy();
|
||||||
expect(mockChildObject.hasCapability)
|
expect(mockChildObject.hasCapability)
|
||||||
.toHaveBeenCalledWith('telemetry');
|
.toHaveBeenCalledWith('telemetry');
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* Open MCT, Copyright (c) 2014-2016, United States Government
|
||||||
|
* as represented by the Administrator of the National Aeronautics and Space
|
||||||
|
* Administration. All rights reserved.
|
||||||
|
*
|
||||||
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
* Open MCT includes source code licensed under additional open source
|
||||||
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||||
|
* this source code distribution or the Licensing information page available
|
||||||
|
* at runtime from the About dialog for additional information.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
define(
|
||||||
|
["../src/PersistableCompositionPolicy"],
|
||||||
|
function (PersistableCompositionPolicy) {
|
||||||
|
describe("Persistable Composition policy", function () {
|
||||||
|
var objectAPI;
|
||||||
|
var mockOpenMCT;
|
||||||
|
var persistableCompositionPolicy;
|
||||||
|
var mockParent;
|
||||||
|
var mockChild;
|
||||||
|
var mockEditorCapability;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
objectAPI = jasmine.createSpyObj('objectsAPI', [
|
||||||
|
'getProvider'
|
||||||
|
]);
|
||||||
|
|
||||||
|
mockOpenMCT = {
|
||||||
|
objects: objectAPI
|
||||||
|
};
|
||||||
|
mockParent = jasmine.createSpyObj('domainObject', [
|
||||||
|
'hasCapability',
|
||||||
|
'getCapability',
|
||||||
|
'getId'
|
||||||
|
]);
|
||||||
|
mockParent.hasCapability.andReturn(true);
|
||||||
|
mockParent.getId.andReturn('someNamespace:someId');
|
||||||
|
mockChild = {};
|
||||||
|
mockEditorCapability = jasmine.createSpyObj('domainObject', [
|
||||||
|
'isEditContextRoot'
|
||||||
|
]);
|
||||||
|
mockParent.getCapability.andReturn(mockEditorCapability);
|
||||||
|
|
||||||
|
objectAPI.getProvider.andReturn({
|
||||||
|
save: function () {}
|
||||||
|
});
|
||||||
|
persistableCompositionPolicy = new PersistableCompositionPolicy(mockOpenMCT);
|
||||||
|
});
|
||||||
|
|
||||||
|
//Parent
|
||||||
|
// - getCapability ('editor')
|
||||||
|
// - isEditContextRoot
|
||||||
|
// - openMct.objects.getProvider
|
||||||
|
|
||||||
|
it("Does not allow composition for objects that are not persistable", function () {
|
||||||
|
mockEditorCapability.isEditContextRoot.andReturn(false);
|
||||||
|
expect(persistableCompositionPolicy.allow(mockParent, mockChild)).toBe(true);
|
||||||
|
objectAPI.getProvider.andReturn({});
|
||||||
|
expect(persistableCompositionPolicy.allow(mockParent, mockChild)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Always allows composition of objects in edit mode to support object creation", function () {
|
||||||
|
mockEditorCapability.isEditContextRoot.andReturn(true);
|
||||||
|
expect(persistableCompositionPolicy.allow(mockParent, mockChild)).toBe(true);
|
||||||
|
expect(objectAPI.getProvider).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
mockEditorCapability.isEditContextRoot.andReturn(false);
|
||||||
|
expect(persistableCompositionPolicy.allow(mockParent, mockChild)).toBe(true);
|
||||||
|
expect(objectAPI.getProvider).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
@ -47,7 +47,7 @@ define(
|
|||||||
}
|
}
|
||||||
return this.policyService.allow(
|
return this.policyService.allow(
|
||||||
"composition",
|
"composition",
|
||||||
parentCandidate.getCapability('type'),
|
parentCandidate,
|
||||||
object
|
object
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -51,7 +51,7 @@ define(
|
|||||||
}
|
}
|
||||||
return this.policyService.allow(
|
return this.policyService.allow(
|
||||||
"composition",
|
"composition",
|
||||||
parentCandidate.getCapability('type'),
|
parentCandidate,
|
||||||
object
|
object
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -55,7 +55,7 @@ define(
|
|||||||
}
|
}
|
||||||
return this.policyService.allow(
|
return this.policyService.allow(
|
||||||
"composition",
|
"composition",
|
||||||
parentCandidate.getCapability('type'),
|
parentCandidate,
|
||||||
object
|
object
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -103,7 +103,7 @@ define(
|
|||||||
validate();
|
validate();
|
||||||
expect(policyService.allow).toHaveBeenCalledWith(
|
expect(policyService.allow).toHaveBeenCalledWith(
|
||||||
"composition",
|
"composition",
|
||||||
parentCandidate.capabilities.type,
|
parentCandidate,
|
||||||
object
|
object
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -113,7 +113,7 @@ define(
|
|||||||
validate();
|
validate();
|
||||||
expect(mockPolicyService.allow).toHaveBeenCalledWith(
|
expect(mockPolicyService.allow).toHaveBeenCalledWith(
|
||||||
"composition",
|
"composition",
|
||||||
parentCandidate.capabilities.type,
|
parentCandidate,
|
||||||
object
|
object
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -123,7 +123,7 @@ define(
|
|||||||
validate();
|
validate();
|
||||||
expect(policyService.allow).toHaveBeenCalledWith(
|
expect(policyService.allow).toHaveBeenCalledWith(
|
||||||
"composition",
|
"composition",
|
||||||
parentCandidate.capabilities.type,
|
parentCandidate,
|
||||||
object
|
object
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -34,7 +34,8 @@ define(
|
|||||||
function LayoutCompositionPolicy() {
|
function LayoutCompositionPolicy() {
|
||||||
}
|
}
|
||||||
|
|
||||||
LayoutCompositionPolicy.prototype.allow = function (parentType, child) {
|
LayoutCompositionPolicy.prototype.allow = function (parent, child) {
|
||||||
|
var parentType = parent.getCapability('type');
|
||||||
if (parentType.instanceOf('layout') &&
|
if (parentType.instanceOf('layout') &&
|
||||||
child.getCapability('type').instanceOf('folder')) {
|
child.getCapability('type').instanceOf('folder')) {
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ define(
|
|||||||
function (LayoutCompositionPolicy) {
|
function (LayoutCompositionPolicy) {
|
||||||
describe("Layout's composition policy", function () {
|
describe("Layout's composition policy", function () {
|
||||||
var mockChild,
|
var mockChild,
|
||||||
|
mockCandidateObj,
|
||||||
mockCandidate,
|
mockCandidate,
|
||||||
mockContext,
|
mockContext,
|
||||||
candidateType,
|
candidateType,
|
||||||
@ -41,6 +42,11 @@ define(
|
|||||||
mockContext =
|
mockContext =
|
||||||
jasmine.createSpyObj('contextType', ['instanceOf']);
|
jasmine.createSpyObj('contextType', ['instanceOf']);
|
||||||
|
|
||||||
|
mockCandidateObj = jasmine.createSpyObj('domainObj', [
|
||||||
|
'getCapability'
|
||||||
|
]);
|
||||||
|
mockCandidateObj.getCapability.andReturn(mockCandidate);
|
||||||
|
|
||||||
mockChild.getCapability.andReturn(mockContext);
|
mockChild.getCapability.andReturn(mockContext);
|
||||||
|
|
||||||
mockCandidate.instanceOf.andCallFake(function (t) {
|
mockCandidate.instanceOf.andCallFake(function (t) {
|
||||||
@ -56,19 +62,19 @@ define(
|
|||||||
it("disallows folders in layouts", function () {
|
it("disallows folders in layouts", function () {
|
||||||
candidateType = 'layout';
|
candidateType = 'layout';
|
||||||
contextType = 'folder';
|
contextType = 'folder';
|
||||||
expect(policy.allow(mockCandidate, mockChild)).toBe(false);
|
expect(policy.allow(mockCandidateObj, mockChild)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not disallow folders elsewhere", function () {
|
it("does not disallow folders elsewhere", function () {
|
||||||
candidateType = 'nonlayout';
|
candidateType = 'nonlayout';
|
||||||
contextType = 'folder';
|
contextType = 'folder';
|
||||||
expect(policy.allow(mockCandidate, mockChild)).toBe(true);
|
expect(policy.allow(mockCandidateObj, mockChild)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows things other than folders in layouts", function () {
|
it("allows things other than folders in layouts", function () {
|
||||||
candidateType = 'layout';
|
candidateType = 'layout';
|
||||||
contextType = 'nonfolder';
|
contextType = 'nonfolder';
|
||||||
expect(policy.allow(mockCandidate, mockChild)).toBe(true);
|
expect(policy.allow(mockCandidateObj, mockChild)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -26,13 +26,11 @@ define([], function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AdapterCompositionPolicy.prototype.allow = function (
|
AdapterCompositionPolicy.prototype.allow = function (
|
||||||
parentType,
|
parent,
|
||||||
child
|
child
|
||||||
) {
|
) {
|
||||||
var container = parentType.getInitialModel();
|
|
||||||
|
|
||||||
return this.openmct.composition.checkPolicy(
|
return this.openmct.composition.checkPolicy(
|
||||||
container,
|
parent,
|
||||||
child
|
child
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user