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

107 lines
3.6 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/>.
*/
import {
SCPluginAdd,
SCPluginAlreadyRegisteredErrorResponse,
SCPluginRemove,
SCNotFoundErrorResponse,
} 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 throw a "not found" error when removing a plugin whose route doesn\'t exist'() {
// register one plugin
await pluginRegisterHandler(registerAddRequest, {});
return pluginRegisterHandler({...registerRemoveRequest, route: '/not-foo'}, {})
.should.eventually.be.rejectedWith('Resource not found')
.and.be.an.instanceOf(SCNotFoundErrorResponse);
}
}