Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/internxt/drive-server-wip
Browse files Browse the repository at this point in the history
…into fix/unique-constraint-workspace-users
  • Loading branch information
jzunigax2 committed Nov 4, 2024
2 parents c0b1203 + c4c3028 commit 81ab0f2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/modules/workspaces/workspaces.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1931,8 +1931,37 @@ describe('WorkspacesUsecases', () => {
).rejects.toThrow(BadRequestException);
});

it('When new space to be assigned is higher than the max assignable space per user in the workspace, then it should throw', async () => {
const workspace = newWorkspace({ attributes: { numberOfSeats: 11 } });
const member = newWorkspaceUser({ attributes: { spaceLimit: 500 } });
const workspaceUser = newUser();
const mockedUsedSpace = 400;

jest.spyOn(workspaceRepository, 'findById').mockResolvedValue(workspace);
jest
.spyOn(workspaceRepository, 'findWorkspaceUser')
.mockResolvedValue(member);
jest.spyOn(userRepository, 'findByUuid').mockResolvedValue(workspaceUser);
jest
.spyOn(service, 'getAssignableSpaceInWorkspace')
.mockResolvedValue(2000);
jest.spyOn(member, 'getUsedSpace').mockReturnValue(mockedUsedSpace);
jest.spyOn(service, 'adjustOwnerStorage').mockResolvedValue();
jest
.spyOn(service, 'getWorkspaceNetworkLimit')
.mockResolvedValueOnce(10000);

await expect(
service.changeUserAssignedSpace(
workspaceId,
memberId,
changeAssignedSpace,
),
).rejects.toThrow(BadRequestException);
});

it('When new space to be assigned is valid, then it should update the space', async () => {
const workspace = newWorkspace();
const workspace = newWorkspace({ attributes: { numberOfSeats: 5 } });
const member = newWorkspaceUser({ attributes: { spaceLimit: 500 } });
const workspaceUser = newUser();
const mockedUsedSpace = 400;
Expand All @@ -1947,6 +1976,7 @@ describe('WorkspacesUsecases', () => {
.mockResolvedValue(2000);
jest.spyOn(member, 'getUsedSpace').mockReturnValue(mockedUsedSpace);
jest.spyOn(service, 'adjustOwnerStorage').mockResolvedValue();
jest.spyOn(service, 'getWorkspaceNetworkLimit').mockResolvedValue(10000);

const updatedMember = await service.changeUserAssignedSpace(
workspaceId,
Expand Down
11 changes: 10 additions & 1 deletion src/modules/workspaces/workspaces.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ export class WorkspacesUsecases {

if (newSpaceLimit > spaceLeftWithoutUser) {
throw new BadRequestException(
`Space limit set for the invitation is superior to the space assignable in workspace. Assignable space: ${spaceLeftWithoutUser}`,
`Space limit set for the user is superior to the space assignable in workspace. Assignable space: ${spaceLeftWithoutUser}`,
);
}

Expand All @@ -584,6 +584,15 @@ export class WorkspacesUsecases {
);
}

const workspaceLimit = await this.getWorkspaceNetworkLimit(workspace);
const maxSpacePerUser = workspaceLimit / workspace.numberOfSeats;

if (newSpaceLimit > maxSpacePerUser) {
throw new BadRequestException(
`Space limit set for the user is superior to the space assignable per user in workspace. Max space per user: ${maxSpacePerUser}`,
);
}

await this.adjustOwnerStorage(
workspaceId,
Math.abs(limitDifference),
Expand Down

0 comments on commit 81ab0f2

Please sign in to comment.