Files
directus/api/tests/services/payload.test.ts
Rijk van Zanten eea9f45624 Add authenticate hook to implement custom auth checks against current request (#11942)
* Add "authenticate" filter hook that allows custom auth check

* Start on test

* Update Jest, restructure API tests, start implementing authenticate test

* Move access token verify to util function

* Ensure jest can show inline warnings on correct lines

* Update is-directus-jwt to use jsonwebtoken decode + add tests

* Remove unused package

* Tweak and finish + test authenticate

* Tweak test

* Add authenticate filter to docs

* Don't scan tests for codeql

* No seriously, ignore tests
2022-03-03 16:29:13 -05:00

98 lines
2.3 KiB
TypeScript

import knex, { Knex } from 'knex';
import { MockClient, Tracker, getTracker } from 'knex-mock-client';
import { PayloadService } from '../../src/services';
jest.mock('../../src/database/index', () => {
return { getDatabaseClient: jest.fn().mockReturnValue('postgres') };
});
jest.requireMock('../../src/database/index');
describe('Integration Tests', () => {
let db: jest.Mocked<Knex>;
let tracker: Tracker;
beforeAll(async () => {
db = knex({ client: MockClient }) as jest.Mocked<Knex>;
tracker = getTracker();
});
afterEach(() => {
tracker.reset();
});
describe('Services / PayloadService', () => {
describe('transformers', () => {
let service: PayloadService;
beforeEach(() => {
service = new PayloadService('test', {
knex: db,
schema: { collections: {}, relations: [] },
});
});
describe('csv', () => {
it('Returns undefined for illegal values', async () => {
const result = await service.transformers.csv({
value: 123,
action: 'read',
payload: {},
accountability: { role: null },
specials: [],
});
expect(result).toBe(undefined);
});
it('Returns [] for empty strings', async () => {
const result = await service.transformers.csv({
value: '',
action: 'read',
payload: {},
accountability: { role: null },
specials: [],
});
expect(result).toMatchObject([]);
});
it('Splits the CSV string', async () => {
const result = await service.transformers.csv({
value: 'test,directus',
action: 'read',
payload: {},
accountability: { role: null },
specials: [],
});
expect(result).toMatchObject(['test', 'directus']);
});
it('Saves array values as joined string', async () => {
const result = await service.transformers.csv({
value: ['test', 'directus'],
action: 'create',
payload: {},
accountability: { role: null },
specials: [],
});
expect(result).toBe('test,directus');
});
it('Saves string values as is', async () => {
const result = await service.transformers.csv({
value: 'test,directus',
action: 'create',
payload: {},
accountability: { role: null },
specials: [],
});
expect(result).toBe('test,directus');
});
});
});
});
});