Files
TREK/server/tests/unit/nest/common/row-id.pipe.test.ts
T
Maurice 3eac508baf fix(api): parse path ids with a rowId pipe that answers like an unknown id
RowIdPipe (src/nest/common/row-id.pipe.ts) parses a path param with toRowId. It answers a refused spelling with the route's own not-found answer, so '0x5', '5e0' and 'abc' get what an id that names no row gets. ParseRowIdPipe replaces Nest's ParseIntPipe with the same 400 and message: ParseIntPipe let '-1' and numbers past 2^53 through, and Postgres refuses the latter as out of range. Journey, journey recordings, day import, book design, map, public photo, collections, doc-sync, vacay, atlas and the plugin user-data export declare their ids through it. The upload routes parse in the handler with withRowId, so a refused id takes the existing cleanup path, or carry SpoolCleanupInterceptor (collections).
2026-11-18 13:20:05 +01:00

53 lines
2.0 KiB
TypeScript

import { intParam, ParseRowIdPipe, rowId } from '../../../../src/nest/common/row-id.pipe';
import { HttpException } from '@nestjs/common';
import { describe, expect, it } from 'vitest';
/** The status and body a pipe answers `value` with, or the id it hands on. */
function answer(
pipe: { transform(value: unknown): number },
value: unknown,
): number | { status: number; body: unknown } {
try {
return pipe.transform(value);
} catch (err) {
if (!(err instanceof HttpException)) throw err;
return { status: err.getStatus(), body: err.getResponse() };
}
}
const MALFORMED = ['0x5', '5e0', '5.0', '+5', '5_0', ' 5', '-5', 'abc', '', '99999999999999999999'];
describe('RowIdPipe', () => {
it('ROWIDPIPE-001: hands the handler the id as a number', () => {
expect(answer(rowId('Journey not found'), '5')).toBe(5);
expect(answer(rowId('Journey not found'), '007')).toBe(7);
});
it("ROWIDPIPE-002: answers every spelling toRowId refuses with the route's own not-found", () => {
for (const value of MALFORMED) {
expect(answer(rowId('Journey not found'), value), value).toEqual({
status: 404,
body: { error: 'Journey not found' },
});
}
expect(answer(rowId('Not allowed', 403), '0x5')).toEqual({ status: 403, body: { error: 'Not allowed' } });
});
it("ROWIDPIPE-003: ParseRowIdPipe keeps ParseIntPipe's 400 and message and refuses what it let through", () => {
const pipe = new ParseRowIdPipe();
expect(answer(pipe, '12')).toBe(12);
for (const value of ['-1', '99999999999999999999', '1.5', 'abc']) {
expect(answer(pipe, value), value).toEqual({
status: 400,
body: { error: 'Validation failed (numeric string is expected)' },
});
}
});
it('ROWIDPIPE-004: intParam answers 400 with its message for a whole number that is not one', () => {
expect(answer(intParam('Invalid year'), '2026')).toBe(2026);
expect(answer(intParam('Invalid year'), '2026.0')).toEqual({ status: 400, body: { error: 'Invalid year' } });
});
});