-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validateBooleanMiddleware for controllers (#1023)
* 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
Showing
13 changed files
with
315 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; |
Oops, something went wrong.