@kitstack/nest-powertools
Version:
A comprehensive collection of NestJS powertools, decorators, and utilities to supercharge your backend development
71 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const order_by_field_guard_1 = require("../order-by-field.guard");
const common_1 = require("@nestjs/common");
describe('OrderByFieldValidationGuard', () => {
const getContext = (query = {}, body = {}) => ({
switchToHttp: () => ({
getRequest: () => ({ query, body }),
}),
});
it('allows valid orderBy from allowedFields', async () => {
const Guard = (0, order_by_field_guard_1.SortFieldValidationGuard)(undefined, ['foo', 'bar']);
const context = getContext({ orderBy: 'foo' });
await expect(new Guard().canActivate(context)).resolves.toBe(true);
});
it('rejects invalid orderBy', async () => {
const Guard = (0, order_by_field_guard_1.SortFieldValidationGuard)(undefined, ['foo']);
const context = getContext({ orderBy: 'baz' });
await expect(new Guard().canActivate(context)).rejects.toThrow(common_1.BadRequestException);
});
it('sets default orderBy if not provided', async () => {
const Guard = (0, order_by_field_guard_1.SortFieldValidationGuard)(undefined, ['foo'], 'foo');
const query = {};
const context = getContext(query);
await new Guard().canActivate(context);
expect(query.orderBy).toBe('foo');
});
it('validates orderDir values', async () => {
const Guard = (0, order_by_field_guard_1.SortFieldValidationGuard)();
for (const val of ['ASC', 'DESC', 'asc', 'desc', 1, -1]) {
const context = getContext({ orderDir: val });
await expect(new Guard().canActivate(context)).resolves.toBe(true);
}
const context = getContext({ orderDir: 'bad' });
await expect(new Guard().canActivate(context)).rejects.toThrow(common_1.BadRequestException);
});
it('supports custom field names', async () => {
const Guard = (0, order_by_field_guard_1.SortFieldValidationGuard)(undefined, ['foo'], 'foo', {
orderByField: 'sortBy',
orderDirField: 'sortDir',
});
const context = getContext({ sortBy: 'foo', sortDir: 'ASC' });
await expect(new Guard().canActivate(context)).resolves.toBe(true);
});
it('works with body fields', async () => {
const Guard = (0, order_by_field_guard_1.SortFieldValidationGuard)(undefined, ['foo']);
const context = getContext({}, { orderBy: 'foo', orderDir: 'DESC' });
await expect(new Guard().canActivate(context)).resolves.toBe(true);
});
it('works without entity', async () => {
const Guard = (0, order_by_field_guard_1.SortFieldValidationGuard)();
const context = getContext({ orderBy: 'id' });
await expect(new Guard().canActivate(context)).resolves.toBe(true);
});
it('allows entity columns if TypeORM present', async () => {
jest.resetModules();
jest.doMock('typeorm', () => ({
getMetadataArgsStorage: () => ({
columns: [{ target: class User {
}, propertyName: 'username' }],
}),
}));
const User = class User {
};
const Guard = (0, order_by_field_guard_1.SortFieldValidationGuard)(User);
const context = getContext({ orderBy: 'username' });
await expect(new Guard().canActivate(context)).resolves.toBe(true);
jest.dontMock('typeorm');
});
});
//# sourceMappingURL=order-by-field.guard.spec.js.map