1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 const { authenticate } = require('@feathersjs/authentication').hooks;
\r
18 const { permissions, limitFields } = require('../../hooks/permissions/permissions');
\r
19 const errors = require('@feathersjs/errors');
\r
20 const throwError = require('../../hooks/throw');
\r
21 const { disallow } = require('feathers-hooks-common');
\r
22 const canExecute = function(){
\r
23 return async (context) => {
\r
24 let id = context.id || context.data._id;
\r
27 if(context.method == 'create')
\r
28 throw new errors.BadRequest("'_id' and 'asyncTopic' is required to execute a test instance");
\r
30 throw new errors.BadRequest("An id must be provided to cancel an execution")
\r
33 let testInstanceId = id;
\r
35 if(context.method == 'remove'){
\r
36 let execution = await context.app.services[context.app.get('base-path') + 'test-executions'].get(id, {provider: undefined, query: { $select: ['historicTestInstance._id']}});
\r
37 testInstanceId = execution.historicTestInstance._id;
\r
40 //get group id of the test instance that is being executed
\r
41 let testInstance = await context.app.services[context.app.get('base-path') + 'test-instances'].get(testInstanceId, {query: { $select: ['groupId', 'testDefinitionId', 'disabled'] } });
\r
43 //check if its locked
\r
44 let testDefinition = await context.app.services[context.app.get('base-path') + 'test-definitions'].get(testInstance.testDefinitionId, {query: { $select: ['disabled'] } });
\r
46 if((testInstance.disabled || testDefinition.disabled) && context.method == 'create'){
\r
47 throw new errors.Unavailable('The test instance or definition is locked.');
\r
50 testInstance = new context.app.services[context.app.get('base-path') + 'test-instances'].Model(testInstance);
\r
51 if(context.params.ability.cannot('execute', testInstance)){
\r
52 throw new errors.Forbidden(`You are not allowed to execute this instance.`);
\r
59 all: [authenticate('jwt'), permissions('execute')],
\r
60 find: [ throwError(new errors.MethodNotAllowed()) ],
\r
61 get: [ throwError(new errors.MethodNotAllowed())],
\r
64 context.data.executorId = context.params.user._id;
\r
69 update: [ throwError(new errors.MethodNotAllowed()) ],
\r
70 patch: [ throwError(new errors.MethodNotAllowed()) ],
\r
71 remove: [canExecute()]
\r