added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / services / execute / execute.hooks.js
diff --git a/otf-frontend/server/src/feathers/services/execute/execute.hooks.js b/otf-frontend/server/src/feathers/services/execute/execute.hooks.js
new file mode 100644 (file)
index 0000000..f64d812
--- /dev/null
@@ -0,0 +1,93 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+const { authenticate } = require('@feathersjs/authentication').hooks;\r
+const { permissions, limitFields } = require('../../hooks/permissions/permissions');\r
+const errors = require('@feathersjs/errors');\r
+const throwError = require('../../hooks/throw');\r
+const { disallow } = require('feathers-hooks-common');\r
+const canExecute = function(){\r
+       return async (context) => {\r
+               let id = context.id || context.data._id;\r
+               //must have an _id\r
+               if(!id){\r
+                       if(context.method == 'create')\r
+                               throw new errors.BadRequest("'_id' and 'asyncTopic' is required to execute a test instance");\r
+                       else\r
+                               throw new errors.BadRequest("An id must be provided to cancel an execution")\r
+               }\r
+\r
+               let testInstanceId = id;\r
+\r
+               if(context.method == 'remove'){\r
+                       let execution = await context.app.services[context.app.get('base-path') + 'test-executions'].get(id, {provider: undefined, query: { $select: ['historicTestInstance._id']}});\r
+                       testInstanceId = execution.historicTestInstance._id;\r
+               }\r
+\r
+               //get group id of the test instance that is being executed\r
+               let testInstance = await context.app.services[context.app.get('base-path') + 'test-instances'].get(testInstanceId, {query: { $select: ['groupId', 'testDefinitionId', 'disabled'] } });\r
+\r
+               //check if its locked\r
+               let testDefinition = await context.app.services[context.app.get('base-path') + 'test-definitions'].get(testInstance.testDefinitionId, {query: { $select: ['disabled'] } });\r
+\r
+               if((testInstance.disabled || testDefinition.disabled) && context.method == 'create'){\r
+                       throw new errors.Unavailable('The test instance or definition is locked.');\r
+               }\r
+\r
+               testInstance = new context.app.services[context.app.get('base-path') + 'test-instances'].Model(testInstance);\r
+               if(context.params.ability.cannot('execute', testInstance)){\r
+                       throw new errors.Forbidden(`You are not allowed to execute this instance.`);\r
+               }\r
+       }\r
+}\r
+\r
+module.exports = {\r
+       before: {\r
+               all: [authenticate('jwt'), permissions('execute')],\r
+               find: [ throwError(new errors.MethodNotAllowed()) ],\r
+               get: [ throwError(new errors.MethodNotAllowed())],\r
+               create: [\r
+                       (context) => {\r
+                               context.data.executorId = context.params.user._id;\r
+                               return context;\r
+                       },\r
+                       canExecute()\r
+               ],\r
+               update: [ throwError(new errors.MethodNotAllowed()) ],\r
+               patch: [ throwError(new errors.MethodNotAllowed()) ],\r
+               remove: [canExecute()]\r
+       },\r
+\r
+       after: {\r
+               all: [],\r
+               find: [],\r
+               get: [],\r
+               create: [],\r
+               update: [],\r
+               patch: [],\r
+               remove: []\r
+       },\r
+\r
+       error: {\r
+               all: [],\r
+               find: [],\r
+               get: [],\r
+               create: [],\r
+               update: [],\r
+               patch: [],\r
+               remove: []\r
+       }\r
+};\r