2023-08-09 03:30:02 -07:00
|
|
|
import { filterListQueryMiddleware } from '@/middlewares/listQuery/filter';
|
|
|
|
import { LoggerProxy } from 'n8n-workflow';
|
|
|
|
import { getLogger } from '@/Logger';
|
|
|
|
import { selectListQueryMiddleware } from '@/middlewares/listQuery/select';
|
|
|
|
import { paginationListQueryMiddleware } from '@/middlewares/listQuery/pagination';
|
2023-08-22 04:19:37 -07:00
|
|
|
import * as ResponseHelper from '@/ResponseHelper';
|
|
|
|
import type { ListQuery } from '@/requests';
|
|
|
|
import type { Response, NextFunction } from 'express';
|
2023-08-09 03:30:02 -07:00
|
|
|
|
|
|
|
describe('List query middleware', () => {
|
2023-08-22 04:19:37 -07:00
|
|
|
let mockReq: ListQuery.Request;
|
|
|
|
let mockRes: Response;
|
2023-08-09 03:30:02 -07:00
|
|
|
let nextFn: NextFunction = jest.fn();
|
2023-08-22 04:19:37 -07:00
|
|
|
let args: [ListQuery.Request, Response, NextFunction];
|
|
|
|
|
|
|
|
let sendErrorResponse: jest.SpyInstance;
|
2023-08-09 03:30:02 -07:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.restoreAllMocks();
|
2023-08-22 04:19:37 -07:00
|
|
|
|
|
|
|
LoggerProxy.init(getLogger());
|
|
|
|
mockReq = { baseUrl: '/rest/workflows' } as ListQuery.Request;
|
|
|
|
mockRes = { status: () => ({ json: jest.fn() }) } as unknown as Response;
|
|
|
|
args = [mockReq, mockRes, nextFn];
|
|
|
|
|
|
|
|
sendErrorResponse = jest.spyOn(ResponseHelper, 'sendErrorResponse');
|
2023-08-09 03:30:02 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Query filter', () => {
|
2023-08-22 04:19:37 -07:00
|
|
|
test('should not set filter on request if none sent', async () => {
|
|
|
|
mockReq.query = {};
|
|
|
|
|
|
|
|
await filterListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toBeUndefined();
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should parse valid filter', async () => {
|
2023-08-09 03:30:02 -07:00
|
|
|
mockReq.query = { filter: '{ "name": "My Workflow" }' };
|
2023-08-22 04:19:37 -07:00
|
|
|
|
|
|
|
await filterListQueryMiddleware(...args);
|
2023-08-09 03:30:02 -07:00
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({ filter: { name: 'My Workflow' } });
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2023-08-22 04:19:37 -07:00
|
|
|
test('should ignore invalid filter', async () => {
|
2023-08-09 03:30:02 -07:00
|
|
|
mockReq.query = { filter: '{ "name": "My Workflow", "foo": "bar" }' };
|
2023-08-22 04:19:37 -07:00
|
|
|
|
|
|
|
await filterListQueryMiddleware(...args);
|
2023-08-09 03:30:02 -07:00
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({ filter: { name: 'My Workflow' } });
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2023-08-22 04:19:37 -07:00
|
|
|
test('should throw on invalid JSON', async () => {
|
2023-08-09 03:30:02 -07:00
|
|
|
mockReq.query = { filter: '{ "name" : "My Workflow"' };
|
|
|
|
|
2023-08-22 04:19:37 -07:00
|
|
|
await filterListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw on valid filter with invalid type', async () => {
|
|
|
|
mockReq.query = { filter: '{ "name" : 123 }' };
|
|
|
|
|
|
|
|
await filterListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
|
2023-08-09 03:30:02 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Query select', () => {
|
2023-08-22 04:19:37 -07:00
|
|
|
test('should not set select on request if none sent', async () => {
|
|
|
|
mockReq.query = {};
|
|
|
|
|
|
|
|
await filterListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toBeUndefined();
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2023-08-09 03:30:02 -07:00
|
|
|
test('should parse valid select', () => {
|
|
|
|
mockReq.query = { select: '["name", "id"]' };
|
2023-08-22 04:19:37 -07:00
|
|
|
|
2023-08-09 03:30:02 -07:00
|
|
|
selectListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({ select: { name: true, id: true } });
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('ignore invalid select', () => {
|
|
|
|
mockReq.query = { select: '["name", "foo"]' };
|
2023-08-22 04:19:37 -07:00
|
|
|
|
2023-08-09 03:30:02 -07:00
|
|
|
selectListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({ select: { name: true } });
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throw on invalid JSON', () => {
|
|
|
|
mockReq.query = { select: '["name"' };
|
|
|
|
|
2023-08-22 04:19:37 -07:00
|
|
|
selectListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
|
2023-08-09 03:30:02 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('throw on non-string-array JSON for select', () => {
|
|
|
|
mockReq.query = { select: '"name"' };
|
|
|
|
|
2023-08-22 04:19:37 -07:00
|
|
|
selectListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
|
2023-08-09 03:30:02 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Query pagination', () => {
|
2023-08-22 04:19:37 -07:00
|
|
|
test('should not set pagination options on request if none sent', async () => {
|
|
|
|
mockReq.query = {};
|
|
|
|
|
|
|
|
await filterListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toBeUndefined();
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2023-08-09 03:30:02 -07:00
|
|
|
test('should parse valid pagination', () => {
|
|
|
|
mockReq.query = { skip: '1', take: '2' };
|
|
|
|
paginationListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({ skip: 1, take: 2 });
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should ignore skip without take', () => {
|
|
|
|
mockReq.query = { skip: '1' };
|
|
|
|
paginationListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toBeUndefined();
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should default skip to 0', () => {
|
|
|
|
mockReq.query = { take: '2' };
|
|
|
|
paginationListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({ skip: 0, take: 2 });
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should cap take at 50', () => {
|
|
|
|
mockReq.query = { take: '51' };
|
2023-08-22 04:19:37 -07:00
|
|
|
|
2023-08-09 03:30:02 -07:00
|
|
|
paginationListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({ skip: 0, take: 50 });
|
|
|
|
expect(nextFn).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw on non-numeric-integer take', () => {
|
|
|
|
mockReq.query = { take: '3.2' };
|
|
|
|
|
2023-08-22 04:19:37 -07:00
|
|
|
paginationListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw on non-numeric-integer skip', () => {
|
|
|
|
mockReq.query = { take: '3', skip: '3.2' };
|
|
|
|
|
|
|
|
paginationListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Combinations', () => {
|
|
|
|
test('should combine filter with select', async () => {
|
|
|
|
mockReq.query = { filter: '{ "name": "My Workflow" }', select: '["name", "id"]' };
|
|
|
|
|
|
|
|
await filterListQueryMiddleware(...args);
|
|
|
|
selectListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({
|
|
|
|
select: { name: true, id: true },
|
|
|
|
filter: { name: 'My Workflow' },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(nextFn).toBeCalledTimes(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should combine filter with pagination options', async () => {
|
|
|
|
mockReq.query = { filter: '{ "name": "My Workflow" }', skip: '1', take: '2' };
|
|
|
|
|
|
|
|
await filterListQueryMiddleware(...args);
|
|
|
|
paginationListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({
|
|
|
|
filter: { name: 'My Workflow' },
|
|
|
|
skip: 1,
|
|
|
|
take: 2,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(nextFn).toBeCalledTimes(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should combine select with pagination options', async () => {
|
|
|
|
mockReq.query = { select: '["name", "id"]', skip: '1', take: '2' };
|
|
|
|
|
|
|
|
selectListQueryMiddleware(...args);
|
|
|
|
paginationListQueryMiddleware(...args);
|
|
|
|
|
|
|
|
expect(mockReq.listQueryOptions).toEqual({
|
|
|
|
select: { name: true, id: true },
|
|
|
|
skip: 1,
|
|
|
|
take: 2,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(nextFn).toBeCalledTimes(2);
|
2023-08-09 03:30:02 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|