Skip to content

Commit

Permalink
todo list list command
Browse files Browse the repository at this point in the history
  • Loading branch information
ypcode committed May 24, 2020
1 parent 20117d6 commit 07045ad
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/manual/docs/cmd/todo/list/list-list.md
@@ -0,0 +1,28 @@
# todo list list

Returns a list of Microsoft To Do task lists

## Usage

```sh
todo list list [options]
```

## Options

Option|Description
------|-----------
`--help`|output usage information
`--query [query]`|JMESPath query string. See http://jmespath.org/ for more information and examples
`-o, --output [output]`|Output type. `json,text`. Default `text`
`--pretty`|Prettifies `json` output
`--verbose`|Runs command with verbose logging
`--debug`|Runs command with debug logging

## Examples

Get the list of Microsoft To Do task lists

```sh
todo list list
```
3 changes: 3 additions & 0 deletions docs/manual/mkdocs.yml
Expand Up @@ -443,6 +443,9 @@ nav:
- report servicesusercounts: 'cmd/tenant/report/report-servicesusercounts.md'
- status:
- status list: 'cmd/tenant/status/status-list.md'
- To Do (todo):
- list:
-list list: 'cmd/todo/list/list-list.md'
- Yammer (yammer):
- group:
- group list: 'cmd/yammer/group/group-list.md'
Expand Down
5 changes: 5 additions & 0 deletions src/o365/todo/commands.ts
@@ -0,0 +1,5 @@
const prefix: string = 'todo';

export default {
LIST_LIST: `${prefix} list list`
};
238 changes: 238 additions & 0 deletions src/o365/todo/commands/list/list-list.spec.ts
@@ -0,0 +1,238 @@
import commands from '../../commands';
import Command, { CommandOption } from '../../../../Command';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
const command: Command = require('./list-list');
import * as assert from 'assert';
import request from '../../../../request';
import Utils from '../../../../Utils';

describe(commands.LIST_LIST, () => {
let vorpal: Vorpal;
let log: string[];
let cmdInstance: any;

before(() => {
sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve());
sinon.stub(appInsights, 'trackEvent').callsFake(() => { });
auth.service.connected = true;
});

beforeEach(() => {
vorpal = require('../../../../vorpal-init');
log = [];
cmdInstance = {
commandWrapper: {
command: command.name
},
action: command.action(),
log: (msg: string) => {
log.push(msg);
}
};
(command as any).items = [];
});

afterEach(() => {
Utils.restore([
vorpal.find,
request.get
]);
});

after(() => {
Utils.restore([
auth.restoreAuth,
appInsights.trackEvent
]);
auth.service.connected = false;
});

it('has correct name', () => {
assert.equal(command.name.startsWith(commands.LIST_LIST), true);
});

it('has a description', () => {
assert.notEqual(command.description, null);
});

it('lists To Do task lists', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url === `https://graph.microsoft.com/beta/me/todo/lists`) {
return Promise.resolve({
"@odata.context": "https://graph.microsoft.com/beta/$metadata#lists",
"value": [
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrpQ==\"",
"displayName": "Tasks",
"isOwner": true,
"isShared": false,
"wellknownListName": "defaultList",
"id": "AQMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAADkNZdo3x_lUma2pLT-Ge2rgEAm1fdwWoFiE2YS9yegTKoYwAAAgESAAAA"
},
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrpw==\"",
"displayName": "Foo",
"isOwner": true,
"isShared": false,
"wellknownListName": "none",
"id": "AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIeAAA="
},
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrqQ==\"",
"displayName": "Bar",
"isOwner": true,
"isShared": false,
"wellknownListName": "none",
"id": "AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIjAAA="
}
]
});
}

return Promise.reject('Invalid request');
});

cmdInstance.action = command.action();
cmdInstance.action({
options: {
debug: false
}
}, () => {
try {
const actual = JSON.stringify(log[log.length - 1]);
const expected = JSON.stringify([
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrpQ==\"",
"displayName": "Tasks",
"isOwner": true,
"isShared": false,
"wellknownListName": "defaultList",
"id": "AQMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAADkNZdo3x_lUma2pLT-Ge2rgEAm1fdwWoFiE2YS9yegTKoYwAAAgESAAAA"
},
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrpw==\"",
"displayName": "Foo",
"isOwner": true,
"isShared": false,
"wellknownListName": "none",
"id": "AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIeAAA="
},
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrqQ==\"",
"displayName": "Bar",
"isOwner": true,
"isShared": false,
"wellknownListName": "none",
"id": "AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIjAAA="
}
]);
assert.equal(actual, expected);
done();
}
catch (e) {
done(e);
}
});
});

it('lists To Do task lists (verbose)', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url === `https://graph.microsoft.com/beta/me/todo/lists`) {
return Promise.resolve({
"@odata.context": "https://graph.microsoft.com/beta/$metadata#lists",
"value": [
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrpQ==\"",
"displayName": "Tasks",
"isOwner": true,
"isShared": false,
"wellknownListName": "defaultList",
"id": "AQMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAADkNZdo3x_lUma2pLT-Ge2rgEAm1fdwWoFiE2YS9yegTKoYwAAAgESAAAA"
},
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrpw==\"",
"displayName": "Foo",
"isOwner": true,
"isShared": false,
"wellknownListName": "none",
"id": "AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIeAAA="
},
{
"@odata.etag": "W/\"m1fdwWoFiE2YS9yegTKoYwAA/hqrqQ==\"",
"displayName": "Bar",
"isOwner": true,
"isShared": false,
"wellknownListName": "none",
"id": "AAMkAGI3NDhlZmQzLWQxYjAtNGJjNy04NmYwLWQ0M2IzZTNlMDUwNAAuAAAAAACQ1l2jfH6VSZraktP8Z7auAQCbV93BagWITZhL3J6BMqhjAAD9pHIjAAA="
}
]
});
}

return Promise.reject('Invalid request');
});

cmdInstance.action = command.action();
cmdInstance.action({
options: {
debug: false,
verbose: true
}
}, () => {
try {
assert(log[log.length - 1].indexOf('DONE') > -1);
done();
}
catch (e) {
done(e);
}
});
});

it('supports debug mode', () => {
const options = (command.options() as CommandOption[]);
let containsOption = false;
options.forEach(o => {
if (o.option === '--debug') {
containsOption = true;
}
});
assert(containsOption);
});

it('has help referring to the right command', () => {
const cmd: any = {
log: (msg: string) => { },
prompt: () => { },
helpInformation: () => { }
};
const find = sinon.stub(vorpal, 'find').callsFake(() => cmd);
cmd.help = command.help();
cmd.help({}, () => { });
assert(find.calledWith(commands.LIST_LIST));
});

it('has help with examples', () => {
const _log: string[] = [];
const cmd: any = {
log: (msg: string) => {
_log.push(msg);
},
prompt: () => { },
helpInformation: () => { }
};
sinon.stub(vorpal, 'find').callsFake(() => cmd);
cmd.help = command.help();
cmd.help({}, () => { });
let containsExamples: boolean = false;
_log.forEach(l => {
if (l && l.indexOf('Examples:') > -1) {
containsExamples = true;
}
});
Utils.restore(vorpal.find);
assert(containsExamples);
});
});
58 changes: 58 additions & 0 deletions src/o365/todo/commands/list/list-list.ts
@@ -0,0 +1,58 @@
import commands from '../../commands';
import request from '../../../../request';
import GlobalOptions from '../../../../GlobalOptions';
import GraphCommand from '../../../base/GraphCommand';

const vorpal: Vorpal = require('../../../../vorpal-init');

interface CommandArgs {
options: Options;
}

interface Options extends GlobalOptions { }

class TodoListListCommand extends GraphCommand {
public get name(): string {
return `${commands.LIST_LIST}`;
}

public get description(): string {
return 'Returns a list of Microsoft To Do task lists';
}

public commandAction(cmd: CommandInstance, args: CommandArgs, cb: (err?: any) => void): void {

const requestOptions: any = {
url: `${this.resource}/beta/me/todo/lists`,
headers: {
accept: 'application/json;odata.metadata=none',
'content-type': 'application/json'
},
json: true
};

request
.get(requestOptions)
.then((res: any): void => {
cmd.log(res.value);

if (this.verbose) {
cmd.log(vorpal.chalk.green('DONE'));
}

cb();
}, (err: any) => this.handleRejectedODataJsonPromise(err, cmd, cb));
}

public commandHelp(args: {}, log: (help: string) => void): void {
log(vorpal.find(this.name).helpInformation());
log(
`
Examples:
Get the list of Microsoft To Do task lists
`);
}
}

module.exports = new TodoListListCommand();

0 comments on commit 07045ad

Please sign in to comment.