Skip to content

Commit

Permalink
Merge pull request #582 from sanieni6/bug/throwDepthError
Browse files Browse the repository at this point in the history
refactor(groupsValidation): throw BadRequestException on invalid treeDepth input
  • Loading branch information
vplasencia authored Oct 23, 2024
2 parents 5b19cf7 + 47aaaec commit a9e6146
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions apps/api/src/app/groups/dto/create-group.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions apps/api/src/app/groups/groups.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/app/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions libs/api-sdk/src/apiSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ export default class ApiSdk {
groupCreationDetails: GroupCreationDetails,
apiKey: string
): Promise<Group> {
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],
Expand All @@ -140,6 +147,12 @@ export default class ApiSdk {
groupsCreationDetails: Array<GroupCreationDetails>,
apiKey: string
): Promise<Array<Group>> {
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,
Expand Down
18 changes: 18 additions & 0 deletions libs/api-sdk/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,24 @@ 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<GroupCreationDetails> = [
{
name: "Group3",
description: "This is a new group",
treeDepth: 15,
fingerprintDuration: 3600
}
]
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"
apiSdk = new ApiSdk(SupportedUrl.DEV)
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 () => {
Expand Down

0 comments on commit a9e6146

Please sign in to comment.