Skip to content

Commit

Permalink
Merge pull request #100 from KeychainMDIP/74-groups
Browse files Browse the repository at this point in the history
Support groups within groups
  • Loading branch information
macterra authored Apr 21, 2024
2 parents 0c403c8 + 5db660c commit 8472131
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
34 changes: 27 additions & 7 deletions keymaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,17 @@ export async function groupAdd(group, member) {
}

const didMember = lookupDID(member);

if (didMember === didGroup) {
throw "Invalid member";
}

const isMember = await groupTest(member, group);

if (isMember) {
throw "Invalid member";
}

// test for valid member DID
await resolveDID(didMember);

Expand Down Expand Up @@ -888,19 +899,28 @@ export async function groupTest(group, member) {
return false;
}

// Check if data.members is an array
if (!Array.isArray(data.members)) {
return false;
}

if (member) {
const didMember = lookupDID(member);
// TBD implement recursive test for groups within groups
const isMember = data.members.includes(didMember);
return isMember;
if (!member) {
return true;
}

return true;
const didMember = lookupDID(member);
let isMember = data.members.includes(didMember);

if (!isMember) {
for (const did of data.members) {
isMember = await groupTest(did, didMember);

if (isMember) {
break;
}
}
}

return isMember;
}

export async function createSchema(schema) {
Expand Down
63 changes: 60 additions & 3 deletions keymaster.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1744,9 +1744,40 @@ describe('groupAdd', () => {
}
});

// TBD should not be able to add a group to itself
it('should not add a group to itself', async () => {
mockFs({});

await keymaster.createId('Bob');
const groupDid = await keymaster.createGroup('group');

try {
await keymaster.groupAdd(groupDid, groupDid);
throw 'Expected to throw an exception';
}
catch (error) {
expect(error).toBe('Invalid member');
}
});

it('should not add a member that contains group', async () => {
mockFs({});

await keymaster.createId('Bob');
const group1Did = await keymaster.createGroup('group-1');
const group2Did = await keymaster.createGroup('group-2');
const group3Did = await keymaster.createGroup('group-3');

await keymaster.groupAdd(group1Did, group2Did);
await keymaster.groupAdd(group2Did, group3Did);

// TBD should not be able to create groups that contain themselves
try {
await keymaster.groupAdd(group3Did, group1Did);
throw 'Expected to throw an exception';
}
catch (error) {
expect(error).toBe('Invalid member');
}
});
});

describe('groupRemove', () => {
Expand Down Expand Up @@ -1989,7 +2020,33 @@ describe('groupTest', () => {
expect(test).toBe(false);
});

// TBD test recursive groups
it('should return true when testing recursive groups', async () => {
mockFs({});

await keymaster.createId('Bob');
const group1Did = await keymaster.createGroup('level-1');
const group2Did = await keymaster.createGroup('level-2');
const group3Did = await keymaster.createGroup('level-3');
const group4Did = await keymaster.createGroup('level-4');
const group5Did = await keymaster.createGroup('level-5');

await keymaster.groupAdd(group1Did, group2Did);
await keymaster.groupAdd(group2Did, group3Did);
await keymaster.groupAdd(group3Did, group4Did);
await keymaster.groupAdd(group4Did, group5Did);

const test1 = await keymaster.groupTest(group1Did, group2Did);
expect(test1).toBe(true);

const test2 = await keymaster.groupTest(group1Did, group3Did);
expect(test2).toBe(true);

const test3 = await keymaster.groupTest(group1Did, group4Did);
expect(test3).toBe(true);

const test4 = await keymaster.groupTest(group1Did, group5Did);
expect(test4).toBe(true);
});
});

describe('pollTemplate', () => {
Expand Down

0 comments on commit 8472131

Please sign in to comment.