Skip to content

Commit

Permalink
test: add tests for deleting users from project
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcL committed Dec 20, 2024
1 parent d471ed1 commit 0cd58cb
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 2 deletions.
126 changes: 124 additions & 2 deletions packages/cli/test/integration/public-api/projects.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { FeatureNotLicensedError } from '@/errors/feature-not-licensed.error';
import { Telemetry } from '@/telemetry';
import { mockInstance } from '@test/mocking';
import { createTeamProject, getProjectByNameOrFail } from '@test-integration/db/projects';
import { createMemberWithApiKey, createOwnerWithApiKey } from '@test-integration/db/users';
import {
createTeamProject,
getProjectByNameOrFail,
linkUserToProject,
getAllProjectRelations,
} from '@test-integration/db/projects';
import {
createMemberWithApiKey,
createOwnerWithApiKey,
createMember,
} from '@test-integration/db/users';
import { setupTestServer } from '@test-integration/utils';

import * as testDb from '../shared/test-db';
Expand Down Expand Up @@ -393,4 +402,117 @@ describe('Projects in Public API', () => {
expect(response.body).toHaveProperty('message', 'Forbidden');
});
});

describe('DELETE /projects/:id/users/:userId', () => {
it('if not authenticated, should reject with 401', async () => {
const project = await createTeamProject();
const member = await createMember();

const response = await testServer
.publicApiAgentWithoutApiKey()
.delete(`/projects/${project.id}/users/${member.id}`);

expect(response.status).toBe(401);
expect(response.body).toHaveProperty('message', "'X-N8N-API-KEY' header required");
});

it('if not licensed, should reject with a 403', async () => {
const owner = await createOwnerWithApiKey();
const project = await createTeamProject();
const member = await createMember();

const response = await testServer
.publicApiAgentFor(owner)
.delete(`/projects/${project.id}/users/${member.id}`);

expect(response.status).toBe(403);
expect(response.body).toHaveProperty(
'message',
new FeatureNotLicensedError('feat:projectRole:admin').message,
);
});

it('if missing scope, should reject with 403', async () => {
testServer.license.setQuota('quota:maxTeamProjects', -1);
testServer.license.enable('feat:projectRole:admin');
const member = await createMemberWithApiKey();
const project = await createTeamProject();

const response = await testServer
.publicApiAgentFor(member)
.delete(`/projects/${project.id}/users/${member.id}`);

expect(response.status).toBe(403);
expect(response.body).toHaveProperty('message', 'Forbidden');
});

describe('when user has correct license', () => {
beforeEach(() => {
testServer.license.setQuota('quota:maxTeamProjects', -1);
testServer.license.enable('feat:projectRole:admin');
});

it('should remove given user from project', async () => {
const owner = await createOwnerWithApiKey();
const project = await createTeamProject('shared-project', owner);
const member = await createMember();
await linkUserToProject(member, project, 'project:viewer');
const projectBefore = await getAllProjectRelations({
projectId: project.id,
});

const response = await testServer
.publicApiAgentFor(owner)
.delete(`/projects/${project.id}/users/${member.id}`);

const projectAfter = await getAllProjectRelations({
projectId: project.id,
});

expect(response.status).toBe(204);
expect(projectBefore.length).toEqual(2);
expect(projectBefore[0].userId).toEqual(owner.id);
expect(projectBefore[1].userId).toEqual(member.id);

expect(projectAfter.length).toEqual(1);
expect(projectBefore[0].userId).toEqual(owner.id);
});

it('should reject with 404 if no project found', async () => {
const owner = await createOwnerWithApiKey();
const member = await createMember();

const response = await testServer
.publicApiAgentFor(owner)
.delete(`/projects/123456/users/${member.id}`);

expect(response.status).toBe(404);
expect(response.body).toHaveProperty('message', 'Not found');
});

it('should remain unchanged if user if not in project', async () => {
const owner = await createOwnerWithApiKey();
const project = await createTeamProject('shared-project', owner);
const member = await createMember();
const projectBefore = await getAllProjectRelations({
projectId: project.id,
});

const response = await testServer
.publicApiAgentFor(owner)
.delete(`/projects/${project.id}/users/${member.id}`);

const projectAfter = await getAllProjectRelations({
projectId: project.id,
});

expect(response.status).toBe(204);
expect(projectBefore.length).toEqual(1);
expect(projectBefore[0].userId).toEqual(owner.id);

expect(projectAfter.length).toEqual(1);
expect(projectBefore[0].userId).toEqual(owner.id);
});
});
});
});
8 changes: 8 additions & 0 deletions packages/cli/test/integration/shared/db/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ export const getProjectRelations = async ({
where: { projectId, userId, role },
});
};

export const getAllProjectRelations = async ({
projectId,
}: Partial<ProjectRelation>): Promise<ProjectRelation[]> => {
return await Container.get(ProjectRelationRepository).find({
where: { projectId },
});
};

0 comments on commit 0cd58cb

Please sign in to comment.