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); + }); + }); + }); +}); 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; 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; 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;