From a33f4b546db772362e1979845e3e0f0f3f6c175c Mon Sep 17 00:00:00 2001 From: Zaheer khan <150288870+zaheer-Khan3260@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:55:48 +0530 Subject: [PATCH] fix(api): Empty name `""` accepted as a valid name while creating environments (#583) --- .../create.environment/create.environment.ts | 4 +++- .../api/src/environment/environment.e2e.spec.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/api/src/environment/dto/create.environment/create.environment.ts b/apps/api/src/environment/dto/create.environment/create.environment.ts index db1adb9a..ffa62e3f 100644 --- a/apps/api/src/environment/dto/create.environment/create.environment.ts +++ b/apps/api/src/environment/dto/create.environment/create.environment.ts @@ -1,7 +1,9 @@ -import { IsOptional, IsString } from 'class-validator' +import { IsNotEmpty, IsOptional, IsString, Matches } from 'class-validator' export class CreateEnvironment { @IsString() + @IsNotEmpty() + @Matches(/^[a-zA-Z0-9-_]{1,64}$/) name: string @IsString() diff --git a/apps/api/src/environment/environment.e2e.spec.ts b/apps/api/src/environment/environment.e2e.spec.ts index be15ee7e..ad949f1c 100644 --- a/apps/api/src/environment/environment.e2e.spec.ts +++ b/apps/api/src/environment/environment.e2e.spec.ts @@ -172,6 +172,23 @@ describe('Environment Controller Tests', () => { expect(environmentFromDb).toBeDefined() }) + it('should not be able to create an environment with an empty name', async () => { + const response = await app.inject({ + method: 'POST', + url: `/environment/${project1.slug}`, + payload: { + name: '', + description: 'Empty name test' + }, + headers: { + 'x-e2e-user-email': user1.email + } + }) + + expect(response.statusCode).toBe(400) + expect(response.json().message).toContain('name should not be empty') + }) + it('should not be able to create an environment in a project that does not exist', async () => { const response = await app.inject({ method: 'POST',