Skip to content

Commit

Permalink
Suggestions & util improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Jwaegebaert committed Oct 18, 2024
1 parent 4504b4e commit c72eeb7
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 146 deletions.
8 changes: 4 additions & 4 deletions src/m365/viva/commands/engage/Community.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface Community {
id: string;
displayName: string;
id?: string;
displayName?: string;
description?: string;
privacy: string;
groupId: string;
privacy?: string;
groupId?: string;
}
56 changes: 12 additions & 44 deletions src/m365/viva/commands/engage/engage-community-user-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,7 @@ describe(commands.ENGAGE_COMMUNITY_USER_LIST, () => {
"userPrincipalName": "[email protected]"
}
];
const membersResult = [
{
"id": "1deb8814-8130-451d-8fcb-849dc7ed47e5",
"businessPhones": [
"123-555-1215"
],
"displayName": "Samu Tolonen",
"givenName": "Samu",
"jobTitle": "IT Manager",
"mail": null,
"mobilePhone": "123-555-6645",
"officeLocation": "123455",
"preferredLanguage": null,
"surname": "Tolonen",
"userPrincipalName": "[email protected]",
"roles": [
"Member"
]
}
];
const membersResult = [{ ...membersAPIResult[0], roles: ["Member"] }];
const adminsAPIResult = [
{
"id": "da634de7-d23c-4419-ab83-fcd395b4ebd0",
Expand All @@ -74,26 +55,13 @@ describe(commands.ENGAGE_COMMUNITY_USER_LIST, () => {
"userPrincipalName": "[email protected]"
}
];
const adminsResult = [
{
"id": "da634de7-d23c-4419-ab83-fcd395b4ebd0",
"businessPhones": [
"123-555-1215"
],
"displayName": "Anton Johansen",
"givenName": "Anton",
"jobTitle": "IT Manager",
"mail": null,
"mobilePhone": "123-555-6645",
"officeLocation": "123455",
"preferredLanguage": null,
"surname": "Johansen",
"userPrincipalName": "[email protected]",
"roles": [
"Admin"
]
}
];
const adminsResult = [{ ...adminsAPIResult[0], roles: ["Admin"] }];
const community = {
id: communityId,
displayName: communityDisplayName,
privacy: 'Public',
groupId: entraGroupId
};

let log: string[];
let logger: Logger;
Expand Down Expand Up @@ -130,7 +98,7 @@ describe(commands.ENGAGE_COMMUNITY_USER_LIST, () => {
afterEach(() => {
sinonUtil.restore([
request.get,
vivaEngage.getEntraGroupIdByCommunityId
vivaEngage.getCommunityById
]);
});

Expand Down Expand Up @@ -235,7 +203,7 @@ describe(commands.ENGAGE_COMMUNITY_USER_LIST, () => {
throw 'Invalid request';
});

sinon.stub(vivaEngage, 'getEntraGroupIdByCommunityId').resolves(entraGroupId);
sinon.stub(vivaEngage, 'getCommunityById').resolves(community);

await command.action(logger, { options: { communityId: communityId, verbose: true } });
assert(loggerLogSpy.calledWith([...adminsResult, ...membersResult]));
Expand All @@ -254,7 +222,7 @@ describe(commands.ENGAGE_COMMUNITY_USER_LIST, () => {
throw 'Invalid request';
});

sinon.stub(vivaEngage, 'getEntraGroupIdByCommunityDisplayName').resolves(entraGroupId);
sinon.stub(vivaEngage, 'getCommunityByDisplayName').resolves(community);

await command.action(logger, { options: { communityDisplayName: communityDisplayName, verbose: true } });
assert(loggerLogSpy.calledWith([...adminsResult, ...membersResult]));
Expand Down Expand Up @@ -290,7 +258,7 @@ describe(commands.ENGAGE_COMMUNITY_USER_LIST, () => {
throw 'Invalid request';
});

sinon.stub(vivaEngage, 'getEntraGroupIdByCommunityId').resolves(entraGroupId);
sinon.stub(vivaEngage, 'getCommunityById').resolves(community);

await command.action(logger, { options: { communityId: communityId, role: 'Admin' } });
assert(loggerLogSpy.calledWith(adminsResult));
Expand Down
6 changes: 4 additions & 2 deletions src/m365/viva/commands/engage/engage-community-user-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ class VivaEngageCommunityUserListCommand extends GraphCommand {
let entraGroupId = args.options.entraGroupId;

if (args.options.communityDisplayName) {
entraGroupId = await vivaEngage.getEntraGroupIdByCommunityDisplayName(args.options.communityDisplayName);
const community = await vivaEngage.getCommunityByDisplayName(args.options.communityDisplayName, ['groupId']);
entraGroupId = community.groupId;
}

if (args.options.communityId) {
entraGroupId = await vivaEngage.getEntraGroupIdByCommunityId(args.options.communityId);
const community = await vivaEngage.getCommunityById(args.options.communityId, ['groupId']);
entraGroupId = community.groupId;
}

const requestOptions: CliRequestOptions = {
Expand Down
105 changes: 55 additions & 50 deletions src/utils/vivaEngage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,56 +35,58 @@ describe('utils/vivaEngage', () => {
]);
});

it('correctly get single community id by name using getCommunityIdByDisplayName', async () => {
it('correctly get single community id by name using getCommunityByDisplayName', async () => {
sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'`) {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'&$select=id`) {
return {
value: [
communityResponse
{
id: communityId
}
]
};
}

return 'Invalid Request';
});

const actual = await vivaEngage.getCommunityIdByDisplayName(displayName);
assert.deepStrictEqual(actual, 'eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI0NzY5MTM1ODIwOSJ9');
const actual = await vivaEngage.getCommunityByDisplayName(displayName, ['id']);
assert.deepStrictEqual(actual, { id: communityId });
});

it('handles selecting single community when multiple communities with the specified name found using getCommunityIdByDisplayName and cli is set to prompt', async () => {
it('handles selecting single community when multiple communities with the specified name found using getCommunityByDisplayName and cli is set to prompt', async () => {
sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'`) {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'&$select=id`) {
return {
value: [
communityResponse,
anotherCommunityResponse
{ id: communityId },
{ id: "eyJfdHlwZ0NzY5SIwiIiSJ9IwO6IaWQiOIMTM1ODikdyb3Vw" }
]
};
}

return 'Invalid Request';
});

sinon.stub(cli, 'handleMultipleResultsFound').resolves(communityResponse);
sinon.stub(cli, 'handleMultipleResultsFound').resolves({ id: communityId });

const actual = await vivaEngage.getCommunityIdByDisplayName(displayName);
assert.deepStrictEqual(actual, 'eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI0NzY5MTM1ODIwOSJ9');
const actual = await vivaEngage.getCommunityByDisplayName(displayName, ['id']);
assert.deepStrictEqual(actual, { id: 'eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI0NzY5MTM1ODIwOSJ9' });
});

it('throws error message when no community was found using getCommunityIdByDisplayName', async () => {
it('throws error message when no community was found using getCommunityByDisplayName', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(invalidDisplayName)}'`) {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(invalidDisplayName)}'&$select=id`) {
return { value: [] };
}

throw 'Invalid Request';
});

await assert.rejects(vivaEngage.getCommunityIdByDisplayName(invalidDisplayName)), Error(`The specified Viva Engage community '${invalidDisplayName}' does not exist.`);
await assert.rejects(vivaEngage.getCommunityByDisplayName(invalidDisplayName, ['id'])), Error(`The specified Viva Engage community '${invalidDisplayName}' does not exist.`);
});

it('throws error message when multiple communities were found using getCommunityIdByDisplayName', async () => {
it('throws error message when multiple communities were found using getCommunityByDisplayName', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
return false;
Expand All @@ -94,94 +96,97 @@ describe('utils/vivaEngage', () => {
});

sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'`) {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'&$select=id`) {
return {
value: [
communityResponse,
anotherCommunityResponse
{ id: communityId },
{ id: "eyJfdHlwZ0NzY5SIwiIiSJ9IwO6IaWQiOIMTM1ODikdyb3Vw" }
]
};
}

return 'Invalid Request';
});

await assert.rejects(vivaEngage.getCommunityIdByDisplayName(displayName),
await assert.rejects(vivaEngage.getCommunityByDisplayName(displayName, ['id']),
Error(`Multiple Viva Engage communities with name '${displayName}' found. Found: ${communityResponse.id}, ${anotherCommunityResponse.id}.`));
});

it('correctly get single community id by group id using getCommunityIdByEntraGroupId', async () => {
it('correctly get single community by group id using getCommunityByEntraGroupId', async () => {
sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === 'https://graph.microsoft.com/v1.0/employeeExperience/communities?$select=id,groupId') {
return {
value: [
communityResponse
{
id: communityId,
groupId: entraGroupId
}
]
};
}

return 'Invalid Request';
});

const actual = await vivaEngage.getCommunityIdByEntraGroupId(entraGroupId);
assert.deepStrictEqual(actual, 'eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI0NzY5MTM1ODIwOSJ9');
const actual = await vivaEngage.getCommunityByEntraGroupId(entraGroupId, ['id']);
assert.deepStrictEqual(actual, { id: communityId, groupId: entraGroupId });
});

it('throws error message when no community was found using getCommunityIdByEntraGroupId', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === 'https://graph.microsoft.com/v1.0/employeeExperience/communities?$select=id,groupId') {
return { value: [] };
it('correctly get single community by group id using getCommunityByEntraGroupId and multiple properties', async () => {
sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === 'https://graph.microsoft.com/v1.0/employeeExperience/communities?$select=id,groupId,displayName') {
return {
value: [
{
id: communityId,
groupId: entraGroupId,
displayName: displayName
}
]
};
}

throw 'Invalid Request';
return 'Invalid Request';
});

await assert.rejects(vivaEngage.getCommunityIdByEntraGroupId(entraGroupId)), Error(`The Microsoft Entra group with id '${entraGroupId}' is not associated with any Viva Engage community.`);
const actual = await vivaEngage.getCommunityByEntraGroupId(entraGroupId, ['id', 'groupId', 'displayName']);
assert.deepStrictEqual(actual, { id: communityId, groupId: entraGroupId, displayName: displayName });
});

it('correctly gets Entra group ID by community ID using getEntraGroupIdByCommunityId', async () => {
it('throws error message when no community was found using getCommunityByEntraGroupId', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities/${communityId}?$select=groupId`) {
return communityResponse;
if (opts.url === 'https://graph.microsoft.com/v1.0/employeeExperience/communities?$select=id,groupId') {
return { value: [] };
}

throw 'Invalid Request';
});

const actual = await vivaEngage.getEntraGroupIdByCommunityId(communityId);
assert.deepStrictEqual(actual, '0bed8b86-5026-4a93-ac7d-56750cc099f1');
await assert.rejects(vivaEngage.getCommunityByEntraGroupId(entraGroupId, ['id'])), Error(`The Microsoft Entra group with id '${entraGroupId}' is not associated with any Viva Engage community.`);
});

it('throws error message when no Entra group ID was found using getEntraGroupIdByCommunityId', async () => {
it('correctly gets Entra group ID by community ID using getEntraGroupIdByCommunityId', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities/${communityId}?$select=groupId`) {
return null;
return { groupId: entraGroupId };
}

throw 'Invalid Request';
});

await assert.rejects(vivaEngage.getEntraGroupIdByCommunityId(communityId)), Error(`The specified Viva Engage community with ID '${communityId}' does not exist.`);
const actual = await vivaEngage.getCommunityById(communityId, ['groupId']);
assert.deepStrictEqual(actual, { groupId: '0bed8b86-5026-4a93-ac7d-56750cc099f1' });
});

it('correctly gets Entra group ID by community display name using getEntraGroupIdByCommunityDisplayName', async () => {
it('throws error message when no Entra group ID was found using getCommunityById', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities/${communityId}?$select=groupId`) {
return communityResponse;
}

if (opts.url === `https://graph.microsoft.com/v1.0/employeeExperience/communities?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'`) {
return {
value: [
communityResponse
]
};
return null;
}

throw 'Invalid Request';
});

const actual = await vivaEngage.getEntraGroupIdByCommunityDisplayName(displayName);
assert.deepStrictEqual(actual, entraGroupId);
await assert.rejects(vivaEngage.getCommunityById(communityId, ['groupId'])), Error(`The specified Viva Engage community with ID '${communityId}' does not exist.`);
});
});
Loading

0 comments on commit c72eeb7

Please sign in to comment.