diff --git a/src/m365/todo/commands/task/task-get.spec.ts b/src/m365/todo/commands/task/task-get.spec.ts index 778d7491116..4e8dee772ec 100644 --- a/src/m365/todo/commands/task/task-get.spec.ts +++ b/src/m365/todo/commands/task/task-get.spec.ts @@ -42,10 +42,10 @@ describe(commands.TASK_GET, () => { before(() => { cli = Cli.getInstance(); - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; commandInfo = Cli.getCommandInfo(command); }); @@ -81,7 +81,7 @@ describe(commands.TASK_GET, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.TASK_GET), true); + assert.strictEqual(command.name, commands.TASK_GET); }); it('has a description', () => { diff --git a/src/m365/todo/commands/task/task-list.spec.ts b/src/m365/todo/commands/task/task-list.spec.ts index 2397768d6b8..430ecc85441 100644 --- a/src/m365/todo/commands/task/task-list.spec.ts +++ b/src/m365/todo/commands/task/task-list.spec.ts @@ -22,10 +22,10 @@ describe(commands.TASK_LIST, () => { before(() => { cli = Cli.getInstance(); - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; commandInfo = Cli.getCommandInfo(command); }); @@ -61,7 +61,7 @@ describe(commands.TASK_LIST, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.TASK_LIST), true); + assert.strictEqual(command.name, commands.TASK_LIST); }); it('has a description', () => { @@ -91,11 +91,11 @@ describe(commands.TASK_LIST, () => { }); it('fails to get ToDo Task list when the specified task list does not exist', async () => { - sinon.stub(request, 'get').callsFake((opts) => { + sinon.stub(request, 'get').callsFake(async (opts) => { if ((opts.url as string).indexOf(`/me/todo/lists?$filter=displayName eq '`) > -1) { - return Promise.resolve({ value: [] }); + return { value: [] }; } - return Promise.reject('The specified task list does not exist'); + throw 'The specified task list does not exist'; }); await assert.rejects(command.action(logger, { options: { listName: 'Tasks List' } } as any), new CommandError('The specified task list does not exist')); @@ -120,9 +120,9 @@ describe(commands.TASK_LIST, () => { }); it('lists To Do tasks using listId in JSON output mode', async () => { - sinon.stub(request, 'get').callsFake((opts) => { + sinon.stub(request, 'get').callsFake(async (opts) => { if ((opts.url as string).indexOf(`/tasks`) > -1) { - return Promise.resolve({ + return { value: [ { "importance": "normal", @@ -151,10 +151,10 @@ describe(commands.TASK_LIST, () => { } } ] - }); + }; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { @@ -196,9 +196,9 @@ describe(commands.TASK_LIST, () => { }); it('lists To Do tasks using listName', async () => { - sinon.stub(request, 'get').callsFake((opts) => { + sinon.stub(request, 'get').callsFake(async (opts) => { if ((opts.url as string).indexOf(`/me/todo/lists?$filter=displayName eq '`) > -1) { - return Promise.resolve({ + return { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('e1251b10-1ba4-49e3-b35a-933e3f21772b')/todo/lists", "value": [ { @@ -210,11 +210,11 @@ describe(commands.TASK_LIST, () => { "id": "AQMkAGYzNjMxYTU4LTJjZjYtNDlhMi1iMzQ2LWVmMTU3YmUzOGM5MAAuAAADMN-7V4K8g0q_adetip1DygEAxMBBaLl1lk_dAn8KkjfXKQABF-BAgwAAAA==" } ] - }); + }; } if ((opts.url as string).indexOf(`/tasks`) > -1) { - return Promise.resolve({ + return { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('e1251b10-1ba4-49e3-b35a-933e3f21772b')/todo/lists('AQMkAGYzNjMxYTU4LTJjZjYtNDlhMi1iMzQ2LWVmMTU3YmUzOGM5MAAuAAADMN-7V4K8g0q_adetip1DygEAxMBBaLl1lk_dAn8KkjfXKQABF-BAgwAAAA%3D%3D')/tasks", "value": [ { @@ -232,10 +232,10 @@ describe(commands.TASK_LIST, () => { "lastModifiedDateTime": "2020-11-01T17:13:13.1037095Z" } ] - }); + }; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { diff --git a/src/m365/todo/commands/task/task-list.ts b/src/m365/todo/commands/task/task-list.ts index 9d22e9be30a..60733ac43ba 100644 --- a/src/m365/todo/commands/task/task-list.ts +++ b/src/m365/todo/commands/task/task-list.ts @@ -61,9 +61,9 @@ class TodoTaskListCommand extends GraphCommand { this.optionSets.push({ options: ['listId', 'listName'] }); } - private getTodoListId(args: CommandArgs): Promise { + private async getTodoListId(args: CommandArgs): Promise { if (args.options.listId) { - return Promise.resolve(args.options.listId); + return args.options.listId; } const requestOptions: any = { @@ -74,16 +74,14 @@ class TodoTaskListCommand extends GraphCommand { responseType: 'json' }; - return request.get<{ value: [{ id: string }] }>(requestOptions) - .then(response => { - const taskList: { id: string } | undefined = response.value[0]; + const response: any = await request.get<{ value: [{ id: string }] }>(requestOptions); + const taskList: { id: string } | undefined = response.value[0]; - if (!taskList) { - return Promise.reject(`The specified task list does not exist`); - } + if (!taskList) { + throw `The specified task list does not exist`; + } - return Promise.resolve(taskList.id); - }); + return taskList.id; } public async commandAction(logger: Logger, args: CommandArgs): Promise { diff --git a/src/m365/todo/commands/task/task-remove.spec.ts b/src/m365/todo/commands/task/task-remove.spec.ts index b1eff43b49b..6e307c3ed1c 100644 --- a/src/m365/todo/commands/task/task-remove.spec.ts +++ b/src/m365/todo/commands/task/task-remove.spec.ts @@ -22,10 +22,10 @@ describe(commands.TASK_REMOVE, () => { before(() => { cli = Cli.getInstance(); - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; sinon.stub(Cli, 'prompt').callsFake(async (options) => { promptOptions = options; @@ -64,7 +64,7 @@ describe(commands.TASK_REMOVE, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.TASK_REMOVE), true); + assert.strictEqual(command.name, commands.TASK_REMOVE); }); it('has a description', () => { @@ -72,9 +72,9 @@ describe(commands.TASK_REMOVE, () => { }); it('removes a To Do task by task id and task list name', async () => { - sinon.stub(request, 'get').callsFake((opts) => { + sinon.stub(request, 'get').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists?$filter=displayName eq 'Tasks'`) { - return Promise.resolve({ + return { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('eded3a2a-8f01-40aa-998a-e4f02ec693ba')/todo/lists", "value": [ { @@ -86,18 +86,18 @@ describe(commands.TASK_REMOVE, () => { "id": "BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=" } ] - }); + }; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); - sinon.stub(request, 'delete').callsFake((opts) => { + sinon.stub(request, 'delete').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists/BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=/tasks/AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhAAA=`) { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { @@ -110,9 +110,9 @@ describe(commands.TASK_REMOVE, () => { }); it('removes a To Do task by task id and task list name when confirm option is passed', async () => { - sinon.stub(request, 'get').callsFake((opts) => { + sinon.stub(request, 'get').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists?$filter=displayName eq 'Tasks'`) { - return Promise.resolve({ + return { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('eded3a2a-8f01-40aa-998a-e4f02ec693ba')/todo/lists", "value": [ { @@ -124,17 +124,17 @@ describe(commands.TASK_REMOVE, () => { "id": "BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=" } ] - }); + }; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); - sinon.stub(request, 'delete').callsFake((opts) => { + sinon.stub(request, 'delete').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists/BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=/tasks/AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhAAA=`) { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { @@ -148,9 +148,9 @@ describe(commands.TASK_REMOVE, () => { }); it('removes a To Do task by task id and task list id', async () => { - sinon.stub(request, 'get').callsFake((opts) => { + sinon.stub(request, 'get').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists/BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=`) { - return Promise.resolve({ + return { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('eded3a2a-8f01-40aa-998a-e4f02ec693ba')/todo/lists/$entity", "@odata.etag": "W/\"tPAryi+qT0uvQKa/pHXU5AAAQchLxw==\"", "displayName": "Tasks", @@ -158,17 +158,17 @@ describe(commands.TASK_REMOVE, () => { "isShared": false, "wellknownListName": "defaultList", "id": "BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=" - }); + }; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); - sinon.stub(request, 'delete').callsFake((opts) => { + sinon.stub(request, 'delete').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists/BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=/tasks/AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhAAA=`) { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { @@ -181,9 +181,9 @@ describe(commands.TASK_REMOVE, () => { }); it('handles error correctly', async () => { - sinon.stub(request, 'get').callsFake((opts) => { + sinon.stub(request, 'get').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists?$filter=displayName eq 'FooList'`) { - return Promise.resolve({ + return { "error": { "code": "invalidRequest", "message": "The list FooList cannot be found", @@ -195,13 +195,13 @@ describe(commands.TASK_REMOVE, () => { "client-request-id": "085a2508-e115-63b6-fcc9-05acc2133231" } } - }); + }; } - return Promise.reject('The list FooList cannot be found'); + throw 'The list FooList cannot be found'; }); sinon.stub(request, 'delete').callsFake(() => { - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await assert.rejects(command.action(logger, { @@ -214,9 +214,9 @@ describe(commands.TASK_REMOVE, () => { }); it('prompts before removing the To Do task when confirm option not passed', async () => { - sinon.stub(request, 'get').callsFake((opts) => { + sinon.stub(request, 'get').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists?$filter=displayName eq 'Tasks'`) { - return Promise.resolve({ + return { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('eded3a2a-8f01-40aa-998a-e4f02ec693ba')/todo/lists", "value": [ { @@ -228,17 +228,17 @@ describe(commands.TASK_REMOVE, () => { "id": "BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=" } ] - }); + }; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); - sinon.stub(request, 'delete').callsFake((opts) => { + sinon.stub(request, 'delete').callsFake(async (opts) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists/BBMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhBBB=/tasks/AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIhAAA=`) { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); sinonUtil.restore(Cli.prompt); diff --git a/src/m365/todo/commands/task/task-remove.ts b/src/m365/todo/commands/task/task-remove.ts index a071765941f..a2e5ed610c7 100644 --- a/src/m365/todo/commands/task/task-remove.ts +++ b/src/m365/todo/commands/task/task-remove.ts @@ -65,49 +65,8 @@ class TodoTaskRemoveCommand extends GraphCommand { } public async commandAction(logger: Logger, args: CommandArgs): Promise { - const getToDoListId = async (): Promise => { - if (args.options.listName) { - // Search list by its name - const requestOptions: any = { - url: `${this.resource}/v1.0/me/todo/lists?$filter=displayName eq '${escape(args.options.listName)}'`, - headers: { - accept: "application/json;odata.metadata=none" - }, - responseType: 'json' - }; - const response: { value: { id: string }[] } = await request.get<{ value: { id: string }[] }>(requestOptions); - - return response.value && response.value.length === 1 ? response.value[0].id : undefined; - } - - return Promise.resolve(args.options.listId as string); - }; - - const removeToDoTask = async (): Promise => { - try { - const toDoListId: string | undefined = await getToDoListId(); - - if (!toDoListId) { - throw `The list ${args.options.listName} cannot be found`; - } - - const requestOptions: any = { - url: `${this.resource}/v1.0/me/todo/lists/${toDoListId}/tasks/${args.options.id}`, - headers: { - accept: "application/json;odata.metadata=none" - }, - responseType: 'json' - }; - - await request.delete(requestOptions); - } - catch (err: any) { - this.handleRejectedODataJsonPromise(err); - } - }; - if (args.options.confirm) { - await removeToDoTask(); + await this.removeToDoTask(args); } else { const result = await Cli.prompt<{ continue: boolean }>({ @@ -118,10 +77,51 @@ class TodoTaskRemoveCommand extends GraphCommand { }); if (result.continue) { - await removeToDoTask(); + await this.removeToDoTask(args); } } } + + private async getToDoListId(args: CommandArgs): Promise { + if (args.options.listName) { + // Search list by its name + const requestOptions: any = { + url: `${this.resource}/v1.0/me/todo/lists?$filter=displayName eq '${escape(args.options.listName)}'`, + headers: { + accept: "application/json;odata.metadata=none" + }, + responseType: 'json' + }; + const response: { value: { id: string }[] } = await request.get<{ value: { id: string }[] }>(requestOptions); + + return response.value && response.value.length === 1 ? response.value[0].id : undefined; + } + + return args.options.listId as string; + } + + private async removeToDoTask(args: CommandArgs): Promise { + try { + const toDoListId: string | undefined = await this.getToDoListId(args); + + if (!toDoListId) { + throw `The list ${args.options.listName} cannot be found`; + } + + const requestOptions: any = { + url: `${this.resource}/v1.0/me/todo/lists/${toDoListId}/tasks/${args.options.id}`, + headers: { + accept: "application/json;odata.metadata=none" + }, + responseType: 'json' + }; + + await request.delete(requestOptions); + } + catch (err: any) { + this.handleRejectedODataJsonPromise(err); + } + } } module.exports = new TodoTaskRemoveCommand(); \ No newline at end of file diff --git a/src/m365/todo/commands/task/task-set.spec.ts b/src/m365/todo/commands/task/task-set.spec.ts index 1b2ef7ed126..a700366936f 100644 --- a/src/m365/todo/commands/task/task-set.spec.ts +++ b/src/m365/todo/commands/task/task-set.spec.ts @@ -49,10 +49,10 @@ describe(commands.TASK_SET, () => { before(() => { cli = Cli.getInstance(); - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; commandInfo = Cli.getCommandInfo(command); }); @@ -71,19 +71,19 @@ describe(commands.TASK_SET, () => { } }; (command as any).items = []; - patchStub = sinon.stub(request, 'patch').callsFake((opts: any) => { + patchStub = sinon.stub(request, 'patch').callsFake(async (opts: any) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists/AQMkADlhMTRkOGEzLWQ1M2QtNGVkNS04NjdmLWU0NzJhMjZmZWNmMwAuAAADKvwNgAMNPE_zFNRJXVrU1wEAhHKQZHItDEOVCn8U3xuA2AABmQeVPwAAAA==/tasks/abc`) { - return Promise.resolve(patchRequestData); + return patchRequestData; } - return Promise.reject(); + throw null; }); - sinon.stub(request, 'get').callsFake((opts: any) => { + sinon.stub(request, 'get').callsFake(async (opts: any) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists?$filter=displayName eq 'Tasks%20List'`) { - return Promise.resolve(getRequestData); + return getRequestData; } - return Promise.reject(); + throw null; }); sinon.stub(cli, 'getSettingWithDefaultValue').callsFake(((settingName, defaultValue) => defaultValue)); @@ -104,7 +104,7 @@ describe(commands.TASK_SET, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.TASK_SET), true); + assert.strictEqual(command.name, commands.TASK_SET); }); it('has a description', () => { @@ -257,16 +257,14 @@ describe(commands.TASK_SET, () => { it('rejects if no tasks list is found with the specified list name', async () => { sinonUtil.restore(request.get); - sinon.stub(request, 'get').callsFake((opts: any) => { + sinon.stub(request, 'get').callsFake(async (opts: any) => { if (opts.url === `https://graph.microsoft.com/v1.0/me/todo/lists?$filter=displayName eq 'Tasks%20List'`) { - return Promise.resolve( - { - "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('4cb2b035-ad76-406c-bdc4-6c72ad403a22')/todo/lists", - "value": [] - } - ); + return { + "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('4cb2b035-ad76-406c-bdc4-6c72ad403a22')/todo/lists", + "value": [] + }; } - return Promise.reject(); + throw null; }); await assert.rejects(command.action(logger, { @@ -281,9 +279,7 @@ describe(commands.TASK_SET, () => { it('handles error correctly', async () => { sinonUtil.restore(request.patch); - sinon.stub(request, 'patch').callsFake(() => { - return Promise.reject('An error has occurred'); - }); + sinon.stub(request, 'patch').rejects(new Error('An error has occurred')); await assert.rejects(command.action(logger, { options: { diff --git a/src/m365/todo/commands/task/task-set.ts b/src/m365/todo/commands/task/task-set.ts index 12c9479a860..8b7ff015553 100644 --- a/src/m365/todo/commands/task/task-set.ts +++ b/src/m365/todo/commands/task/task-set.ts @@ -179,9 +179,9 @@ class TodoTaskSetCommand extends GraphCommand { } } - private getTodoListId(args: CommandArgs): Promise { + private async getTodoListId(args: CommandArgs): Promise { if (args.options.listId) { - return Promise.resolve(args.options.listId); + return args.options.listId; } const requestOptions: any = { @@ -192,16 +192,14 @@ class TodoTaskSetCommand extends GraphCommand { responseType: 'json' }; - return request.get<{ value: [{ id: string }] }>(requestOptions) - .then(response => { - const taskList: { id: string } | undefined = response.value[0]; + const response: any = await request.get<{ value: [{ id: string }] }>(requestOptions); + const taskList: { id: string } | undefined = response.value[0]; - if (!taskList) { - return Promise.reject(`The specified task list does not exist`); - } + if (!taskList) { + throw `The specified task list does not exist`; + } - return Promise.resolve(taskList.id); - }); + return taskList.id; } private getDateTimeTimeZone(dateTime: string): { dateTime: string, timeZone: string } { diff --git a/src/m365/util/commands/accesstoken/accesstoken-get.spec.ts b/src/m365/util/commands/accesstoken/accesstoken-get.spec.ts index f9f4eb86600..782b7b02c37 100644 --- a/src/m365/util/commands/accesstoken/accesstoken-get.spec.ts +++ b/src/m365/util/commands/accesstoken/accesstoken-get.spec.ts @@ -16,10 +16,10 @@ describe(commands.ACCESSTOKEN_GET, () => { let loggerLogSpy: sinon.SinonSpy; before(() => { - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; }); @@ -52,7 +52,7 @@ describe(commands.ACCESSTOKEN_GET, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.ACCESSTOKEN_GET), true); + assert.strictEqual(command.name, commands.ACCESSTOKEN_GET); }); it('has a description', () => { @@ -85,7 +85,7 @@ describe(commands.ACCESSTOKEN_GET, () => { }); it('correctly handles error when retrieving access token', async () => { - sinon.stub(auth, 'ensureAccessToken').callsFake(() => Promise.reject('An error has occurred')); + sinon.stub(auth, 'ensureAccessToken').rejects(new Error('An error has occurred')); await assert.rejects(command.action(logger, { options: { resource: 'https://graph.microsoft.com' } } as any), new CommandError('An error has occurred')); }); diff --git a/src/m365/viva/commands/connections/connections-app-create.spec.ts b/src/m365/viva/commands/connections/connections-app-create.spec.ts index f4d72702232..d1c83b3319e 100644 --- a/src/m365/viva/commands/connections/connections-app-create.spec.ts +++ b/src/m365/viva/commands/connections/connections-app-create.spec.ts @@ -66,7 +66,7 @@ describe(commands.CONNECTIONS_APP_CREATE, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.CONNECTIONS_APP_CREATE), true); + assert.strictEqual(command.name, commands.CONNECTIONS_APP_CREATE); }); it('has a description', () => { @@ -74,13 +74,15 @@ describe(commands.CONNECTIONS_APP_CREATE, () => { }); it('creates app package for the specified communication site (root site)', async () => { - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(() => Promise.resolve({ - stdout: JSON.stringify({ - Configuration: 0, - WebTemplate: 'SITEPAGEPUBLISHING' - }), - stderr: '' - })); + sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async () => { + return { + stdout: JSON.stringify({ + Configuration: 0, + WebTemplate: 'SITEPAGEPUBLISHING' + }), + stderr: '' + }; + }); const admZipMockAddFileSpy = sinon.spy(admZipMock, 'addFile'); const admZipMockAddLocalFileSpy = sinon.spy(admZipMock, 'addLocalFile'); const admZipMockWriteZipSpy = sinon.spy(admZipMock, 'writeZip'); @@ -104,13 +106,15 @@ describe(commands.CONNECTIONS_APP_CREATE, () => { }); it('creates app package for the specified communication site (/sites)', async () => { - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(() => Promise.resolve({ - stdout: JSON.stringify({ - Configuration: 0, - WebTemplate: 'SITEPAGEPUBLISHING' - }), - stderr: '' - })); + sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async () => { + return { + stdout: JSON.stringify({ + Configuration: 0, + WebTemplate: 'SITEPAGEPUBLISHING' + }), + stderr: '' + }; + }); const admZipMockAddFileSpy = sinon.spy(admZipMock, 'addFile'); const admZipMockAddLocalFileSpy = sinon.spy(admZipMock, 'addLocalFile'); const admZipMockWriteZipSpy = sinon.spy(admZipMock, 'writeZip'); @@ -135,13 +139,15 @@ describe(commands.CONNECTIONS_APP_CREATE, () => { }); it('creates app package for the specified communication site (/teams + query string)', async () => { - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(() => Promise.resolve({ - stdout: JSON.stringify({ - Configuration: 0, - WebTemplate: 'SITEPAGEPUBLISHING' - }), - stderr: '' - })); + sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async () => { + return { + stdout: JSON.stringify({ + Configuration: 0, + WebTemplate: 'SITEPAGEPUBLISHING' + }), + stderr: '' + }; + }); const admZipMockAddFileSpy = sinon.spy(admZipMock, 'addFile'); const admZipMockAddLocalFileSpy = sinon.spy(admZipMock, 'addLocalFile'); const admZipMockWriteZipSpy = sinon.spy(admZipMock, 'writeZip'); @@ -165,10 +171,10 @@ describe(commands.CONNECTIONS_APP_CREATE, () => { }); it(`fails with an error if the specified site doesn't exist`, async () => { - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(() => Promise.reject({ + sinon.stub(Cli, 'executeCommandWithOutput').rejects({ error: '404 - FILE NOT FOUND', stderr: '404 - FILE NOT FOUND' - })); + }); const admZipMockWriteZipSpy = sinon.spy(admZipMock, 'writeZip'); await assert.rejects(command.action(logger, { @@ -187,10 +193,10 @@ describe(commands.CONNECTIONS_APP_CREATE, () => { }); it(`fails with an error if the specified site doesn't exist (debug)`, async () => { - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(() => Promise.reject({ + sinon.stub(Cli, 'executeCommandWithOutput').rejects({ error: '404 - FILE NOT FOUND', stderr: '404 - FILE NOT FOUND stderr' - })); + }); await assert.rejects(command.action(logger, { options: { @@ -208,13 +214,15 @@ describe(commands.CONNECTIONS_APP_CREATE, () => { }); it('fails with an error if the specified site is not a communication site', async () => { - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(() => Promise.resolve({ - stdout: JSON.stringify({ - Configuration: 0, - WebTemplate: 'TEAM' - }), - stderr: '' - })); + sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async () => { + return { + stdout: JSON.stringify({ + Configuration: 0, + WebTemplate: 'TEAM' + }), + stderr: '' + }; + }); await assert.rejects(command.action(logger, { options: { @@ -232,13 +240,15 @@ describe(commands.CONNECTIONS_APP_CREATE, () => { }); it(`fails with an error if creating the zip file failed`, async () => { - sinon.stub(Cli, 'executeCommandWithOutput').callsFake(() => Promise.resolve({ - stdout: JSON.stringify({ - Configuration: 0, - WebTemplate: 'SITEPAGEPUBLISHING' - }), - stderr: '' - })); + sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async () => { + return { + stdout: JSON.stringify({ + Configuration: 0, + WebTemplate: 'SITEPAGEPUBLISHING' + }), + stderr: '' + }; + }); sinon.stub(admZipMock, 'writeZip').callsFake(() => { throw new Error('An error has occurred'); }); diff --git a/src/m365/yammer/commands/group/group-list.spec.ts b/src/m365/yammer/commands/group/group-list.spec.ts index 1c9307b785f..3fca9a9bbe0 100644 --- a/src/m365/yammer/commands/group/group-list.spec.ts +++ b/src/m365/yammer/commands/group/group-list.spec.ts @@ -184,10 +184,10 @@ describe(commands.GROUP_LIST, () => { }]; before(() => { - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; commandInfo = Cli.getCommandInfo(command); }); @@ -221,7 +221,7 @@ describe(commands.GROUP_LIST, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.GROUP_LIST), true); + assert.strictEqual(command.name, commands.GROUP_LIST); }); it('has a description', () => { @@ -234,11 +234,11 @@ describe(commands.GROUP_LIST, () => { it('correctly handles error', async () => { sinon.stub(request, 'get').callsFake(() => { - return Promise.reject({ + throw { "error": { "base": "An error has occurred." } - }); + }; }); await assert.rejects(command.action(logger, { options: {} } as any), new CommandError('An error has occurred.')); @@ -267,9 +267,9 @@ describe(commands.GROUP_LIST, () => { it('returns groups without more results', async () => { sinon.stub(request, 'get').callsFake((opts) => { if (opts.url === 'https://www.yammer.com/api/v1/groups.json?page=1') { - return Promise.resolve(groupsSecondBatchList); + return groupsSecondBatchList; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { options: {} } as any); @@ -314,7 +314,7 @@ describe(commands.GROUP_LIST, () => { it('returns groups with a specific limit', async () => { sinon.stub(request, 'get').callsFake(() => { - return Promise.resolve(groupsFirstBatchList); + return groupsFirstBatchList; }); await command.action(logger, { options: { limit: 1, output: 'json' } } as any); @@ -325,9 +325,9 @@ describe(commands.GROUP_LIST, () => { it('handles correct parameters userId', async () => { sinon.stub(request, 'get').callsFake((opts) => { if (opts.url === 'https://www.yammer.com/api/v1/groups/for_user/10123190123128.json?page=1') { - return Promise.resolve(groupsSecondBatchList); + return groupsSecondBatchList; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { options: { userId: 10123190123128, output: 'json' } } as any); diff --git a/src/m365/yammer/commands/group/group-user-add.spec.ts b/src/m365/yammer/commands/group/group-user-add.spec.ts index 7f8651acaa7..f7cec640fba 100644 --- a/src/m365/yammer/commands/group/group-user-add.spec.ts +++ b/src/m365/yammer/commands/group/group-user-add.spec.ts @@ -19,10 +19,10 @@ describe(commands.GROUP_USER_ADD, () => { let commandInfo: CommandInfo; before(() => { - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; commandInfo = Cli.getCommandInfo(command); }); @@ -55,7 +55,7 @@ describe(commands.GROUP_USER_ADD, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.GROUP_USER_ADD), true); + assert.strictEqual(command.name, commands.GROUP_USER_ADD); }); it('has a description', () => { @@ -64,11 +64,11 @@ describe(commands.GROUP_USER_ADD, () => { it('correctly handles error', async () => { sinon.stub(request, 'post').callsFake(() => { - return Promise.reject({ + throw { "error": { "base": "An error has occurred." } - }); + }; }); await assert.rejects(command.action(logger, { options: {} } as any), new CommandError('An error has occurred.')); @@ -90,11 +90,11 @@ describe(commands.GROUP_USER_ADD, () => { }); it('calls the service if the current user is added to the group', async () => { - const requestPostedStub = sinon.stub(request, 'post').callsFake((opts) => { + const requestPostedStub = sinon.stub(request, 'post').callsFake(async (opts) => { if (opts.url === 'https://www.yammer.com/api/v1/group_memberships.json') { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); sinon.stub(Cli, 'prompt').callsFake(async () => ( @@ -107,11 +107,11 @@ describe(commands.GROUP_USER_ADD, () => { }); it('calls the service if the user 989998789 is added to the group 1231231', async () => { - const requestPostedStub = sinon.stub(request, 'post').callsFake((opts) => { + const requestPostedStub = sinon.stub(request, 'post').callsFake(async (opts) => { if (opts.url === 'https://www.yammer.com/api/v1/group_memberships.json') { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { options: { debug: true, groupId: 1231231, id: 989998789 } }); @@ -120,11 +120,11 @@ describe(commands.GROUP_USER_ADD, () => { }); it('calls the service if the user suzy@contoso.com is added to the group 1231231', async () => { - const requestPostedStub = sinon.stub(request, 'post').callsFake((opts) => { + const requestPostedStub = sinon.stub(request, 'post').callsFake(async (opts) => { if (opts.url === 'https://www.yammer.com/api/v1/group_memberships.json') { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { options: { debug: true, groupId: 1231231, email: "suzy@contoso.com" } }); diff --git a/src/m365/yammer/commands/group/group-user-remove.spec.ts b/src/m365/yammer/commands/group/group-user-remove.spec.ts index e85e7ff8c6b..a4da2230137 100644 --- a/src/m365/yammer/commands/group/group-user-remove.spec.ts +++ b/src/m365/yammer/commands/group/group-user-remove.spec.ts @@ -20,10 +20,10 @@ describe(commands.GROUP_USER_REMOVE, () => { let commandInfo: CommandInfo; before(() => { - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; commandInfo = Cli.getCommandInfo(command); }); @@ -57,7 +57,7 @@ describe(commands.GROUP_USER_REMOVE, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.GROUP_USER_REMOVE), true); + assert.strictEqual(command.name, commands.GROUP_USER_REMOVE); }); it('has a description', () => { @@ -66,11 +66,11 @@ describe(commands.GROUP_USER_REMOVE, () => { it('correctly handles error', async () => { sinon.stub(request, 'delete').callsFake(() => { - return Promise.reject({ + throw { "error": { "base": "An error has occurred." } - }); + }; }); sinon.stub(Cli, 'prompt').callsFake(async () => ( @@ -96,11 +96,11 @@ describe(commands.GROUP_USER_REMOVE, () => { }); it('calls the service if the current user is removed from the group', async () => { - const requestDeleteStub = sinon.stub(request, 'delete').callsFake((opts) => { + const requestDeleteStub = sinon.stub(request, 'delete').callsFake(async (opts) => { if (opts.url === 'https://www.yammer.com/api/v1/group_memberships.json') { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); sinon.stub(Cli, 'prompt').callsFake(async () => ( @@ -113,11 +113,11 @@ describe(commands.GROUP_USER_REMOVE, () => { }); it('calls the service if the user 989998789 is removed from the group 1231231 with the confirm command', async () => { - const requestDeleteStub = sinon.stub(request, 'delete').callsFake((opts) => { + const requestDeleteStub = sinon.stub(request, 'delete').callsFake(async (opts) => { if (opts.url === 'https://www.yammer.com/api/v1/group_memberships.json') { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { options: { debug: true, groupId: 1231231, id: 989998789, confirm: true } }); @@ -126,11 +126,11 @@ describe(commands.GROUP_USER_REMOVE, () => { }); it('calls the service if the user 989998789 is removed from the group 1231231', async () => { - const requestDeleteStub = sinon.stub(request, 'delete').callsFake((opts) => { + const requestDeleteStub = sinon.stub(request, 'delete').callsFake(async (opts) => { if (opts.url === 'https://www.yammer.com/api/v1/group_memberships.json') { - return Promise.resolve(); + return; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); sinon.stub(Cli, 'prompt').callsFake(async () => ( diff --git a/src/m365/yammer/commands/group/group-user-remove.ts b/src/m365/yammer/commands/group/group-user-remove.ts index ac3e4edc113..ba29d389a10 100644 --- a/src/m365/yammer/commands/group/group-user-remove.ts +++ b/src/m365/yammer/commands/group/group-user-remove.ts @@ -72,32 +72,8 @@ class YammerGroupUserRemoveCommand extends YammerCommand { } public async commandAction(logger: Logger, args: CommandArgs): Promise { - const executeRemoveAction: () => Promise = async (): Promise => { - const endpoint = `${this.resource}/v1/group_memberships.json`; - - const requestOptions: any = { - url: endpoint, - headers: { - accept: 'application/json;odata.metadata=none', - 'content-type': 'application/json;odata=nometadata' - }, - responseType: 'json', - data: { - group_id: args.options.groupId, - user_id: args.options.id - } - }; - - try { - await request.delete(requestOptions); - } - catch (err: any) { - this.handleRejectedODataJsonPromise(err); - } - }; - if (args.options.confirm) { - await executeRemoveAction(); + await this.executeRemoveAction(args); } else { let messagePrompt: string = `Are you sure you want to leave group ${args.options.groupId}?`; @@ -113,8 +89,32 @@ class YammerGroupUserRemoveCommand extends YammerCommand { }); if (result.continue) { - await executeRemoveAction(); + await this.executeRemoveAction(args); + } + } + } + + private async executeRemoveAction(args: CommandArgs): Promise { + const endpoint = `${this.resource}/v1/group_memberships.json`; + + const requestOptions: any = { + url: endpoint, + headers: { + accept: 'application/json;odata.metadata=none', + 'content-type': 'application/json;odata=nometadata' + }, + responseType: 'json', + data: { + group_id: args.options.groupId, + user_id: args.options.id } + }; + + try { + await request.delete(requestOptions); + } + catch (err: any) { + this.handleRejectedODataJsonPromise(err); } } } diff --git a/src/m365/yammer/commands/message/message-add.spec.ts b/src/m365/yammer/commands/message/message-add.spec.ts index e3b9c4c3a14..78cb8ed2102 100644 --- a/src/m365/yammer/commands/message/message-add.spec.ts +++ b/src/m365/yammer/commands/message/message-add.spec.ts @@ -21,10 +21,10 @@ describe(commands.MESSAGE_ADD, () => { const firstMessage: any = { messages: [{ "id": 470839661887488, "sender_id": 1496550646, "replied_to_id": null, "created_at": "2019/12/22 17:20:30 +0000", "network_id": 801445, "message_type": "update", "sender_type": "user", "url": "https://www.yammer.com/api/v1/messages/470839661887488", "web_url": "https://www.yammer.com/nubo.eu/messages/470839661887488", "group_id": 13114941440, "body": { "parsed": "send a letter to me", "plain": "send a letter to me", "rich": "send a letter to me" }, "thread_id": 470839661887488, "client_type": "O365 Api Auth", "client_url": "https://api.yammer.com", "system_message": false, "direct_message": false, "chat_client_sequence": null, "language": null, "notified_user_ids": [], "privacy": "public", "attachments": [], "liked_by": { "count": 0, "names": [] }, "content_excerpt": "send a letter to me", "group_created_id": 13114941440 }] }; before(() => { - sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); - sinon.stub(telemetry, 'trackEvent').callsFake(() => { }); - sinon.stub(pid, 'getProcessName').callsFake(() => ''); - sinon.stub(session, 'getId').callsFake(() => ''); + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); auth.service.connected = true; commandInfo = Cli.getCommandInfo(command); }); @@ -57,7 +57,7 @@ describe(commands.MESSAGE_ADD, () => { }); it('has correct name', () => { - assert.strictEqual(command.name.startsWith(commands.MESSAGE_ADD), true); + assert.strictEqual(command.name, commands.MESSAGE_ADD); }); it('has a description', () => { @@ -106,9 +106,9 @@ describe(commands.MESSAGE_ADD, () => { it('posts a message', async () => { sinon.stub(request, 'post').callsFake((opts) => { if (opts.url === 'https://www.yammer.com/api/v1/messages.json') { - return Promise.resolve(firstMessage); + return firstMessage; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { options: { body: "send a letter to me", groupId: 13114941440, debug: true } } as any); @@ -119,9 +119,9 @@ describe(commands.MESSAGE_ADD, () => { it('posts a message handling json', async () => { sinon.stub(request, 'post').callsFake((opts) => { if (opts.url === 'https://www.yammer.com/api/v1/messages.json') { - return Promise.resolve(firstMessage); + return firstMessage; } - return Promise.reject('Invalid request'); + throw 'Invalid request'; }); await command.action(logger, { options: { body: "send a letter to me", groupId: 13114941440, debug: true, output: "json" } } as any); @@ -131,11 +131,11 @@ describe(commands.MESSAGE_ADD, () => { it('correctly handles error', async () => { sinon.stub(request, 'post').callsFake(() => { - return Promise.reject({ + throw { "error": { "base": "An error has occurred." } - }); + }; }); await assert.rejects(command.action(logger, { options: {} } as any), new CommandError('An error has occurred.'));