Skip to content

Commit

Permalink
feat: validateBooleanMiddleware for controllers (#1023)
Browse files Browse the repository at this point in the history
* add utility testing functions to util file

* refactor address middleware tests with util functions

* add validateBooleanMiddleware

* validateBooleanMiddleware tests

* export middleware

* add validateBooleanMiddleware to controllers that use it

* fix inline comments

* remove hardcoded values

* fix test

* refactor middleware to be more efficient

* fix query check
  • Loading branch information
TarikGul committed Sep 13, 2022
1 parent d67d3a5 commit fc74d4a
Show file tree
Hide file tree
Showing 13 changed files with 315 additions and 84 deletions.
8 changes: 6 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,11 @@ export default class AccountsBalanceController extends AbstractController<Accoun
}

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

this.safeMountAsyncGetHandlers([['', this.getAccountBalanceInfo]]);
}
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/accounts/AccountsStakingPayoutsController.ts
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,11 @@ export default class AccountsStakingPayoutsController extends AbstractController
}

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

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

import { validateBoolean } from '../../middleware/validate';
import { BlocksService } from '../../services';
import { ControllerOptions } from '../../types/chains-config';
import { INumberParam, IRangeQueryParam } from '../../types/requests';
Expand Down Expand Up @@ -101,6 +102,10 @@ export default class BlocksController extends AbstractController<BlocksService>
}

protected initRoutes(): void {
this.router.use(
this.path,
validateBoolean(['eventDocs', 'extrinsicDocs', 'finalized'])
);
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(['actions']));
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(['includeFee']));
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(['adjustMetadataV13']));
this.safeMountAsyncGetHandlers([
['/:storageItemId', this.getStorageItem],
['/', this.getStorage],
Expand Down
5 changes: 5 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,10 @@ export default class ParasController extends AbstractController<ParasService> {
}

protected initRoutes(): void {
this.router.use(
this.path + '/paras/leases/current',
validateBoolean(['currentLeaseHolders'])
);
this.safeMountAsyncGetHandlers([
['/paras', this.getParas],
['/paras/crowdloans', this.getCrowdloans],
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/transaction/TransactionMaterialController.ts
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(['noMeta']));
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 the given request.
*
* @param name String for tests to log.
* @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 String for tests to log.
* @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

0 comments on commit fc74d4a

Please sign in to comment.