mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
e5620ab1e4
* feat(cli): Implement users account quota guards Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Remove comment Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Address PR comments - Getting `usersQuota` from `Settings` repo - Revert `isUserManagementEnabled` helper - Fix FE listing of users Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Refactor isWithinUserQuota getter and fix tests Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Revert testDb.ts changes Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Cleanup & improve types Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Fix duplicated method * Fix failing test * Remove `isUserManagementEnabled` completely Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Check for globalRole.name to determine if user is owner Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Fix unit tests Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Set isInstanceOwnerSetUp in specs * Fix SettingsUserView UM Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * refactor: License typings suggestions for users quota guards (#6636) refactor: License typings suggestions * Update packages/cli/src/Ldap/helpers.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Update packages/cli/test/integration/shared/utils.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Address PR comments Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> * Use 403 for all user quota related errors Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> --------- Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
141 lines
4 KiB
TypeScript
141 lines
4 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';
|
|
|
|
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;
|
|
|
|
beforeAll(async () => {
|
|
const app = await utils.initTestServer({ endpointGroups: ['license'] });
|
|
|
|
const globalOwnerRole = await testDb.getGlobalOwnerRole();
|
|
const globalMemberRole = await testDb.getGlobalMemberRole();
|
|
owner = await testDb.createUserShell(globalOwnerRole);
|
|
member = await testDb.createUserShell(globalMemberRole);
|
|
|
|
const authAgent = utils.createAuthAgent(app);
|
|
authOwnerAgent = authAgent(owner);
|
|
authMemberAgent = authAgent(member);
|
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', true);
|
|
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']);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await testDb.terminate();
|
|
});
|
|
|
|
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, { code: 403, message: NON_OWNER_ACTIVATE_RENEW_MESSAGE });
|
|
});
|
|
|
|
test('errors out properly', async () => {
|
|
License.prototype.activate = jest.fn().mockImplementation(() => {
|
|
throw new Error(ACTIVATION_FAILED_MESSAGE);
|
|
});
|
|
|
|
await authOwnerAgent
|
|
.post('/license/activate')
|
|
.send({ activationKey: 'abcde' })
|
|
.expect(400, { code: 400, message: ACTIVATION_FAILED_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 } = {
|
|
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';
|
|
const ACTIVATION_FAILED_MESSAGE = 'Failed to activate license';
|
|
const RENEW_ERROR_MESSAGE = 'Something went wrong when trying to renew license';
|