mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-10 03:32:52 +00:00
feat: add functionality to register plugins via http
Also: - Add functionality for serving the responses from plugins - Add tests for related methods and routes Closes #2, #37
This commit is contained in:
committed by
Rainer Killinger
parent
59ea7a5ba6
commit
3d51ccfac2
240
test/app.spec.ts
Normal file
240
test/app.spec.ts
Normal file
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
import * as supertest from 'supertest';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import {timeout, slow} from 'mocha-typescript';
|
||||
import {should, use} from 'chai';
|
||||
import {configureApp, app} from '../src/app';
|
||||
import {registerAddRequest, registerRemoveRequest} from './routes/plugin-register-route.spec';
|
||||
import {plugins} from '../src/common';
|
||||
import {SCPluginRemove} from '@openstapps/core';
|
||||
import * as nock from 'nock';
|
||||
import * as got from 'got';
|
||||
import * as sinon from 'sinon';
|
||||
|
||||
should();
|
||||
use(chaiAsPromised);
|
||||
let appTest: supertest.SuperTest<supertest.Test>;
|
||||
// configures the backend and creates supertest
|
||||
const prepareTestApp = async () => {
|
||||
await configureApp();
|
||||
appTest = supertest(app);
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests plugin registration routes
|
||||
*/
|
||||
@suite(timeout(10000), slow(5000))
|
||||
export class AppPluginRegisterSpec {
|
||||
static async before() {
|
||||
if (typeof appTest === 'undefined') {
|
||||
await prepareTestApp();
|
||||
}
|
||||
}
|
||||
async after() {
|
||||
// remove plugins
|
||||
plugins.clear();
|
||||
}
|
||||
|
||||
@test
|
||||
'should respond with success when providing valid request'() {
|
||||
return appTest
|
||||
.post('/plugin/register')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send(registerAddRequest)
|
||||
.expect(200, {success: true});
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should respond with bad request if when providing invalid request'() {
|
||||
return appTest
|
||||
.post('/plugin/register')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send({foo: 'bar'})
|
||||
.expect(400);
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should respond with false if something went wrong with a valid request'() {
|
||||
// lets simulate that a plugin is already registered
|
||||
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
|
||||
|
||||
// registering a different plugin for the same route causes the expected error
|
||||
const registerAddRequestAltered = {...registerAddRequest, plugin: {...registerAddRequest.plugin, name: 'FooBar Plugin'}};
|
||||
|
||||
await appTest
|
||||
.post('/plugin/register')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send(registerAddRequestAltered)
|
||||
.expect(409)
|
||||
.expect((res: supertest.Response) => {
|
||||
res.body.should.have.property('name', 'SCPluginAlreadyRegisteredError');
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should respond with success when deregistering already registered plugin'() {
|
||||
// lets simulate that a plugin is already registered
|
||||
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
|
||||
|
||||
await appTest
|
||||
.post('/plugin/register')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send(registerRemoveRequest)
|
||||
.expect(200, {success: true});
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should response with false when deleting non previously registered plugin'() {
|
||||
// lets simulate that the plugin is already registered
|
||||
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
|
||||
|
||||
await appTest
|
||||
.post('/plugin/register')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
// using a different route for the remove request
|
||||
.send({action: 'remove', route: '/not-foo'} as SCPluginRemove)
|
||||
.expect(200, {success: false});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests functioning of already registered plugins
|
||||
*/
|
||||
@suite(timeout(10000), slow(5000))
|
||||
export class AppPluginSpec {
|
||||
static async before() {
|
||||
if (typeof appTest === 'undefined') {
|
||||
await prepareTestApp();
|
||||
}
|
||||
}
|
||||
|
||||
async after() {
|
||||
// remove plugins
|
||||
plugins.clear();
|
||||
// restore everything to default methods (remove stubs)
|
||||
sinon.restore();
|
||||
// clean up request mocks (fixes issue with receiving response from mock from previous test case)
|
||||
nock.cleanAll();
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should properly provide the response of a registered plugin'() {
|
||||
// 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'}]});
|
||||
|
||||
await appTest
|
||||
.post('/foo')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send({query: 'foo'})
|
||||
.expect(200, {result: [{foo: 'foo'}, {bar: 'foo'}]});
|
||||
|
||||
await appTest
|
||||
.post('/foo')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send({query: 'bar'})
|
||||
.expect(200, {result: [{foo: 'bar'}, {bar: 'bar'}]});
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should provide 404 if plugin/route is not registered'() {
|
||||
// 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(200, {result: [{foo: 'bar'}, {bar: 'foo'}]});
|
||||
|
||||
return appTest
|
||||
.post('/not-foo')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send({query: 'foo'})
|
||||
.expect(404);
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should return error response if plugin address is not responding'() {
|
||||
// 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
|
||||
sinon.stub(got, 'post')
|
||||
.callsFake(() => {
|
||||
throw new FooError();
|
||||
});
|
||||
|
||||
return appTest
|
||||
.post('/foo')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send({query: 'foo'})
|
||||
.expect(502);
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should return error response if request is not valid'() {
|
||||
// lets simulate that the plugin is already registered
|
||||
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
|
||||
|
||||
return appTest
|
||||
.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(502)
|
||||
.expect((res: supertest.Response) => {
|
||||
res.body.additionalData.should.have.property('name', 'ValidationError');
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should return error response if plugin response is not valid'() {
|
||||
// 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(200, {not_valid_field: ['foo bar']});
|
||||
|
||||
return appTest
|
||||
.post('/foo')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.send({query: 'foo'})
|
||||
.expect(502)
|
||||
.expect((res: supertest.Response) => {
|
||||
res.body.additionalData.should.have.property('name', 'ValidationError');
|
||||
});
|
||||
}
|
||||
}
|
||||
110
test/routes/plugin-register-route.spec.ts
Normal file
110
test/routes/plugin-register-route.spec.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
import {
|
||||
SCPluginAdd,
|
||||
SCPluginAlreadyRegisteredErrorResponse,
|
||||
SCPluginRemove,
|
||||
} from '@openstapps/core';
|
||||
import {should, use} from 'chai';
|
||||
import {slow, timeout} from 'mocha-typescript';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import {plugins} from '../../src/common';
|
||||
import {pluginRegisterHandler} from '../../src/routes/plugin-register-route';
|
||||
|
||||
should();
|
||||
use(chaiAsPromised);
|
||||
|
||||
export const registerAddRequest: SCPluginAdd =
|
||||
require('../../node_modules/@openstapps/core/test/resources/PluginRegisterRequest.1.json')
|
||||
.instance;
|
||||
|
||||
export const registerRemoveRequest: SCPluginRemove = {
|
||||
action: 'remove',
|
||||
route: registerAddRequest.plugin.route
|
||||
};
|
||||
|
||||
@suite(timeout(10000), slow(5000))
|
||||
export class PluginRegisterRouteSpec {
|
||||
after() {
|
||||
// remove plugins
|
||||
plugins.clear();
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should register a plugin'() {
|
||||
// register one plugin
|
||||
const response = await pluginRegisterHandler(registerAddRequest, {});
|
||||
|
||||
return response.should.eql({success: true})
|
||||
&& plugins.size.should.equal(1);
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should allow re-registering the same plugin'() {
|
||||
// register one plugin
|
||||
await pluginRegisterHandler(registerAddRequest, {});
|
||||
|
||||
// register the same plugin again
|
||||
const responseReregister = await pluginRegisterHandler(registerAddRequest, {});
|
||||
|
||||
return responseReregister.should.eql({success: true})
|
||||
&& plugins.size.should.equal(1);
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should show an error if a plugin has already been registered'() {
|
||||
// register one plugin
|
||||
await pluginRegisterHandler(registerAddRequest, {});
|
||||
|
||||
// create new request for adding a plugin with only name that changed
|
||||
let registerAddRequestAltered: SCPluginAdd = {...registerAddRequest, plugin: {...registerAddRequest.plugin, name: 'FooBar Plugin'}};
|
||||
|
||||
// register the same plugin again
|
||||
return pluginRegisterHandler(registerAddRequestAltered, {})
|
||||
.should.eventually.be.rejectedWith('Plugin already registered')
|
||||
.and.be.an.instanceOf(SCPluginAlreadyRegisteredErrorResponse)
|
||||
// check that the right plugin information (of the previously registered plugin) is provided with the error
|
||||
.and.have.property('additionalData', registerAddRequest.plugin);
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should remove a plugin if it exists'() {
|
||||
// register one plugin
|
||||
await pluginRegisterHandler(registerAddRequest, {});
|
||||
|
||||
plugins.size.should.equal(1);
|
||||
|
||||
const response = await pluginRegisterHandler(registerRemoveRequest, {});
|
||||
|
||||
return response.should.eql({success: true})
|
||||
&& plugins.size.should.equal(0);
|
||||
}
|
||||
|
||||
@test
|
||||
async 'should return false when removing a plugin whose route doesn\'t exist'() {
|
||||
// register one plugin
|
||||
await pluginRegisterHandler(registerAddRequest, {});
|
||||
|
||||
const response = await pluginRegisterHandler({...registerRemoveRequest, route: '/not-foo'}, {});
|
||||
|
||||
return response.should.eql({success: false});
|
||||
|
||||
// TODO: use 404 response
|
||||
// return pluginRegisterHandler({...registerRemoveRequest, route: '/bar'}, {})
|
||||
// .should.eventually.be.rejectedWith('Resource not found')
|
||||
// .and.be.an.instanceOf(SCNotFoundErrorResponse);
|
||||
}
|
||||
}
|
||||
142
test/routes/virtual-plugin-route.spec.ts
Normal file
142
test/routes/virtual-plugin-route.spec.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
import {should, use, expect} from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import {slow, timeout} 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user