mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +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
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user