Merge pull request #1350 from automatisch/test/get-config

test: Implement tests for getConfig graphQL query
This commit is contained in:
Ömer Faruk Aydın
2023-10-16 15:12:53 +02:00
committed by GitHub
4 changed files with 129 additions and 10 deletions

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

View File

@@ -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;

View 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;

View File

@@ -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;