Merge pull request #1350 from automatisch/test/get-config
test: Implement tests for getConfig graphQL query
This commit is contained in:
96
packages/backend/src/graphql/queries/get-config.ee.test.ts
Normal file
96
packages/backend/src/graphql/queries/get-config.ee.test.ts
Normal file
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@@ -1,26 +1,20 @@
|
|||||||
import { hasValidLicense } from '../../helpers/license.ee';
|
import { hasValidLicense } from '../../helpers/license.ee';
|
||||||
import Config from '../../models/config';
|
import Config from '../../models/config';
|
||||||
import Context from '../../types/express/context';
|
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
keys: string[];
|
keys: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const getConfig = async (
|
const getConfig = async (_parent: unknown, params: Params) => {
|
||||||
_parent: unknown,
|
if (!(await hasValidLicense())) return {};
|
||||||
params: Params,
|
|
||||||
context: Context
|
|
||||||
) => {
|
|
||||||
if (!await hasValidLicense()) return {};
|
|
||||||
|
|
||||||
const configQuery = Config
|
const configQuery = Config.query();
|
||||||
.query();
|
|
||||||
|
|
||||||
if (Array.isArray(params.keys)) {
|
if (Array.isArray(params.keys)) {
|
||||||
configQuery.whereIn('key', params.keys);
|
configQuery.whereIn('key', params.keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
const config = await configQuery;
|
const config = await configQuery.orderBy('key', 'asc');
|
||||||
|
|
||||||
return config.reduce((computedConfig, configEntry) => {
|
return config.reduce((computedConfig, configEntry) => {
|
||||||
const { key, value } = configEntry;
|
const { key, value } = configEntry;
|
||||||
|
23
packages/backend/test/fixtures/config.ts
vendored
Normal file
23
packages/backend/test/fixtures/config.ts
vendored
Normal file
@@ -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;
|
6
packages/types/index.d.ts
vendored
6
packages/types/index.d.ts
vendored
@@ -125,6 +125,12 @@ export interface IPermissionCatalog {
|
|||||||
conditions: { label: string; key: string }[];
|
conditions: { label: string; key: string }[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IConfig {
|
||||||
|
id: string;
|
||||||
|
key: string;
|
||||||
|
value: IJSONObject;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IFieldDropdown {
|
export interface IFieldDropdown {
|
||||||
key: string;
|
key: string;
|
||||||
label: string;
|
label: string;
|
||||||
|
Reference in New Issue
Block a user