Skip to content

Commit

Permalink
Update to remove function getUserId
Browse files Browse the repository at this point in the history
  • Loading branch information
reshmee011 committed Oct 9, 2023
1 parent 5cdf5ff commit 9e451fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 62 deletions.
63 changes: 18 additions & 45 deletions src/m365/teams/commands/user/user-app-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { CommandInfo } from '../../../../cli/CommandInfo.js';
import { Logger } from '../../../../cli/Logger.js';
import { CommandError } from '../../../../Command.js';
import request from '../../../../request.js';
import { telemetry } from '../../../../telemetry.js';
import { formatting } from '../../../../utils/formatting.js';
import { telemetry } from '../../../../telemetry.js';
import { pid } from '../../../../utils/pid.js';
import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
Expand Down Expand Up @@ -82,14 +82,6 @@ describe(commands.USER_APP_REMOVE, () => {
assert.notStrictEqual(actual, true);
});

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

it('fails validation if the userName is not a valid UPN.', async () => {
const actual = await command.validate({
options: {
Expand All @@ -100,16 +92,6 @@ describe(commands.USER_APP_REMOVE, () => {
assert.notStrictEqual(actual, true);
});

it('fails validation if the both userId and userName are provided.', async () => {
const actual = await command.validate({
options: {
userId: userId,
userName: userName
}
}, commandInfo);
assert.notStrictEqual(actual, true);
});

it('passes validation when the input is correct', async () => {
const actual = await command.validate({
options: {
Expand Down Expand Up @@ -174,69 +156,60 @@ describe(commands.USER_APP_REMOVE, () => {
} as any);
});

it('removes the app for the specified user when prompt is confirmed (debug)', async () => {
sinon.stub(request, 'delete').callsFake((opts) => {
if ((opts.url as string).indexOf(`/users/${userId}/teamwork/installedApps/${appId}`) > -1) {
it('removes the app for the specified user using username when confirmation is specified.', async () => {
sinon.stub(request, 'delete').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/users/${formatting.encodeQueryParameter(userName)}/teamwork/installedApps/${appId}`) > -1) {
return Promise.resolve();
}
throw 'Invalid request';
});

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

await command.action(logger, {
options: {
userId: userId,
userName: userName,
id: appId,
debug: true
force: true
}
} as any);
});

it('removes the app for the specified user using username', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
it('removes the app for the specified user when prompt is confirmed (debug)', async () => {
sinon.stub(request, 'delete').callsFake((opts) => {
if ((opts.url as string).indexOf(`/users/${userId}/teamwork/installedApps/${appId}`) > -1) {
return Promise.resolve();
}

if ((opts.url as string).indexOf(`/users/${formatting.encodeQueryParameter(userName)}/id`) > -1) {
return { "value": userId };
}

throw 'Invalid request';
});

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

await command.action(logger, {
options: {
userName: userName,
id: appId
userId: userId,
id: appId,
debug: true
}
} as any);
});

it('removes the app for the specified user using username when confirmation is specified.', async () => {
sinon.stub(request, 'delete').callsFake(async (opts) => {
it('removes the app for the specified user using username', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/users/${userId}/teamwork/installedApps/${appId}`) > -1) {
return Promise.resolve();
}

if ((opts.url as string).indexOf(`/users/${formatting.encodeQueryParameter(userName)}/id`) > -1) {
return { "value": userId };
}

throw 'Invalid request';
});

await command.action(logger, {
options: {
userName: userName,
id: appId,
force: true
id: appId
}
} as any);
});


it('correctly handles error while removing teams app', async () => {
const error = {
"error": {
Expand Down
18 changes: 1 addition & 17 deletions src/m365/teams/commands/user/user-app-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class TeamsUserAppRemoveCommand extends GraphCommand {

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

const requestOptions: CliRequestOptions = {
Expand Down Expand Up @@ -120,22 +120,6 @@ class TeamsUserAppRemoveCommand extends GraphCommand {
}
}
}

private async getUserId(args: CommandArgs): Promise<{ value: string }> {
if (args.options.userId) {
return { value: args.options.userId };
}

const requestOptions: CliRequestOptions = {
url: `${this.resource}/v1.0/users/${formatting.encodeQueryParameter(args.options.userName)}/id`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};

return request.get<{ value: string; }>(requestOptions);
}
}

export default new TeamsUserAppRemoveCommand();

0 comments on commit 9e451fe

Please sign in to comment.