UNPKG

declarapi

Version:
332 lines 17 kB
import { transform } from './crud.js'; describe('transform crud', () => { const getArgs = (result, method) => { var _a, _b; const res = (_b = (_a = result.results) === null || _a === void 0 ? void 0 : _a.find(x => x.method === method)) === null || _b === void 0 ? void 0 : _b.arguments; if (!res) throw new Error(`Method not found ${method}`); return res; }; const getReturns = (result, method) => { var _a, _b; const res = (_b = (_a = result.results) === null || _a === void 0 ? void 0 : _a.find(x => x.method === method)) === null || _b === void 0 ? void 0 : _b.returns; if (!res) throw new Error(`Method not found ${method}`); return res; }; it('id must be present on input', async () => { const resultErr = await transform({ name: 'test', authentication: false, dataType: { notId: 'string' } }); expect(resultErr).toStrictEqual({ type: 'error', errors: 'id field does not exist in the data declaration' }); const result = await transform({ name: 'test', authentication: false, dataType: { id: 'string', notId: 'string' } }); expect(result).toHaveProperty('type', 'result'); expect(result).toHaveProperty('results'); expect(result.results).toHaveLength(5); }); it('does not generate arguments for get is search is not set', async () => { var _a, _b, _c, _d; const input = { name: 'test', authentication: false, dataType: { id: 'string', myNumber: 'number', myString: 'string' } }; const output = await transform(input); expect((_b = (_a = output.results) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.arguments).toStrictEqual({}); expect((_d = (_c = output.results) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.method).toBe('GET'); // expect(output).toMatchSnapshot() }); it.skip('generates full, parametric search option', async () => { const input = { name: 'test', authentication: false, dataType: { id: 'string', myNumber: 'number', myString: 'string' }, search: 'full' }; expect(await transform(input)).toMatchSnapshot(); }); it('generates a full text search search option', async () => { var _a, _b, _c, _d; const input = { name: 'test', authentication: false, dataType: { id: 'string', myNumber: 'number', myString: 'string' }, search: 'textSearch' }; const output = await transform(input); expect((_b = (_a = output.results) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.arguments).toStrictEqual({ id: ['string', { $array: 'string' }, '?'], search: ['string', '?'] }); expect((_d = (_c = output.results) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.method).toBe('GET'); // expect(output).toMatchSnapshot() }); it.skip('generates an id only get', async () => { const input = { name: 'test', authentication: false, dataType: { id: 'string', myNumber: 'number', myString: 'string' }, search: 'idOnly' }; expect(await transform(input)).toMatchSnapshot(); }); it('does not generate methods that are disabled ver 1', async () => { var _a, _b, _c, _d, _e; const input = { name: 'test', authentication: false, methods: { put: false, patch: false }, dataType: { id: 'string', myNumber: 'number', myString: 'string' }, search: 'idOnly' }; const result = await transform(input); expect(result.results).toHaveLength(3); expect((_a = result.results) === null || _a === void 0 ? void 0 : _a.find(x => x.method === 'PUT')).toBeUndefined(); expect((_b = result.results) === null || _b === void 0 ? void 0 : _b.find(x => x.method === 'PATCH')).toBeUndefined(); expect((_c = result.results) === null || _c === void 0 ? void 0 : _c.find(x => x.method === 'GET')).toBeTruthy(); expect((_d = result.results) === null || _d === void 0 ? void 0 : _d.find(x => x.method === 'POST')).toBeTruthy(); expect((_e = result.results) === null || _e === void 0 ? void 0 : _e.find(x => x.method === 'DELETE')).toBeTruthy(); }); it('does not generate methods that are disabled ver 2', async () => { var _a, _b, _c, _d, _e; const input = { name: 'test', authentication: false, methods: { get: false, post: false, delete: false }, dataType: { id: 'string', myNumber: 'number', myString: 'string' }, search: 'idOnly' }; const result = await transform(input); expect(result.results).toHaveLength(2); expect((_a = result.results) === null || _a === void 0 ? void 0 : _a.find(x => x.method === 'PUT')).toBeTruthy(); expect((_b = result.results) === null || _b === void 0 ? void 0 : _b.find(x => x.method === 'PATCH')).toBeTruthy(); expect((_c = result.results) === null || _c === void 0 ? void 0 : _c.find(x => x.method === 'GET')).toBeUndefined(); expect((_d = result.results) === null || _d === void 0 ? void 0 : _d.find(x => x.method === 'POST')).toBeUndefined(); expect((_e = result.results) === null || _e === void 0 ? void 0 : _e.find(x => x.method === 'DELETE')).toBeUndefined(); }); it('generates a custom arguments parameters for get ', async () => { var _a, _b, _c; const input = { name: 'test', authentication: false, dataType: { id: 'string', myNumber: 'number', myString: 'string' }, search: { customSearchField: 'string' } }; const result = await transform(input); expect((_c = (_b = (_a = result.results) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.arguments) === null || _c === void 0 ? void 0 : _c.customSearchField).toEqual('string'); // expect(result).toMatchSnapshot() }); it('accepts regex validated id', async () => { var _a, _b; const input = { name: 'test', authentication: false, dataType: { id: { $string: { regex: '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' } }, notAnId: 'boolean' }, search: 'idOnly' }; const output = await transform(input); expect((_b = (_a = output.results) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.arguments).toStrictEqual({ id: [ { $string: { regex: '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' } }, { $array: { $string: { regex: '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' } } }, '?' ] }); expect(output.results).toHaveLength(5); }); it('returns an object with an error message on invalid id type', async () => { const input = { name: 'test', authentication: false, dataType: { id: 'number', notAnId: 'boolean' } }; const result = await transform(input); expect(result.errors).toEqual('Type of id field must be string'); }); it('returns an object with an error message on validation error', async () => { var _a; const input = { name: 'test', authenticationz: false, dataType: { id: 'string', notAnId: 'boolean' } }; const result = await transform(input); expect(result.errors).toHaveLength(2); expect((_a = result.errors) === null || _a === void 0 ? void 0 : _a[0]).toStrictEqual({ dataPath: '', keyword: 'additionalProperties', message: 'should NOT have additional properties', params: { additionalProperty: 'authenticationz' }, schemaPath: '#/additionalProperties' }); }); it('makes all parameters optional for patch', async () => { var _a, _b; const input = { name: 'test', authentication: false, dataType: { id: 'string', notAnId: ['boolean', '?'], singleElementArrayType: ['string'], obj: { a: 'string', b: 'number' }, obj2: { a: ['string', '?'], b: ['number', '?'] }, duoType: ['boolean', 'number'] } }; const result = await transform(input); expect((_b = (_a = result.results) === null || _a === void 0 ? void 0 : _a.find(x => x.method === 'PATCH')) === null || _b === void 0 ? void 0 : _b.arguments).toStrictEqual({ id: 'string', notAnId: ['boolean', '?'], singleElementArrayType: ['string', '?'], obj: [{ a: 'string', b: 'number' }, '?'], obj2: [{ a: ['string', '?'], b: ['number', '?'] }, '?'], duoType: ['boolean', 'number', '?'] }); }); it('supports different auth protocols', async () => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; const withAuth = (auth = true) => ({ name: 'test', authentication: auth, dataType: { id: 'string', notAnId: 'boolean' } }); expect((_a = (await transform(withAuth(false))).results) === null || _a === void 0 ? void 0 : _a.every(x => x.authentication === false)).toBeTruthy(); expect((_b = (await transform(withAuth(true))).results) === null || _b === void 0 ? void 0 : _b.every(x => x.authentication === true)).toBeTruthy(); const stringAllRole = (await transform(withAuth(['aUserRole']))).results || []; expect(stringAllRole).toHaveLength(5); stringAllRole.forEach(x => expect(x.authentication).toStrictEqual(['aUserRole'])); const resultAuthSome = await transform(withAuth({ get: false, modify: ['admin'] })); expect((_d = (_c = resultAuthSome.results) === null || _c === void 0 ? void 0 : _c.find(x => x.method === 'GET')) === null || _d === void 0 ? void 0 : _d.authentication).toStrictEqual(false); const authSomeOtherMethods = ((_e = resultAuthSome.results) === null || _e === void 0 ? void 0 : _e.filter(x => x.method !== 'GET')) || []; expect(authSomeOtherMethods).toHaveLength(4); authSomeOtherMethods.forEach(x => expect(x.authentication).toStrictEqual(['admin'])); const resultAuth = await transform(withAuth({ get: false, post: true, put: ['owner'], delete: ['admin'] })); expect((_g = (_f = resultAuth.results) === null || _f === void 0 ? void 0 : _f.find(x => x.method === 'GET')) === null || _g === void 0 ? void 0 : _g.authentication).toStrictEqual(false); expect((_j = (_h = resultAuth.results) === null || _h === void 0 ? void 0 : _h.find(x => x.method === 'POST')) === null || _j === void 0 ? void 0 : _j.authentication).toStrictEqual(true); expect((_l = (_k = resultAuth.results) === null || _k === void 0 ? void 0 : _k.find(x => x.method === 'PUT')) === null || _l === void 0 ? void 0 : _l.authentication).toStrictEqual(['owner']); expect((_o = (_m = resultAuth.results) === null || _m === void 0 ? void 0 : _m.find(x => x.method === 'DELETE')) === null || _o === void 0 ? void 0 : _o.authentication).toStrictEqual(['admin']); }); it('does not required user to post when user auth is set globally', async () => { var _a, _b, _c, _d, _e, _f, _g, _h; const auth = ['admin', { createdBy: true }]; const withAuth = (auth = true) => ({ name: 'test', authentication: auth, dataType: { id: 'string', notAnId: 'boolean', createdBy: 'string' } }); const stringAllRole = (await transform(withAuth(['aUserRole']))).results || []; expect(stringAllRole).toHaveLength(5); stringAllRole.forEach(x => expect(x.authentication).toStrictEqual(['aUserRole'])); const boolAll = (await transform(withAuth(true))).results || []; expect(boolAll).toHaveLength(5); boolAll.forEach(x => expect(x.authentication).toStrictEqual(true)); const resultAuth = await transform(withAuth(auth)); expect((_b = (_a = resultAuth.results) === null || _a === void 0 ? void 0 : _a.find(x => x.method === 'GET')) === null || _b === void 0 ? void 0 : _b.authentication).toStrictEqual(auth); expect((_d = (_c = resultAuth.results) === null || _c === void 0 ? void 0 : _c.find(x => x.method === 'POST')) === null || _d === void 0 ? void 0 : _d.authentication).toStrictEqual(true); expect((_f = (_e = resultAuth.results) === null || _e === void 0 ? void 0 : _e.find(x => x.method === 'PUT')) === null || _f === void 0 ? void 0 : _f.authentication).toStrictEqual(auth); expect((_h = (_g = resultAuth.results) === null || _g === void 0 ? void 0 : _g.find(x => x.method === 'DELETE')) === null || _h === void 0 ? void 0 : _h.authentication).toStrictEqual(auth); }); describe('manageFields', () => { const schema = () => ({ name: 'test', authentication: false, manageFields: { createdBy: true }, dataType: { id: 'string', notAnId: 'boolean' } }); it('returns error when manageFields createdBy is set to true, but the field is missing', async () => { const result = await transform(schema()); expect(result).toHaveProperty('type', 'error'); expect(result).toHaveProperty('errors', 'managed field "createdBy" is not present on data type'); }); it('returns error when manageFields createdBy is set to true, but field is not declared as string', async () => { const input = schema(); input.dataType.createdBy = 'number'; const result = await transform(input); expect(result).toHaveProperty('type', 'error'); expect(result).toHaveProperty('errors', 'managed field "createdBy" must be a string, current type :number'); }); it('does not generate error when createdBy managedField is a string', async () => { const input = schema(); input.dataType.createdBy = 'string'; expect(await transform(input)).toHaveProperty('type', 'result'); input.dataType.createdBy = { $string: {} }; expect(await transform(input)).toHaveProperty('type', 'result'); }); it('does not generate error when createdBy managedField is disabled', async () => { const input = schema(); input.manageFields.createdBy = false; input.dataType.createdBy = 'number'; expect(await transform(input)).toHaveProperty('type', 'result'); }); it('removes managed field from post arguments', async () => { const input = schema(); input.dataType.createdBy = 'string'; const result = await transform(input); expect(result).toHaveProperty('type', 'result'); expect(getReturns(result, 'GET').$array).toHaveProperty('createdBy', 'string'); expect(getArgs(result, 'POST')).not.toHaveProperty('createdBy'); expect(getReturns(result, 'POST')).toHaveProperty('createdBy', 'string'); expect(getArgs(result, 'PUT')).not.toHaveProperty('createdBy'); expect(getReturns(result, 'PUT')).toHaveProperty('createdBy', 'string'); expect(getArgs(result, 'PATCH')).not.toHaveProperty('createdBy'); expect(getReturns(result, 'PATCH')).toHaveProperty('createdBy', 'string'); expect(getArgs(result, 'DELETE')).not.toHaveProperty('createdBy'); expect(getReturns(result, 'DELETE').$array).toHaveProperty('createdBy', 'string'); }); }); }); //# sourceMappingURL=crud.spec.js.map