Skip to content

Commit

Permalink
Merge pull request #272 from internxt/feat/pb-1706-retrieve-items-by-…
Browse files Browse the repository at this point in the history
…folder-uuid

[PB-1706]: Retrieve subitems by folder UUID
  • Loading branch information
sg-gs authored Feb 29, 2024
2 parents a9d64bc + fe9b0ae commit d5d429c
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ lerna-debug.log*

.npmrc

.env
.env.development
.env.production
5 changes: 4 additions & 1 deletion src/externals/notifications/events/event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export class Event {
private createdAt: Date;
constructor(public name: string, public payload: Record<string, any>) {
constructor(
public name: string,
public payload: Record<string, any>,
) {
this.createdAt = new Date();
}
}
2 changes: 1 addition & 1 deletion src/modules/file/file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class FileController {
@UserDecorator() user: User,
@Query('limit') limit: number,
@Query('offset') offset: number,
@Query('status') status: typeof filesStatuses[number],
@Query('status') status: (typeof filesStatuses)[number],
@Query('sort') sort?: string,
@Query('order') order?: 'ASC' | 'DESC',
@Query('updatedAt') updatedAt?: string,
Expand Down
1 change: 1 addition & 0 deletions src/modules/folder/folder.attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Sharing } from '../sharing/sharing.domain';
export interface FolderAttributes {
id: number;
parentId: number;
parentUuid?: string;
parent?: any;
name: string;
bucket: string;
Expand Down
190 changes: 188 additions & 2 deletions src/modules/folder/folder.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
import { createMock } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { newFolder } from '../../../test/fixtures';
import { BadRequestException } from '@nestjs/common';
import { newFile, newFolder } from '../../../test/fixtures';
import { FileUseCases } from '../file/file.usecase';
import { FolderController } from './folder.controller';
import {
BadRequestInvalidOffsetException,
BadRequestOutOfRangeLimitException,
FolderController,
} from './folder.controller';
import { Folder } from './folder.domain';
import { FolderUseCases } from './folder.usecase';
import { CalculateFolderSizeTimeoutException } from './exception/calculate-folder-size-timeout.exception';
import { User } from '../user/user.domain';
import { FileStatus } from '../file/file.domain';

describe('FolderController', () => {
let folderController: FolderController;
let folderUseCases: FolderUseCases;
let fileUseCases: FileUseCases;
let folder: Folder;

const userMocked = User.build({
id: 1,
userId: 'userId',
name: 'User Owner',
lastname: 'Lastname',
email: '[email protected]',
username: 'fake',
bridgeUser: null,
rootFolderId: 1,
errorLoginCount: 0,
isEmailActivitySended: 1,
referralCode: null,
referrer: null,
syncDate: new Date(),
uuid: 'uuid',
lastResend: new Date(),
credit: null,
welcomePack: true,
registerCompleted: true,
backupsBucket: 'bucket',
sharedWorkspace: true,
avatar: 'avatar',
password: '',
mnemonic: '',
hKey: undefined,
secret_2FA: '',
tempKey: '',
lastPasswordChangedAt: new Date(),
});

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [FolderController],
Expand All @@ -23,6 +61,7 @@ describe('FolderController', () => {

folderController = module.get<FolderController>(FolderController);
folderUseCases = module.get<FolderUseCases>(FolderUseCases);
fileUseCases = module.get<FileUseCases>(FileUseCases);
folder = newFolder();
});

Expand All @@ -47,4 +86,151 @@ describe('FolderController', () => {
);
});
});

describe('get folder content', () => {
it('When get folder subfiles are requested by folder uuid, then the child files are returned', async () => {
const expectedSubfiles = [
newFile({ attributes: { id: 1, folderUuid: folder.uuid } }),
newFile({ attributes: { id: 2, folderUuid: folder.uuid } }),
newFile({ attributes: { id: 3, folderUuid: folder.uuid } }),
];
jest.spyOn(fileUseCases, 'getFiles').mockResolvedValue(expectedSubfiles);

const result = await folderController.getFolderContentFiles(
userMocked,
folder.uuid,
50,
0,
'id',
'ASC',
);
expect(result).toEqual({ files: expectedSubfiles });
});

it('When get folder subfolders are requested by folder uuid, then the child folders are returned', async () => {
const expectedSubfolders = [
newFolder({ attributes: { id: 1, parentUuid: folder.uuid } }),
newFolder({ attributes: { id: 2, parentUuid: folder.uuid } }),
newFolder({ attributes: { id: 3, parentUuid: folder.uuid } }),
];
const mappedSubfolders = expectedSubfolders.map((f) => {
let folderStatus: FileStatus;
if (f.removed) {
folderStatus = FileStatus.DELETED;
} else if (f.deleted) {
folderStatus = FileStatus.TRASHED;
} else {
folderStatus = FileStatus.EXISTS;
}
return { ...f, status: folderStatus };
});

jest
.spyOn(folderUseCases, 'getFolders')
.mockResolvedValue(expectedSubfolders);

const result = await folderController.getFolderContentFolders(
userMocked,
folder.uuid,
50,
0,
'id',
'ASC',
);

expect(result).toEqual({ folders: mappedSubfolders });
});

it('When get folder subfiles are requested by invalid params, then it should throw an error', () => {
expect(
folderController.getFolderContentFiles(
userMocked,
'invalidUUID',
50,
0,
'id',
'ASC',
),
).rejects.toThrow(BadRequestException);

expect(
folderController.getFolderContentFiles(
userMocked,
folder.uuid,
0,
0,
'id',
'ASC',
),
).rejects.toThrow(BadRequestOutOfRangeLimitException);

expect(
folderController.getFolderContentFiles(
userMocked,
folder.uuid,
51,
0,
'id',
'ASC',
),
).rejects.toThrow(BadRequestOutOfRangeLimitException);

expect(
folderController.getFolderContentFiles(
userMocked,
folder.uuid,
50,
-1,
'id',
'ASC',
),
).rejects.toThrow(BadRequestInvalidOffsetException);
});

it('When get folder subfolders are requested by invalid folder uuid, then it should throw an error', async () => {
expect(
folderController.getFolderContentFolders(
userMocked,
'invalidUUID',
50,
0,
'id',
'ASC',
),
).rejects.toThrow(BadRequestException);

expect(
folderController.getFolderContentFolders(
userMocked,
folder.uuid,
0,
0,
'id',
'ASC',
),
).rejects.toThrow(BadRequestOutOfRangeLimitException);

expect(
folderController.getFolderContentFolders(
userMocked,
folder.uuid,
51,
0,
'id',
'ASC',
),
).rejects.toThrow(BadRequestOutOfRangeLimitException);

expect(
folderController.getFolderContentFolders(
userMocked,
folder.uuid,
50,
-1,
'id',
'ASC',
),
).rejects.toThrow(BadRequestInvalidOffsetException);
});
});
});
Loading

0 comments on commit d5d429c

Please sign in to comment.