feat(core): Allow admin creation (#7837)

https://linear.app/n8n/issue/PAY-1038
This commit is contained in:
Iván Ovejero
2023-11-29 13:55:41 +01:00
committed by GitHub
parent 5ba5ed8e3c
commit 476806ebb0
9 changed files with 318 additions and 136 deletions

View File

@@ -18,9 +18,16 @@ import * as testDb from './shared/testDb';
import type { SuperAgentTest } from 'supertest';
import type { Role } from '@db/entities/Role';
import type { User } from '@db/entities/User';
import { License } from '@/License';
import { mockInstance } from '../shared/mocking';
const testServer = utils.setupTestServer({ endpointGroups: ['users'] });
const license = mockInstance(License, {
isAdvancedPermissionsLicensed: jest.fn().mockReturnValue(true),
isWithinUsersLimit: jest.fn().mockReturnValue(true),
});
describe('GET /users', () => {
let owner: User;
let member: User;
@@ -362,6 +369,7 @@ describe('PATCH /users/:id/role', () => {
NO_USER_TO_OWNER,
NO_USER,
NO_OWNER_ON_OWNER,
NO_ADMIN_IF_UNLICENSED,
} = UsersController.ERROR_MESSAGES.CHANGE_ROLE;
beforeAll(async () => {
@@ -518,6 +526,17 @@ describe('PATCH /users/:id/role', () => {
expect(response.body.message).toBe(NO_USER_TO_OWNER);
});
test('should fail to promote member to admin if not licensed', async () => {
license.isAdvancedPermissionsLicensed.mockReturnValueOnce(false);
const response = await adminAgent.patch(`/users/${member.id}/role`).send({
newRole: { scope: 'global', name: 'admin' },
});
expect(response.statusCode).toBe(403);
expect(response.body.message).toBe(NO_ADMIN_IF_UNLICENSED);
});
test('should be able to demote admin to member', async () => {
const response = await adminAgent.patch(`/users/${otherAdmin.id}/role`).send({
newRole: { scope: 'global', name: 'member' },
@@ -556,7 +575,7 @@ describe('PATCH /users/:id/role', () => {
adminAgent = testServer.authAgentFor(admin);
});
test('should be able to promote member to admin', async () => {
test('should be able to promote member to admin if licensed', async () => {
const response = await adminAgent.patch(`/users/${member.id}/role`).send({
newRole: { scope: 'global', name: 'admin' },
});
@@ -613,7 +632,18 @@ describe('PATCH /users/:id/role', () => {
expect(response.body.message).toBe(NO_USER_TO_OWNER);
});
test('should be able to promote member to admin', async () => {
test('should fail to promote member to admin if not licensed', async () => {
license.isAdvancedPermissionsLicensed.mockReturnValueOnce(false);
const response = await ownerAgent.patch(`/users/${member.id}/role`).send({
newRole: { scope: 'global', name: 'admin' },
});
expect(response.statusCode).toBe(403);
expect(response.body.message).toBe(NO_ADMIN_IF_UNLICENSED);
});
test('should be able to promote member to admin if licensed', async () => {
const response = await ownerAgent.patch(`/users/${member.id}/role`).send({
newRole: { scope: 'global', name: 'admin' },
});