Skip to content

Commit

Permalink
Merge pull request dmolchanenko#4 from wolterskluwer-redwoodhq/rest-a…
Browse files Browse the repository at this point in the history
…pi-changes

Add verifyexecution endpoint and email after executions
  • Loading branch information
Wolters Kluwer - UpToDate authored Sep 20, 2016
2 parents 7d7a412 + 0f8c212 commit 85d48ee
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 22 deletions.
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var express = require('express')
, syncIDE = require('./routes/syncIDE')
, testcaseHistory = require('./routes/testcaseHistory')
, remoteexecution = require('./routes/remoteexecution');
//, elk = require('./routes/elk');

var realFs = require("fs");
var gracefulFs = require("graceful-fs");
Expand Down Expand Up @@ -163,9 +164,12 @@ app.post('/variableTags',auth.auth, variableTags.variableTagsPost);

//start execution
app.post('/executionengine/startexecution', executionengine.startexecutionPost);
//app.post('/elk/pushtoelk', elk.publishToElkPost);

// remote execution
app.get('/api/remoteexecution/startexecution', remoteexecution.startexecutionPost);
app.get('/api/remoteexecution/verifyexecution', remoteexecution.verifyexecutionGet);
// verify remote execution request

//stop
app.post('/executionengine/stopexecution',auth.auth, executionengine.stopexecutionPost);
Expand Down
12 changes: 9 additions & 3 deletions cli/CIExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ common.parseConfig(function(){
execution.lastRunDate = null;
execution.testsetname = argv.testset;
execution.pullLatest = argv.pullLatest;
execution.sendEmail=true;

if(argv.emails){
execution.emails = argv.emails.split(",");

}
if(argv.tags){
execution.tag = argv.tags.split(",");
}
Expand Down Expand Up @@ -90,7 +96,7 @@ function saveExecutionTestCases(testsetID,executionID,callback){
if(dbtestcase.tcData && dbtestcase.tcData.length > 0){
var ddTCCount = 0;
dbtestcase.tcData.forEach(function(row,rowIndex){
var insertTC = {executionID:executionID,name:dbtestcase.name,tag:testcase.tag,status:"Not Run",testcaseID:testcase._id.toString(),_id: new ObjectID().toString()};
var insertTC = {executionID:executionID,name:dbtestcase.name,tag:dbtestcase.tag,status:"Not Run",testcaseID:testcase._id.toString(),_id: new ObjectID().toString()};
insertTC.rowIndex = rowIndex+1;
insertTC.name = insertTC.name +"_"+(rowIndex+1);
insertTC.tcData = row;
Expand All @@ -104,7 +110,7 @@ function saveExecutionTestCases(testsetID,executionID,callback){
})
}
else{
var insertTC = {executionID:executionID,name:dbtestcase.name,tag:testcase.tag,status:"Not Run",testcaseID:testcase._id.toString(),_id: new ObjectID().toString()};
var insertTC = {executionID:executionID,name:dbtestcase.name,tag:dbtestcase.tag,status:"Not Run",testcaseID:testcase._id.toString(),_id: new ObjectID().toString()};
testcases.push(insertTC);
ExeTCCollection.insert(insertTC, {safe:true},function(err,returnData){
if(index+1 == dbtestcases.testcases.length){
Expand Down Expand Up @@ -185,7 +191,7 @@ function StartExecution(execution,testcases,callback){
});

// write data to request body
req.write(JSON.stringify({retryCount:execution.retryCount,ignoreAfterState:false,ignoreStatus:execution.ignoreStatus,ignoreScreenshots:execution.ignoreScreenshots,testcases:testcases,variables:execution.variables,executionID:execution._id,machines:execution.machines,templates:execution.templates}));
req.write(JSON.stringify({retryCount:execution.retryCount,ignoreAfterState:false,sendEmail:execution.sendEmail,emails:execution.emails,ignoreStatus:execution.ignoreStatus,ignoreScreenshots:execution.ignoreScreenshots,testcases:testcases,variables:execution.variables,executionID:execution._id,machines:execution.machines,templates:execution.templates}));
req.end();
}

Expand Down
2 changes: 1 addition & 1 deletion public/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
<div style="width:200px; margin:0 auto 40px;"><a href="/agentsetup/Agent_RedwoodHQ_Setup.exe" style="color:blue;width:200px; margin-left:60px;">Download Agent</a></div>
<BR>&nbsp;<BR>
<div style="color:#9aa94f;width:200px; margin:0 auto;">Default credentials are: admin/admin</div>

<div style="width:70px; margin:0 auto 20px;">Version 2.4.9</div>
</body>
</html>
196 changes: 178 additions & 18 deletions routes/remoteexecution.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/*
Add ability to trigger test executions with a URL
(eg, http://auto-poc01d:8080/executions?testURL=xxxx&Project=Microservices&user=hkirk&pullLatest=false)
e.g. http://localhost:3000/api/remoteexecution/startexecution?name=Amazon%20Shopping&user=admin&testset=Amazon%20Shopping&machines=127.0.0.1&pullLatest=false&retryCount=1&project=Sample&ignoreScreenshots=true
node CIExecution.js --name "Amazon Shopping" --user admin --testset "Amazon Shopping" --machines "127.0.0.1" --pullLatest false --retryCount 1 --project Sample --ignoreScreenshots true
http://localhost:3000/api/remoteexecution/startexecution?name=Amazon%20Shopping&user=admin&testset=Amazon%20Shopping&machines=127.0.0.1&pullLatest=false&retryCount=1&project=Sample&ignoreScreenshots=true
http header
http body: { user=admin,
testset=Amazon Shopping,
Expand Down Expand Up @@ -63,6 +66,12 @@ exports.startexecutionPost = function(req, res){
execution.lastRunDate = null;
execution.testsetname = query.testset;
execution.pullLatest = query.pullLatest;
execution.sendEmail = true;

if(query.emails){
execution.emails = query.emails.split(",");

}

if(query.tags){
execution.tag = query.tags.split(",");
Expand All @@ -81,26 +90,20 @@ exports.startexecutionPost = function(req, res){
}
}
execution._id = new ObjectID().toString();
if(!_validateQueryParams(query)) {
var exitDetails = {statusCode : 400, error : "Invalid Query Parameters"};
var validationDetails = { status : true, error : "" }
if(!_validateQueryParams(query, validationDetails)) {
var exitDetails = {statusCode : 400, error : "Mandatory parameter value(s) missing: "
+ validationDetails.error};
_sendStatus(res, exitDetails);
return;
}

if(!db) {
common.initDB(common.Config.DBPort,function(){
db = app.getDB();
console.log("call _prepareAndRunExecution 1")
_prepareAndRunExecution(execution, query, res, function() {
console.log("_prepareAndRunExecution completed 1")
});
});
} else {
console.log("call _prepareAndRunExecution 2")
_initializeDB(function(){
console.log("call _prepareAndRunExecution 1")
_prepareAndRunExecution(execution, query, res, function() {
console.log("_prepareAndRunExecution completed 2")
})
}
console.log("_prepareAndRunExecution completed 1")
});
})
}

function _prepareAndRunExecution(execution, query, res, callback){
Expand Down Expand Up @@ -146,7 +149,7 @@ function _prepareAndRunExecution(execution, query, res, callback){
});
}

function _validateQueryParams(query) {
function _validateQueryParams(query, validationDetails) {
var valid = true;
if(!query.project ||
!query.user ||
Expand All @@ -156,6 +159,15 @@ function _validateQueryParams(query) {
valid = false;
}

if(valid === false && validationDetails) {
if(!query.project) validationDetails.error = " project "
if(!query.user) validationDetails.error += " user "
if(!query.testset) validationDetails.error += " testset "
if(!query.machines) validationDetails.error += " machines "
if(!query.name) validationDetails.error += " name "
validationDetails.status = false
}

return valid;
}

Expand Down Expand Up @@ -268,7 +280,7 @@ function StartExecution(execution,testcases,finalResponse,callback){
});

// write data to request body
req.write(JSON.stringify({retryCount:execution.retryCount,ignoreAfterState:false,ignoreStatus:execution.ignoreStatus,ignoreScreenshots:execution.ignoreScreenshots,testcases:testcases,variables:execution.variables,executionID:execution._id,machines:execution.machines,templates:execution.templates}));
req.write(JSON.stringify({retryCount:execution.retryCount,ignoreAfterState:false,sendEmail:execution.sendEmail,emails:execution.emails,ignoreStatus:execution.ignoreStatus,ignoreScreenshots:execution.ignoreScreenshots,testcases:testcases,variables:execution.variables,executionID:execution._id,machines:execution.machines,templates:execution.templates}));
req.end();
}

Expand Down Expand Up @@ -483,3 +495,151 @@ function GenerateReport(finalResponse, queryParams,cliexecution,xw,callback){
});
}

function _initializeDB(callback) {
if(!db) {
common.initDB(common.Config.DBPort,function(){
db = app.getDB()
callback()
})
} else {
callback()
}
}

/*
**************************************************************************************
API : /api/remoteexecution/verifyexecution
Mandatory params:
(a)user
(b)name
(c)project
(d)testset
(e)machines
Purpose:
(1) Verify required parameters are passed in
(2) Verify whether the param values exists in DB
**************************************************************************************
*/
exports.verifyexecutionGet = function(req, res) {
console.log("verifyexecutionGet");
_verifyexecution(req, res);
}

function _verifyexecution(req, res) {
console.log("/api/remoteexecution/verifyexecution");
var validationDetails = {status: true, error: "" };
var newQueryObj = {};
var query = require('url').parse(req.url,true).query;

// query param validation
if(!_validateQueryParams(query, validationDetails)) {
var exitDetails = {statusCode : 400, error : "Mandatory parameter value(s) missing: "
+ validationDetails.error };
_sendStatus(res, exitDetails);
return;
}

_initializeDB(function(){
// (a) validate user
verifyUser(query.user, function(error) {
if(error) {
validationDetails.status = false;
validationDetails.error = validationDetails.error + error;
}else {
console.log(query.user);
newQueryObj.user = query.user;
}
})

// (b) validate execution 'name' parameter
/*verifyExecution(query.name, function(error) {
if(error) {
validationDetails.status = false;
validationDetails.error = validationDetails.error + error;
}else {
console.log(query.name);
newQueryObj.name = query.name;
}
});*/

// (c) & (d) validates testset, project
validateTestSet(query.testset, query.project, function(error, testsetObj){
if(error || !testsetID) {
validationDetails.status = false;
validationDetails.error = validationDetails.error + error;
} else {
newQueryObj.project = testsetObj.project;
newQueryObj.testset = testsetObj._id;
}
})

// (e) parses query for machines and returns an array of machines
// Also verifies the DB for valid machine in 'machines' collection
formatMachines(query, function(machines) {
if(!machines || machines.length == 0) {
validationDetails.status = false;
validationDetails.error = validationDetails.error + " machines ";
} else {
newQueryObj.machines = machines;
}
})

setTimeout(function(){
console.log(newQueryObj);
console.log(validationDetails);

if(!validationDetails.status) {
var exitDetails = {statusCode : 400, error : "Invalid Query Parameter value(s): " + validationDetails.error };
_sendStatus(res, exitDetails);
} else {
res.status(200).json({ status: 'success' });
}
},400);
})
}

function validateTestSet(testset,project,callback){
if(!testset || !project) callback(" testset/project ", null);
db.collection('testsets', function(err, collection) {
collection.findOne({name:testset,project:project}, function(err, dbtestset) {
if(err || !dbtestset) {
console.log("validateTestSet failure ")
callback(" testset/project ", null);
} else {
console.log("validateTestSet success ")
console.log(dbtestset);
callback(null, dbtestset);
}
});
});
}

function verifyUser(username,callback){
if(!username) callback(" user ");
db.collection('users', function(err, collection) {
collection.findOne({username:username},function(err,user){
if (err || !user){
callback(" user ");
}
else{
callback();
}
});
})
}

/*
function verifyExecution(executionName,callback){
if(!executionName) callback(" name ");
db.collection('executions', function(err, collection) {
collection.findOne({name:executionName},function(err,execution){
if (execution == null){
callback(" name ");
}
else{
callback();
}
});
})
}*/

0 comments on commit 85d48ee

Please sign in to comment.