2023-03-17 09:24:05 -07:00
|
|
|
import type { SuperAgentTest } from 'supertest';
|
2022-12-20 01:52:01 -08:00
|
|
|
import config from '@/config';
|
2023-03-17 09:24:05 -07:00
|
|
|
import type { User } from '@db/entities/User';
|
2023-05-02 01:37:19 -07:00
|
|
|
import type { ILicensePostResponse, ILicenseReadResponse } from '@/Interfaces';
|
2022-12-20 01:52:01 -08:00
|
|
|
import { License } from '@/License';
|
2023-03-17 09:24:05 -07:00
|
|
|
import * as testDb from './shared/testDb';
|
|
|
|
import * as utils from './shared/utils';
|
2022-12-20 01:52:01 -08:00
|
|
|
|
|
|
|
const MOCK_SERVER_URL = 'https://server.com/v1';
|
|
|
|
const MOCK_RENEW_OFFSET = 259200;
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
let owner: User;
|
|
|
|
let member: User;
|
|
|
|
let authOwnerAgent: SuperAgentTest;
|
|
|
|
let authMemberAgent: SuperAgentTest;
|
2022-12-20 01:52:01 -08:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const app = await utils.initTestServer({ endpointGroups: ['license'] });
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const globalOwnerRole = await testDb.getGlobalOwnerRole();
|
|
|
|
const globalMemberRole = await testDb.getGlobalMemberRole();
|
|
|
|
owner = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
member = await testDb.createUserShell(globalMemberRole);
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const authAgent = utils.createAuthAgent(app);
|
|
|
|
authOwnerAgent = authAgent(owner);
|
|
|
|
authMemberAgent = authAgent(member);
|
2022-12-20 01:52:01 -08:00
|
|
|
|
|
|
|
config.set('license.serverUrl', MOCK_SERVER_URL);
|
|
|
|
config.set('license.autoRenewEnabled', true);
|
|
|
|
config.set('license.autoRenewOffset', MOCK_RENEW_OFFSET);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.truncate(['Settings']);
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.terminate();
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
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);
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
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);
|
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('POST /license/activate', () => {
|
|
|
|
test('should work for instance owner', async () => {
|
|
|
|
await authOwnerAgent
|
|
|
|
.post('/license/activate')
|
|
|
|
.send({ activationKey: 'abcde' })
|
|
|
|
.expect(200, DEFAULT_POST_RESPONSE);
|
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
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 });
|
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('errors out properly', async () => {
|
|
|
|
License.prototype.activate = jest.fn().mockImplementation(() => {
|
2023-05-03 05:26:29 -07:00
|
|
|
throw new Error(ACTIVATION_FAILED_MESSAGE);
|
2023-03-17 09:24:05 -07:00
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
|
|
|
.post('/license/activate')
|
|
|
|
.send({ activationKey: 'abcde' })
|
2023-05-03 05:26:29 -07:00
|
|
|
.expect(400, { code: 400, message: ACTIVATION_FAILED_MESSAGE });
|
2023-03-17 09:24:05 -07:00
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
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);
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('does not work for regular users', async () => {
|
|
|
|
await authMemberAgent
|
|
|
|
.post('/license/renew')
|
|
|
|
.expect(403, { code: 403, message: NON_OWNER_ACTIVATE_RENEW_MESSAGE });
|
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('errors out properly', async () => {
|
|
|
|
License.prototype.renew = jest.fn().mockImplementation(() => {
|
|
|
|
throw new Error(RENEW_ERROR_MESSAGE);
|
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
|
|
|
.post('/license/renew')
|
|
|
|
.expect(400, { code: 400, message: RENEW_ERROR_MESSAGE });
|
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
const DEFAULT_LICENSE_RESPONSE: { data: ILicenseReadResponse } = {
|
|
|
|
data: {
|
|
|
|
usage: {
|
|
|
|
executions: {
|
|
|
|
value: 0,
|
|
|
|
limit: -1,
|
|
|
|
warningThreshold: 0.8,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
license: {
|
|
|
|
planId: '',
|
|
|
|
planName: 'Community',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const DEFAULT_POST_RESPONSE: { data: ILicensePostResponse } = {
|
|
|
|
data: {
|
|
|
|
usage: {
|
|
|
|
executions: {
|
|
|
|
value: 0,
|
|
|
|
limit: -1,
|
|
|
|
warningThreshold: 0.8,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
license: {
|
|
|
|
planId: '',
|
|
|
|
planName: 'Community',
|
|
|
|
},
|
|
|
|
managementToken: '',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const NON_OWNER_ACTIVATE_RENEW_MESSAGE = 'Only an instance owner may activate or renew a license';
|
2023-05-03 05:26:29 -07:00
|
|
|
const ACTIVATION_FAILED_MESSAGE = 'Failed to activate license';
|
2022-12-20 01:52:01 -08:00
|
|
|
const RENEW_ERROR_MESSAGE = 'Something went wrong when trying to renew license';
|