Skip to content

Commit

Permalink
add test draft
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaraFinogina committed Jan 14, 2025
1 parent 1f4aa51 commit 713d46c
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 13 deletions.
149 changes: 149 additions & 0 deletions src/app/store/slices/sharedLinks/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* @jest-environment jsdom
*/
import { describe, expect, it, vi, beforeEach, beforeAll } from 'vitest';
import { sharedThunks, ShareFileWithUserPayload } from './index';
const { shareItemWithUser } = sharedThunks;
import { generateNewKeys, hybridDecryptMessageWithPrivateKey } from '../../../crypto/services/pgp.service';
import navigationService from 'app/core/services/navigation.service';
import { RootState } from '../..';
import { Buffer } from 'buffer';
import { UserSettings } from '@internxt/sdk/dist/shared/types/userSettings';
import userService from '../../../auth/services/user.service';
import * as shareService from 'app/share/services/share.service';

describe('Encryption and Decryption', () => {
beforeAll(() => {
vi.mock('app/core/types', () => ({
AppView: vi.fn(),
}));
vi.mock('app/core/services/navigation.service', () => ({
default: { push: vi.fn() },
}));
vi.mock('app/share/services/share.service', () => ({
inviteUserToSharedFolder: vi.fn(),
getSharedFolderInvitationsAsInvitedUser: vi.fn(),
getSharingRoles: vi.fn(),
}));
vi.mock('../../../auth/services/user.service', () => ({
default: {
getPublicKeyByEmail: vi.fn(),
preCreateUser: vi.fn(),
},
}));

vi.mock('../../../notifications/services/notifications.service', () => ({
default: {
show: vi.fn(),
},
ToastType: {
Error: 'ERROR',
},
}));
vi.mock('app/core/services/error.service', () => ({
default: {
castError: vi.fn().mockImplementation((e) => ({ message: e.message || 'Default error message' })),
reportError: vi.fn(),
},
}));
});

beforeEach(() => {
vi.clearAllMocks();
});

it('should setup workspace and encrypt mnemonic with kyber', async () => {
const keys = await generateNewKeys();
const mockPayload: ShareFileWithUserPayload = {
publicKey: keys.publicKeyArmored,
publicKyberKey: keys.publicKyberKeyBase64,
isNewUser: false,
itemId: 'mock-itemId',
itemType: 'file',
notifyUser: false,
notificationMessage: 'mock-notificationMessage',
sharedWith: 'mock-sharedWith',
encryptionAlgorithm: 'mock-encryptionAlgorithm',
roleId: 'mock-roleId',
};

const mockUser: Partial<UserSettings> = {
mnemonic:
'truck arch rather sell tilt return warm nurse rack vacuum rubber tribe unfold scissors copper sock panel ozone harsh ahead danger soda legal state',
keys: {
ecc: {
publicKey: keys.publicKeyArmored,
privateKeyEncrypted: Buffer.from(keys.privateKeyArmored).toString('base64'),
},
kyber: {
publicKey: keys.publicKyberKeyBase64,
privateKeyEncrypted: keys.privateKyberKeyBase64,
},
},
};

const mockRootState: Partial<RootState> = {
user: { user: mockUser as UserSettings, isInitializing: false, isAuthenticated: false, isInitialized: false },
};

const user = mockUser as UserSettings;
vi.spyOn(navigationService, 'push').mockImplementation(() => {});

const mockShareService = {
inviteUserToSharedFolder: vi.fn(),
getSharedFolderInvitationsAsInvitedUser: vi.fn(),
getSharingRoles: vi.fn(),
};

vi.spyOn(shareService, 'getSharedFolderInvitationsAsInvitedUser').mockImplementation(
mockShareService.getSharedFolderInvitationsAsInvitedUser,
);
vi.spyOn(shareService, 'getSharingRoles').mockImplementation(mockShareService.getSharingRoles);
vi.spyOn(shareService, 'inviteUserToSharedFolder').mockImplementation(mockShareService.inviteUserToSharedFolder);

vi.spyOn(userService, 'getPublicKeyByEmail').mockReturnValue(
Promise.resolve({ publicKey: user.keys.ecc.publicKey, publicKyberKey: user.keys?.kyber.publicKey }),
);
vi.spyOn(userService, 'preCreateUser').mockReturnValue(
Promise.resolve({
publicKey: user.keys.ecc.publicKey,
publicKyberKey: user.keys?.kyber.publicKey,
user: {
uuid: user.userId,
email: user.email,
},
}),
);

const getStateMock = vi.fn(() => mockRootState as RootState);
const dispatchMock = vi.fn();

const thunk = shareItemWithUser(mockPayload);
await thunk(dispatchMock, getStateMock, undefined);

const [inviteUserToSharedFolderInput] = mockShareService.inviteUserToSharedFolder.mock.calls[0];
expect(inviteUserToSharedFolderInput.encryptionKey).toBeDefined();

const { encryptionKey = '' } = inviteUserToSharedFolderInput;
const decryptedMessage = await hybridDecryptMessageWithPrivateKey({
encryptedMessageInBase64: encryptionKey,
privateKeyInBase64: Buffer.from(keys.privateKeyArmored).toString('base64'),
privateKyberKeyInBase64: keys.privateKyberKeyBase64,
});

expect(decryptedMessage).toEqual(mockUser.mnemonic);
expect(mockShareService.inviteUserToSharedFolder).toHaveBeenCalledWith(
expect.objectContaining({
itemId: mockPayload.itemId,
itemType: mockPayload.itemType,
sharedWith: mockPayload.sharedWith,
notifyUser: mockPayload.notifyUser,
notificationMessage: mockPayload.notificationMessage,
encryptionKey: encryptionKey,
encryptionAlgorithm: mockPayload.encryptionAlgorithm,
roleId: mockPayload.roleId,
persistPreviousSharing: true,
}),
);
});
});
22 changes: 9 additions & 13 deletions src/app/store/slices/sharedLinks/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { PayloadAction, createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import shareService, {
getSharedFolderInvitationsAsInvitedUser,
getSharingRoles,
inviteUserToSharedFolder,
} from 'app/share/services/share.service';
import * as shareService from 'app/share/services/share.service';
import { RootState } from '../..';

import { Role, SharedFoldersInvitationsAsInvitedUserResponse } from '@internxt/sdk/dist/drive/share/types';
Expand Down Expand Up @@ -78,7 +74,7 @@ const shareItemWithUser = createAsyncThunk<string | void, ShareFileWithUserPaylo
publicKyberKeyBase64: publicKyberKey,
});

await inviteUserToSharedFolder({
await shareService.inviteUserToSharedFolder({
itemId: payload.itemId,
itemType: payload.itemType,
sharedWith: payload.sharedWith,
Expand Down Expand Up @@ -107,7 +103,7 @@ const shareItemWithUser = createAsyncThunk<string | void, ShareFileWithUserPaylo
}
},
);

/*
interface StopSharingItemPayload {
itemType: string;
itemId: string;
Expand Down Expand Up @@ -245,14 +241,14 @@ export const sharedSelectors = {
},
};
export const sharedActions = sharedSlice.actions;
export const sharedActions = sharedSlice.actions; */

export const sharedThunks = {
shareItemWithUser,
stopSharingItem,
removeUserFromSharedFolder,
getSharedFolderRoles,
getPendingInvitations,
//stopSharingItem,
//removeUserFromSharedFolder,
//getSharedFolderRoles,
//getPendingInvitations,
};

export default sharedSlice.reducer;
//export default sharedSlice.reducer;

0 comments on commit 713d46c

Please sign in to comment.