Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BR-250] fix: allow remove sharings in workspaces #379

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading