From 99ebd1208148b1899d81c98e389849405bc282f2 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sun, 15 Oct 2023 19:59:26 +0200 Subject: [PATCH 1/4] chore: Add IConfig type --- packages/types/index.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 228558e3..514eb206 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -125,6 +125,12 @@ export interface IPermissionCatalog { conditions: { label: string; key: string }[]; } +export interface IConfig { + id: string; + key: string; + value: IJSONObject; +} + export interface IFieldDropdown { key: string; label: string; From cdf7a1adc490c6acdf28360756a378d1d6302495 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sun, 15 Oct 2023 20:00:07 +0200 Subject: [PATCH 2/4] test: Implement config fixture --- packages/backend/test/fixtures/config.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 packages/backend/test/fixtures/config.ts diff --git a/packages/backend/test/fixtures/config.ts b/packages/backend/test/fixtures/config.ts new file mode 100644 index 00000000..f32ff167 --- /dev/null +++ b/packages/backend/test/fixtures/config.ts @@ -0,0 +1,23 @@ +import { IJSONObject } from '@automatisch/types'; +import { faker } from '@faker-js/faker'; + +type ConfigParams = { + key?: string; + value?: IJSONObject; +}; + +const createConfig = async (params: ConfigParams = {}) => { + const configData = { + key: params?.key || faker.lorem.word(), + value: params?.value || { data: 'sampleConfig' }, + }; + + const [config] = await global.knex + .table('config') + .insert(configData) + .returning('*'); + + return config; +}; + +export default createConfig; From 48dc2312d95e0f9afc3b1d69f0966fb8967e2c4e Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sun, 15 Oct 2023 20:00:51 +0200 Subject: [PATCH 3/4] refactor: Remove redundant context variable from getConfig --- .../backend/src/graphql/queries/get-config.ee.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/backend/src/graphql/queries/get-config.ee.ts b/packages/backend/src/graphql/queries/get-config.ee.ts index 31e3eb13..844dfd6a 100644 --- a/packages/backend/src/graphql/queries/get-config.ee.ts +++ b/packages/backend/src/graphql/queries/get-config.ee.ts @@ -1,26 +1,20 @@ import { hasValidLicense } from '../../helpers/license.ee'; import Config from '../../models/config'; -import Context from '../../types/express/context'; type Params = { keys: string[]; }; -const getConfig = async ( - _parent: unknown, - params: Params, - context: Context -) => { - if (!await hasValidLicense()) return {}; +const getConfig = async (_parent: unknown, params: Params) => { + if (!(await hasValidLicense())) return {}; - const configQuery = Config - .query(); + const configQuery = Config.query(); if (Array.isArray(params.keys)) { configQuery.whereIn('key', params.keys); } - const config = await configQuery; + const config = await configQuery.orderBy('key', 'asc'); return config.reduce((computedConfig, configEntry) => { const { key, value } = configEntry; From 2d8421943fc09a61c234896d187660f433502be2 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sun, 15 Oct 2023 20:01:05 +0200 Subject: [PATCH 4/4] test: Implement tests for getConfig graphQL query --- .../src/graphql/queries/get-config.ee.test.ts | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 packages/backend/src/graphql/queries/get-config.ee.test.ts diff --git a/packages/backend/src/graphql/queries/get-config.ee.test.ts b/packages/backend/src/graphql/queries/get-config.ee.test.ts new file mode 100644 index 00000000..d31e3943 --- /dev/null +++ b/packages/backend/src/graphql/queries/get-config.ee.test.ts @@ -0,0 +1,96 @@ +import request from 'supertest'; +import app from '../../app'; +import createConfig from '../../../test/fixtures/config'; +import { IConfig } from '@automatisch/types'; +import * as license from '../../helpers/license.ee'; + +describe('graphQL getConfig query', () => { + let configOne: IConfig, + configTwo: IConfig, + configThree: IConfig, + query: string; + + beforeEach(async () => { + configOne = await createConfig({ key: 'configOne' }); + configTwo = await createConfig({ key: 'configTwo' }); + configThree = await createConfig({ key: 'configThree' }); + + query = ` + query { + getConfig + } + `; + }); + + describe('and without valid license', () => { + beforeEach(async () => { + jest.spyOn(license, 'hasValidLicense').mockResolvedValue(false); + }); + + describe('and correct permissions', () => { + it('should return empty config data', async () => { + const response = await request(app) + .post('/graphql') + .send({ query }) + .expect(200); + + const expectedResponsePayload = { data: { getConfig: {} } }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + }); + + describe('and with valid license', () => { + beforeEach(async () => { + jest.spyOn(license, 'hasValidLicense').mockResolvedValue(true); + }); + + describe('and without providing specific keys', () => { + it('should return all config data', async () => { + const response = await request(app) + .post('/graphql') + .send({ query }) + .expect(200); + + const expectedResponsePayload = { + data: { + getConfig: { + [configOne.key]: configOne.value.data, + [configTwo.key]: configTwo.value.data, + [configThree.key]: configThree.value.data, + }, + }, + }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + + describe('and with providing specific keys', () => { + it('should return all config data', async () => { + query = ` + query { + getConfig(keys: ["configOne", "configTwo"]) + } + `; + + const response = await request(app) + .post('/graphql') + .send({ query }) + .expect(200); + + const expectedResponsePayload = { + data: { + getConfig: { + [configOne.key]: configOne.value.data, + [configTwo.key]: configTwo.value.data, + }, + }, + }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + }); +});