Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validateBooleanMiddleware for controllers #1023

Merged
merged 11 commits into from
Sep 13, 2022
4 changes: 2 additions & 2 deletions src/controllers/accounts/AccountsBalanceInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';
import { IAddressParam } from 'src/types/requests';

import { validateAddress } from '../../middleware';
import { validateAddress, validateBoolean } from '../../middleware';
import { AccountsBalanceInfoService } from '../../services';
import AbstractController from '../AbstractController';

Expand Down Expand Up @@ -66,7 +66,7 @@ export default class AccountsBalanceController extends AbstractController<Accoun
}

protected initRoutes(): void {
this.router.use(this.path, validateAddress);
this.router.use(this.path, validateAddress, validateBoolean);

this.safeMountAsyncGetHandlers([['', this.getAccountBalanceInfo]]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import BN from 'bn.js';
import { RequestHandler } from 'express';
import { BadRequest, InternalServerError } from 'http-errors';

import { validateAddress } from '../../middleware';
import { validateAddress, validateBoolean } from '../../middleware';
import { AccountsStakingPayoutsService } from '../../services';
import { IAddressParam } from '../../types/requests';
import AbstractController from '../AbstractController';
Expand Down Expand Up @@ -87,7 +87,7 @@ export default class AccountsStakingPayoutsController extends AbstractController
}

protected initRoutes(): void {
this.router.use(this.path, validateAddress);
this.router.use(this.path, validateAddress, validateBoolean);

this.safeMountAsyncGetHandlers([['', this.getStakingPayoutsByAccountId]]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/blocks/BlocksController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { RequestHandler } from 'express';
import { BadRequest } from 'http-errors';
import LRU from 'lru-cache';

import { validateBoolean } from '../../middleware/validate';
import { BlocksService } from '../../services';
import { INumberParam, IRangeQueryParam } from '../../types/requests';
import { IBlock } from '../../types/responses';
Expand Down Expand Up @@ -102,6 +103,7 @@ export default class BlocksController extends AbstractController<BlocksService>
}

protected initRoutes(): void {
this.router.use(this.path, validateBoolean);
this.safeMountAsyncGetHandlers([
['/', this.getBlocks],
['/head', this.getLatestBlock],
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/blocks/BlocksTraceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express-serve-static-core';

import { validateBoolean } from '../../middleware';
import { BlocksTraceService } from '../../services';
import AbstractController from '../AbstractController';
import BlocksController from './BlocksController';
Expand All @@ -28,6 +29,7 @@ export default class BlocksTraceController extends AbstractController<BlocksTrac
}

protected initRoutes(): void {
this.router.use(this.path, validateBoolean);
this.safeMountAsyncGetHandlers([
['/head/traces', this.getLatestBlockTraces],
['/:number/traces', this.getBlockTraces],
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/node/NodeTransactionPoolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';

import { validateBoolean } from '../../middleware';
import { NodeTransactionPoolService } from '../../services';
import AbstractController from '../AbstractController';

Expand All @@ -39,6 +40,7 @@ export default class NodeTransactionPoolController extends AbstractController<No
}

protected initRoutes(): void {
this.router.use(this.path, validateBoolean);
this.safeMountAsyncGetHandlers([['', this.getNodeTransactionPool]]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/controllers/pallets/PalletsStorageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ApiPromise } from '@polkadot/api';
import { stringCamelCase } from '@polkadot/util';
import { RequestHandler } from 'express-serve-static-core';

import { validateBoolean } from '../..//middleware';
import { Log } from '../../logging/Log';
import { PalletsStorageService } from '../../services';
import AbstractController from '../AbstractController';
Expand Down Expand Up @@ -46,7 +47,7 @@ export default class PalletsStorageController extends AbstractController<Pallets
}

protected initRoutes(): void {
// TODO look into middleware validation of in path IDs. https://github.com/paritytech/substrate-api-sidecar/issues/281
this.router.use(this.path, validateBoolean);
this.safeMountAsyncGetHandlers([
['/:storageItemId', this.getStorageItem],
['/', this.getStorage],
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/paras/ParasController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';

import { validateBoolean } from '../../middleware';
import { ParasService } from '../../services';
import { IParaIdParam } from '../../types/requests';
import AbstractController from '../AbstractController';
Expand All @@ -28,6 +29,7 @@ export default class ParasController extends AbstractController<ParasService> {
}

protected initRoutes(): void {
this.router.use(this.path + '/paras/leases/current', validateBoolean);
this.safeMountAsyncGetHandlers([
['/paras', this.getParas],
['/paras/crowdloans', this.getCrowdloans],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';

import { Log } from '../../logging/Log';
import { validateBoolean } from '../../middleware';
import { TransactionMaterialService } from '../../services';
import AbstractController from '../AbstractController';

Expand Down Expand Up @@ -60,6 +61,7 @@ export default class TransactionMaterialController extends AbstractController<Tr
}

protected initRoutes(): void {
this.router.use(this.path, validateBoolean);
this.safeMountAsyncGetHandlers([['', this.getTransactionMaterial]]);
}

Expand Down
1 change: 1 addition & 0 deletions src/middleware/validate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

export { validateAddressMiddleware as validateAddress } from './validateAddressMiddleware';
export { validateBooleanMiddleware as validateBoolean } from './validateBooleanMiddleware';
60 changes: 60 additions & 0 deletions src/middleware/validate/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { Request, Response } from 'express';
import { RequestHandler } from 'express-serve-static-core';

/**
* Assert that a middleware does not error with a the given `req`.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
*
* @param name thing it does not error on
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
* @param req Express Request containing thing it errors on
*/
export const doesNotErrorWith = (
name: string,
req: Request,
middleware: RequestHandler
): void => {
it(`does not error with ${name}`, () => {
const next = jest.fn();
middleware(req, null as unknown as Response, next);
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith();
});
};

/**
* Assert that a middleware passes `err` to next with the given
* `req`.
*
* @param name thing it errors on
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
* @param req Express Request containing thing it errors on
* @param err expected error that it passes to next
*/
export const errorsWith = (
name: string,
req: Request,
err: unknown,
middleware: RequestHandler
): void => {
it(`errors with ${name}`, () => {
const next = jest.fn();

middleware(req, null as unknown as Response, next);
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith(err);
});
};
Loading