test: add tests for routes

This commit is contained in:
Jovan Krunić
2020-09-28 11:12:56 +02:00
committed by Rainer Killinger
parent 751693bebc
commit d3955b3cdd
22 changed files with 1315 additions and 455 deletions

View File

@@ -13,95 +13,183 @@
* 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/>.
*/
// tslint:disable
import {
SCPluginAdd,
SCPluginAlreadyRegisteredErrorResponse,
SCPluginRemove,
SCNotFoundErrorResponse,
SCPluginAdd,
SCPluginAlreadyRegisteredErrorResponse, SCPluginRegisterResponse, SCPluginRegisterRoute,
SCPluginRemove, SCValidationErrorResponse,
} from '@openstapps/core';
import {should, use} from 'chai';
import {slow, timeout, test, suite} from 'mocha-typescript';
import chaiAsPromised from 'chai-as-promised';
import nock from 'nock';
import {plugins} from '../../src/common';
import {pluginRegisterHandler} from '../../src/routes/plugin-register-route';
import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {instance as registerRequest} from '@openstapps/core/test/resources/PluginRegisterRequest.1.json';
import {DEFAULT_TEST_TIMEOUT} from '../common';
import {testApp} from '../tests-setup';
should();
// for using promises in expectations (to.eventually.be...)
use(chaiAsPromised);
export const registerAddRequest: SCPluginAdd =
require('../../node_modules/@openstapps/core/test/resources/PluginRegisterRequest.1.json')
.instance;
// cast it because of "TS2322: Type 'string' is not assignable to type '"add"'"
export const registerAddRequest: SCPluginAdd = registerRequest as SCPluginAdd;
export const registerRemoveRequest: SCPluginRemove = {
action: 'remove',
route: registerAddRequest.plugin.route
};
@suite(timeout(10000), slow(5000))
export class PluginRegisterRouteSpec {
after() {
// remove plugins
plugins.clear();
}
describe('Plugin registration', async function () {
const bodySuccess: SCPluginRegisterResponse = {success: true};
describe('Middleware', async function () {
after(function () {
// remove plugins
plugins.clear();
});
@test
async 'should register a plugin'() {
// register one plugin
const response = await pluginRegisterHandler(registerAddRequest, {});
it('should register a plugin', async function () {
// register one plugin
const response = await pluginRegisterHandler(registerAddRequest, {});
return response.should.eql({success: true})
&& plugins.size.should.equal(1);
}
expect(response).to.deep.equal(bodySuccess) && expect(plugins.size).to.equal(1);
});
@test
async 'should allow re-registering the same plugin'() {
// register one plugin
await pluginRegisterHandler(registerAddRequest, {});
it('should allow re-registering the same plugin', async function () {
// register one plugin
await pluginRegisterHandler(registerAddRequest, {});
// register the same plugin again
const responseReregister = await pluginRegisterHandler(registerAddRequest, {});
// register the same plugin again
const response = await pluginRegisterHandler(registerAddRequest, {});
return responseReregister.should.eql({success: true})
&& plugins.size.should.equal(1);
}
return expect(response).to.deep.equal(bodySuccess)
&& expect(plugins.size).to.equal(1);
});
@test
async 'should show an error if a plugin has already been registered'() {
// register one plugin
await pluginRegisterHandler(registerAddRequest, {});
it('should show an error if a plugin has already been registered', async function () {
// 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'}};
// create new request for adding a plugin with only name that changed
let registerAddRequestAltered: SCPluginAdd = {
...registerAddRequest,
plugin: {...registerAddRequest.plugin, name: registerAddRequest.plugin.name + 'foo'}
};
// 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);
}
// register the same plugin again
expect(pluginRegisterHandler(registerAddRequestAltered, {}))
.to.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, {});
it('should remove plugin if it exists', async function () {
// register one plugin
await pluginRegisterHandler(registerAddRequest, {});
plugins.size.should.equal(1);
expect(plugins.size).to.equal(1);
const response = await pluginRegisterHandler(registerRemoveRequest, {});
const response = await pluginRegisterHandler(registerRemoveRequest, {});
return response.should.eql({success: true})
&& plugins.size.should.equal(0);
}
expect(response).to.deep.equal(bodySuccess)
&& expect(plugins.size).to.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, {});
it('should throw a "not found" error when removing a plugin whose registered route does not exist', async function () {
// register one plugin
await pluginRegisterHandler(registerAddRequest, {});
return pluginRegisterHandler({...registerRemoveRequest, route: '/not-foo'}, {})
.should.eventually.be.rejectedWith('Resource not found')
.and.be.an.instanceOf(SCNotFoundErrorResponse);
}
}
expect(pluginRegisterHandler({...registerRemoveRequest, route: '/not-foo'}, {}))
.to.eventually.be.rejectedWith('Resource not found')
.and.be.an.instanceOf(SCNotFoundErrorResponse);
});
});
describe('Routes', async function () {
// increase timeout for the suite
this.timeout(DEFAULT_TEST_TIMEOUT);
const pluginRegisterRoute = new SCPluginRegisterRoute();
const validationError = new SCValidationErrorResponse([]);
const notFoundError = new SCNotFoundErrorResponse();
//@ts-ignore
const alreadyRegisteredError = new SCPluginAlreadyRegisteredErrorResponse('Foo Message', {});
afterEach(async function() {
// remove routes
plugins.clear();
// clean up request mocks (fixes issue with receiving response from mock from previous test case)
nock.cleanAll();
});
it('should provide "not found" (404) if plugin/route is not registered', async function () {
// 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'}]});
const {status} = await testApp
.post('/not-foo')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({query: 'foo'});
expect(status).to.be.equal(notFoundError.statusCode);
});
it('should respond with bad request if when providing invalid request', async function () {
const {status} = await testApp
.post(pluginRegisterRoute.urlFragment)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({foo: 'bar'});
expect(status).to.be.equal(validationError.statusCode);
});
it('should respond with an error if something went wrong with a valid request', async function () {
// 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'}};
const {status, body} = await testApp
.post(pluginRegisterRoute.urlFragment)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(registerAddRequestAltered);
expect(status).to.be.equal(alreadyRegisteredError.statusCode);
expect(body).to.haveOwnProperty('name', 'SCPluginAlreadyRegisteredError');
});
it('should respond with success when de-registering already registered plugin', async function() {
// lets simulate that a plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
const {status, body} = await testApp
.post('/plugin/register')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(registerRemoveRequest);
expect(status).to.be.equal(new SCPluginRegisterRoute().statusCodeSuccess);
expect(body).to.be.deep.equal(bodySuccess);
});
it('should response with 404 when deleting a plugin which was not registered', async function() {
// lets simulate that the plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
const {status} = await testApp
.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(status).to.be.equal(notFoundError.statusCode);
});
});
});