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:
Jovan Krunić
2019-07-04 17:43:36 +02:00
committed by Rainer Killinger
parent 59ea7a5ba6
commit 3d51ccfac2
12 changed files with 3035 additions and 188 deletions

View 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);
}
}

View 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);
}
}