ci: Refactor cli tests to speed up CI (no-changelog) (#5718)

* ci: Refactor cli tests to speed up CI (no-changelog)

* upgrade jest to address memory leaks
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-03-17 17:24:05 +01:00
committed by GitHub
parent be172cb720
commit 6242cac53b
29 changed files with 4229 additions and 4762 deletions

View File

@@ -1,41 +1,36 @@
import express from 'express';
import type { SuperAgentTest } from 'supertest';
import config from '@/config';
import type { Role } from '@db/entities/Role';
import * as testDb from './shared/testDb';
import type { AuthAgent } from './shared/types';
import * as utils from './shared/utils';
import type { User } from '@db/entities/User';
import { ILicensePostResponse, ILicenseReadResponse } from '@/Interfaces';
import { License } from '@/License';
import * as testDb from './shared/testDb';
import * as utils from './shared/utils';
const MOCK_SERVER_URL = 'https://server.com/v1';
const MOCK_RENEW_OFFSET = 259200;
const MOCK_INSTANCE_ID = 'instance-id';
let app: express.Application;
let globalOwnerRole: Role;
let globalMemberRole: Role;
let authAgent: AuthAgent;
let license: License;
let owner: User;
let member: User;
let authOwnerAgent: SuperAgentTest;
let authMemberAgent: SuperAgentTest;
beforeAll(async () => {
app = await utils.initTestServer({ endpointGroups: ['license'] });
const app = await utils.initTestServer({ endpointGroups: ['license'] });
globalOwnerRole = await testDb.getGlobalOwnerRole();
globalMemberRole = await testDb.getGlobalMemberRole();
const globalOwnerRole = await testDb.getGlobalOwnerRole();
const globalMemberRole = await testDb.getGlobalMemberRole();
owner = await testDb.createUserShell(globalOwnerRole);
member = await testDb.createUserShell(globalMemberRole);
authAgent = utils.createAuthAgent(app);
const authAgent = utils.createAuthAgent(app);
authOwnerAgent = authAgent(owner);
authMemberAgent = authAgent(member);
config.set('license.serverUrl', MOCK_SERVER_URL);
config.set('license.autoRenewEnabled', true);
config.set('license.autoRenewOffset', MOCK_RENEW_OFFSET);
});
beforeEach(async () => {
license = new License();
await license.init(MOCK_INSTANCE_ID);
});
afterEach(async () => {
await testDb.truncate(['Settings']);
});
@@ -44,98 +39,66 @@ afterAll(async () => {
await testDb.terminate();
});
test('GET /license should return license information to the instance owner', async () => {
const userShell = await testDb.createUserShell(globalOwnerRole);
const response = await authAgent(userShell).get('/license');
expect(response.statusCode).toBe(200);
// No license defined so we just expect the result to be the defaults
expect(response.body).toStrictEqual(DEFAULT_LICENSE_RESPONSE);
});
test('GET /license should return license information to a regular user', async () => {
const userShell = await testDb.createUserShell(globalMemberRole);
const response = await authAgent(userShell).get('/license');
expect(response.statusCode).toBe(200);
// No license defined so we just expect the result to be the defaults
expect(response.body).toStrictEqual(DEFAULT_LICENSE_RESPONSE);
});
test('POST /license/activate should work for instance owner', async () => {
const userShell = await testDb.createUserShell(globalOwnerRole);
const response = await authAgent(userShell)
.post('/license/activate')
.send({ activationKey: 'abcde' });
expect(response.statusCode).toBe(200);
// No license defined so we just expect the result to be the defaults
expect(response.body).toMatchObject(DEFAULT_POST_RESPONSE);
});
test('POST /license/activate does not work for regular users', async () => {
const userShell = await testDb.createUserShell(globalMemberRole);
const response = await authAgent(userShell)
.post('/license/activate')
.send({ activationKey: 'abcde' });
expect(response.statusCode).toBe(403);
expect(response.body.message).toBe(NON_OWNER_ACTIVATE_RENEW_MESSAGE);
});
test('POST /license/activate errors out properly', async () => {
License.prototype.activate = jest.fn().mockImplementation(() => {
throw new Error(INVALID_ACIVATION_KEY_MESSAGE);
describe('GET /license', () => {
test('should return license information to the instance owner', async () => {
// No license defined so we just expect the result to be the defaults
await authOwnerAgent.get('/license').expect(200, DEFAULT_LICENSE_RESPONSE);
});
const userShell = await testDb.createUserShell(globalOwnerRole);
const response = await authAgent(userShell)
.post('/license/activate')
.send({ activationKey: 'abcde' });
expect(response.statusCode).toBe(400);
expect(response.body.message).toBe(INVALID_ACIVATION_KEY_MESSAGE);
test('should return license information to a regular user', async () => {
// No license defined so we just expect the result to be the defaults
await authMemberAgent.get('/license').expect(200, DEFAULT_LICENSE_RESPONSE);
});
});
test('POST /license/renew should work for instance owner', async () => {
const userShell = await testDb.createUserShell(globalOwnerRole);
const response = await authAgent(userShell).post('/license/renew');
expect(response.statusCode).toBe(200);
// No license defined so we just expect the result to be the defaults
expect(response.body).toMatchObject(DEFAULT_POST_RESPONSE);
});
test('POST /license/renew does not work for regular users', async () => {
const userShell = await testDb.createUserShell(globalMemberRole);
const response = await authAgent(userShell).post('/license/renew');
expect(response.statusCode).toBe(403);
expect(response.body.message).toBe(NON_OWNER_ACTIVATE_RENEW_MESSAGE);
});
test('POST /license/renew errors out properly', async () => {
License.prototype.renew = jest.fn().mockImplementation(() => {
throw new Error(RENEW_ERROR_MESSAGE);
describe('POST /license/activate', () => {
test('should work for instance owner', async () => {
await authOwnerAgent
.post('/license/activate')
.send({ activationKey: 'abcde' })
.expect(200, DEFAULT_POST_RESPONSE);
});
const userShell = await testDb.createUserShell(globalOwnerRole);
test('does not work for regular users', async () => {
await authMemberAgent
.post('/license/activate')
.send({ activationKey: 'abcde' })
.expect(403, { code: 403, message: NON_OWNER_ACTIVATE_RENEW_MESSAGE });
});
const response = await authAgent(userShell).post('/license/renew');
test('errors out properly', async () => {
License.prototype.activate = jest.fn().mockImplementation(() => {
throw new Error(INVALID_ACIVATION_KEY_MESSAGE);
});
expect(response.statusCode).toBe(400);
expect(response.body.message).toBe(RENEW_ERROR_MESSAGE);
await authOwnerAgent
.post('/license/activate')
.send({ activationKey: 'abcde' })
.expect(400, { code: 400, message: INVALID_ACIVATION_KEY_MESSAGE });
});
});
describe('POST /license/renew', () => {
test('should work for instance owner', async () => {
// No license defined so we just expect the result to be the defaults
await authOwnerAgent.post('/license/renew').expect(200, DEFAULT_POST_RESPONSE);
});
test('does not work for regular users', async () => {
await authMemberAgent
.post('/license/renew')
.expect(403, { code: 403, message: NON_OWNER_ACTIVATE_RENEW_MESSAGE });
});
test('errors out properly', async () => {
License.prototype.renew = jest.fn().mockImplementation(() => {
throw new Error(RENEW_ERROR_MESSAGE);
});
await authOwnerAgent
.post('/license/renew')
.expect(400, { code: 400, message: RENEW_ERROR_MESSAGE });
});
});
const DEFAULT_LICENSE_RESPONSE: { data: ILicenseReadResponse } = {