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 { iff, disallow } = require('feathers-hooks-common');
\r
19 const logger = require('../../../lib/logger');
\r
20 const request = require('request-promise');
\r
21 const agendaJobPopulate = require('../../hooks/agendaJobPopulate');
\r
22 const utils = require('../../../lib/otf-util');
\r
23 const axios = require('axios');
\r
24 const util = require('../../../lib/otf-util');
\r
25 const checkLocks = require('../../hooks/checkLocks');
\r
26 const { permissions, limitFields } = require('../../hooks/permissions/permissions');
\r
27 const errors = require('@feathersjs/errors');
\r
29 const canExecute = function(){
\r
30 return async (context) => {
\r
31 let id = context.id || context.data.testInstanceId;
\r
33 let testInstanceId = id;
\r
35 if(context.method == 'remove'){
\r
36 let job = await context.app.services[context.app.get('base-path') + 'jobs'].get(id, {provider: undefined});
\r
38 testInstanceId = job.data.testSchedule._testInstanceId;
\r
39 console.log(testInstanceId)
\r
42 //get group id of the test instance that is being executed
\r
43 let testInstance = await context.app.services[context.app.get('base-path') + 'test-instances'].get(testInstanceId, {provider: undefined, query: { $select: ['groupId', 'testDefinitionId', 'disabled'] } });
\r
45 //check if its locked
\r
46 let testDefinition = await context.app.services[context.app.get('base-path') + 'test-definitions'].get(testInstance.testDefinitionId, {provider: undefined, query: { $select: ['disabled'] } });
\r
48 if((testInstance.disabled || testDefinition.disabled) && context.method == 'create'){
\r
49 throw new errors.Unavailable('The test instance or definition is locked.');
\r
52 testInstance = new context.app.services[context.app.get('base-path') + 'test-instances'].Model(testInstance);
\r
53 if(context.params.ability.cannot('execute', testInstance)){
\r
54 throw new errors.Forbidden(`You are not allowed to execute this instance.`);
\r
61 all: [authenticate('jwt')],
\r
63 async function(context){
\r
64 if(context.params.query.testInstanceId){
\r
65 const mongoose = context.app.get('mongooseClient');
\r
66 const toObjectId = v => mongoose.Types.ObjectId(v);
\r
68 let Model = context.app.service(context.app.get('base-path') + 'jobs').Model;
\r
69 const conditions = [{
\r
71 "data.testSchedule._testInstanceId": toObjectId(context.params.query.testInstanceId),
\r
75 // "testSchedule": {
\r
76 // "_testInstanceId": toObjectId(context.params.query.testInstanceId)
\r
82 await new Promise(function(resolve, reject){
\r
83 Model.aggregate(conditions).exec(function(error, result){
\r
91 if(result.length == 1){
\r
92 context.params.query._id = result[0]._id;
\r
93 }else if(result.length == 0){
\r
97 result.forEach(elem => {
\r
100 context.params.query._id = {
\r
105 context.params.query._id = result._id;
\r
111 delete context.params.query.testInstanceId;
\r
118 permissions('jobs'),
\r
119 (context) => { console.log("AFTER PERMISSIONS")},
\r
121 async (context) => {
\r
122 const fullUrl = this.options.app.get('otf').url + context.app.get('base-path') + 'schedule-test';
\r
124 context.data.executorId = context.params.user._id;
\r
129 body: JSON.stringify(context.data),
\r
131 'Content-Type': 'application/json',
\r
132 'Authorization': 'Basic ' +
\r
134 context.app.get('serviceApi').aafId + ':' +
\r
135 context.app.get('serviceApi').aafPassword)
\r
137 rejectUnauthorized: false
\r
138 }, function (err, res, body) {
\r
144 context.result = JSON.parse(body);
\r
155 permissions('jobs'),
\r
157 async function (context) {
\r
158 const fullUrl = this.options.app.get('otf').url + context.app.get('base-path') + 'cancel-test';
\r
160 if (context.id == null || context.params.user._id == null ||
\r
161 utils.isValidObjectId(context.id) || utils.isValidObjectId(context.params.user._id)) {
\r
164 message: 'Request is invalid.'
\r
170 executorId: context.params.user._id
\r
173 // console.log(JSON.stringify(postData));
\r
178 body: JSON.stringify(postData),
\r
180 'Content-Type': 'application/json',
\r
181 'Authorization': 'Basic ' +
\r
183 context.app.get('serviceApi').aafId + ':' +
\r
184 context.app.get('serviceApi').aafPassword)
\r
186 rejectUnauthorized: false
\r
187 }, function (err, res, body) {
\r
192 context.result = JSON.parse(body);
\r
201 find: [agendaJobPopulate()],
\r
202 get: [agendaJobPopulate()],
\r