-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from internxt/feat/workspaces-files-and-folde…
…r-existence [PB-2309] feat: created endpoint for folder and files existence
- Loading branch information
Showing
13 changed files
with
577 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/modules/folder/dto/files-existence-in-folder.dto.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { validate } from 'class-validator'; | ||
import { plainToInstance } from 'class-transformer'; | ||
import { CheckFileExistenceInFolderDto } from './files-existence-in-folder.dto'; | ||
|
||
describe('CheckFileExistenceInFolderDto', () => { | ||
it('When valid data is passed, then no errors should be returned', async () => { | ||
const dto = plainToInstance(CheckFileExistenceInFolderDto, { | ||
plainName: ['file1', 'file2'], | ||
type: 'txt', | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBe(0); | ||
}); | ||
|
||
it('When a single string is passed for plainName, then it should be transformed into an array and validate successfully', async () => { | ||
const dto = plainToInstance(CheckFileExistenceInFolderDto, { | ||
plainName: 'file1', | ||
type: 'txt', | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBe(0); | ||
expect(dto.plainName).toEqual(['file1']); | ||
}); | ||
|
||
it('When plainName array exceeds max size, then it should fail', async () => { | ||
const plainName = Array.from({ length: 51 }, (_, i) => `file${i + 1}`); | ||
const dto = plainToInstance(CheckFileExistenceInFolderDto, { | ||
plainName, | ||
type: 'txt', | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
expect(errors[0].constraints).toBeDefined(); | ||
}); | ||
|
||
it('When plainName contains non-string values, then it should fail', async () => { | ||
const dto = plainToInstance(CheckFileExistenceInFolderDto, { | ||
plainName: [1, 2, 3], | ||
type: 'txt', | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
}); | ||
|
||
it('When plainName is not provided, then it should fail', async () => { | ||
const dto = plainToInstance(CheckFileExistenceInFolderDto, { | ||
type: 'txt', | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
}); | ||
|
||
it('When plainName is an empty array, then it should validate successfully', async () => { | ||
const dto = plainToInstance(CheckFileExistenceInFolderDto, { | ||
plainName: [], | ||
type: 'txt', | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBe(0); | ||
}); | ||
|
||
it('When type is not provided, then it should validate successfully', async () => { | ||
const dto = plainToInstance(CheckFileExistenceInFolderDto, { | ||
plainName: ['file1', 'file2'], | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Transform } from 'class-transformer'; | ||
import { IsString, ArrayMaxSize, IsArray, IsOptional } from 'class-validator'; | ||
import { FileAttributes } from '../../file/file.domain'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class CheckFileExistenceInFolderDto { | ||
@ApiProperty({ | ||
description: 'Type of file', | ||
example: 'pdf', | ||
required: false, | ||
}) | ||
@IsString() | ||
@IsOptional() | ||
type?: FileAttributes['type']; | ||
|
||
@ApiProperty({ | ||
description: 'Plain name of file', | ||
example: 'example', | ||
}) | ||
@IsArray() | ||
@ArrayMaxSize(50, { | ||
message: 'Names parameter cannot contain more than 50 names', | ||
}) | ||
@IsString({ each: true }) | ||
@Transform(({ value }) => { | ||
if (typeof value === 'string') { | ||
return [value]; | ||
} | ||
return value; | ||
}) | ||
plainName: FileAttributes['plainName'][]; | ||
} |
56 changes: 56 additions & 0 deletions
56
src/modules/folder/dto/folder-existence-in-folder.dto.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { validate } from 'class-validator'; | ||
import { plainToInstance } from 'class-transformer'; | ||
import { CheckFoldersExistenceDto } from './folder-existence-in-folder.dto'; | ||
|
||
describe('CheckFoldersExistenceDto', () => { | ||
it('When valid data is passed, then no errors should be returned', async () => { | ||
const dto = plainToInstance(CheckFoldersExistenceDto, { | ||
plainName: ['folder1', 'folder2'], | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBe(0); | ||
}); | ||
|
||
it('When a single string is passed, then it should be transformed into an array and validate successfully', async () => { | ||
const dto = plainToInstance(CheckFoldersExistenceDto, { | ||
plainName: 'folder1', | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBe(0); | ||
expect(dto.plainName).toEqual(['folder1']); | ||
}); | ||
|
||
it('When plainName array exceeds max size, then it should fail', async () => { | ||
const plainName = Array.from({ length: 51 }, (_, i) => `folder${i + 1}`); | ||
const dto = plainToInstance(CheckFoldersExistenceDto, { plainName }); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
expect(errors[0].constraints).toBeDefined(); | ||
}); | ||
|
||
it('When plainName contains non-string values, then it should fail', async () => { | ||
const dto = plainToInstance(CheckFoldersExistenceDto, { | ||
plainName: [1, 2, 3], | ||
}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
}); | ||
|
||
it('When plainName is not provided, then it should fail', async () => { | ||
const dto = plainToInstance(CheckFoldersExistenceDto, {}); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
}); | ||
|
||
it('When plainName is an empty array, then it should validate successfully', async () => { | ||
const dto = plainToInstance(CheckFoldersExistenceDto, { plainName: [] }); | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Transform } from 'class-transformer'; | ||
import { IsString, ArrayMaxSize, IsArray } from 'class-validator'; | ||
|
||
export class CheckFoldersExistenceDto { | ||
@ApiProperty({ | ||
description: 'Plain name of folder', | ||
example: 'my folder', | ||
}) | ||
@IsArray() | ||
@ArrayMaxSize(50, { | ||
message: 'Names parameter cannot contain more than 50 names', | ||
}) | ||
@IsString({ each: true }) | ||
@Transform(({ value }) => { | ||
if (typeof value === 'string') { | ||
return [value]; | ||
} | ||
return value; | ||
}) | ||
plainName: string[]; | ||
} |
Oops, something went wrong.