Skip to content

Commit

Permalink
Merge pull request #379 from internxt/fix/allow-remove-sharings
Browse files Browse the repository at this point in the history
fix: allow remove sharings in workspaces
  • Loading branch information
apsantiso authored Jul 31, 2024
2 parents 2559e25 + ccb6d3e commit f125796
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/modules/sharing/sharing.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ export class SharingController {
})
@ApiOkResponse({ description: 'Item removed from sharing' })
@ApiBearerAuth()
@WorkspacesInBehalfGuard([
{ sourceKey: 'params', fieldName: 'itemId' },
{ sourceKey: 'params', fieldName: 'itemType' },
])
removeSharing(
@UserDecorator() user: User,
@Param('itemType') itemType: Sharing['itemType'],
Expand Down
51 changes: 51 additions & 0 deletions src/modules/sharing/sharing.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { SequelizeUserReferralsRepository } from '../user/user-referrals.reposit
import {
SharedWithType,
SharingActionName,
SharingItemType,
SharingType,
} from './sharing.domain';
import { FileStatus } from '../file/file.domain';
Expand Down Expand Up @@ -598,6 +599,56 @@ describe('Sharing Use Cases', () => {
});
});

describe('removeSharing', () => {
const owner = newUser();
const itemFile = newFile();
const itemId = itemFile.uuid;
const itemType = SharingItemType.File;
const sharing = newSharing({ owner, item: itemFile });

it('When sharing exists and user is owner, then it removes invites and sharings', async () => {
sharingRepository.findOneSharing.mockResolvedValue(sharing);

await sharingService.removeSharing(owner, itemId, itemType);

expect(sharingRepository.findOneSharing).toHaveBeenCalledWith({
itemId,
itemType,
});
expect(sharingRepository.deleteInvitesBy).toHaveBeenCalledWith({
itemId,
itemType,
});
expect(sharingRepository.deleteSharingsBy).toHaveBeenCalledWith({
itemId,
itemType,
});
});

it('When sharing does not exist, then it does nothing', async () => {
sharingRepository.findOneSharing.mockResolvedValue(null);

await sharingService.removeSharing(owner, itemId, itemType);

expect(sharingRepository.findOneSharing).toHaveBeenCalledWith({
itemId,
itemType,
});
expect(sharingRepository.deleteInvitesBy).not.toHaveBeenCalled();
expect(sharingRepository.deleteSharingsBy).not.toHaveBeenCalled();
});

it('When user is not owner, then it throws', async () => {
const otherUser = newUser();

sharingRepository.findOneSharing.mockResolvedValue(sharing);

await expect(
sharingService.removeSharing(otherUser, itemId, itemType),
).rejects.toThrow(ForbiddenException);
});
});

describe('getSharedFoldersInWorkspace', () => {
const user = newUser();
const teamId = v4();
Expand Down
2 changes: 0 additions & 2 deletions src/modules/sharing/sharing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,6 @@ export class SharingService {
user: User,
itemId: Sharing['itemId'],
itemType: Sharing['itemType'],
sharedWithType: SharedWithType = SharedWithType.Individual,
) {
const sharing = await this.sharingRepository.findOneSharing({
itemId,
Expand All @@ -1484,7 +1483,6 @@ export class SharingService {
await this.sharingRepository.deleteSharingsBy({
itemId,
itemType,
sharedWithType,
});
}

Expand Down

0 comments on commit f125796

Please sign in to comment.