test: add tests for routes

This commit is contained in:
Jovan Krunić
2020-09-28 11:12:56 +02:00
committed by Rainer Killinger
parent 751693bebc
commit d3955b3cdd
22 changed files with 1315 additions and 455 deletions

View File

@@ -13,131 +13,238 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// tslint:disable
import {should, use, expect} from 'chai';
import {
SCInternalServerErrorResponse,
SCPluginMetaData,
SCValidationErrorResponse
} from '@openstapps/core';
import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {slow, timeout, test, suite} from 'mocha-typescript';
import {SCPluginMetaData, SCInternalServerErrorResponse, SCValidationErrorResponse} from '@openstapps/core';
import {virtualPluginRoute} from '../../src/routes/virtual-plugin-route';
import {mockReq} from 'sinon-express-mock'
import nock from 'nock';
import got from 'got';
import sinon from 'sinon';
import {Request} from 'express';
import got from 'got';
import nock from 'nock';
import sinon from 'sinon';
import {mockReq} from 'sinon-express-mock';
import {plugins, validator} from '../../src/common';
import {virtualPluginRoute} from '../../src/routes/virtual-plugin-route';
import {DEFAULT_TEST_TIMEOUT, FooError} from '../common';
import {registerAddRequest} from './plugin-register-route.spec';
import {testApp} from '../tests-setup';
should();
use(chaiAsPromised);
const plugin = registerAddRequest.plugin;
@suite(timeout(10000), slow(5000))
export class VirtualPluginRouteSpec {
/**
* Internal method which provides information about the specific error inside of an internal server error
*
* @param req Express request
* @param plugin Plugin information (metadata)
* @param specificError Class of a specific error
*/
private async testRejection(req: Request, plugin: SCPluginMetaData, specificError: object) {
let thrownError: Error = new Error();
try {
describe('Virtual plugin routes', async function () {
describe('Middleware', async function () {
const sandbox = sinon.createSandbox();
/**
* Internal method which provides information about the specific error inside of an internal server error
*
* @param req Express request
* @param plugin Plugin information (metadata)
* @param specificError Class of a specific error
*/
async function testRejection(req: Request, plugin: SCPluginMetaData, specificError: object) {
let thrownError: Error = new Error();
try {
await virtualPluginRoute(req, plugin);
} catch (e) {
thrownError = e;
}
// return virtualPluginRoute(req, plugin).should.be.rejectedWith(SCInternalServerErrorResponse); was not working for some reason
expect(thrownError).to.be.instanceOf(SCInternalServerErrorResponse);
expect((thrownError as SCInternalServerErrorResponse).additionalData).to.be.instanceOf(specificError);
}
afterEach(function () {
// clean up request mocks (fixes issue with receiving response from mock from previous test case)
nock.cleanAll();
// restore everything to default methods (remove spies and stubs)
sandbox.restore();
});
it('should forward body of the request to address and route of the plugin', async function () {
const request = {
body: {
query: 'bar',
},
};
// spy the post method of got
// @ts-ignore
const gotStub = sandbox.stub(got, 'post').returns({body: {}});
// @ts-ignore
sandbox.stub(validator, 'validate').returns({errors: []});
const req = mockReq(request);
await virtualPluginRoute(req, plugin);
} catch (e) {
thrownError = e;
}
// return virtualPluginRoute(req, plugin).should.be.rejectedWith(SCInternalServerErrorResponse); was not working for some reason
expect(thrownError).to.be.instanceOf(SCInternalServerErrorResponse);
expect((thrownError as SCInternalServerErrorResponse).additionalData).to.be.instanceOf(specificError);
}
after() {
// clean up request mocks (fixes issue with receiving response from mock from previous test case)
nock.cleanAll();
// restore everything to default methods (remove spies and stubs)
sinon.restore();
}
expect(gotStub.args[0][0]).to.equal(plugin.route);
expect(gotStub.args[0][1].baseUrl).to.equal(plugin.address);
expect(gotStub.args[0][1].body).to.equal(req.body);
});
@test
'should forward body of the request to address and route of the plugin'() {
const request = {
body: {
query: 'bar',
},
};
// spy the got's post method
let spyGot = sinon.spy(got, 'post');
const req = mockReq(request);
virtualPluginRoute(req, plugin);
it('should provide data from the plugin when its route is called', async function () {
const request = {
body: {
query: 'bar',
},
};
const response = {
result: [
{foo: 'bar'},
{bar: 'foo'},
]
}
// mock response of the plugin's address
nock('http://foo.com:1234')
.post('/foo')
.reply(200, response);
const req = mockReq(request);
expect(spyGot.args[0][0]).to.equal(plugin.route);
expect(spyGot.args[0][1].baseUrl).to.equal(plugin.address);
expect(spyGot.args[0][1].body).to.equal(req.body);
}
expect(await virtualPluginRoute(req, plugin)).to.eql(response);
});
@test
async 'should provide data from the plugin when its route is called'() {
const request = {
body: {
query: 'bar',
},
};
const response = {
result: [
{foo: 'bar'},
{bar: 'foo'},
]
}
// mock response of the plugin's address
nock('http://foo.com:1234')
it('should throw the validation error if request is not valid', async function () {
const request = {
body: {
invalid_query_field: 'foo',
},
};
const req = mockReq(request);
await testRejection(req, plugin, SCValidationErrorResponse);
});
it('should throw the validation error if response is not valid', async function () {
const request = {
body: {
query: 'foo',
},
};
// mock response of the plugin service
nock('http://foo.com:1234')
.post('/foo')
.reply(200, {invalid_result: ['foo bar']});
const req = mockReq(request);
await testRejection(req, plugin, SCValidationErrorResponse);
});
it('should throw error if there is a problem with reaching the address of a plugin', async function () {
const request = {
body: {
query: 'foo',
},
};
// fake that post method of got throws an error
sandbox.stub(got, 'post')
.callsFake(() => {
throw new FooError();
});
const req = mockReq(request);
await testRejection(req, plugin, FooError);
});
});
describe('Routes', async function () {
// increase timeout for the suite
this.timeout(DEFAULT_TEST_TIMEOUT);
const sandbox = sinon.createSandbox();
// http status code
const OK = 200;
const internalServerError = new SCInternalServerErrorResponse();
afterEach(async function() {
// remove routes
plugins.clear();
// // restore everything to default methods (remove stubs)
sandbox.restore();
// clean up request mocks (fixes issue with receiving response from mock from previous test case)
nock.cleanAll();
});
it('should properly provide the response of a plugin', async function () {
// lets simulate that the plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
// mock responses of the plugin, depending on the body sent
nock('http://foo.com:1234')
.post('/foo', {query: 'foo'})
.reply(200, {result: [{foo: 'foo'}, {bar: 'foo'}]});
nock('http://foo.com:1234')
.post('/foo', {query: 'bar'})
.reply(200, {result: [{foo: 'bar'}, {bar: 'bar'}]});
const fooResponse = await testApp
.post('/foo')
.reply(200, response);
const req = mockReq(request);
expect(await virtualPluginRoute(req, plugin)).to.eql(response);
}
@test
async 'should throw error if request is not valid'() {
const request = {
body: {
invalid_query_field: 'foo',
},
};
const req = mockReq(request);
await this.testRejection(req, plugin, SCValidationErrorResponse);
}
@test
async 'should throw error if response is not valid'() {
const request = {
body: {
query: 'foo',
},
};
// mock response of the plugin service
nock('http://foo.com:1234')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({query: 'foo'});
const barResponse = await testApp
.post('/foo')
.reply(200, {invalid_result: ['foo bar']});
const req = mockReq(request);
await this.testRejection(req, plugin, SCValidationErrorResponse);
}
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({query: 'bar'});
@test
async 'should throw error if there is a problem with reaching the address of a plugin'() {
const request = {
body: {
query: 'foo',
},
};
class FooError extends Error {
};
// fake that got's post method throws an error
sinon.stub(got, 'post')
.callsFake(() => {
throw new FooError();
});
const req = mockReq(request);
await this.testRejection(req, plugin, FooError);
}
}
expect(fooResponse.status).to.be.equal(OK);
expect(fooResponse.body).to.be.deep.equal({result: [{foo: 'foo'}, {bar: 'foo'}]});
expect(barResponse.status).to.be.equal(OK);
expect(barResponse.body).to.be.deep.equal({result: [{foo: 'bar'}, {bar: 'bar'}]});
});
it('should return error response if plugin address is not responding', async function() {
// lets simulate that the plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
class FooError extends Error {}
// fake that got's post method throws an error
sandbox.stub(got, 'post')
.callsFake(() => {
throw new FooError();
});
const {status} = await testApp
.post('/foo')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({query: 'foo'});
expect(status).to.be.equal(internalServerError.statusCode);
});
it('should return the validation error response if plugin request is not valid', async function() {
// lets simulate that the plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
const {status, body} = await testApp
.post('/foo')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
// using number for query instead of (in request schema) required text
.send({query: 123})
expect(status).to.be.equal(502);
expect(body.additionalData).to.haveOwnProperty('name','ValidationError');
});
it('should return the validation error response if plugin response is not valid', async function() {
// lets simulate that the plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
// mock response of the plugin address
nock('http://foo.com:1234')
.post('/foo')
.reply(OK, {not_valid_field: ['foo bar']});
const {status, body} = await testApp
.post('/foo')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({query: 'foo'});
expect(status).to.be.equal(internalServerError.statusCode);
expect(body.additionalData).to.haveOwnProperty('name','ValidationError');
});
});
});