2023-11-08 07:29:39 -08:00
|
|
|
import Container from 'typedi';
|
2023-07-13 01:14:48 -07:00
|
|
|
import type { SuperAgentTest } from 'supertest';
|
|
|
|
import type { Variables } from '@db/entities/Variables';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { VariablesRepository } from '@db/repositories/variables.repository';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { generateNanoId } from '@db/utils/generators';
|
2023-11-27 04:17:09 -08:00
|
|
|
import { VariablesService } from '@/environments/variables/variables.service.ee';
|
2023-11-10 06:04:26 -08:00
|
|
|
|
2023-04-18 03:41:55 -07:00
|
|
|
import * as testDb from './shared/testDb';
|
2023-07-13 01:14:48 -07:00
|
|
|
import * as utils from './shared/utils/';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { createOwner, createUser } from './shared/db/users';
|
2023-04-18 03:41:55 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
let authOwnerAgent: SuperAgentTest;
|
|
|
|
let authMemberAgent: SuperAgentTest;
|
2023-04-18 03:41:55 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const testServer = utils.setupTestServer({ endpointGroups: ['variables'] });
|
2023-11-30 00:23:09 -08:00
|
|
|
const license = testServer.license;
|
2023-04-18 03:41:55 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
async function createVariable(key: string, value: string) {
|
|
|
|
const result = await Container.get(VariablesRepository).save({
|
|
|
|
id: generateNanoId(),
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
await Container.get(VariablesService).updateCache();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getVariableByKey(key: string) {
|
|
|
|
return Container.get(VariablesRepository).findOne({
|
|
|
|
where: {
|
|
|
|
key,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getVariableById(id: string) {
|
|
|
|
return Container.get(VariablesRepository).findOne({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
beforeAll(async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const owner = await createOwner();
|
2023-07-13 01:14:48 -07:00
|
|
|
authOwnerAgent = testServer.authAgentFor(owner);
|
2023-11-08 07:29:39 -08:00
|
|
|
const member = await createUser();
|
2023-07-13 01:14:48 -07:00
|
|
|
authMemberAgent = testServer.authAgentFor(member);
|
2023-11-30 00:23:09 -08:00
|
|
|
|
|
|
|
license.setDefaults({
|
|
|
|
features: ['feat:variables'],
|
|
|
|
// quota: {
|
|
|
|
// 'quota:maxVariables': -1,
|
|
|
|
// },
|
|
|
|
});
|
2023-04-18 03:41:55 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await testDb.truncate(['Variables']);
|
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// GET /variables - fetch all variables
|
|
|
|
// ----------------------------------------
|
2023-07-13 01:14:48 -07:00
|
|
|
describe('GET /variables', () => {
|
|
|
|
beforeEach(async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
await Promise.all([createVariable('test1', 'value1'), createVariable('test2', 'value2')]);
|
2023-07-13 01:14:48 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should return all variables for an owner', async () => {
|
|
|
|
const response = await authOwnerAgent.get('/variables');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return all variables for a member', async () => {
|
|
|
|
const response = await authMemberAgent.get('/variables');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.length).toBe(2);
|
|
|
|
});
|
2023-04-18 03:41:55 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// GET /variables/:id - get a single variable
|
|
|
|
// ----------------------------------------
|
2023-07-13 01:14:48 -07:00
|
|
|
describe('GET /variables/:id', () => {
|
|
|
|
let var1: Variables, var2: Variables;
|
|
|
|
beforeEach(async () => {
|
|
|
|
[var1, var2] = await Promise.all([
|
2023-11-08 07:29:39 -08:00
|
|
|
createVariable('test1', 'value1'),
|
|
|
|
createVariable('test2', 'value2'),
|
2023-07-13 01:14:48 -07:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return a single variable for an owner', async () => {
|
|
|
|
const response1 = await authOwnerAgent.get(`/variables/${var1.id}`);
|
|
|
|
expect(response1.statusCode).toBe(200);
|
|
|
|
expect(response1.body.data.key).toBe('test1');
|
|
|
|
|
|
|
|
const response2 = await authOwnerAgent.get(`/variables/${var2.id}`);
|
|
|
|
expect(response2.statusCode).toBe(200);
|
|
|
|
expect(response2.body.data.key).toBe('test2');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return a single variable for a member', async () => {
|
|
|
|
const response1 = await authMemberAgent.get(`/variables/${var1.id}`);
|
|
|
|
expect(response1.statusCode).toBe(200);
|
|
|
|
expect(response1.body.data.key).toBe('test1');
|
|
|
|
|
|
|
|
const response2 = await authMemberAgent.get(`/variables/${var2.id}`);
|
|
|
|
expect(response2.statusCode).toBe(200);
|
|
|
|
expect(response2.body.data.key).toBe('test2');
|
|
|
|
});
|
2023-04-18 03:41:55 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// POST /variables - create a new variable
|
|
|
|
// ----------------------------------------
|
2023-07-13 01:14:48 -07:00
|
|
|
describe('POST /variables', () => {
|
|
|
|
const generatePayload = (i = 1) => ({
|
2023-04-18 03:41:55 -07:00
|
|
|
key: `create${i}`,
|
|
|
|
value: `createvalue${i}`,
|
2023-07-13 01:14:48 -07:00
|
|
|
});
|
|
|
|
const toCreate = generatePayload();
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should create a new variable and return it for an owner', async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.post('/variables').send(toCreate);
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.key).toBe(toCreate.key);
|
|
|
|
expect(response.body.data.value).toBe(toCreate.value);
|
|
|
|
|
|
|
|
const [byId, byKey] = await Promise.all([
|
2023-11-08 07:29:39 -08:00
|
|
|
getVariableById(response.body.data.id),
|
|
|
|
getVariableByKey(toCreate.key),
|
2023-07-13 01:14:48 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
expect(byId).not.toBeNull();
|
|
|
|
expect(byId!.key).toBe(toCreate.key);
|
|
|
|
expect(byId!.value).toBe(toCreate.value);
|
|
|
|
|
|
|
|
expect(byKey).not.toBeNull();
|
|
|
|
expect(byKey!.id).toBe(response.body.data.id);
|
|
|
|
expect(byKey!.value).toBe(toCreate.value);
|
|
|
|
});
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should not create a new variable and return it for a member', async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authMemberAgent.post('/variables').send(toCreate);
|
2023-11-27 04:17:09 -08:00
|
|
|
expect(response.statusCode).toBe(403);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(response.body.data?.key).not.toBe(toCreate.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toCreate.value);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const byKey = await getVariableByKey(toCreate.key);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(byKey).toBeNull();
|
|
|
|
});
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test("POST /variables should not create a new variable and return it if the instance doesn't have a license", async () => {
|
2023-11-30 00:23:09 -08:00
|
|
|
license.disable('feat:variables');
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.post('/variables').send(toCreate);
|
2023-11-27 04:17:09 -08:00
|
|
|
expect(response.statusCode).toBe(403);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(response.body.data?.key).not.toBe(toCreate.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toCreate.value);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const byKey = await getVariableByKey(toCreate.key);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(byKey).toBeNull();
|
|
|
|
});
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should fail to create a new variable and if one with the same key exists', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
await createVariable(toCreate.key, toCreate.value);
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.post('/variables').send(toCreate);
|
|
|
|
expect(response.statusCode).toBe(500);
|
|
|
|
expect(response.body.data?.key).not.toBe(toCreate.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toCreate.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not fail if variable limit not reached', async () => {
|
2023-11-30 00:23:09 -08:00
|
|
|
license.setQuota('quota:maxVariables', 5);
|
2023-07-13 01:14:48 -07:00
|
|
|
let i = 1;
|
|
|
|
let toCreate = generatePayload(i);
|
|
|
|
while (i < 3) {
|
2023-11-08 07:29:39 -08:00
|
|
|
await createVariable(toCreate.key, toCreate.value);
|
2023-07-13 01:14:48 -07:00
|
|
|
i++;
|
|
|
|
toCreate = generatePayload(i);
|
|
|
|
}
|
|
|
|
const response = await authOwnerAgent.post('/variables').send(toCreate);
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data?.key).toBe(toCreate.key);
|
|
|
|
expect(response.body.data?.value).toBe(toCreate.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail if variable limit reached', async () => {
|
2023-11-30 00:23:09 -08:00
|
|
|
license.setQuota('quota:maxVariables', 5);
|
2023-07-13 01:14:48 -07:00
|
|
|
let i = 1;
|
|
|
|
let toCreate = generatePayload(i);
|
|
|
|
while (i < 6) {
|
2023-11-08 07:29:39 -08:00
|
|
|
await createVariable(toCreate.key, toCreate.value);
|
2023-07-13 01:14:48 -07:00
|
|
|
i++;
|
|
|
|
toCreate = generatePayload(i);
|
|
|
|
}
|
|
|
|
const response = await authOwnerAgent.post('/variables').send(toCreate);
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
expect(response.body.data?.key).not.toBe(toCreate.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toCreate.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail if key too long', async () => {
|
|
|
|
const toCreate = {
|
|
|
|
// 51 'a's
|
|
|
|
key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
|
|
|
value: 'value',
|
2023-04-18 03:41:55 -07:00
|
|
|
};
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.post('/variables').send(toCreate);
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
expect(response.body.data?.key).not.toBe(toCreate.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toCreate.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail if value too long', async () => {
|
|
|
|
const toCreate = {
|
|
|
|
key: 'key',
|
|
|
|
// 256 'a's
|
|
|
|
value:
|
|
|
|
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
2023-04-18 03:41:55 -07:00
|
|
|
};
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.post('/variables').send(toCreate);
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
expect(response.body.data?.key).not.toBe(toCreate.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toCreate.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should fail if key contain's prohibited characters", async () => {
|
|
|
|
const toCreate = {
|
|
|
|
// 51 'a's
|
|
|
|
key: 'te$t',
|
|
|
|
value: 'value',
|
|
|
|
};
|
|
|
|
const response = await authOwnerAgent.post('/variables').send(toCreate);
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
expect(response.body.data?.key).not.toBe(toCreate.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toCreate.value);
|
|
|
|
});
|
2023-04-18 03:41:55 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// PATCH /variables/:id - change a variable
|
|
|
|
// ----------------------------------------
|
2023-07-13 01:14:48 -07:00
|
|
|
describe('PATCH /variables/:id', () => {
|
2023-04-18 03:41:55 -07:00
|
|
|
const toModify = {
|
|
|
|
key: 'create1',
|
|
|
|
value: 'createvalue1',
|
|
|
|
};
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should modify existing variable if use is an owner', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const variable = await createVariable('test1', 'value1');
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.patch(`/variables/${variable.id}`).send(toModify);
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.key).toBe(toModify.key);
|
|
|
|
expect(response.body.data.value).toBe(toModify.value);
|
|
|
|
|
|
|
|
const [byId, byKey] = await Promise.all([
|
2023-11-08 07:29:39 -08:00
|
|
|
getVariableById(response.body.data.id),
|
|
|
|
getVariableByKey(toModify.key),
|
2023-07-13 01:14:48 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
expect(byId).not.toBeNull();
|
|
|
|
expect(byId!.key).toBe(toModify.key);
|
|
|
|
expect(byId!.value).toBe(toModify.value);
|
|
|
|
|
|
|
|
expect(byKey).not.toBeNull();
|
|
|
|
expect(byKey!.id).toBe(response.body.data.id);
|
|
|
|
expect(byKey!.value).toBe(toModify.value);
|
|
|
|
});
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should modify existing variable if use is an owner', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const variable = await createVariable('test1', 'value1');
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.patch(`/variables/${variable.id}`).send(toModify);
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.key).toBe(toModify.key);
|
|
|
|
expect(response.body.data.value).toBe(toModify.value);
|
|
|
|
|
|
|
|
const [byId, byKey] = await Promise.all([
|
2023-11-08 07:29:39 -08:00
|
|
|
getVariableById(response.body.data.id),
|
|
|
|
getVariableByKey(toModify.key),
|
2023-07-13 01:14:48 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
expect(byId).not.toBeNull();
|
|
|
|
expect(byId!.key).toBe(toModify.key);
|
|
|
|
expect(byId!.value).toBe(toModify.value);
|
|
|
|
|
|
|
|
expect(byKey).not.toBeNull();
|
|
|
|
expect(byKey!.id).toBe(response.body.data.id);
|
|
|
|
expect(byKey!.value).toBe(toModify.value);
|
|
|
|
});
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should not modify existing variable if use is a member', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const variable = await createVariable('test1', 'value1');
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authMemberAgent.patch(`/variables/${variable.id}`).send(toModify);
|
2023-11-27 04:17:09 -08:00
|
|
|
expect(response.statusCode).toBe(403);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(response.body.data?.key).not.toBe(toModify.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toModify.value);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const byId = await getVariableById(variable.id);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(byId).not.toBeNull();
|
|
|
|
expect(byId!.key).not.toBe(toModify.key);
|
|
|
|
expect(byId!.value).not.toBe(toModify.value);
|
|
|
|
});
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should not modify existing variable if one with the same key exists', async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
const [var1, var2] = await Promise.all([
|
2023-11-08 07:29:39 -08:00
|
|
|
createVariable('test1', 'value1'),
|
|
|
|
createVariable(toModify.key, toModify.value),
|
2023-07-13 01:14:48 -07:00
|
|
|
]);
|
|
|
|
const response = await authOwnerAgent.patch(`/variables/${var1.id}`).send(toModify);
|
|
|
|
expect(response.statusCode).toBe(500);
|
|
|
|
expect(response.body.data?.key).not.toBe(toModify.key);
|
|
|
|
expect(response.body.data?.value).not.toBe(toModify.value);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const byId = await getVariableById(var1.id);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(byId).not.toBeNull();
|
|
|
|
expect(byId!.key).toBe(var1.key);
|
|
|
|
expect(byId!.value).toBe(var1.value);
|
|
|
|
});
|
2023-04-18 03:41:55 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// DELETE /variables/:id - change a variable
|
|
|
|
// ----------------------------------------
|
2023-07-13 01:14:48 -07:00
|
|
|
describe('DELETE /variables/:id', () => {
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should delete a single variable for an owner', async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
const [var1, var2, var3] = await Promise.all([
|
2023-11-08 07:29:39 -08:00
|
|
|
createVariable('test1', 'value1'),
|
|
|
|
createVariable('test2', 'value2'),
|
|
|
|
createVariable('test3', 'value3'),
|
2023-07-13 01:14:48 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
const delResponse = await authOwnerAgent.delete(`/variables/${var1.id}`);
|
|
|
|
expect(delResponse.statusCode).toBe(200);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const byId = await getVariableById(var1.id);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(byId).toBeNull();
|
|
|
|
|
|
|
|
const getResponse = await authOwnerAgent.get('/variables');
|
|
|
|
expect(getResponse.body.data.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
2023-08-02 05:51:09 -07:00
|
|
|
test('should not delete a single variable for a member', async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
const [var1, var2, var3] = await Promise.all([
|
2023-11-08 07:29:39 -08:00
|
|
|
createVariable('test1', 'value1'),
|
|
|
|
createVariable('test2', 'value2'),
|
|
|
|
createVariable('test3', 'value3'),
|
2023-07-13 01:14:48 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
const delResponse = await authMemberAgent.delete(`/variables/${var1.id}`);
|
2023-11-27 04:17:09 -08:00
|
|
|
expect(delResponse.statusCode).toBe(403);
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const byId = await getVariableById(var1.id);
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(byId).not.toBeNull();
|
|
|
|
|
|
|
|
const getResponse = await authMemberAgent.get('/variables');
|
|
|
|
expect(getResponse.body.data.length).toBe(3);
|
|
|
|
});
|
2023-04-18 03:41:55 -07:00
|
|
|
});
|