Skip to content
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
17 changes: 15 additions & 2 deletions src/elements/content-sharing/ContentSharingV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,23 @@ function ContentSharingV2({
const [avatarURLMap, setAvatarURLMap] = React.useState<AvatarURLMap | null>(null);
const [item, setItem] = React.useState<Item | null>(null);
const [sharedLink, setSharedLink] = React.useState<SharedLink | null>(null);
const [sharingServiceProps, setSharingServiceProps] = React.useState(null);
const [currentUser, setCurrentUser] = React.useState<User | null>(null);
const [collaborationRoles, setCollaborationRoles] = React.useState<CollaborationRole[] | null>(null);
const [collaborators, setCollaborators] = React.useState<Collaborator[] | null>(null);
const [collaboratorsData, setCollaboratorsData] = React.useState<Collaborations | null>(null);
const [owner, setOwner] = React.useState({ id: '', email: '', name: '' });

const { sharingService } = useSharingService(api, item, itemID, itemType, setItem, setSharedLink);
const { sharingService } = useSharingService({
api,
item,
itemId: itemID,
itemType,
sharedLink,
sharingServiceProps,
setItem,
setSharedLink,
});

// Handle successful GET requests to /files or /folders
const handleGetItemSuccess = React.useCallback(itemData => {
Expand All @@ -58,10 +68,12 @@ function ContentSharingV2({
item: itemFromApi,
ownedBy,
sharedLink: sharedLinkFromApi,
sharingService: sharingServicePropsFromApi,
} = convertItemResponse(itemData);

setItem(itemFromApi);
setSharedLink(sharedLinkFromApi);
setSharingServiceProps(sharingServicePropsFromApi);
setCollaborationRoles(collaborationRolesFromApi);
setOwner({ id: ownedBy.id, email: ownedBy.login, name: ownedBy.name });
}, []);
Expand Down Expand Up @@ -92,11 +104,12 @@ function ContentSharingV2({
if (!api || isEmpty(api) || !item || currentUser) return;

const getUserSuccess = userData => {
const { enterprise, id } = userData;
const { id, enterprise, hostname } = userData;
setCurrentUser({
id,
enterprise: { name: enterprise ? enterprise.name : '' },
});
setSharedLink(prevSharedLink => ({ ...prevSharedLink, serverURL: hostname ? `${hostname}v/` : '' }));
};

(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('elements/content-sharing/ContentSharingV2', () => {
test('should render UnifiedShareModal when sharingService is available', async () => {
const mockSharingService = {
changeSharedLinkPermission: jest.fn(),
updateSharedLink: jest.fn(),
};

(useSharingService as jest.Mock).mockReturnValue({
Expand Down
169 changes: 144 additions & 25 deletions src/elements/content-sharing/__tests__/sharingService.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import { PERMISSION_CAN_DOWNLOAD, PERMISSION_CAN_PREVIEW } from '../../../constants';
import { CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS } from '../constants';
import { convertSharedLinkPermissions, createSharingService } from '../sharingService';
import { createSharingService } from '../sharingService';
import { convertSharedLinkPermissions, convertSharedLinkSettings } from '../utils';

jest.mock('../utils');

const mockItemApiInstance = {
updateSharedLink: jest.fn(),
};
const options = { id: '123', permissions: { can_set_share_access: true, can_share: true } };
const mockOnSuccess = jest.fn();

describe('elements/content-sharing/sharingService', () => {
describe('convertSharedLinkPermissions', () => {
test.each([
[PERMISSION_CAN_DOWNLOAD, { [PERMISSION_CAN_DOWNLOAD]: true, [PERMISSION_CAN_PREVIEW]: false }],
[PERMISSION_CAN_PREVIEW, { [PERMISSION_CAN_DOWNLOAD]: false, [PERMISSION_CAN_PREVIEW]: true }],
])('should return correct permissions for download permission level', (permissionLevel, expected) => {
const result = convertSharedLinkPermissions(permissionLevel);
expect(result).toEqual(expected);
beforeEach(() => {
(convertSharedLinkPermissions as jest.Mock).mockReturnValue({
[PERMISSION_CAN_DOWNLOAD]: true,
[PERMISSION_CAN_PREVIEW]: false,
});

test('should handle empty string permission level', () => {
const result = convertSharedLinkPermissions('');
expect(result).toEqual({});
(convertSharedLinkSettings as jest.Mock).mockReturnValue({
unshared_at: null,
vanity_url: 'https://example.com/vanity-url',
});
});

describe('createSharingService', () => {
const mockItemApiInstance = {
updateSharedLink: jest.fn(),
};
const mockItemData = { id: '123' };
const mockOnSuccess = jest.fn();

afterEach(() => {
jest.clearAllMocks();
});
afterEach(() => {
jest.clearAllMocks();
});

describe('changeSharedLinkPermission', () => {
test('should return an object with changeSharedLinkPermission method', () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
itemData: mockItemData,
onSuccess: mockOnSuccess,
options,
});

expect(service).toHaveProperty('changeSharedLinkPermission');
Expand All @@ -43,8 +42,8 @@ describe('elements/content-sharing/sharingService', () => {
test('should call updateSharedLink with correct parameters when changeSharedLinkPermission is called', async () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
itemData: mockItemData,
onSuccess: mockOnSuccess,
options,
});

const permissionLevel = PERMISSION_CAN_DOWNLOAD;
Expand All @@ -56,12 +55,132 @@ describe('elements/content-sharing/sharingService', () => {
await service.changeSharedLinkPermission(permissionLevel);

expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
mockItemData,
options,
{ permissions: expectedPermissions },
mockOnSuccess,
{},
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
);
});
});

describe('updateSharedLink', () => {
test('should return an object with updateSharedLink method', () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options,
});

expect(service).toHaveProperty('updateSharedLink');
expect(typeof service.updateSharedLink).toBe('function');
});

test('should call updateSharedLink with basic shared link settings', async () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options,
});

const sharedLinkSettings = {
expiration: null,
isDownloadEnabled: true,
isExpirationEnabled: false,
isPasswordEnabled: false,
password: '',
vanityName: 'vanity-name',
};

const expectedConvertedSettings = {
unshared_at: null,
vanity_url: 'https://example.com/vanity-url',
};

await service.updateSharedLink(sharedLinkSettings);

expect(convertSharedLinkSettings).toHaveBeenCalledWith(
sharedLinkSettings,
undefined, // access
undefined, // isDownloadAvailable
undefined, // serverURL
);
expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
options,
expectedConvertedSettings,
mockOnSuccess,
{},
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
);
});

test('should call updateSharedLink with options including access, isDownloadAvailable, and serverURL', async () => {
const mockConvertedSharedLinkSettings = {
password: 'test-password',
permissions: { can_download: false, can_preview: true },
unshared_at: null,
vanity_url: 'https://example.com/vanity-url',
};

(convertSharedLinkSettings as jest.Mock).mockReturnValue(mockConvertedSharedLinkSettings);

const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options: {
...options,
access: 'open',
isDownloadAvailable: true,
serverURL: 'https://example.com/server-url',
},
});

const sharedLinkSettings = {
expiration: null,
isDownloadEnabled: false,
isExpirationEnabled: true,
isPasswordEnabled: true,
password: 'test-password',
vanityName: 'vanity-name',
};

await service.updateSharedLink(sharedLinkSettings);

expect(convertSharedLinkSettings).toHaveBeenCalledWith(
sharedLinkSettings,
'open',
true,
'https://example.com/server-url',
);
expect(mockItemApiInstance.updateSharedLink).toHaveBeenCalledWith(
options,
mockConvertedSharedLinkSettings,
mockOnSuccess,
{},
CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
);
});

test('should handle shared link settings correctly', async () => {
const service = createSharingService({
itemApiInstance: mockItemApiInstance,
onSuccess: mockOnSuccess,
options,
});

const expirationDate = new Date('2024-12-31T23:59:59Z');
const sharedLinkSettings = {
expiration: expirationDate,
isDownloadEnabled: false,
isExpirationEnabled: true,
isPasswordEnabled: false,
password: 'test-password',
vanityName: 'vanity-name',
};

await service.updateSharedLink(sharedLinkSettings);

expect(convertSharedLinkSettings).toHaveBeenCalledWith(sharedLinkSettings, undefined, undefined, undefined);
});
});
});
Loading