From 2b941dc09d18e019a2ed901ca7989f808f4a120c Mon Sep 17 00:00:00 2001 From: Vedant Lakshminarayanan Date: Tue, 26 Sep 2023 09:52:57 +0530 Subject: [PATCH 1/3] make spo user get command return current user if no args are provided --- docs/docs/cmd/spo/user/user-get.mdx | 5 +++++ src/m365/spo/commands/user/user-get.spec.ts | 17 +++++------------ src/m365/spo/commands/user/user-get.ts | 12 +++++++++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/docs/docs/cmd/spo/user/user-get.mdx b/docs/docs/cmd/spo/user/user-get.mdx index d692223f0d9..1f3d40e347a 100644 --- a/docs/docs/cmd/spo/user/user-get.mdx +++ b/docs/docs/cmd/spo/user/user-get.mdx @@ -49,6 +49,11 @@ Get user by login name for a web ```sh m365 spo user get --webUrl https://contoso.sharepoint.com/sites/project-x --loginName "i:0#.f|membership|john.doe@mytenant.onmicrosoft.com" ``` +Get user logged-in currently + +```sh +m365 spo user get --webUrl https://contoso.sharepoint.com/sites/project-x +``` ## Response diff --git a/src/m365/spo/commands/user/user-get.spec.ts b/src/m365/spo/commands/user/user-get.spec.ts index 70a07409615..68dcf849585 100644 --- a/src/m365/spo/commands/user/user-get.spec.ts +++ b/src/m365/spo/commands/user/user-get.spec.ts @@ -244,18 +244,6 @@ describe(commands.USER_GET, () => { assert.notStrictEqual(actual, true); }); - it('fails validation if id or email or loginName options are not passed', async () => { - sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => { - if (settingName === settingsNames.prompt) { - return false; - } - - return defaultValue; - }); - - const actual = await command.validate({ options: { webUrl: 'https://contoso.sharepoint.com' } }, commandInfo); - assert.notStrictEqual(actual, true); - }); it('fails validation if id, email and loginName options are passed (multiple options)', async () => { sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => { @@ -328,4 +316,9 @@ describe(commands.USER_GET, () => { const actual = await command.validate({ options: { webUrl: 'https://contoso.sharepoint.com', loginName: "i:0#.f|membership|john.doe@mytenant.onmicrosoft.com" } }, commandInfo); assert.strictEqual(actual, true); }); + + it('passes validation if the url is valid and no other options are provided', async () => { + const actual = await command.validate({ options: { webUrl: 'https://contoso.sharepoint.com' } }, commandInfo); + assert.strictEqual(actual, true); + }); }); diff --git a/src/m365/spo/commands/user/user-get.ts b/src/m365/spo/commands/user/user-get.ts index dce58a5c360..b8ce5a335fd 100644 --- a/src/m365/spo/commands/user/user-get.ts +++ b/src/m365/spo/commands/user/user-get.ts @@ -76,7 +76,10 @@ class SpoUserGetCommand extends SpoCommand { } #initOptionSets(): void { - this.optionSets.push({ options: ['id', 'email', 'loginName'] }); + this.optionSets.push({ + options: ['id', 'email', 'loginName'], + runsWhen: (args) => args.options.id || args.options.loginName || args.options.email + }); } public async commandAction(logger: Logger, args: CommandArgs): Promise { @@ -90,10 +93,13 @@ class SpoUserGetCommand extends SpoCommand { requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetById('${formatting.encodeQueryParameter(args.options.id.toString())}')`; } else if (args.options.email) { - requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByEmail('${formatting.encodeQueryParameter(args.options.email)}')`; + requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByEmail('${formatting.encodeQueryParameter(args.options.email as string)}')`; } else if (args.options.loginName) { - requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByLoginName('${formatting.encodeQueryParameter(args.options.loginName)}')`; + requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByLoginName('${formatting.encodeQueryParameter(args.options.loginName as string)}')`; + } + else { + requestUrl = `${args.options.webUrl}/_api/web/currentuser`; } const requestOptions: CliRequestOptions = { From 601a28a53e06c3b8f3c23cb9ee3fe66a186baf3f Mon Sep 17 00:00:00 2001 From: Vedant Lakshminarayanan Date: Tue, 26 Sep 2023 10:00:22 +0530 Subject: [PATCH 2/3] remove string casting --- src/m365/spo/commands/user/user-get.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/m365/spo/commands/user/user-get.ts b/src/m365/spo/commands/user/user-get.ts index b8ce5a335fd..6b1aa52920f 100644 --- a/src/m365/spo/commands/user/user-get.ts +++ b/src/m365/spo/commands/user/user-get.ts @@ -93,10 +93,10 @@ class SpoUserGetCommand extends SpoCommand { requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetById('${formatting.encodeQueryParameter(args.options.id.toString())}')`; } else if (args.options.email) { - requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByEmail('${formatting.encodeQueryParameter(args.options.email as string)}')`; + requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByEmail('${formatting.encodeQueryParameter(args.options.email)}')`; } else if (args.options.loginName) { - requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByLoginName('${formatting.encodeQueryParameter(args.options.loginName as string)}')`; + requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByLoginName('${formatting.encodeQueryParameter(args.options.loginName)}')`; } else { requestUrl = `${args.options.webUrl}/_api/web/currentuser`; From bae1c1c75cbd9df0e35573feeb7e4c4a59f2dfd2 Mon Sep 17 00:00:00 2001 From: Vedant Lakshminarayanan Date: Tue, 26 Sep 2023 13:56:51 +0530 Subject: [PATCH 3/3] add test --- src/m365/spo/commands/user/user-get.spec.ts | 48 ++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/m365/spo/commands/user/user-get.spec.ts b/src/m365/spo/commands/user/user-get.spec.ts index 68dcf849585..f1d8076378d 100644 --- a/src/m365/spo/commands/user/user-get.spec.ts +++ b/src/m365/spo/commands/user/user-get.spec.ts @@ -116,7 +116,6 @@ describe(commands.USER_GET, () => { })); }); - it('retrieves user by email with output option json', async () => { sinon.stub(request, 'get').callsFake(async (opts) => { if ((opts.url as string).indexOf('/_api/web/siteusers/GetByEmail') > -1) { @@ -215,6 +214,53 @@ describe(commands.USER_GET, () => { })); }); + + it('retrieves current logged in user', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === 'https://contoso.sharepoint.com/_api/web/currentuser') { + return { + "value": [{ + "Id": 6, + "IsHiddenInUI": false, + "LoginName": "i:0#.f|membership|john.doe@mytenant.onmicrosoft.com", + "Title": "John Doe", + "PrincipalType": 1, + "Email": "john.deo@mytenant.onmicrosoft.com", + "Expiration": "", + "IsEmailAuthenticationGuestUser": false, + "IsShareByEmailGuestUser": false, + "IsSiteAdmin": false, + "UserId": { "NameId": "10010001b0c19a2", "NameIdIssuer": "urn:federation:microsoftonline" }, + "UserPrincipalName": "john.deo@mytenant.onmicrosoft.com" + }] + }; + } + throw 'Invalid request'; + }); + + await command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com' + } + }); + assert(loggerLogSpy.calledWith({ + value: [{ + Id: 6, + IsHiddenInUI: false, + LoginName: "i:0#.f|membership|john.doe@mytenant.onmicrosoft.com", + Title: "John Doe", + PrincipalType: 1, + Email: "john.deo@mytenant.onmicrosoft.com", + Expiration: "", + IsEmailAuthenticationGuestUser: false, + IsShareByEmailGuestUser: false, + IsSiteAdmin: false, + UserId: { NameId: "10010001b0c19a2", NameIdIssuer: "urn:federation:microsoftonline" }, + UserPrincipalName: "john.deo@mytenant.onmicrosoft.com" + }] + })); + }); + it('handles error correctly', async () => { sinon.stub(request, 'get').callsFake(() => { throw 'An error has occurred';