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

@@ -55,14 +55,11 @@ describe('Bulk routes', async function () {
});
it('should return (throw) error if a bulk with the provided UID cannot be found when adding to a bulk', async function () {
await testApp
.post(bulkRoute.urlPath)
.set('Content-Type', 'application/json')
.send(request);
const bulkAddRouteurlPath = bulkAddRoute.urlPath.toLocaleLowerCase().replace(':uid', 'a-wrong-uid');
await testApp.post(bulkRoute.urlPath).set('Content-Type', 'application/json').send(request);
const bulkAddRouteUrlPath = bulkAddRoute.urlPath.toLocaleLowerCase().replace(':uid', 'a-wrong-uid');
const {status} = await testApp
.post(bulkAddRouteurlPath)
.post(bulkAddRouteUrlPath)
.set('Content-Type', 'application/json')
.send(book);
@@ -86,10 +83,7 @@ describe('Bulk routes', async function () {
});
it('should return (throw) error if a bulk with the provided UID cannot be found when closing a bulk (done)', async function () {
await testApp
.post(bulkRoute.urlPath)
.set('Content-Type', 'application/json')
.send(request);
await testApp.post(bulkRoute.urlPath).set('Content-Type', 'application/json').send(request);
const bulkDoneRouteurlPath = bulkDoneRoute.urlPath.toLocaleLowerCase().replace(':uid', 'a-wrong-uid');
const {status} = await testApp
@@ -100,7 +94,7 @@ describe('Bulk routes', async function () {
expect(status).to.be.equal(new SCNotFoundErrorResponse().statusCode);
});
it ('should close the bulk (done)', async function () {
it('should close the bulk (done)', async function () {
const response = await testApp
.post(bulkRoute.urlPath)
.set('Content-Type', 'application/json')

View File

@@ -16,8 +16,11 @@
import {
SCNotFoundErrorResponse,
SCPluginAdd,
SCPluginAlreadyRegisteredErrorResponse, SCPluginRegisterResponse, SCPluginRegisterRoute,
SCPluginRemove, SCValidationErrorResponse,
SCPluginAlreadyRegisteredErrorResponse,
SCPluginRegisterResponse,
SCPluginRegisterRoute,
SCPluginRemove,
SCValidationErrorResponse,
} from '@openstapps/core';
import nock from 'nock';
import {configFile, plugins} from '../../src/common';
@@ -36,7 +39,7 @@ export const registerAddRequest: SCPluginAdd = registerRequest as SCPluginAdd;
export const registerRemoveRequest: SCPluginRemove = {
action: 'remove',
route: registerAddRequest.plugin.route
route: registerAddRequest.plugin.route,
};
describe('Plugin registration', async function () {
@@ -52,9 +55,9 @@ describe('Plugin registration', async function () {
// register one plugin
const response = await pluginRegisterHandler(registerAddRequest, {});
expect(response).to.deep.equal(bodySuccess)
&& expect(plugins.size).to.equal(1)
&& expect(configFile.app.features.plugins!['Foo Plugin']).to.not.be.empty;
expect(response).to.deep.equal(bodySuccess) &&
expect(plugins.size).to.equal(1) &&
expect(configFile.app.features.plugins!['Foo Plugin']).to.not.be.empty;
});
it('should allow re-registering the same plugin', async function () {
@@ -64,9 +67,11 @@ describe('Plugin registration', async function () {
// register the same plugin again
const response = await pluginRegisterHandler(registerAddRequest, {});
return expect(response).to.deep.equal(bodySuccess)
&& expect(plugins.size).to.equal(1)
&& expect(configFile.app.features.plugins!['Foo Plugin']).to.not.be.empty;
return (
expect(response).to.deep.equal(bodySuccess) &&
expect(plugins.size).to.equal(1) &&
expect(configFile.app.features.plugins!['Foo Plugin']).to.not.be.empty
);
});
it('should show an error if a plugin has already been registered', async function () {
@@ -74,9 +79,9 @@ describe('Plugin registration', async function () {
await pluginRegisterHandler(registerAddRequest, {});
// create new request for adding a plugin with only name that changed
let registerAddRequestAltered: SCPluginAdd = {
const registerAddRequestAltered: SCPluginAdd = {
...registerAddRequest,
plugin: {...registerAddRequest.plugin, name: registerAddRequest.plugin.name + 'foo'}
plugin: {...registerAddRequest.plugin, name: registerAddRequest.plugin.name + 'foo'},
};
// register the same plugin again
@@ -95,9 +100,9 @@ describe('Plugin registration', async function () {
const response = await pluginRegisterHandler(registerRemoveRequest, {});
expect(response).to.deep.equal(bodySuccess)
&& expect(plugins.size).to.equal(0)
&& expect(configFile.app.features.plugins).to.be.empty;
expect(response).to.deep.equal(bodySuccess) &&
expect(plugins.size).to.equal(0) &&
expect(configFile.app.features.plugins).to.be.empty;
});
it('should throw a "not found" error when removing a plugin whose registered route does not exist', async function () {
@@ -116,10 +121,10 @@ describe('Plugin registration', async function () {
const pluginRegisterRoute = new SCPluginRegisterRoute();
const validationError = new SCValidationErrorResponse([]);
const notFoundError = new SCNotFoundErrorResponse();
//@ts-ignore
// @ts-expect-error not assignable
const alreadyRegisteredError = new SCPluginAlreadyRegisteredErrorResponse('Foo Message', {});
afterEach(async function() {
afterEach(async function () {
// remove routes
plugins.clear();
// clean up request mocks (fixes issue with receiving response from mock from previous test case)
@@ -157,7 +162,10 @@ describe('Plugin registration', 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 registerAddRequestAltered = {
...registerAddRequest,
plugin: {...registerAddRequest.plugin, name: 'FooBar Plugin'},
};
const {status, body} = await testApp
.post(pluginRegisterRoute.urlPath)
@@ -169,7 +177,7 @@ describe('Plugin registration', async function () {
expect(body).to.haveOwnProperty('name', 'SCPluginAlreadyRegisteredError');
});
it('should respond with success when de-registering already registered plugin', async function() {
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);
@@ -183,7 +191,7 @@ describe('Plugin registration', async function () {
expect(body).to.be.deep.equal(bodySuccess);
});
it('should response with 404 when deleting a plugin which was not registered', async function() {
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);

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
* Copyright (C) 2020 StApps
* This program is free software: you can redistribute it and/or modify
@@ -17,7 +18,8 @@ import {
SCInternalServerErrorResponse,
SCMethodNotAllowedErrorResponse,
SCRoute,
SCRouteHttpVerbs, SCValidationErrorResponse,
SCRouteHttpVerbs,
SCValidationErrorResponse,
} from '@openstapps/core';
import * as bodyParser from 'body-parser';
import sinon from 'sinon';
@@ -31,15 +33,16 @@ import {Logger} from '@openstapps/logger';
import {DEFAULT_TEST_TIMEOUT} from '../common';
interface ReturnType {
foo: boolean;
foo: boolean;
}
describe('Create route', async function () {
let routeClass: SCRoute;
let handler: (
validatedBody: any,
app: Application, params?: { [parameterName: string]: string; },
) => Promise<ReturnType>;
validatedBody: any,
app: Application,
parameters?: {[parameterName: string]: string},
) => Promise<ReturnType>;
let app: Express;
const statusCodeSuccess = 222;
const bodySuccess = {foo: true};
@@ -75,10 +78,12 @@ describe('Create route', async function () {
it('should complain (throw an error) if used method is other than defined in the route creation', async function () {
const methodNotAllowedError = new SCMethodNotAllowedErrorResponse();
// @ts-ignore
sandbox.stub(validator, 'validate').returns({errors: []})
// @ts-expect-error not assignable
sandbox.stub(validator, 'validate').returns({errors: []});
let error: any = {};
sandbox.stub(Logger, 'warn').callsFake((err) => { error = err });
sandbox.stub(Logger, 'warn').callsFake(error_ => {
error = error_;
});
const router = createRoute<any, any>(routeClass, handler);
await app.use(router);
@@ -92,14 +97,12 @@ describe('Create route', async function () {
});
it('should provide a route which returns handler response and success code', async function () {
// @ts-ignore
// @ts-expect-error not assignable
sandbox.stub(validator, 'validate').returns({errors: []});
const router = createRoute<any, any>(routeClass, handler);
app.use(router);
const response = await supertest(app)
.post(routeClass.urlPath)
.send();
const response = await supertest(app).post(routeClass.urlPath).send();
expect(response.status).to.be.equal(statusCodeSuccess);
expect(response.body).to.be.deep.equal(bodySuccess);
@@ -112,7 +115,7 @@ describe('Create route', async function () {
app.use(router);
const startApp = supertest(app);
const validatorStub = sandbox.stub(validator, 'validate');
// @ts-ignore
// @ts-expect-error not assignable
validatorStub.withArgs(body, routeClass.requestBodyName).returns({errors: [new Error('Foo Error')]});
const response = await startApp
@@ -128,14 +131,14 @@ describe('Create route', async function () {
const router = createRoute<any, any>(routeClass, handler);
await app.use(router);
const startApp = supertest(app);
// @ts-ignore
const validatorStub = sandbox.stub(validator, 'validate').returns({errors:[]});
// @ts-ignore
validatorStub.withArgs(bodySuccess, routeClass.responseBodyName).returns({errors: [new Error('Foo Error')]});
// @ts-expect-error not assignable
const validatorStub = sandbox.stub(validator, 'validate').returns({errors: []});
validatorStub
.withArgs(bodySuccess, routeClass.responseBodyName)
// @ts-expect-error not assignable
.returns({errors: [new Error('Foo Error')]});
const response = await startApp
.post(routeClass.urlPath)
.send();
const response = await startApp.post(routeClass.urlPath).send();
expect(response.status).to.be.equal(internalServerError.statusCode);
});
@@ -143,8 +146,11 @@ describe('Create route', async function () {
it('should return internal server error if error response not allowed', async function () {
class FooErrorResponse {
statusCode: number;
name: string;
message: string;
constructor(statusCode: number, name: string, message: string) {
this.statusCode = statusCode;
this.name = name;
@@ -153,6 +159,7 @@ describe('Create route', async function () {
}
class BarErrorResponse {
statusCode: number;
constructor(statusCode: number) {
this.statusCode = statusCode;
}
@@ -170,12 +177,10 @@ describe('Create route', async function () {
await app.use(router);
const startApp = supertest(app);
// @ts-ignore
sandbox.stub(validator, 'validate').returns({errors:[]});
// @ts-expect-error not assignable
sandbox.stub(validator, 'validate').returns({errors: []});
const response = await startApp
.post(routeClass.urlPath)
.send();
const response = await startApp.post(routeClass.urlPath).send();
expect(response.status).to.be.equal(internalServerError.statusCode);
});
@@ -183,8 +188,11 @@ describe('Create route', async function () {
it('should return the exact error if error response is allowed', async function () {
class FooErrorResponse {
statusCode: number;
name: string;
message: string;
constructor(statusCode: number, name: string, message: string) {
this.statusCode = statusCode;
this.name = name;
@@ -205,12 +213,10 @@ describe('Create route', async function () {
await app.use(router);
const startApp = supertest(app);
// @ts-ignore
sandbox.stub(validator, 'validate').returns({errors:[]});
// @ts-expect-error not assignable
sandbox.stub(validator, 'validate').returns({errors: []});
const response = await startApp
.post(routeClass.urlPath)
.send();
const response = await startApp.post(routeClass.urlPath).send();
expect(response.status).to.be.equal(fooErrorResponse.statusCode);
});

View File

@@ -18,7 +18,7 @@ import {
SCMultiSearchRoute,
SCSearchRoute,
SCSyntaxErrorResponse,
SCTooManyRequestsErrorResponse
SCTooManyRequestsErrorResponse,
} from '@openstapps/core';
import {expect} from 'chai';
import {configFile} from '../../src/common';
@@ -37,33 +37,24 @@ describe('Search route', async function () {
it('should reject GET, PUT with a valid search query', async function () {
// const expectedParams = JSON.parse(JSON.stringify(defaultParams));
const {status} = await testApp
.get('/search')
.set('Accept', 'application/json')
.send({
query: 'Some search terms'
});
const {status} = await testApp.get('/search').set('Accept', 'application/json').send({
query: 'Some search terms',
});
expect(status).to.equal(methodNotAllowedError.statusCode);
});
describe('Basic POST /search', async function() {
describe('Basic POST /search', async function () {
it('should accept empty JSON object', async function () {
const {status} = await testApp
.post(searchRoute.urlPath)
.set('Accept', 'application/json')
.send({});
const {status} = await testApp.post(searchRoute.urlPath).set('Accept', 'application/json').send({});
expect(status).to.equal(searchRoute.statusCodeSuccess);
});
it('should accept valid search request', async function () {
const {status} = await testApp
.post(searchRoute.urlPath)
.set('Accept', 'application/json')
.send({
query: 'Some search terms'
});
const {status} = await testApp.post(searchRoute.urlPath).set('Accept', 'application/json').send({
query: 'Some search terms',
});
expect(status).to.equal(searchRoute.statusCodeSuccess);
});
@@ -74,14 +65,14 @@ describe('Search route', async function () {
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({
some: {invalid: 'search'}
some: {invalid: 'search'},
});
expect(status).to.equal(syntaxError.statusCode);
});
});
describe('Basic POST /multi/search', async function() {
describe('Basic POST /multi/search', async function () {
it('should respond with bad request on invalid search request (empty JSON object as body)', async function () {
const {status} = await testApp
.post(multiSearchRoute.urlPath)
@@ -96,8 +87,8 @@ describe('Search route', async function () {
.post(multiSearchRoute.urlPath)
.set('Accept', 'application/json')
.send({
one: { query: 'Some search terms for one search'},
two: { query: 'Some search terms for another search'}
one: {query: 'Some search terms for one search'},
two: {query: 'Some search terms for another search'},
});
expect(status).to.equal(multiSearchRoute.statusCodeSuccess);

View File

@@ -28,7 +28,8 @@ describe('Thing update route', async function () {
const thingUpdateRoute = new SCThingUpdateRoute();
it('should update a thing', async function () {
const thingUpdateRouteurlPath = thingUpdateRoute.urlPath.toLocaleLowerCase()
const thingUpdateRouteurlPath = thingUpdateRoute.urlPath
.toLocaleLowerCase()
.replace(':type', book.type)
.replace(':uid', book.uid);

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');
});
});
});