added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / services / execute / execute.hooks.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \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
25                 //must have an _id\r
26                 if(!id){\r
27                         if(context.method == 'create')\r
28                                 throw new errors.BadRequest("'_id' and 'asyncTopic' is required to execute a test instance");\r
29                         else\r
30                                 throw new errors.BadRequest("An id must be provided to cancel an execution")\r
31                 }\r
32 \r
33                 let testInstanceId = id;\r
34 \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
38                 }\r
39 \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
42 \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
45 \r
46                 if((testInstance.disabled || testDefinition.disabled) && context.method == 'create'){\r
47                         throw new errors.Unavailable('The test instance or definition is locked.');\r
48                 }\r
49 \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
53                 }\r
54         }\r
55 }\r
56 \r
57 module.exports = {\r
58         before: {\r
59                 all: [authenticate('jwt'), permissions('execute')],\r
60                 find: [ throwError(new errors.MethodNotAllowed()) ],\r
61                 get: [ throwError(new errors.MethodNotAllowed())],\r
62                 create: [\r
63                         (context) => {\r
64                                 context.data.executorId = context.params.user._id;\r
65                                 return context;\r
66                         },\r
67                         canExecute()\r
68                 ],\r
69                 update: [ throwError(new errors.MethodNotAllowed()) ],\r
70                 patch: [ throwError(new errors.MethodNotAllowed()) ],\r
71                 remove: [canExecute()]\r
72         },\r
73 \r
74         after: {\r
75                 all: [],\r
76                 find: [],\r
77                 get: [],\r
78                 create: [],\r
79                 update: [],\r
80                 patch: [],\r
81                 remove: []\r
82         },\r
83 \r
84         error: {\r
85                 all: [],\r
86                 find: [],\r
87                 get: [],\r
88                 create: [],\r
89                 update: [],\r
90                 patch: [],\r
91                 remove: []\r
92         }\r
93 };\r