Skip to content

Commit

Permalink
SCIM: Fix tests for PUT and add PATCH ones
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent committed Jan 17, 2025
1 parent b1266ae commit 02ce475
Showing 1 changed file with 93 additions and 42 deletions.
135 changes: 93 additions & 42 deletions test/server/lib/Scim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ describe('Scim', () => {
.getMany();
}

async function withGroupNames(groupNames: string[], cb: (groupNames: string[]) => Promise<void>) {
async function withGroupNames<T>(groupNames: string[], cb: (groupNames: string[]) => Promise<T>) {
try {
const existingGroups = await getGroupByNames(groupNames);
if (existingGroups.length > 0) {
Expand All @@ -640,7 +640,7 @@ describe('Scim', () => {
}
}

async function withGroupName(groupName: string, cb: (groupName: string) => Promise<void>) {
async function withGroupName<T>(groupName: string, cb: (groupName: string) => Promise<T>) {
return await withGroupNames([groupName], (groupNames) => cb(groupNames[0]));
}

Expand All @@ -652,6 +652,17 @@ describe('Scim', () => {
return { ...getUserMember(user), $ref: `/api/scim/v2/Users/${userIdByName[user]}` };
}

function withGroup<T>(cb: (groupId: string) => Promise<T>) {
return withGroupName('test-group', async (groupName) => {
const {id} = await getDbManager().createGroup({
name: groupName,
type: Group.RESOURCE_USERS_TYPE,
memberUsers: [userIdByName['chimpy']!]
});
return await cb(String(id));
});
}


describe('GET /Groups/{id}', function () {
it(`should return a "${Group.RESOURCE_USERS_TYPE}" group for chimpy`, async function () {
Expand Down Expand Up @@ -910,50 +921,44 @@ describe('Scim', () => {
});

describe('PUT /Groups/{id}', function () {
beforeEach(async function () {
await withGroupName('test-group', async (groupName) => {
await getDbManager().createGroup({
name: groupName,
type: Group.RESOURCE_USERS_TYPE,
memberUsers: [userIdByName['chimpy']!]
});
});
});

it('should update an existing group', async function () {
const newGroupName = 'Updated Group Name';
const res = await axios.put(scimUrl('/Groups/1'), {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
displayName: newGroupName,
members: [
getUserMember('kiwi'),
]
}, chimpy);
assert.equal(res.status, 200);
assert.deepEqual(res.data, {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
id: '1',
displayName: newGroupName,
members: [
getUserMemberWithRef('kiwi'),
],
meta: { resourceType: 'Group', location: '/api/scim/v2/Groups/1' }
return withGroup(async (groupId) => {
const newGroupName = 'Updated Group Name';
const res = await axios.put(scimUrl('/Groups/' + groupId), {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
displayName: newGroupName,
members: [
getUserMember('kiwi'),
]
}, chimpy);
assert.equal(res.status, 200);
assert.deepEqual(res.data, {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
id: groupId,
displayName: newGroupName,
members: [
getUserMemberWithRef('kiwi'),
],
meta: { resourceType: 'Group', location: '/api/scim/v2/Groups/' + groupId }
});
});
});

it('should updating a group with members omitted', async function () {
const newGroupName = 'Updated Group Name';
const res = await axios.put(scimUrl('/Groups/1'), {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
displayName: newGroupName,
}, chimpy);
assert.equal(res.status, 200);
assert.deepEqual(res.data, {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
id: '1',
displayName: newGroupName,
members: [],
meta: { resourceType: 'Group', location: '/api/scim/v2/Groups/1' }
it('should update a group with members omitted', async function () {
return withGroup(async (groupId) => {
const newGroupName = 'Updated Group Name';
const res = await axios.put(scimUrl('/Groups/' + groupId), {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
displayName: newGroupName,
}, chimpy);
assert.equal(res.status, 200);
assert.deepEqual(res.data, {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
id: groupId,
displayName: newGroupName,
members: [],
meta: { resourceType: 'Group', location: '/api/scim/v2/Groups/' + groupId }
});
});
});

Expand Down Expand Up @@ -1000,6 +1005,52 @@ describe('Scim', () => {
});
});

describe('PATCH /Groups/{id}', function () {
it('should update an existing group name', async function () {
return withGroup(async (groupId) => {
const newGroupName = 'Updated Group Name';
const res = await axios.patch(scimUrl('/Groups/' + groupId), {
schemas: ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
Operations: [{
op: 'replace', path: 'displayName', value: newGroupName
}]
}, chimpy);
assert.equal(res.status, 200);
assert.deepEqual(res.data, {
schemas: ['urn:ietf:params:scim:schemas:core:2.0:Group'],
id: groupId,
displayName: newGroupName,
members: [
getUserMemberWithRef('chimpy'),
],
meta: { resourceType: 'Group', location: '/api/scim/v2/Groups/' + groupId }
});
});
});

it('should add a member to a group', async function () {
return withGroup(async (groupId) => {
const res = await axios.patch(scimUrl('/Groups/' + groupId), {
schemas: ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
Operations: [{
op: 'add', path: 'members', value: [ getUserMember('kiwi') ]
}]
}, chimpy);
assert.equal(res.status, 200);
assert.deepEqual(res.data.members, [
getUserMemberWithRef('chimpy'),
getUserMemberWithRef('kiwi'),
]);
});
});

checkCommonErrors('patch', '/Groups/1', {
schemas: ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
Operations: [{
op: 'replace', path: 'displayName', value: 'Updated Group Name'
}]
});
});
});

describe('POST /Bulk', function () {
Expand Down

0 comments on commit 02ce475

Please sign in to comment.