Files
openstapps/test/routes/virtual-plugin-route.spec.ts
2021-04-27 13:01:32 +02:00

144 lines
4.5 KiB
TypeScript

/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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 * as 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 * as nock from 'nock';
import * as got from 'got';
import * as sinon from 'sinon';
import {Request} from 'express';
import {registerAddRequest} from './plugin-register-route.spec';
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 {
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();
}
@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);
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);
}
@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')
.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')
.post('/foo')
.reply(200, {invalid_result: ['foo bar']});
const req = mockReq(request);
await this.testRejection(req, plugin, SCValidationErrorResponse);
}
@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);
}
}