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

Extends 'teams user app remove' command with support for name option. Closes #5447 #5460

Closed
wants to merge 11 commits into from
15 changes: 12 additions & 3 deletions docs/docs/cmd/teams/user/user-app-remove.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ m365 teams user app remove [options]
## Options

```md definition-list
`--id <id>`
: The unique id of the app instance installed for the user.
`--id [id]`
: The unique id of the app instance installed for the user. Specify either `id` or `name`.

`--name [name]`
: Name of the app instance installed for the user. Specify either `id` or `name`.

`--userId <userId>`
: The ID of the user to uninstall the app for.
Expand All @@ -32,12 +35,18 @@ Do not use the ID from the manifest of the zip app package or the id from the Mi

## Examples

Uninstall an app for the specified user.
Uninstall an app by id for the specified user.

```sh
m365 teams user app remove --id YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY= --userId 2609af39-7775-4f94-a3dc-0dd67657e900
```

Uninstall an app by name for the specified user.

```sh
m365 teams user app remove --name HelloWorld --userId 2609af39-7775-4f94-a3dc-0dd67657e900
```

## Response

The command won't return a response on success.
121 changes: 115 additions & 6 deletions src/m365/teams/commands/user/user-app-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe(commands.USER_APP_REMOVE, () => {

afterEach(() => {
sinonUtil.restore([
request.get,
request.delete,
Cli.prompt
]);
Expand All @@ -68,6 +69,24 @@ describe(commands.USER_APP_REMOVE, () => {
assert.notStrictEqual(command.description, null);
});

it('fails validation if both id and name options are passed', async () => {
const actual = await command.validate({
options: {
id: 'e3e29acb-8c79-412b-b746-e6c39ff4cd22',
name: 'TeamsApp'
}
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation if both id and name options are not passed', async () => {
const actual = await command.validate({
options: {
}
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation if the userId is not a valid guid.', async () => {
const actual = await command.validate({
options: {
Expand Down Expand Up @@ -95,10 +114,12 @@ describe(commands.USER_APP_REMOVE, () => {
id: 'YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY='
}
} as any);

let promptIssued = false;
if (promptOptions && promptOptions.type === 'confirm') {
promptIssued = true;
}

assert(promptIssued);
});

Expand All @@ -114,11 +135,12 @@ describe(commands.USER_APP_REMOVE, () => {
assert(requestDeleteSpy.notCalled);
});

it('removes the app for the specified user when confirmation is specified (debug)', async () => {
it('removes the app by id for the specified user when confirmation is specified (debug)', async () => {
sinon.stub(request, 'delete').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps/YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY=`) > -1) {
if (opts.url === `https://graph.microsoft.com/v1.0/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps/YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY=`) {
return;
}

throw 'Invalid request';
});

Expand All @@ -132,11 +154,12 @@ describe(commands.USER_APP_REMOVE, () => {
} as any);
});

it('removes the app for the specified user when prompt is confirmed (debug)', async () => {
it('removes the app by id for the specified user when prompt is confirmed (debug)', async () => {
sinon.stub(request, 'delete').callsFake((opts) => {
if ((opts.url as string).indexOf(`/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps/YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY=`) > -1) {
if (opts.url === `https://graph.microsoft.com/v1.0/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps/YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY=`) {
return Promise.resolve();
}

throw 'Invalid request';
});

Expand All @@ -152,6 +175,91 @@ describe(commands.USER_APP_REMOVE, () => {
} as any);
});

it('removes the app by name for the specified user when prompt is confirmed (debug)', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps?$expand=teamsAppDefinition&$filter=teamsAppDefinition/displayName eq 'TeamsApp'`) {
return {
"value": [
{
"id": "YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY=",
"displayName": "TeamsApp"
}
]
};
}

throw 'Invalid request';
});

sinon.stub(request, 'delete').callsFake((opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps/YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY=`) {
return Promise.resolve();
}

throw 'Invalid request';
});

sinonUtil.restore(Cli.prompt);
sinon.stub(Cli, 'prompt').resolves({ continue: true });

await command.action(logger, {
options: {
userId: 'c527a470-a882-481c-981c-ee6efaba85c7',
name: 'TeamsApp',
debug: true
}
} as any);
});

it('fails to get Teams app when app does not exists', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps?$expand=teamsAppDefinition&$filter=teamsAppDefinition/displayName eq 'TeamsApp'`) {
return { value: [] };
}

throw 'Invalid request';
});

await assert.rejects(command.action(logger, {
options: {
debug: true,
userId: 'c527a470-a882-481c-981c-ee6efaba85c7',
name: 'TeamsApp',
force: true
}
} as any), new CommandError('The specified Teams app does not exist'));
});

it('handles error when multiple Teams apps with the specified name found', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps?$expand=teamsAppDefinition&$filter=teamsAppDefinition/displayName eq 'TeamsApp'`) {
return {
"value": [
{
"id": "ZDczZWVjZmQtYzFkNS00MzY2LWJkMjEtZDUyOTM1ZThkYjkxIyMxLjYuMC4wIyNQdWJsaXNoZWQ=",
"displayName": "TeamsApp"
},
{
"id": "NmY0ODM2N2EtMjVmMC00NjNmLTlmMGQtMmFiZTBiYmYzNzRjIyMxLjAuMCMjUHVibGlzaGVk",
"displayName": "TeamsApp"
}
]
};
}

throw 'Invalid request';
});

await assert.rejects(command.action(logger, {
options: {
debug: true,
userId: 'c527a470-a882-481c-981c-ee6efaba85c7',
name: 'TeamsApp',
force: true
}
} as any), new CommandError('Multiple Teams apps with name TeamsApp found. Please choose one of these ids: ZDczZWVjZmQtYzFkNS00MzY2LWJkMjEtZDUyOTM1ZThkYjkxIyMxLjYuMC4wIyNQdWJsaXNoZWQ=, NmY0ODM2N2EtMjVmMC00NjNmLTlmMGQtMmFiZTBiYmYzNzRjIyMxLjAuMCMjUHVibGlzaGVk'));
});

it('correctly handles error while removing teams app', async () => {
const error = {
"error": {
Expand All @@ -166,9 +274,10 @@ describe(commands.USER_APP_REMOVE, () => {
};

sinon.stub(request, 'delete').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps/YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY=`) > -1) {
if (opts.url === `https://graph.microsoft.com/v1.0/users/c527a470-a882-481c-981c-ee6efaba85c7/teamwork/installedApps/YzUyN2E0NzAtYTg4Mi00ODFjLTk4MWMtZWU2ZWZhYmE4NWM3IyM0ZDFlYTA0Ny1mMTk2LTQ1MGQtYjJlOS0wZDI4NTViYTA1YTY=`) {
throw error;
}

throw 'Invalid request';
});

Expand All @@ -180,4 +289,4 @@ describe(commands.USER_APP_REMOVE, () => {
}
} as any), new CommandError(error.error.message));
});
});
});
54 changes: 49 additions & 5 deletions src/m365/teams/commands/user/user-app-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Cli } from '../../../../cli/Cli.js';
import { Logger } from '../../../../cli/Logger.js';
import GlobalOptions from '../../../../GlobalOptions.js';
import request, { CliRequestOptions } from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
import { validation } from '../../../../utils/validation.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
Expand All @@ -11,7 +12,8 @@ interface CommandArgs {
}

interface Options extends GlobalOptions {
id: string;
id?: string;
name?: string;
userId: string;
force?: boolean;
}
Expand All @@ -31,20 +33,26 @@ class TeamsUserAppRemoveCommand extends GraphCommand {
this.#initTelemetry();
this.#initOptions();
this.#initValidators();
this.#initOptionSets();
}

#initTelemetry(): void {
this.telemetry.push((args: CommandArgs) => {
Object.assign(this.telemetryProperties, {
force: (!!args.options.force).toString()
force: (!!args.options.force).toString(),
id: typeof args.options.id !== 'undefined',
name: typeof args.options.name !== 'undefined'
});
});
}

#initOptions(): void {
this.options.unshift(
{
option: '--id <id>'
option: '--id [id]'
},
{
option: '--name [name]'
},
{
option: '--userId <userId>'
Expand All @@ -67,12 +75,21 @@ class TeamsUserAppRemoveCommand extends GraphCommand {
);
}

#initOptionSets(): void {
this.optionSets.push({ options: ['id', 'name'] });
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
const removeApp = async (): Promise<void> => {
const appId: string = await this.getAppId(args);
const endpoint: string = `${this.resource}/v1.0`;

if (this.verbose) {
await logger.logToStderr(`Removing app with ID ${args.options.id} for user ${args.options.userId}`);
}

const requestOptions: CliRequestOptions = {
url: `${endpoint}/users/${args.options.userId}/teamwork/installedApps/${args.options.id}`,
url: `${endpoint}/users/${args.options.userId}/teamwork/installedApps/${appId}`,
headers: {
'accept': 'application/json;odata.metadata=none'
},
Expand All @@ -95,14 +112,41 @@ class TeamsUserAppRemoveCommand extends GraphCommand {
type: 'confirm',
name: 'continue',
default: false,
message: `Are you sure you want to remove the app with id ${args.options.id} for user ${args.options.userId}?`
message: `Are you sure you want to remove the app ${args.options.id || args.options.name} for user ${args.options.userId}?`
});

if (result.continue) {
await removeApp();
}
}
}

private async getAppId(args: CommandArgs): Promise<string> {
if (args.options.id) {
return args.options.id;
}

const requestOptions: CliRequestOptions = {
url: `${this.resource}/v1.0/users/${args.options.userId}/teamwork/installedApps?$expand=teamsAppDefinition&$filter=teamsAppDefinition/displayName eq '${formatting.encodeQueryParameter(args.options.name as string)}'`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};

const response = await request.get<{ value: { id: string; }[] }>(requestOptions);
const app: { id: string; } | undefined = response.value[0];

if (!app) {
throw `The specified Teams app does not exist`;
}

if (response.value.length > 1) {
throw `Multiple Teams apps with name ${args.options.name} found. Please choose one of these ids: ${response.value.map(x => x.id).join(', ')}`;
Copy link
Member

@waldekmastykarz waldekmastykarz Nov 11, 2023

Choose a reason for hiding this comment

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

Rather than throwing an error here, we should use Cli.handleMultipleResultsFound() which shows a disambiguation prompt or throws error depending on a user's settings. Sorry I missed it earlier.

}

return app.id;
}
}

export default new TeamsUserAppRemoveCommand();