Resolve "Transition to ESLint"

This commit is contained in:
Thea Schöbl
2022-06-27 14:40:09 +00:00
committed by Rainer Killinger
parent ca1d2444e0
commit 418ba67d15
47 changed files with 1854 additions and 1634 deletions

View File

@@ -1,3 +1,4 @@
/* eslint-disable unicorn/consistent-function-scoping,@typescript-eslint/no-explicit-any */
/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify
@@ -13,11 +14,7 @@
* 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 {
SCInternalServerErrorResponse,
SCPluginMetaData,
SCValidationErrorResponse
} from '@openstapps/core';
import {SCInternalServerErrorResponse, SCPluginMetaData, SCValidationErrorResponse} from '@openstapps/core';
import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {Request} from 'express';
@@ -41,16 +38,17 @@ describe('Virtual plugin routes', async function () {
/**
* Internal method which provides information about the specific error inside of an internal server error
*
* @param req Express request
* @param request Express request
* @param plugin Plugin information (metadata)
* @param specificError Class of a specific error
*/
async function testRejection(req: Request, plugin: SCPluginMetaData, specificError: object) {
async function testRejection(request: Request, plugin: SCPluginMetaData, specificError: object) {
// eslint-disable-next-line unicorn/error-message
let thrownError: Error = new Error();
try {
await virtualPluginRoute(req, plugin);
} catch (e) {
thrownError = e;
await virtualPluginRoute(request, plugin);
} catch (error) {
thrownError = error;
}
// return virtualPluginRoute(req, plugin).should.be.rejectedWith(SCInternalServerErrorResponse); was not working for some reason
expect(thrownError).to.be.instanceOf(SCInternalServerErrorResponse);
@@ -71,17 +69,17 @@ describe('Virtual plugin routes', async function () {
},
};
// spy the post method of got
// @ts-ignore
// @ts-expect-error not assignable
const gotStub = sandbox.stub(got, 'post').returns({body: {}});
// @ts-ignore
// @ts-expect-error not assignable
sandbox.stub(validator, 'validate').returns({errors: []});
const req = mockReq(request);
const request_ = mockReq(request);
await virtualPluginRoute(req, plugin);
await virtualPluginRoute(request_, plugin);
expect(gotStub.args[0][0]).to.equal(plugin.route.substr(1));
expect(gotStub.args[0][0]).to.equal(plugin.route.slice(1));
expect(((gotStub.args[0] as any)[1] as Options).prefixUrl).to.equal(plugin.address);
expect(((gotStub.args[0] as any)[1] as Options).json).to.equal(req.body);
expect(((gotStub.args[0] as any)[1] as Options).json).to.equal(request_.body);
});
it('should provide data from the plugin when its route is called', async function () {
@@ -91,18 +89,13 @@ describe('Virtual plugin routes', async function () {
},
};
const response = {
result: [
{foo: 'bar'},
{bar: 'foo'},
]
}
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);
nock('http://foo.com:1234').post('/foo').reply(200, response);
const request_ = mockReq(request);
expect(await virtualPluginRoute(req, plugin)).to.eql(response);
expect(await virtualPluginRoute(request_, plugin)).to.eql(response);
});
it('should throw the validation error if request is not valid', async function () {
@@ -111,9 +104,9 @@ describe('Virtual plugin routes', async function () {
invalid_query_field: 'foo',
},
};
const req = mockReq(request);
const request_ = mockReq(request);
await testRejection(req, plugin, SCValidationErrorResponse);
await testRejection(request_, plugin, SCValidationErrorResponse);
});
it('should throw the validation error if response is not valid', async function () {
@@ -126,9 +119,9 @@ describe('Virtual plugin routes', async function () {
nock('http://foo.com:1234')
.post('/foo')
.reply(200, {invalid_result: ['foo bar']});
const req = mockReq(request);
const request_ = mockReq(request);
await testRejection(req, plugin, SCValidationErrorResponse);
await testRejection(request_, plugin, SCValidationErrorResponse);
});
it('should throw error if there is a problem with reaching the address of a plugin', async function () {
@@ -139,13 +132,12 @@ describe('Virtual plugin routes', async function () {
};
// fake that post method of got throws an error
sandbox.stub(got, 'post')
.callsFake(() => {
throw new FooError();
});
const req = mockReq(request);
sandbox.stub(got, 'post').callsFake(() => {
throw new FooError();
});
const request_ = mockReq(request);
await testRejection(req, plugin, FooError);
await testRejection(request_, plugin, FooError);
});
});
@@ -157,7 +149,7 @@ describe('Virtual plugin routes', async function () {
const OK = 200;
const internalServerError = new SCInternalServerErrorResponse();
afterEach(async function() {
afterEach(async function () {
// remove routes
plugins.clear();
// // restore everything to default methods (remove stubs)
@@ -194,16 +186,15 @@ describe('Virtual plugin routes', async function () {
expect(barResponse.body).to.be.deep.equal({result: [{foo: 'bar'}, {bar: 'bar'}]});
});
it('should return error response if plugin address is not responding', async function() {
it('should return error response if plugin address is not responding', async function () {
// lets simulate that the plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
class FooError extends Error {}
// fake that got's post method throws an error
sandbox.stub(got, 'post')
.callsFake(() => {
throw new FooError();
});
sandbox.stub(got, 'post').callsFake(() => {
throw new FooError();
});
const {status} = await testApp
.post('/foo')
@@ -214,7 +205,7 @@ describe('Virtual plugin routes', async function () {
expect(status).to.be.equal(internalServerError.statusCode);
});
it('should return the validation error response if plugin request is not valid', async function() {
it('should return the validation error response if plugin request is not valid', async function () {
// lets simulate that the plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
@@ -223,13 +214,13 @@ describe('Virtual plugin routes', async function () {
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
// using number for query instead of (in request schema) required text
.send({query: 123})
.send({query: 123});
expect(status).to.be.equal(502);
expect(body.additionalData).to.haveOwnProperty('name','ValidationError');
expect(body.additionalData).to.haveOwnProperty('name', 'ValidationError');
});
it('should return the validation error response if plugin response is not valid', async function() {
it('should return the validation error response if plugin response is not valid', async function () {
// lets simulate that the plugin is already registered
plugins.set(registerAddRequest.plugin.route, registerAddRequest.plugin);
// mock response of the plugin address
@@ -244,7 +235,7 @@ describe('Virtual plugin routes', async function () {
.send({query: 'foo'});
expect(status).to.be.equal(internalServerError.statusCode);
expect(body.additionalData).to.haveOwnProperty('name','ValidationError');
expect(body.additionalData).to.haveOwnProperty('name', 'ValidationError');
});
});
});