From 2b20010e403f3cb3215eb02b9d6acdd19b5e5018 Mon Sep 17 00:00:00 2001 From: william chang Date: Thu, 25 Apr 2019 17:30:43 +0800 Subject: [PATCH] add 'POST /{realm}/groups/{id}/children' api --- src/resources/groups.ts | 16 ++++++++++++++++ test/groups.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/resources/groups.ts b/src/resources/groups.ts index e627d516..d5ece8e1 100644 --- a/src/resources/groups.ts +++ b/src/resources/groups.ts @@ -50,6 +50,22 @@ export class Groups extends Resource<{realm?: string}> { urlParamKeys: ['id'], }); + /** + * Set or create child. + * This will just set the parent if it exists. Create it and set the parent if the group doesn’t exist. + */ + + public setOrCreateChild = this.makeUpdateRequest< + {id: string}, + GroupRepresentation, + {id: string} + >({ + method: 'POST', + path: '/{id}/children', + urlParamKeys: ['id'], + returnResourceIdInLocationHeader: {field: 'id'}, + }); + /** * Members */ diff --git a/test/groups.spec.ts b/test/groups.spec.ts index d3cbd528..71a012a6 100644 --- a/test/groups.spec.ts +++ b/test/groups.spec.ts @@ -72,6 +72,26 @@ describe('Groups', function() { }); }); + it('set or create child', async () => { + const groupName = 'child-group'; + const groupId = this.currentGroup.id; + const childGroup = await this.kcAdminClient.groups.setOrCreateChild( + {id: groupId}, + {name: groupName}, + ); + + expect(childGroup.id).to.be.ok; + + const group = await this.kcAdminClient.groups.findOne({ + id: groupId, + }); + expect(group.subGroups[0]).to.deep.include({ + id: childGroup.id, + name: groupName, + path: `/${group.name}/${groupName}`, + }); + }); + /** * Role mappings */