Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make spo user get command return current user if no arguments are provided #5514

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/docs/cmd/spo/user/user-get.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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|[email protected]"
```
Get user logged-in currently
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Get user logged-in currently
Get the currently logged-in user


```sh
m365 spo user get --webUrl https://contoso.sharepoint.com/sites/project-x
```

## Response

Expand Down
65 changes: 52 additions & 13 deletions src/m365/spo/commands/user/user-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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|[email protected]",
"Title": "John Doe",
"PrincipalType": 1,
"Email": "[email protected]",
"Expiration": "",
"IsEmailAuthenticationGuestUser": false,
"IsShareByEmailGuestUser": false,
"IsSiteAdmin": false,
"UserId": { "NameId": "10010001b0c19a2", "NameIdIssuer": "urn:federation:microsoftonline" },
"UserPrincipalName": "[email protected]"
}]
};
}
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|[email protected]",
Title: "John Doe",
PrincipalType: 1,
Email: "[email protected]",
Expiration: "",
IsEmailAuthenticationGuestUser: false,
IsShareByEmailGuestUser: false,
IsSiteAdmin: false,
UserId: { NameId: "10010001b0c19a2", NameIdIssuer: "urn:federation:microsoftonline" },
UserPrincipalName: "[email protected]"
}]
}));
});

it('handles error correctly', async () => {
sinon.stub(request, 'get').callsFake(() => {
throw 'An error has occurred';
Expand Down Expand Up @@ -244,18 +290,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) => {
Expand Down Expand Up @@ -328,4 +362,9 @@ describe(commands.USER_GET, () => {
const actual = await command.validate({ options: { webUrl: 'https://contoso.sharepoint.com', loginName: "i:0#.f|membership|[email protected]" } }, 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);
});
});
8 changes: 7 additions & 1 deletion src/m365/spo/commands/user/user-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand All @@ -95,6 +98,9 @@ class SpoUserGetCommand extends SpoCommand {
else if (args.options.loginName) {
requestUrl = `${args.options.webUrl}/_api/web/siteusers/GetByLoginName('${formatting.encodeQueryParameter(args.options.loginName)}')`;
}
else {
requestUrl = `${args.options.webUrl}/_api/web/currentuser`;
}

const requestOptions: CliRequestOptions = {
url: requestUrl,
Expand Down