mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 09:04:07 -08:00
596c472ecc
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Val <68596159+valya@users.noreply.github.com> Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in> Co-authored-by: Valya Bullions <valya@n8n.io> Co-authored-by: Danny Martini <danny@n8n.io> Co-authored-by: Danny Martini <despair.blue@gmail.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: oleg <me@olegivaniv.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: Elias Meire <elias@meire.dev> Co-authored-by: Giulio Andreini <andreini@netseven.it> Co-authored-by: Giulio Andreini <g.andreini@gmail.com> Co-authored-by: Ayato Hayashi <go12limchangyong@gmail.com>
135 lines
3.8 KiB
TypeScript
135 lines
3.8 KiB
TypeScript
import type { SuperAgentTest } from 'supertest';
|
|
import config from '@/config';
|
|
import type { User } from '@db/entities/User';
|
|
import type { ILicensePostResponse, ILicenseReadResponse } from '@/Interfaces';
|
|
import { License } from '@/License';
|
|
import * as testDb from './shared/testDb';
|
|
import * as utils from './shared/utils/';
|
|
import { createUserShell } from './shared/db/users';
|
|
import { RESPONSE_ERROR_MESSAGES } from '@/constants';
|
|
|
|
const MOCK_SERVER_URL = 'https://server.com/v1';
|
|
const MOCK_RENEW_OFFSET = 259200;
|
|
|
|
let owner: User;
|
|
let member: User;
|
|
let authOwnerAgent: SuperAgentTest;
|
|
let authMemberAgent: SuperAgentTest;
|
|
|
|
const testServer = utils.setupTestServer({ endpointGroups: ['license'] });
|
|
|
|
beforeAll(async () => {
|
|
owner = await createUserShell('global:owner');
|
|
member = await createUserShell('global:member');
|
|
|
|
authOwnerAgent = testServer.authAgentFor(owner);
|
|
authMemberAgent = testServer.authAgentFor(member);
|
|
|
|
config.set('license.serverUrl', MOCK_SERVER_URL);
|
|
config.set('license.autoRenewEnabled', true);
|
|
config.set('license.autoRenewOffset', MOCK_RENEW_OFFSET);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await testDb.truncate(['Settings']);
|
|
});
|
|
|
|
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);
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|
|
|
|
describe('POST /license/activate', () => {
|
|
test('should work for instance owner', async () => {
|
|
await authOwnerAgent
|
|
.post('/license/activate')
|
|
.send({ activationKey: 'abcde' })
|
|
.expect(200, DEFAULT_POST_RESPONSE);
|
|
});
|
|
|
|
test('does not work for regular users', async () => {
|
|
await authMemberAgent
|
|
.post('/license/activate')
|
|
.send({ activationKey: 'abcde' })
|
|
.expect(403, { status: 'error', message: RESPONSE_ERROR_MESSAGES.MISSING_SCOPE });
|
|
});
|
|
|
|
test('errors out properly', async () => {
|
|
License.prototype.activate = jest.fn().mockImplementation(() => {
|
|
throw new Error('some fake error');
|
|
});
|
|
|
|
await authOwnerAgent
|
|
.post('/license/activate')
|
|
.send({ activationKey: 'abcde' })
|
|
.expect(400, { code: 400, message: `${ACTIVATION_FAILED_MESSAGE}: some fake error` });
|
|
});
|
|
});
|
|
|
|
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, { status: 'error', message: RESPONSE_ERROR_MESSAGES.MISSING_SCOPE });
|
|
});
|
|
|
|
test('errors out properly', async () => {
|
|
License.prototype.renew = jest.fn().mockImplementation(() => {
|
|
throw new Error(GENERIC_ERROR_MESSAGE);
|
|
});
|
|
|
|
await authOwnerAgent
|
|
.post('/license/renew')
|
|
.expect(400, { code: 400, message: `Failed to renew license: ${GENERIC_ERROR_MESSAGE}` });
|
|
});
|
|
});
|
|
|
|
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 UNAUTHORIZED_RESPONSE = { status: 'error', message: 'Unauthorized' };
|
|
const ACTIVATION_FAILED_MESSAGE = 'Failed to activate license';
|
|
const GENERIC_ERROR_MESSAGE = 'Something went wrong';
|