diff --git a/app/src/api/controllers/buildingController.ts b/app/src/api/controllers/buildingController.ts index 41d53b01..0bb6ad23 100644 --- a/app/src/api/controllers/buildingController.ts +++ b/app/src/api/controllers/buildingController.ts @@ -1,6 +1,6 @@ import express from 'express'; -import { parseIntParam, processParam } from '../parameters'; +import { parsePositiveIntParam, processParam } from '../parameters'; import asyncController from '../routes/asyncController'; import * as buildingService from '../services/building'; import * as userService from '../services/user'; @@ -35,7 +35,7 @@ const getBuildingsByReference = asyncController(async (req: express.Request, res // GET individual building, POST building updates 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 { 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) { - const buildingId = processParam(req.params, 'building_id', parseIntParam, true); + const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true); const buildingUpdate = req.body; @@ -84,7 +84,7 @@ async function updateBuilding(req: express.Request, res: express.Response, userI // GET building UPRNs 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 { 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 } - const buildingId = processParam(req.params, 'building_id', parseIntParam, true); + const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true); try { 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 buildingId = processParam(req.params, 'building_id', parseIntParam, true); + const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true); try { 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' }); } - const buildingId = processParam(req.params, 'building_id', parseIntParam, true); + const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true); const { like } = req.body; try { diff --git a/app/src/api/controllers/extractController.ts b/app/src/api/controllers/extractController.ts index 29d041ae..077d69c5 100644 --- a/app/src/api/controllers/extractController.ts +++ b/app/src/api/controllers/extractController.ts @@ -1,6 +1,6 @@ import express from 'express'; -import { parseIntParam, processParam } from '../parameters'; +import { parsePositiveIntParam, processParam } from '../parameters'; import asyncController from '../routes/asyncController'; 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 extractId = processParam(req.params, 'extract_id', parseIntParam, true); + const extractId = processParam(req.params, 'extract_id', parsePositiveIntParam, true); try { const extract = await dataExtractService.getDataExtractById(extractId); diff --git a/app/src/api/dataAccess/__mocks__/editHistory.ts b/app/src/api/dataAccess/__mocks__/editHistory.ts index 50d01a79..b4636864 100644 --- a/app/src/api/dataAccess/__mocks__/editHistory.ts +++ b/app/src/api/dataAccess/__mocks__/editHistory.ts @@ -1,6 +1,11 @@ import { EditHistoryEntry } from '../../../frontend/models/edit-history-entry'; 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 = jest.genMockFromModule('../editHistory') as typeof import('../editHistory') & { __setHistory: (mockHistoryData: EditHistoryEntry[]) => void @@ -44,7 +49,6 @@ const { __setHistory, getHistoryAfterId, getHistoryBeforeId, - getLatestHistory, getIdNewerThan, getIdOlderThan } = mockEditHistory; @@ -53,7 +57,6 @@ export { __setHistory, getHistoryAfterId, getHistoryBeforeId, - getLatestHistory, getIdNewerThan, getIdOlderThan }; diff --git a/app/src/api/parameters.ts b/app/src/api/parameters.ts index 0146dc97..19c84fcb 100644 --- a/app/src/api/parameters.ts +++ b/app/src/api/parameters.ts @@ -23,12 +23,12 @@ export function processParam(params: object, paramName: string, processingFn: } } -export function parseIntParam(param: string) { +export function parsePositiveIntParam(param: string) { if(param == undefined) return undefined; const result = strictParseInt(param); if (isNaN(result)) { - throw new ParamInvalidFormatError('Invalid format: not an integer'); + throw new ParamInvalidFormatError('Invalid format: not a positive integer'); } return result; }