Clarify param parser handling positive integers

This commit is contained in:
Maciej Ziarkowski 2020-01-31 15:35:34 +00:00
parent 297f7ccf5f
commit 928eed6671
4 changed files with 16 additions and 13 deletions

View File

@ -1,6 +1,6 @@
import express from 'express'; import express from 'express';
import { parseIntParam, processParam } from '../parameters'; import { parsePositiveIntParam, processParam } from '../parameters';
import asyncController from '../routes/asyncController'; import asyncController from '../routes/asyncController';
import * as buildingService from '../services/building'; import * as buildingService from '../services/building';
import * as userService from '../services/user'; import * as userService from '../services/user';
@ -35,7 +35,7 @@ const getBuildingsByReference = asyncController(async (req: express.Request, res
// GET individual building, POST building updates // GET individual building, POST building updates
const getBuildingById = asyncController(async (req: express.Request, res: express.Response) => { const getBuildingById = asyncController(async (req: express.Request, res: express.Response) => {
const buildingId = processParam(req.params, 'building_id', parseIntParam, true); const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
try { try {
const result = await buildingService.getBuildingById(buildingId); const result = await buildingService.getBuildingById(buildingId);
@ -63,7 +63,7 @@ const updateBuildingById = asyncController(async (req: express.Request, res: exp
}); });
async function updateBuilding(req: express.Request, res: express.Response, userId: string) { async function updateBuilding(req: express.Request, res: express.Response, userId: string) {
const buildingId = processParam(req.params, 'building_id', parseIntParam, true); const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
const buildingUpdate = req.body; const buildingUpdate = req.body;
@ -84,7 +84,7 @@ async function updateBuilding(req: express.Request, res: express.Response, userI
// GET building UPRNs // GET building UPRNs
const getBuildingUPRNsById = asyncController(async (req: express.Request, res: express.Response) => { const getBuildingUPRNsById = asyncController(async (req: express.Request, res: express.Response) => {
const buildingId = processParam(req.params, 'building_id', parseIntParam, true); const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
try { try {
const result = await buildingService.getBuildingUPRNsById(buildingId); const result = await buildingService.getBuildingUPRNsById(buildingId);
@ -105,7 +105,7 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
return res.send({ like: false }); // not logged in, so cannot have liked return res.send({ like: false }); // not logged in, so cannot have liked
} }
const buildingId = processParam(req.params, 'building_id', parseIntParam, true); const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
try { try {
const like = await buildingService.getBuildingLikeById(buildingId, req.session.user_id); const like = await buildingService.getBuildingLikeById(buildingId, req.session.user_id);
@ -118,7 +118,7 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
}); });
const getBuildingEditHistoryById = asyncController(async (req: express.Request, res: express.Response) => { const getBuildingEditHistoryById = asyncController(async (req: express.Request, res: express.Response) => {
const buildingId = processParam(req.params, 'building_id', parseIntParam, true); const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
try { try {
const editHistory = await buildingService.getBuildingEditHistory(buildingId); const editHistory = await buildingService.getBuildingEditHistory(buildingId);
@ -134,7 +134,7 @@ const updateBuildingLikeById = asyncController(async (req: express.Request, res:
return res.send({ error: 'Must be logged in' }); return res.send({ error: 'Must be logged in' });
} }
const buildingId = processParam(req.params, 'building_id', parseIntParam, true); const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
const { like } = req.body; const { like } = req.body;
try { try {

View File

@ -1,6 +1,6 @@
import express from 'express'; import express from 'express';
import { parseIntParam, processParam } from '../parameters'; import { parsePositiveIntParam, processParam } from '../parameters';
import asyncController from '../routes/asyncController'; import asyncController from '../routes/asyncController';
import * as dataExtractService from '../services/dataExtract'; import * as dataExtractService from '../services/dataExtract';
@ -15,7 +15,7 @@ const getAllDataExtracts = asyncController(async function(req: express.Request,
}); });
const getDataExtract = asyncController(async function(req: express.Request, res: express.Response) { const getDataExtract = asyncController(async function(req: express.Request, res: express.Response) {
const extractId = processParam(req.params, 'extract_id', parseIntParam, true); const extractId = processParam(req.params, 'extract_id', parsePositiveIntParam, true);
try { try {
const extract = await dataExtractService.getDataExtractById(extractId); const extract = await dataExtractService.getDataExtractById(extractId);

View File

@ -1,6 +1,11 @@
import { EditHistoryEntry } from '../../../frontend/models/edit-history-entry'; import { EditHistoryEntry } from '../../../frontend/models/edit-history-entry';
import { numAsc, numDesc } from '../../../helpers'; import { numAsc, numDesc } from '../../../helpers';
/**
* Create an object mocking all method of editHistory dataAccess
* The type is set to reflect the type of that module, with added methods
* used when testing
*/
const mockEditHistory = const mockEditHistory =
jest.genMockFromModule('../editHistory') as typeof import('../editHistory') & { jest.genMockFromModule('../editHistory') as typeof import('../editHistory') & {
__setHistory: (mockHistoryData: EditHistoryEntry[]) => void __setHistory: (mockHistoryData: EditHistoryEntry[]) => void
@ -44,7 +49,6 @@ const {
__setHistory, __setHistory,
getHistoryAfterId, getHistoryAfterId,
getHistoryBeforeId, getHistoryBeforeId,
getLatestHistory,
getIdNewerThan, getIdNewerThan,
getIdOlderThan getIdOlderThan
} = mockEditHistory; } = mockEditHistory;
@ -53,7 +57,6 @@ export {
__setHistory, __setHistory,
getHistoryAfterId, getHistoryAfterId,
getHistoryBeforeId, getHistoryBeforeId,
getLatestHistory,
getIdNewerThan, getIdNewerThan,
getIdOlderThan getIdOlderThan
}; };

View File

@ -23,12 +23,12 @@ export function processParam<T>(params: object, paramName: string, processingFn:
} }
} }
export function parseIntParam(param: string) { export function parsePositiveIntParam(param: string) {
if(param == undefined) return undefined; if(param == undefined) return undefined;
const result = strictParseInt(param); const result = strictParseInt(param);
if (isNaN(result)) { if (isNaN(result)) {
throw new ParamInvalidFormatError('Invalid format: not an integer'); throw new ParamInvalidFormatError('Invalid format: not a positive integer');
} }
return result; return result;
} }