diff --git a/src/m365/aad/commands/o365group/o365group-get.spec.ts b/src/m365/aad/commands/o365group/o365group-get.spec.ts index 92bbf9cf2c6..666ce4e7cb2 100644 --- a/src/m365/aad/commands/o365group/o365group-get.spec.ts +++ b/src/m365/aad/commands/o365group/o365group-get.spec.ts @@ -383,6 +383,41 @@ describe(commands.O365GROUP_GET, () => { })); }); + it('throws error if retrieved group is not a M365 group', async () => { + const groupId = '1caf7dcd-7e83-4c3a-94f7-932a1299c844'; + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://graph.microsoft.com/v1.0/groups/1caf7dcd-7e83-4c3a-94f7-932a1299c844`) { + return { + "id": groupId, + "deletedDateTime": null, + "classification": null, + "createdDateTime": "2017-11-29T03:27:05Z", + "description": "This is the Contoso Finance Group. Please come here and check out the latest news, posts, files, and more.", + "displayName": "Finance", + "groupTypes": [], + "mail": "finance@contoso.onmicrosoft.com", + "mailEnabled": true, + "mailNickname": "finance", + "onPremisesLastSyncDateTime": null, + "onPremisesProvisioningErrors": [], + "onPremisesSecurityIdentifier": null, + "onPremisesSyncEnabled": null, + "preferredDataLocation": null, + "proxyAddresses": [ + "SMTP:finance@contoso.onmicrosoft.com" + ], + "renewedDateTime": "2017-11-29T03:27:05Z", + "securityEnabled": false, + "visibility": "Public" + }; + } + + throw 'Invalid request'; + }); + + await assert.rejects(command.action(logger, { options: { id: groupId } }), new CommandError(`Specified group with id '${groupId}' is not a Microsoft 365 group.`)); + }); + it('handles random API error', async () => { const errorMessage = 'Something went wrong'; sinon.stub(request, 'get').rejects(new Error(errorMessage)); diff --git a/src/m365/aad/commands/o365group/o365group-get.ts b/src/m365/aad/commands/o365group/o365group-get.ts index d0b580bce52..3ec9aa95e45 100644 --- a/src/m365/aad/commands/o365group/o365group-get.ts +++ b/src/m365/aad/commands/o365group/o365group-get.ts @@ -1,6 +1,6 @@ import { Logger } from '../../../../cli/Logger'; import GlobalOptions from '../../../../GlobalOptions'; -import request from '../../../../request'; +import request, { CliRequestOptions } from '../../../../request'; import { validation } from '../../../../utils/validation'; import { aadGroup } from '../../../../utils/aadGroup'; import GraphCommand from '../../../base/GraphCommand'; @@ -27,11 +27,11 @@ class AadO365GroupGetCommand extends GraphCommand { constructor() { super(); - + this.#initOptions(); this.#initValidators(); } - + #initOptions(): void { this.options.unshift( { @@ -42,14 +42,14 @@ class AadO365GroupGetCommand extends GraphCommand { } ); } - + #initValidators(): void { this.validators.push( async (args: CommandArgs) => { if (!validation.isValidGuid(args.options.id)) { return `${args.options.id} is not a valid GUID`; } - + return true; } ); @@ -61,8 +61,12 @@ class AadO365GroupGetCommand extends GraphCommand { try { group = await aadGroup.getGroupById(args.options.id); + if (!group.groupTypes!.some(type => type === 'Unified')) { + throw `Specified group with id '${args.options.id}' is not a Microsoft 365 group.`; + } + if (args.options.includeSiteUrl) { - const requestOptions: any = { + const requestOptions: CliRequestOptions = { url: `${this.resource}/v1.0/groups/${group.id}/drive?$select=webUrl`, headers: { accept: 'application/json;odata.metadata=none'