Skip to content
Open
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: 0 additions & 4 deletions src/modules/file/file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,6 @@ export class FileController {
delete f.removed;
delete f.removedAt;

if (!f.plainName) {
f.plainName = this.fileUseCases.decrypFileName(f).plainName;
}

return f;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/file/file.domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export class File implements FileAttributes {
this.fileId = fileId;
this.folderId = folderId;
this.setFolder(folder);
this.name = name;
this.setType(type);
this.size = size;
this.bucket = bucket;
Expand All @@ -130,6 +129,7 @@ export class File implements FileAttributes {
this.status = status;
this.thumbnails = thumbnails;
this.sharings = sharings;
this.name = this.plainName;
}

static build(file: FileAttributes): File {
Expand Down
7 changes: 1 addition & 6 deletions src/modules/file/file.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { type Sharing } from '../sharing/sharing.domain';
import { WorkspaceItemUserModel } from '../workspaces/models/workspace-items-users.model';
import { Sequelize } from 'sequelize';
import { AesService } from '../../externals/crypto/aes';

Check warning on line 25 in src/modules/file/file.model.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import of 'AesService'.

See more on https://sonarcloud.io/project/issues?id=internxt_drive-server-wip&issues=AZ2QQVV9NbA7A6bO_kyp&open=AZ2QQVV9NbA7A6bO_kyp&pullRequest=972

@Table({
underscored: true,
Expand All @@ -46,12 +46,7 @@
@Column(DataType.VIRTUAL)
get name(): string {
const plainName = this.getDataValue('plainName');
const folderId = this.getDataValue('folderId');
if (!plainName || !folderId) return plainName ?? null;
return new AesService(process.env.CRYPTO_SECRET2).encrypt(
plainName,
folderId,
);
return plainName;
}

@Index
Expand Down
169 changes: 9 additions & 160 deletions src/modules/file/file.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { PaymentRequiredException } from '../feature-limit/exceptions/payment-required.exception';
import {
File,
type FileAttributes,

Check warning on line 22 in src/modules/file/file.usecase.spec.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import of 'FileAttributes'.

See more on https://sonarcloud.io/project/issues?id=internxt_drive-server-wip&issues=AZ2QP8Mn6OZC7Je6Ud8S&open=AZ2QP8Mn6OZC7Je6Ud8S&pullRequest=972
FileStatus,
type SortableFileAttributes,
} from './file.domain';
Expand Down Expand Up @@ -328,101 +328,6 @@
});
});

describe('decrypt file name', () => {
const fileAttributes: FileAttributes = {
id: 0,
fileId: '4fda5d98-e5b4-56da-a4f2-000084ac0678',
name: 'Myanmar',
type: 'type',
size: BigInt(60),
bucket: 'bucket',
folderId,
folder: null,
encryptVersion: 'aes-2',
deleted: false,
deletedAt: new Date('2022-09-21T11:11:30.742Z'),
userId: 3431709237,
user: null,
creationTime: new Date('2022-09-21T11:11:30.742Z'),
modificationTime: new Date('2022-09-21T11:11:30.742Z'),
createdAt: new Date('2022-09-21T11:11:30.742Z'),
updatedAt: new Date('2022-09-21T11:11:30.742Z'),
uuid: '',
folderUuid: '',
removed: false,
removedAt: undefined,
plainName: 'Myanmar',
status: FileStatus.EXISTS,
};

it('returns a file with the name decrypted', () => {
const folderId = 523;

const encryptedName = cryptoService.encryptName(
fileAttributes['name'],
folderId,
);

const file = {
...fileAttributes,
name: encryptedName,
folderId,
plainName: null,
};

const decryptedName = 'decryptedName';
jest.spyOn(cryptoService, 'decryptName').mockReturnValue(decryptedName);

delete fileAttributes['user'];

const result = service.decrypFileName(file as File);

expect(cryptoService.decryptName).toHaveBeenCalledWith(
file.name,
file.folderId,
);
expect(result).toEqual(
File.build({
...file,
name: decryptedName,
plainName: decryptedName,
}),
);
});

it('When the file has a plain name, then the plain name is returned', () => {
const file = File.build({
...fileAttributes,
plainName: 'plain name',
});

const result = service.decrypFileName(file);
expect(result).toEqual(
File.build({
...file,
name: 'plain name',
plainName: 'plain name',
}),
);
});

it('fails when name is not encrypted', () => {
const decyptedName = 'not encrypted name';

const file = File.build({
...fileAttributes,
name: decyptedName,
});

try {
service.decrypFileName(file);
} catch (err: any) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('Unable to decrypt file name');
}
});
});

describe('move file', () => {
const file = newFile({ attributes: { userId: userMocked.id } });
const destinationFolder = newFolder({
Expand All @@ -433,7 +338,6 @@
const expectedFile = newFile({
attributes: {
...file,
name: 'newencrypted-' + file.name,
folderId: destinationFolder.id,
folderUuid: destinationFolder.uuid,
status: FileStatus.EXISTS,
Expand All @@ -445,10 +349,6 @@
.spyOn(folderUseCases, 'getFolderByUuid')
.mockResolvedValueOnce(destinationFolder);

jest
.spyOn(cryptoService, 'encryptName')
.mockReturnValueOnce(expectedFile.name);

jest
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(null);
Expand All @@ -469,7 +369,7 @@
{
folderId: destinationFolder.id,
folderUuid: destinationFolder.uuid,
name: expectedFile.name,
name: expectedFile.plainName,
status: FileStatus.EXISTS,
plainName: expectedFile.plainName,
type: expectedFile.type,
Expand Down Expand Up @@ -593,6 +493,7 @@
folderUuid: destinationFolder.uuid,
status: FileStatus.EXISTS,
plainName: newAttributes.name,
name: newAttributes.name,
type: newAttributes.type,
},
});
Expand Down Expand Up @@ -633,7 +534,7 @@
{
folderId: destinationFolder.id,
folderUuid: destinationFolder.uuid,
name: expectedFile.name,
name: expectedFile.plainName,
status: FileStatus.EXISTS,
plainName: expectedFile.plainName,
type: expectedFile.type,
Expand Down Expand Up @@ -665,6 +566,7 @@
folderUuid: destinationFolder.uuid,
status: FileStatus.EXISTS,
plainName: newAttributes.name,
name: newAttributes.name,
type: newAttributes.type,
},
});
Expand Down Expand Up @@ -701,7 +603,7 @@
{
folderId: expectedFile.folderId,
folderUuid: expectedFile.folderUuid,
name: expectedFile.name,
name: expectedFile.plainName,
status: FileStatus.EXISTS,
plainName: expectedFile.plainName,
type: expectedFile.type,
Expand Down Expand Up @@ -733,10 +635,6 @@
.spyOn(folderUseCases, 'getFolderByUuid')
.mockResolvedValueOnce(destinationFolder);

jest
.spyOn(cryptoService, 'encryptName')
.mockReturnValueOnce(fileToBeRenamed.name);

jest
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(null);
Expand Down Expand Up @@ -1618,7 +1516,7 @@
attributes: {
...mockFile,
plainName: newFileMeta.plainName,
name: encryptedName,
name: newFileMeta.plainName,
},
});

Expand Down Expand Up @@ -1651,15 +1549,16 @@
userMocked.id,
expect.objectContaining({
plainName: newFileMeta.plainName,
name: encryptedName,
}),
);
const {
modificationTime: resultFileModificationTime,
name: resultFilePlainName,
...resultWithoutModificationTime
} = result;
const {
modificationTime: updatedFileModificationTime,
name: updatedFilePlainName,
...updatedFileWithoutModificationTime
} = updatedFile;

Expand Down Expand Up @@ -1687,7 +1586,6 @@
jest
.spyOn(fileRepository, 'findByPlainNameAndFolderId')
.mockResolvedValueOnce(null);
jest.spyOn(cryptoService, 'encryptName').mockReturnValue(mockFile.name);

const result = await service.updateFileMetaData(
userMocked,
Expand All @@ -1712,7 +1610,6 @@
userMocked.id,
expect.objectContaining({
plainName: mockFile.plainName,
name: mockFile.name,
type: newTypeFileMeta.type,
}),
);
Expand Down Expand Up @@ -1783,27 +1680,6 @@
);
});

it('When files have no plainName, then it should decrypt them', async () => {
const encryptedFile = newFile({
attributes: { plainName: null, thumbnails: [], sharings: [] },
});

jest
.spyOn(fileRepository, 'findTrashedNotExpired')
.mockResolvedValue([encryptedFile]);

const decryptSpy = jest
.spyOn(service, 'decrypFileName' as any)
.mockReturnValue({ ...encryptedFile, plainName: 'decrypted' });

await service.getTrashedFiles(userId, null, {
limit: 20,
offset: 0,
});

expect(decryptSpy).toHaveBeenCalledTimes(1);
});

it('When files already have plainName, then it should not decrypt them', async () => {
const decryptedFile = newFile({
attributes: { plainName: 'my-file', thumbnails: [], sharings: [] },
Expand Down Expand Up @@ -1964,27 +1840,6 @@

expect(decryptSpy).not.toHaveBeenCalled();
});

it('When files have no plainName, then it should decrypt them', async () => {
const encryptedFile = newFile({
attributes: { plainName: null, thumbnails: [], sharings: [] },
});

jest
.spyOn(fileRepository, 'findTrashedNotExpiredInWorkspace')
.mockResolvedValue([encryptedFile]);

const decryptSpy = jest
.spyOn(service as any, 'decrypFileName')
.mockReturnValue({ ...encryptedFile, plainName: 'decrypted' });

await service.getTrashedFilesInWorkspace(createdBy, workspace.id, null, {
limit: 20,
offset: 0,
});

expect(decryptSpy).toHaveBeenCalledTimes(1);
});
});

describe('getWorkspaceFilesSizeSumByStatuses', () => {
Expand Down Expand Up @@ -2439,16 +2294,10 @@
it('When file exists, then it should return the file', async () => {
const mockFile = newFile({ owner: userMocked });
jest.spyOn(fileRepository, 'findByUuid').mockResolvedValue(mockFile);
jest.spyOn(cryptoService, 'decryptName').mockReturnValue('');

const result = await service.getFileMetadata(userMocked, mockFile.uuid);

expect(result).toEqual(
File.build({
...mockFile,
name: mockFile.plainName,
}),
);
expect(result).toEqual(mockFile);
expect(fileRepository.findByUuid).toHaveBeenCalledWith(
mockFile.uuid,
userMocked.id,
Expand Down
Loading
Loading