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

Extend prompting to getGroupIdByDisplayName. Closes #5592 #5612

Closed
wants to merge 2 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
68 changes: 68 additions & 0 deletions src/m365/aad/commands/group/group-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { aadGroup } from '../../../../utils/aadGroup.js';
import { Cli } from '../../../../cli/Cli.js';
import { CommandInfo } from '../../../../cli/CommandInfo.js';
import command from './group-remove.js';
import { settingsNames } from '../../../../settingsNames.js';
import { formatting } from '../../../../utils/formatting.js';

describe(commands.GROUP_REMOVE, () => {
const groupId = '2c1ba4c4-cd9b-4417-832f-92a34bc34b2a';
Expand All @@ -22,6 +24,7 @@ describe(commands.GROUP_REMOVE, () => {
let logger: Logger;
let commandInfo: CommandInfo;
let promptOptions: any;
let cli: Cli;

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
Expand All @@ -30,6 +33,7 @@ describe(commands.GROUP_REMOVE, () => {
sinon.stub(session, 'getId').returns('');
auth.service.connected = true;
commandInfo = Cli.getCommandInfo(command);
cli = Cli.getInstance();
});

beforeEach(() => {
Expand All @@ -54,8 +58,11 @@ describe(commands.GROUP_REMOVE, () => {

afterEach(() => {
sinonUtil.restore([
request.get,
request.delete,
aadGroup.getGroupIdByDisplayName,
cli.getSettingWithDefaultValue,
Cli.handleMultipleResultsFound,
Cli.prompt
]);
});
Expand Down Expand Up @@ -116,6 +123,7 @@ describe(commands.GROUP_REMOVE, () => {
}
}
};

sinon.stub(request, 'delete').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups/${groupId}`) {
throw error;
Expand All @@ -139,6 +147,66 @@ describe(commands.GROUP_REMOVE, () => {
assert(promptIssued);
});

it('handles error when multiple groups with the specified displayName found', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
return false;
}

return defaultValue;
});

sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'&$select=id`) {
return {
value: [
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' },
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67g' }
]
};
}

return 'Invalid Request';
});

sinon.stub(request, 'delete').rejects('DELETE request executed');

await assert.rejects(command.action(logger, {
options: {
displayName: displayName,
force: true
}
}), new CommandError(`Multiple groups with name 'CLI Test Group' found. Found: 9b1b1e42-794b-4c71-93ac-5ed92488b67f, 9b1b1e42-794b-4c71-93ac-5ed92488b67g.`));
});

it('handles selecting single result when multiple groups with the specified name found and cli is set to prompt', async () => {
sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'&$select=id`) {
return {
value: [
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' },
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67g' }
]
};
}

throw 'Invalid request';
});

sinon.stub(Cli, 'handleMultipleResultsFound').resolves({ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' });

const deleteRequestStub = sinon.stub(request, 'delete').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups/9b1b1e42-794b-4c71-93ac-5ed92488b67f`) {
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { displayName: displayName, force: true } });
assert(deleteRequestStub.called);
});

it('aborts removing group when prompt not confirmed', async () => {
const deleteSpy = sinon.stub(request, 'delete').resolves();

Expand Down
72 changes: 71 additions & 1 deletion src/m365/aad/commands/group/group-user-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { aadGroup } from '../../../../utils/aadGroup.js';
import { pid } from '../../../../utils/pid.js';
import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
import { settingsNames } from '../../../../settingsNames.js';
import { formatting } from '../../../../utils/formatting.js';
import commands from '../../commands.js';
import command from './group-user-list.js';

Expand All @@ -22,6 +24,7 @@ describe(commands.GROUP_USER_LIST, () => {
let logger: Logger;
let loggerLogSpy: sinon.SinonSpy;
let commandInfo: CommandInfo;
let cli: Cli;

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
Expand All @@ -30,6 +33,7 @@ describe(commands.GROUP_USER_LIST, () => {
sinon.stub(session, 'getId').returns('');
auth.service.connected = true;
commandInfo = Cli.getCommandInfo(command);
cli = Cli.getInstance();
});

beforeEach(() => {
Expand All @@ -52,7 +56,9 @@ describe(commands.GROUP_USER_LIST, () => {
afterEach(() => {
sinonUtil.restore([
request.get,
aadGroup.getGroupIdByDisplayName
aadGroup.getGroupIdByDisplayName,
cli.getSettingWithDefaultValue,
Cli.handleMultipleResultsFound
]);
});

Expand Down Expand Up @@ -210,6 +216,70 @@ describe(commands.GROUP_USER_LIST, () => {
]));
});

it('handles error when multiple Azure AD groups with the specified displayName found', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
return false;
}

return defaultValue;
});

sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '${formatting.encodeQueryParameter(groupDisplayName)}'&$select=id`) {
return {
value: [
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' },
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67g' }
]
};
}

return 'Invalid Request';
});

await assert.rejects(command.action(logger, {
options: {
groupDisplayName: groupDisplayName
}
}), new CommandError(`Multiple groups with name 'CLI Test Group' found. Found: 9b1b1e42-794b-4c71-93ac-5ed92488b67f, 9b1b1e42-794b-4c71-93ac-5ed92488b67g.`));
});

it('handles selecting single result when multiple Azure AD groups with the specified name found and cli is set to prompt', async () => {
sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '${formatting.encodeQueryParameter(groupDisplayName)}'&$select=id`) {
return {
value: [
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' },
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67g' }
]
};
}

if (opts.url === `https://graph.microsoft.com/v1.0/groups/9b1b1e42-794b-4c71-93ac-5ed92488b67f/Owners/microsoft.graph.user?$select=id,displayName,userPrincipalName,givenName,surname`) {
return {
"value": [{ "id": "00000000-0000-0000-0000-000000000000", "displayName": "Anne Matthews", "userPrincipalName": "[email protected]", "givenName": "Anne", "surname": "Matthews" }]
};
}

throw 'Invalid request';
});

sinon.stub(Cli, 'handleMultipleResultsFound').resolves({ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' });

await command.action(logger, { options: { groupDisplayName: groupDisplayName, role: "Owner" } });
assert(loggerLogSpy.calledOnceWithExactly([
{
"id": "00000000-0000-0000-0000-000000000000",
"displayName": "Anne Matthews",
"userPrincipalName": "[email protected]",
"givenName": "Anne",
"surname": "Matthews",
"roles": ["Owner"]
}
]));
});

it('correctly lists all members in a Azure AD group', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups/2c1ba4c4-cd9b-4417-832f-92a34bc34b2a/Members/microsoft.graph.user?$select=id,displayName,userPrincipalName,givenName,surname`) {
Expand Down
49 changes: 48 additions & 1 deletion src/m365/flow/commands/owner/owner-ensure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe(commands.OWNER_ENSURE, () => {
let log: string[];
let logger: Logger;
let commandInfo: CommandInfo;
let cli: Cli;

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
Expand All @@ -36,6 +37,7 @@ describe(commands.OWNER_ENSURE, () => {
sinon.stub(session, 'getId').returns('');
auth.service.connected = true;
commandInfo = Cli.getCommandInfo(command);
cli = Cli.getInstance();
});

beforeEach(() => {
Expand All @@ -55,9 +57,12 @@ describe(commands.OWNER_ENSURE, () => {

afterEach(() => {
sinonUtil.restore([
request.get,
request.post,
aadGroup.getGroupByDisplayName,
aadUser.getUserIdByUpn,
request.post
cli.getSettingWithDefaultValue,
Cli.handleMultipleResultsFound
]);
});

Expand Down Expand Up @@ -216,6 +221,48 @@ describe(commands.OWNER_ENSURE, () => {
assert.deepStrictEqual(postRequestStub.lastCall.args[0].data, requestBody);
});

it('handles selecting single result when multiple groups with the specified name found and cli is set to prompt', async () => {
const requestBody = {
put: [
{
properties: {
principal: {
id: validGroupId,
type: 'Group'
},
roleName: 'CanEdit'
}
}
]
};

sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '${formatting.encodeQueryParameter(validGroupName)}'&$select=id`) {
return {
value: [
{ id: validGroupId },
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67g' }
]
};
}

throw 'Invalid request';
});

sinon.stub(Cli, 'handleMultipleResultsFound').resolves({ id: validGroupId });

const postRequestStub = sinon.stub(request, 'post').callsFake(async opts => {
if (opts.url === `https://management.azure.com/providers/Microsoft.ProcessSimple/scopes/admin/environments/${formatting.encodeQueryParameter(validEnvironmentName)}/flows/${formatting.encodeQueryParameter(validFlowName)}/modifyPermissions?api-version=2016-11-01`) {
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { verbose: true, environmentName: validEnvironmentName, flowName: validFlowName, groupName: validGroupName, roleName: validRoleName, asAdmin: true } });
assert.deepStrictEqual(postRequestStub.lastCall.args[0].data, requestBody);
});

it('correctly handles API OData error', async () => {
const error = {
error: {
Expand Down
65 changes: 65 additions & 0 deletions src/m365/flow/commands/owner/owner-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { telemetry } from '../../../../telemetry.js';
import { aadGroup } from '../../../../utils/aadGroup.js';
import { aadUser } from '../../../../utils/aadUser.js';
import { formatting } from '../../../../utils/formatting.js';
import { settingsNames } from '../../../../settingsNames.js';
import { pid } from '../../../../utils/pid.js';
import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
Expand All @@ -32,6 +33,7 @@ describe(commands.OWNER_REMOVE, () => {
let logger: Logger;
let commandInfo: CommandInfo;
let promptOptions: any;
let cli: Cli;

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
Expand All @@ -40,6 +42,7 @@ describe(commands.OWNER_REMOVE, () => {
sinon.stub(session, 'getId').returns('');
auth.service.connected = true;
commandInfo = Cli.getCommandInfo(command);
cli = Cli.getInstance();
});

beforeEach(() => {
Expand All @@ -64,8 +67,11 @@ describe(commands.OWNER_REMOVE, () => {

afterEach(() => {
sinonUtil.restore([
request.get,
aadGroup.getGroupIdByDisplayName,
aadUser.getUserIdByUpn,
cli.getSettingWithDefaultValue,
Cli.handleMultipleResultsFound,
Cli.prompt,
request.post
]);
Expand Down Expand Up @@ -140,6 +146,65 @@ describe(commands.OWNER_REMOVE, () => {
assert.deepStrictEqual(postStub.lastCall.args[0].data, requestBodyGroup);
});

it('handles error when multiple groups with the specified name found', async () => {
sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => {
if (settingName === settingsNames.prompt) {
return false;
}

return defaultValue;
});

sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '${formatting.encodeQueryParameter(groupName)}'&$select=id`) {
return {
value: [
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' },
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67g' }
]
};
}

return 'Invalid Request';
});

sinon.stub(request, 'post').rejects('POST request executed');

await assert.rejects(command.action(logger, {
options: {
verbose: true, environmentName: environmentName, flowName: flowName, groupName: groupName, asAdmin: true, force: true
}
}), new CommandError(`Multiple groups with name 'Test Group' found. Found: 9b1b1e42-794b-4c71-93ac-5ed92488b67f, 9b1b1e42-794b-4c71-93ac-5ed92488b67g.`));
});

it('handles selecting single result when multiple groups with the name found and cli is set to prompt', async () => {
sinon.stub(request, 'get').callsFake(async opts => {
if (opts.url === `https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '${formatting.encodeQueryParameter(groupName)}'&$select=id`) {
return {
value: [
{ id: '9b1b1e42-794b-4c71-93ac-5ed92488b67f' },
{ id: '37a0264d-fea4-4e87-8e5e-e574ff878cf2' }
]
};
}

throw 'Invalid request';
});

sinon.stub(Cli, 'handleMultipleResultsFound').resolves({ id: '37a0264d-fea4-4e87-8e5e-e574ff878cf2' });

const postStub = sinon.stub(request, 'post').callsFake(async opts => {
if (opts.url === requestUrlAdmin) {
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { verbose: true, environmentName: environmentName, flowName: flowName, groupName: groupName, asAdmin: true, force: true } });
assert.deepStrictEqual(postStub.lastCall.args[0].data, requestBodyGroup);
});

it('throws error when no environment found', async () => {
const error = {
error: {
Expand Down
Loading