Skip to content

Commit

Permalink
feat(server): server-side checking of duplicate import paths and excl…
Browse files Browse the repository at this point in the history
…usion patterns (#6993)

validate path and pattern
  • Loading branch information
etnoy committed Feb 9, 2024
1 parent 2ee9044 commit 954c1c2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
54 changes: 52 additions & 2 deletions server/e2e/api/specs/library.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ describe(`${LibraryController.name} (e2e)`, () => {
);
});

it('should not create an external library with duplicate import paths', async () => {
const { status, body } = await request(server)
.post('/library')
.set('Authorization', `Bearer ${admin.accessToken}`)
.send({
type: LibraryType.EXTERNAL,
name: 'My Awesome Library',
importPaths: ['/path', '/path'],
exclusionPatterns: ['**/Raw/**'],
});

expect(status).toBe(400);
expect(body).toEqual(errorStub.badRequest(["All importPaths's elements must be unique"]));
});

it('should not create an external library with duplicate exclusion patterns', async () => {
const { status, body } = await request(server)
.post('/library')
.set('Authorization', `Bearer ${admin.accessToken}`)
.send({
type: LibraryType.EXTERNAL,
name: 'My Awesome Library',
importPaths: ['/path/to/import'],
exclusionPatterns: ['**/Raw/**', '**/Raw/**'],
});

expect(status).toBe(400);
expect(body).toEqual(errorStub.badRequest(["All exclusionPatterns's elements must be unique"]));
});

it('should create an upload library with defaults', async () => {
const { status, body } = await request(server)
.post('/library')
Expand Down Expand Up @@ -229,7 +259,7 @@ describe(`${LibraryController.name} (e2e)`, () => {
);
});

it('should not allow an empty import path', async () => {
it('should reject an empty import path', async () => {
const { status, body } = await request(server)
.put(`/library/${library.id}`)
.set('Authorization', `Bearer ${admin.accessToken}`)
Expand All @@ -239,6 +269,16 @@ describe(`${LibraryController.name} (e2e)`, () => {
expect(body).toEqual(errorStub.badRequest(['each value in importPaths should not be empty']));
});

it('should reject duplicate import paths', async () => {
const { status, body } = await request(server)
.put(`/library/${library.id}`)
.set('Authorization', `Bearer ${admin.accessToken}`)
.send({ importPaths: ['/path', '/path'] });

expect(status).toBe(400);
expect(body).toEqual(errorStub.badRequest(["All importPaths's elements must be unique"]));
});

it('should change the exclusion pattern', async () => {
const { status, body } = await request(server)
.put(`/library/${library.id}`)
Expand All @@ -253,7 +293,17 @@ describe(`${LibraryController.name} (e2e)`, () => {
);
});

it('should not allow an empty exclusion pattern', async () => {
it('should reject duplicate exclusion patterns', async () => {
const { status, body } = await request(server)
.put(`/library/${library.id}`)
.set('Authorization', `Bearer ${admin.accessToken}`)
.send({ exclusionPatterns: ['**/*.jpg', '**/*.jpg'] });

expect(status).toBe(400);
expect(body).toEqual(errorStub.badRequest(["All exclusionPatterns's elements must be unique"]));
});

it('should reject an empty exclusion pattern', async () => {
const { status, body } = await request(server)
.put(`/library/${library.id}`)
.set('Authorization', `Bearer ${admin.accessToken}`)
Expand Down
6 changes: 5 additions & 1 deletion server/src/domain/library/library.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LibraryEntity, LibraryType } from '@app/infra/entities';
import { ApiProperty } from '@nestjs/swagger';
import { IsBoolean, IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { ArrayUnique, IsBoolean, IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { ValidateUUID } from '../domain.util';

export class CreateLibraryDto {
Expand All @@ -20,11 +20,13 @@ export class CreateLibraryDto {
@IsOptional()
@IsString({ each: true })
@IsNotEmpty({ each: true })
@ArrayUnique()
importPaths?: string[];

@IsOptional()
@IsString({ each: true })
@IsNotEmpty({ each: true })
@ArrayUnique()
exclusionPatterns?: string[];

@IsOptional()
Expand All @@ -45,11 +47,13 @@ export class UpdateLibraryDto {
@IsOptional()
@IsString({ each: true })
@IsNotEmpty({ each: true })
@ArrayUnique()
importPaths?: string[];

@IsOptional()
@IsNotEmpty({ each: true })
@IsString({ each: true })
@ArrayUnique()
exclusionPatterns?: string[];
}

Expand Down

0 comments on commit 954c1c2

Please sign in to comment.