feat(API): Add user management endpoints to the Projects Public API (#12329)

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
Co-authored-by: Danny Martini <danny@n8n.io>
Co-authored-by: Andreas Fitzek <andreas.fitzek@n8n.io>
Co-authored-by: Guillaume Jacquart <jacquart.guillaume@gmail.com>
This commit is contained in:
Marc Littlemore
2025-05-30 12:04:38 +01:00
committed by GitHub
parent c229e915ea
commit 4459c7e7b1
25 changed files with 1391 additions and 71 deletions

View File

@@ -1,24 +1,28 @@
import { SharedWorkflowRepository } from '@n8n/db';
import { Container } from '@n8n/di';
import { License } from '@/license';
import { ProjectService } from '@/services/project.service.ee';
import { LicenseMocker } from '@test-integration/license';
import { linkUserToProject, createTeamProject } from './shared/db/projects';
import { linkUserToProject, createTeamProject, getAllProjectRelations } from './shared/db/projects';
import { createUser } from './shared/db/users';
import { createWorkflow } from './shared/db/workflows';
import * as testDb from './shared/test-db';
describe('ProjectService', () => {
let projectService: ProjectService;
let sharedWorkflowRepository: SharedWorkflowRepository;
beforeAll(async () => {
await testDb.init();
projectService = Container.get(ProjectService);
sharedWorkflowRepository = Container.get(SharedWorkflowRepository);
const license: LicenseMocker = new LicenseMocker();
license.mock(Container.get(License));
license.enable('feat:projectRole:editor');
});
afterEach(async () => {
@@ -35,6 +39,71 @@ describe('ProjectService', () => {
await testDb.terminate();
});
describe('addUsersToProject', () => {
it("don't throw a unique constraint violation error when adding a user that is already part of the project", async () => {
// ARRANGE
const user = await createUser();
const project = await createTeamProject('project', user);
// ACT
// add user again
await projectService.addUsersToProject(project.id, [
{ userId: user.id, role: 'project:admin' },
]);
// ASSERT
const relations = await getAllProjectRelations({ projectId: project.id });
expect(relations).toHaveLength(1);
expect(relations[0]).toMatchObject({
projectId: project.id,
userId: user.id,
role: 'project:admin',
});
});
it('allows changing a users role', async () => {
// ARRANGE
const user = await createUser();
const project = await createTeamProject('project', user);
// ACT
// add user again
await projectService.addUsersToProject(project.id, [
{ userId: user.id, role: 'project:editor' },
]);
// ASSERT
const relations = await getAllProjectRelations({ projectId: project.id });
expect(relations).toHaveLength(1);
expect(relations[0]).toMatchObject({
projectId: project.id,
userId: user.id,
role: 'project:editor',
});
});
});
describe('addUser', () => {
it("don't throw a unique constraint violation error when adding a user that is already part of the project", async () => {
// ARRANGE
const user = await createUser();
const project = await createTeamProject('project', user);
// ACT
// add user again
await projectService.addUser(project.id, { userId: user.id, role: 'project:admin' });
// ASSERT
const relations = await getAllProjectRelations({ projectId: project.id });
expect(relations).toHaveLength(1);
expect(relations[0]).toMatchObject({
projectId: project.id,
userId: user.id,
role: 'project:admin',
});
});
});
describe('findRolesInProjects', () => {
describe('when user has roles in projects where workflow is accessible', () => {
it('should return roles and project IDs', async () => {