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

Adds the graph schemaextension get cmd #14 #886

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/manual/docs/cmd/graph/schemaextension/schemaextension-get.md
@@ -0,0 +1,36 @@
# graph schemaextension get

Gets information about a Microsoft Graph schema extension

## Usage

```sh
graph schemaextension get <options>
```

## Options

Option|Description
------|-----------
`--help`|output usage information
`-i, --id <id>`|The unique identifier for the schema extension definition
`-o, --output [output]`|Output type. `json|text`. Default `text`
`--verbose`|Runs command with verbose logging
`--debug`|Runs command with debug logging


!!! important
Before using this command, log in to the Microsoft Graph, using the [graph login](../login.md) command.

## Remarks

To get information about a schema extension, you have to first log in to the Microsoft Graph using the [graph login](../login.md) command, eg. `graph login`.

To get information about a schema extension, you have to specify the unique ID of the schema extension

## Examples

Gets information about a schema extension with ID MySchemaExtension
```sh
graph schemaextension get --id MySchemaExtension
```
2 changes: 2 additions & 0 deletions docs/manual/mkdocs.yml
Expand Up @@ -236,6 +236,8 @@ nav:
- o365group remove: 'cmd/graph/o365group/o365group-remove.md'
- o365group restore: 'cmd/graph/o365group/o365group-restore.md'
- o365group set: 'cmd/graph/o365group/o365group-set.md'
- schemaextension:
- schemaextension get: 'cmd/graph/schemaextension/schemaextension-get.md'
- siteclassification:
- siteclassification disable: 'cmd/graph/siteclassification/siteclassification-disable.md'
- siteclassification enable: 'cmd/graph/siteclassification/siteclassification-enable.md'
Expand Down
1 change: 1 addition & 0 deletions src/o365/graph/commands.ts
Expand Up @@ -18,6 +18,7 @@ export default {
O365GROUP_SET: `${prefix} o365group set`,
O365GROUP_REMOVE: `${prefix} o365group remove`,
O365GROUP_RESTORE: `${prefix} o365group restore`,
SCHEMAEXTENSION_GET: `${prefix} schemaextension get`,
SITECLASSIFICATION_DISABLE: `${prefix} siteclassification disable`,
SITECLASSIFICATION_ENABLE: `${prefix} siteclassification enable`,
SITECLASSIFICATION_GET: `${prefix} siteclassification get`,
Expand Down
326 changes: 326 additions & 0 deletions src/o365/graph/commands/schemaextension/schemaextension-get.spec.ts
@@ -0,0 +1,326 @@
import commands from '../../commands';
import Command, { CommandOption, CommandError, CommandValidate } from '../../../../Command';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../GraphAuth';
const command: Command = require('./schemaextension-get');
import * as assert from 'assert';
import * as request from 'request-promise-native';
import Utils from '../../../../Utils';
import { Service } from '../../../../Auth';

describe(commands.SCHEMAEXTENSION_GET, () => {
let vorpal: Vorpal;
let log: string[];
let cmdInstance: any;
let cmdInstanceLogSpy: sinon.SinonSpy;
let trackEvent: any;
let telemetry: any;

before(() => {
sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve());
sinon.stub(auth, 'ensureAccessToken').callsFake(() => { return Promise.resolve('ABC'); });
trackEvent = sinon.stub(appInsights, 'trackEvent').callsFake((t) => {
telemetry = t;
});
});

beforeEach(() => {
vorpal = require('../../../../vorpal-init');
log = [];
cmdInstance = {
log: (msg: string) => {
log.push(msg);
}
};
cmdInstanceLogSpy = sinon.spy(cmdInstance, 'log');
auth.service = new Service();
telemetry = null;
(command as any).items = [];
});

afterEach(() => {
Utils.restore([
vorpal.find,
request.get,
request.post
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please remove the request.post? It is no used at all.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, it should be schemaextension not schmeaextension

]);
});

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

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

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

it('calls telemetry', (done) => {
cmdInstance.action = command.action();
cmdInstance.action({ options: {} }, () => {
try {
assert(trackEvent.called);
done();
}
catch (e) {
done(e);
}
});
});

it('logs correct telemetry event', (done) => {
cmdInstance.action = command.action();
cmdInstance.action({ options: {} }, () => {
try {
assert.equal(telemetry.name, commands.SCHEMAEXTENSION_GET);
done();
}
catch (e) {
done(e);
}
});
});

it('aborts when not logged in to Microsoft Graph', (done) => {
auth.service = new Service();
auth.service.connected = false;
cmdInstance.action = command.action();
cmdInstance.action({ options: { debug: true } }, (err?: any) => {
try {
assert.equal(JSON.stringify(err), JSON.stringify(new CommandError('Log in to the Microsoft Graph first')));
done();
}
catch (e) {
done(e);
}
});
});

it('gets schema extension', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url.indexOf(`https://graph.microsoft.com/v1.0/schemaExtensions`) === 0) {
return Promise.resolve({
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity",
"id": "ext6kguklm2_TestSchemaExtension",
"description": "Test Description",
"targetTypes": [
"Group"
],
"status": "InDevelopment",
"owner": "b07a45b3-f7b7-489b-9269-da6f3f93dff0",
"properties": [
{
"name": "MyInt",
"type": "Integer"
},
{
"name": "MyString",
"type": "String"
}
]
});
}

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

auth.service = new Service();
auth.service.connected = true;
auth.service.resource = 'https://graph.microsoft.com';
cmdInstance.action = command.action();
cmdInstance.action({
options: {
debug: false,
id: 'ext6kguklm2_TestSchemaExtension',
}
}, () => {
try {
assert(cmdInstanceLogSpy.calledWith({
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity",
"id": "ext6kguklm2_TestSchemaExtension",
"description": "Test Description",
"targetTypes": [
"Group"
],
"status": "InDevelopment",
"owner": "b07a45b3-f7b7-489b-9269-da6f3f93dff0",
"properties": [
{
"name": "MyInt",
"type": "Integer"
},
{
"name": "MyString",
"type": "String"
}
]
}));
done();
}
catch (e) {
done(e);
}
});
});

it('gets schema extension (debug)', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url.indexOf(`https://graph.microsoft.com/v1.0/schemaExtensions`) === 0) {
return Promise.resolve({
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity",
"id": "ext6kguklm2_TestSchemaExtension",
"description": "Test Description",
"targetTypes": [
"Group"
],
"status": "InDevelopment",
"owner": "b07a45b3-f7b7-489b-9269-da6f3f93dff0",
"properties": [
{
"name": "MyInt",
"type": "Integer"
},
{
"name": "MyString",
"type": "String"
}
]
});
}

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

auth.service = new Service();
auth.service.connected = true;
auth.service.resource = 'https://graph.microsoft.com';
cmdInstance.action = command.action();
cmdInstance.action({
options: {
debug: true,
id: 'ext6kguklm2_TestSchemaExtension',
}
}, () => {
try {
assert(cmdInstanceLogSpy.calledWith({
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity",
"id": "ext6kguklm2_TestSchemaExtension",
"description": "Test Description",
"targetTypes": [
"Group"
],
"status": "InDevelopment",
"owner": "b07a45b3-f7b7-489b-9269-da6f3f93dff0",
"properties": [
{
"name": "MyInt",
"type": "Integer"
},
{
"name": "MyString",
"type": "String"
}
]
}));
done();
}
catch (e) {
done(e);
}
});
});


Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add an extra test to test /schemaExtensions endpoint in a case of failure?

For example schema not found response.
Failure response for schema not found is with status code 404:

{
    "error": {
        "code": "ResourceNotFound",
        "message": "Cannot find resource with id: 123.",
        "innerError": {
            "request-id": "4827c0cb-3580-4e93-98db-cbac18e9951f",
            "date": "2019-03-25T14:09:57"
        }
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, the path should be schemaextension not schmeaextension


it('fails validation if the id is not specified', () => {
const actual = (command.validate() as CommandValidate)({
options: {
debug: false,
id: null
}
});
assert.notEqual(actual, true);
});

it('passes validation if the id is specified', () => {
const actual = (command.validate() as CommandValidate)({
options: {
debug: false,
id: 'schemaExtension'
}
});
assert.equal(actual, true);
});


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.SCHEMAEXTENSION_GET));
});

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);
});

it('correctly handles lack of valid access token', (done) => {
Utils.restore(auth.ensureAccessToken);
sinon.stub(auth, 'ensureAccessToken').callsFake(() => { return Promise.reject(new Error('Error getting access token')); });
auth.service = new Service();
auth.service.connected = true;
auth.service.resource = 'https://graph.microsoft.com';
cmdInstance.action = command.action();
cmdInstance.action({ options: { debug: true } }, (err?: any) => {
try {
assert.equal(JSON.stringify(err), JSON.stringify(new CommandError('Error getting access token')));
done();
}
catch (e) {
done(e);
}
});
});
});