From 1d3e95c8348f6ba4e270fb45a634614fca752169 Mon Sep 17 00:00:00 2001 From: Luis Sanchez Date: Sun, 20 Oct 2024 22:13:06 -0500 Subject: [PATCH 1/4] refactor(groupsvalidation): throw BadRequestException on invalid input --- apps/api/src/app/groups/dto/create-group.dto.ts | 4 ++-- apps/api/src/app/groups/groups.service.ts | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/api/src/app/groups/dto/create-group.dto.ts b/apps/api/src/app/groups/dto/create-group.dto.ts index 7fc0bad1..2dc49185 100644 --- a/apps/api/src/app/groups/dto/create-group.dto.ts +++ b/apps/api/src/app/groups/dto/create-group.dto.ts @@ -31,8 +31,8 @@ export class CreateGroupDto { readonly description: string @IsNumber() - @Min(16) - @Max(32) + @Min(16, { message: "The tree depth must be between 16 and 32." }) + @Max(32, { message: "The tree depth must be between 16 and 32." }) @ApiProperty() readonly treeDepth: number diff --git a/apps/api/src/app/groups/groups.service.ts b/apps/api/src/app/groups/groups.service.ts index 8db01f55..c0c7eae2 100644 --- a/apps/api/src/app/groups/groups.service.ts +++ b/apps/api/src/app/groups/groups.service.ts @@ -163,6 +163,12 @@ export class GroupsService { if (credentials === undefined) credentials = null + if (treeDepth < 16 || treeDepth > 32) { + throw new BadRequestException( + "The tree depth must be between 16 and 32." + ) + } + const group = this.groupRepository.create({ id: _groupId, name, From 45df9f1e866a544101caba6fc5be426d8a297137 Mon Sep 17 00:00:00 2001 From: Luis Sanchez Date: Tue, 22 Oct 2024 21:36:05 -0500 Subject: [PATCH 2/4] refactor(apisdk): add treeDepth error handling to apiSdk --- libs/api-sdk/src/apiSdk.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libs/api-sdk/src/apiSdk.ts b/libs/api-sdk/src/apiSdk.ts index 5fc9dc2b..d7eded59 100644 --- a/libs/api-sdk/src/apiSdk.ts +++ b/libs/api-sdk/src/apiSdk.ts @@ -121,6 +121,14 @@ export default class ApiSdk { groupCreationDetails: GroupCreationDetails, apiKey: string ): Promise { + + if ( + groupCreationDetails.treeDepth < 16 || + groupCreationDetails.treeDepth > 32 + ) { + throw new Error("The tree depth must be between 16 and 32") + } + const groups = await createGroups( this._config, [groupCreationDetails], @@ -140,6 +148,13 @@ export default class ApiSdk { groupsCreationDetails: Array, apiKey: string ): Promise> { + + for (const group of groupsCreationDetails) { + if (group.treeDepth < 16 || group.treeDepth > 32) { + throw new Error("The tree depth must be between 16 and 32") + } + } + const groups = await createGroups( this._config, groupsCreationDetails, From e0097ab52d2ba9c3c9170311d83cc6417447c8e9 Mon Sep 17 00:00:00 2001 From: Luis Sanchez Date: Wed, 23 Oct 2024 08:46:09 -0500 Subject: [PATCH 3/4] test(creategroup): add tests for the implementation --- apps/api/src/app/groups/groups.service.test.ts | 16 ++++++++++++++++ libs/api-sdk/src/apiSdk.ts | 2 -- libs/api-sdk/src/index.test.ts | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/apps/api/src/app/groups/groups.service.test.ts b/apps/api/src/app/groups/groups.service.test.ts index 4d353337..95792863 100644 --- a/apps/api/src/app/groups/groups.service.test.ts +++ b/apps/api/src/app/groups/groups.service.test.ts @@ -90,6 +90,22 @@ describe("GroupsService", () => { expect(treeDepth).toBe(16) expect(members).toHaveLength(0) }) + + it("Should not create a group if the treeDepth is not between 16 and 32", async () => { + const fun = groupsService.createGroup( + { + name: "Group3", + description: "This is a description", + treeDepth: 15, + fingerprintDuration: 3600 + }, + "admin" + ) + + await expect(fun).rejects.toThrow( + "The tree depth must be between 16 and 32." + ) + }) }) describe("# removeGroup", () => { diff --git a/libs/api-sdk/src/apiSdk.ts b/libs/api-sdk/src/apiSdk.ts index d7eded59..18f7975b 100644 --- a/libs/api-sdk/src/apiSdk.ts +++ b/libs/api-sdk/src/apiSdk.ts @@ -121,7 +121,6 @@ export default class ApiSdk { groupCreationDetails: GroupCreationDetails, apiKey: string ): Promise { - if ( groupCreationDetails.treeDepth < 16 || groupCreationDetails.treeDepth > 32 @@ -148,7 +147,6 @@ export default class ApiSdk { groupsCreationDetails: Array, apiKey: string ): Promise> { - for (const group of groupsCreationDetails) { if (group.treeDepth < 16 || group.treeDepth > 32) { throw new Error("The tree depth must be between 16 and 32") diff --git a/libs/api-sdk/src/index.test.ts b/libs/api-sdk/src/index.test.ts index 28664d43..9eb8717f 100644 --- a/libs/api-sdk/src/index.test.ts +++ b/libs/api-sdk/src/index.test.ts @@ -309,6 +309,23 @@ describe("Bandada API SDK", () => { expect(group.credentials).toBeNull() }) }) + + it("Should not create a group if the treeDepth is not between 16 and 32", async () => { + const expectedGroups: Array = [ + { + name: "Group3", + description: "This is a new group", + treeDepth: 15, + fingerprintDuration: 3600 + } + ] + const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc" + const fun = apiSdk.createGroups([expectedGroups[0]], apiKey) + + await expect(fun).rejects.toThrow( + "The tree depth must be between 16 and 32" + ) + }) }) describe("#removeGroup", () => { it("Should create a group", async () => { From 47aaaec476a9d92b881dfebd2bc3234e2b27ad3d Mon Sep 17 00:00:00 2001 From: Luis Sanchez Date: Wed, 23 Oct 2024 14:10:17 -0500 Subject: [PATCH 4/4] fix(test): add new apiSdk instance --- libs/api-sdk/src/index.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/api-sdk/src/index.test.ts b/libs/api-sdk/src/index.test.ts index 9eb8717f..40db7572 100644 --- a/libs/api-sdk/src/index.test.ts +++ b/libs/api-sdk/src/index.test.ts @@ -320,6 +320,7 @@ describe("Bandada API SDK", () => { } ] const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc" + apiSdk = new ApiSdk(SupportedUrl.DEV) const fun = apiSdk.createGroups([expectedGroups[0]], apiKey) await expect(fun).rejects.toThrow(