Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(processors): add tests for POST /processors #217

Merged
merged 3 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let reportsRouter = require('./reports/routes/reportsRoute.js');
let configRouter = require('./configManager/routes/configRoute.js');
let dslRouter = require('./tests/routes/dslRoute.js');
let testsRouter = require('./tests/routes/testsRoute.js');
let processorssRouter = require('./processors/routes/processorsRoute.js');

let swaggerValidator = require('express-ajv-swagger-validation');
let audit = require('express-requests-logger');
Expand Down Expand Up @@ -56,6 +57,7 @@ module.exports = () => {
app.use('/v1/dsl', dslRouter);
app.use('/v1/tests', reportsRouter);
app.use('/v1/tests', testsRouter);
app.use('/v1/processors', processorssRouter);

app.use('/', function (req, res, next) {
res.redirect('/ui');
Expand Down
2 changes: 1 addition & 1 deletion src/processors/controllers/processorController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
let processorManager = require('../models/processorManager');
let processorManager = require('../models/processorsManager');

module.exports.createProcessor = function (req, res, next) {
return processorManager.createProcessor(req.body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function insertProcessor(processorId, processorInfo) {
}

async function initSchemas() {
const processorsFiles = client.define('processors', {
const processorsFiles = client.define('processor', {
processor_id: {
type: Sequelize.DataTypes.UUID,
primaryKey: true
Expand Down
3 changes: 2 additions & 1 deletion src/processors/models/processorsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ module.exports.createProcessor = async function (processor) {
try {
if (processor.type === common.PROCESSOR_TYPE_FILE_DOWNLOAD) {
const file = await fileManager.downloadFile(processor.file_url);
processor.javascript = file.body; // TODO check dis
processor.javascript = file;
}
await databaseConnector.insertProcessor(processorId, processor);
logger.info('Processor saved successfully to database');
return processor;
} catch (error) {
logger.error(error, 'Error occurred trying to create new processor');
return Promise.reject(error);
Expand Down
25 changes: 25 additions & 0 deletions tests/integration-tests/processors/helpers/requestCreator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

const request = require('supertest'),
expressApp = require('../../../../src/app');
let app;
module.exports = {
init,
createProcessor
};
async function init() {
try {
app = await expressApp();
} catch (err){
console.log(err);
process.exit(1);
}
}

function createProcessor(body, headers) {
return request(app).post('/v1/processors')
.send(body)
.set(headers)
.expect(function(res){
return res;
});
}
108 changes: 108 additions & 0 deletions tests/integration-tests/processors/processors-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const should = require('should'),
nock = require('nock');

let validHeaders = { 'Content-Type': 'application/json' };
const requestSender = require('./helpers/requestCreator');
describe('Processors api', function() {
this.timeout(5000000);
before(async function () {
await requestSender.init();
});

describe('Good requests', function() {
it('Create processor with type file_download', async () => {
nock('https://authentication.predator.dev').get('/?dl=1').reply(200,
`{
const uuid = require('uuid/v4');
module.exports = {
createAuthToken
};

function createAuthToken(userContext, events, done) {
userContext.vars.token = uuid();
return done();
}
}`
);

const requestBody = {
name: 'authentication',
description: 'Creates authorization token and saves it in the context',
type: 'file_download',
file_url: 'https://authentication.predator.dev/?dl=1'
};
let createProcessorResponse = await requestSender.createProcessor(requestBody, validHeaders);
createProcessorResponse.statusCode.should.eql(201);
});

it('Create processor with type raw_javascript', async () => {
const requestBody = {
name: 'authentication',
description: 'Creates authorization token and saves it in the context',
type: 'raw_javascript',
javascript:
`{
const uuid = require('uuid/v4');
module.exports = {
createAuthToken
};

function createAuthToken(userContext, events, done) {
userContext.vars.token = uuid();
return done();
}
}`
};
let createProcessorResponse = await requestSender.createProcessor(requestBody, validHeaders);
createProcessorResponse.statusCode.should.eql(201);
});
});

describe('Bad requests', function () {
it('Create processor with unknown type', async () => {
const requestBody = {
name: 'bad-processor',
description: 'Processor with unknown type',
type: 'unknown'
};
let createProcessorResponse = await requestSender.createProcessor(requestBody, validHeaders);
createProcessorResponse.statusCode.should.eql(400);
});

it('Create processor with type file_download and no url', async () => {
const requestBody = {
name: 'download-me',
description: 'Processor with no file url',
type: 'file_download'
};
let createProcessorResponse = await requestSender.createProcessor(requestBody, validHeaders);
createProcessorResponse.statusCode.should.eql(400);
});

it('Create processor with type raw_javascript and no js', async () => {
const requestBody = {
name: 'javascript-me',
description: 'Processor with no js',
type: 'raw_javascript',
file_url: 'bad'
};
let createProcessorResponse = await requestSender.createProcessor(requestBody, validHeaders);
createProcessorResponse.statusCode.should.eql(400);
});
});

describe('Sad requests', function () {
it('Create processor with type file_download and invalid file_url', async () => {
nock('https://authentication.predator.dev').get('/?dl=1').replyWithError('error downloading file');

const requestBody = {
name: 'authentication',
description: 'Creates authorization token and saves it in the context',
type: 'file_download',
file_url: 'https://authentication.predator.dev/?dl=1'
};
let createProcessorResponse = await requestSender.createProcessor(requestBody, validHeaders);
createProcessorResponse.statusCode.should.eql(422);
});
});
});