mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-19 16:13:06 +00:00
30
test/TestPlugin.ts
Normal file
30
test/TestPlugin.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import * as express from 'express';
|
||||
import {Plugin} from '../src/plugin';
|
||||
|
||||
/**
|
||||
* A test plugin we use for all the tests
|
||||
*
|
||||
* It can be constructed without any parameter at all, or with all parameters if we want to test it
|
||||
* It also serves as kind of a minimal plugin
|
||||
*/
|
||||
export class TestPlugin extends Plugin {
|
||||
protected async onRouteInvoke(_req: express.Request, res: express.Response): Promise<void> {
|
||||
res.json({});
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
114
test/plugin-client.spec.ts
Normal file
114
test/plugin-client.spec.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCPluginRegisterRequest, SCPluginRegisterResponse, SCPluginRegisterRoute} from '@openstapps/core';
|
||||
import * as chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import * as chaiSpies from 'chai-spies';
|
||||
import {suite, test, timeout} from 'mocha-typescript';
|
||||
import {HttpClient} from '../src/http-client';
|
||||
import {HttpClientResponse} from '../src/http-client-interface';
|
||||
import {PluginClient} from '../src/plugin-client';
|
||||
import {TestPlugin} from './TestPlugin';
|
||||
|
||||
chai.use(chaiSpies);
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
const httpClient = new HttpClient();
|
||||
|
||||
const pluginRegisterRoute = new SCPluginRegisterRoute();
|
||||
|
||||
const pluginClient = new PluginClient(httpClient, 'http://localhost');
|
||||
|
||||
@suite(timeout(10000))
|
||||
export class PluginClientSpec {
|
||||
static plugin: TestPlugin;
|
||||
|
||||
static async after() {
|
||||
await this.plugin.close();
|
||||
}
|
||||
|
||||
static async before() {
|
||||
this.plugin = new TestPlugin(4000, '', '', '', '', {getSchema: () => {/***/}} as any, '', '', '');
|
||||
}
|
||||
|
||||
async after() {
|
||||
sandbox.restore();
|
||||
}
|
||||
|
||||
@test
|
||||
async registerPlugin() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCPluginRegisterResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
success: true,
|
||||
},
|
||||
headers: {},
|
||||
statusCode: pluginRegisterRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.called();
|
||||
|
||||
await pluginClient.registerPlugin(PluginClientSpec.plugin);
|
||||
|
||||
const request: SCPluginRegisterRequest = {
|
||||
action: 'add',
|
||||
plugin: {
|
||||
address: PluginClientSpec.plugin.fullUrl,
|
||||
name: PluginClientSpec.plugin.name,
|
||||
requestSchema: PluginClientSpec.plugin.requestSchema,
|
||||
responseSchema: PluginClientSpec.plugin.responseSchema,
|
||||
route: PluginClientSpec.plugin.route,
|
||||
},
|
||||
};
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: request,
|
||||
headers: {},
|
||||
method: pluginRegisterRoute.method,
|
||||
url: new URL(`http://localhost${pluginRegisterRoute.getUrlFragment()}`),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async unregisterPlugin() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCPluginRegisterResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
success: true,
|
||||
},
|
||||
headers: {},
|
||||
statusCode: pluginRegisterRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.called();
|
||||
|
||||
await pluginClient.unregisterPlugin(PluginClientSpec.plugin);
|
||||
|
||||
const request: SCPluginRegisterRequest = {
|
||||
action: 'remove',
|
||||
route: PluginClientSpec.plugin.route,
|
||||
};
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: request,
|
||||
headers: {},
|
||||
method: pluginRegisterRoute.method,
|
||||
url: new URL(`http://localhost${pluginRegisterRoute.getUrlFragment()}`),
|
||||
});
|
||||
}
|
||||
}
|
||||
141
test/plugin.spec.ts
Normal file
141
test/plugin.spec.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {Converter} from '@openstapps/core-tools/lib/schema';
|
||||
import * as chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import * as chaiSpies from 'chai-spies';
|
||||
import {readFileSync} from 'fs';
|
||||
import {suite, test, timeout} from 'mocha-typescript';
|
||||
import {resolve} from 'path';
|
||||
import {HttpClient} from '../src/http-client';
|
||||
import {TestPlugin} from './TestPlugin';
|
||||
|
||||
chai.use(chaiSpies);
|
||||
|
||||
process.on('unhandledRejection', (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
const httpClient = new HttpClient();
|
||||
|
||||
@suite(timeout(20000))
|
||||
export class PluginSpec {
|
||||
static testPlugin: TestPlugin;
|
||||
|
||||
static async after() {
|
||||
PluginSpec.testPlugin.close();
|
||||
}
|
||||
|
||||
static async before() {
|
||||
PluginSpec.testPlugin = new TestPlugin(4000, '', '', '', '', {
|
||||
getSchema: () => {/***/
|
||||
},
|
||||
} as any, '', '', '');
|
||||
}
|
||||
|
||||
async after() {
|
||||
sandbox.restore();
|
||||
}
|
||||
|
||||
@test
|
||||
async construct() {
|
||||
const converter = new Converter(__dirname);
|
||||
|
||||
sandbox.on(converter, 'getSchema', (schemaName) => {
|
||||
return {id: schemaName};
|
||||
});
|
||||
|
||||
const constructTestPlugin = new TestPlugin(
|
||||
4001,
|
||||
'A',
|
||||
'http://B',
|
||||
'/C', // this doesn't matter for our tests, it's only something that affects the backend
|
||||
'http://D',
|
||||
// @ts-ignore fake converter is not a converter
|
||||
converter,
|
||||
'PluginTestRequest',
|
||||
'PluginTestResponse',
|
||||
JSON.parse(readFileSync(resolve(__dirname, '..', 'package.json')).toString()).version,
|
||||
);
|
||||
expect(constructTestPlugin.port).to.be.equal(4001);
|
||||
expect(constructTestPlugin.name).to.be.equal('A');
|
||||
expect(constructTestPlugin.url).to.be.equal('http://B');
|
||||
expect(constructTestPlugin.route).to.be.equal('/C');
|
||||
// @ts-ignore backendUrl is protected
|
||||
expect(constructTestPlugin.backendUrl).to.be.equal('http://D');
|
||||
// schemas are already covered, together with the directory and version
|
||||
// @ts-ignore active is private
|
||||
expect(constructTestPlugin.active).to.be.equal(false);
|
||||
expect(constructTestPlugin.requestSchema.id).to.be.equal('PluginTestRequest');
|
||||
expect(constructTestPlugin.responseSchema.id).to.be.equal('PluginTestResponse');
|
||||
|
||||
sandbox.on(constructTestPlugin, 'onRouteInvoke');
|
||||
await httpClient.request({
|
||||
url: new URL('http://localhost:4001'),
|
||||
});
|
||||
// onRouteInvoke is a protected method, but we need to access it from the outside to test it
|
||||
// @ts-ignore
|
||||
expect(constructTestPlugin.onRouteInvoke).not.to.have.been.called();
|
||||
|
||||
await constructTestPlugin.close();
|
||||
sandbox.restore(constructTestPlugin, 'onRouteInvoke');
|
||||
}
|
||||
|
||||
@test
|
||||
async fullUrl() {
|
||||
const constructTestPlugin = new TestPlugin(4001, '', 'http://B', '', '', {
|
||||
getSchema: () => {/***/
|
||||
},
|
||||
} as any, '', '', '');
|
||||
expect(constructTestPlugin.fullUrl).to.be.equal('http://B:4001');
|
||||
await constructTestPlugin.close();
|
||||
}
|
||||
|
||||
@test
|
||||
async start() {
|
||||
PluginSpec.testPlugin.start();
|
||||
|
||||
sandbox.on(PluginSpec.testPlugin, 'onRouteInvoke');
|
||||
|
||||
await httpClient.request({
|
||||
url: new URL('http://localhost:4000'),
|
||||
});
|
||||
|
||||
// onRouteInvoke is a protected method, but we need to access it from the outside to test it
|
||||
// @ts-ignore
|
||||
expect(PluginSpec.testPlugin.onRouteInvoke).to.have.been.called();
|
||||
}
|
||||
|
||||
@test
|
||||
async stop() {
|
||||
// simulate a normal use case by first starting the plugin and then stopping it
|
||||
PluginSpec.testPlugin.start();
|
||||
PluginSpec.testPlugin.stop();
|
||||
|
||||
sandbox.on(PluginSpec.testPlugin, 'onRouteInvoke');
|
||||
|
||||
const response = await httpClient.request({
|
||||
url: new URL('http://localhost:4000'),
|
||||
});
|
||||
|
||||
await expect(response.statusCode).to.be.equal(404);
|
||||
// onRouteInvoke is a protected method, but we need to access it from the outside to test it
|
||||
// @ts-ignore
|
||||
expect(PluginSpec.testPlugin.onRouteInvoke).not.to.have.been.called();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user