|
| 1 | +import commands from '../../commands'; |
| 2 | +import Command, { CommandOption, CommandValidate, CommandError} from '../../../../Command'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | +import appInsights from '../../../../appInsights'; |
| 5 | +import auth from '../../../../Auth'; |
| 6 | +const command: Command = require('./schemaextension-get'); |
| 7 | +import * as assert from 'assert'; |
| 8 | +import request from '../../../../request'; |
| 9 | +import Utils from '../../../../Utils'; |
| 10 | + |
| 11 | +describe(commands.SCHEMAEXTENSION_GET, () => { |
| 12 | + let vorpal: Vorpal; |
| 13 | + let log: string[]; |
| 14 | + let cmdInstance: any; |
| 15 | + let cmdInstanceLogSpy: sinon.SinonSpy; |
| 16 | + |
| 17 | + before(() => { |
| 18 | + sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve()); |
| 19 | + sinon.stub(appInsights, 'trackEvent').callsFake(() => {}); |
| 20 | + auth.service.connected = true; |
| 21 | + }); |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + vorpal = require('../../../../vorpal-init'); |
| 25 | + log = []; |
| 26 | + cmdInstance = { |
| 27 | + commandWrapper: { |
| 28 | + command: command.name |
| 29 | + }, |
| 30 | + action: command.action(), |
| 31 | + log: (msg: string) => { |
| 32 | + log.push(msg); |
| 33 | + } |
| 34 | + }; |
| 35 | + cmdInstanceLogSpy = sinon.spy(cmdInstance, 'log'); |
| 36 | + (command as any).items = []; |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + Utils.restore([ |
| 41 | + vorpal.find, |
| 42 | + request.get |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + after(() => { |
| 47 | + Utils.restore([ |
| 48 | + auth.restoreAuth, |
| 49 | + appInsights.trackEvent |
| 50 | + ]); |
| 51 | + auth.service.connected = false; |
| 52 | + }); |
| 53 | + |
| 54 | + |
| 55 | + it('has correct name', () => { |
| 56 | + assert.equal(command.name.startsWith(commands.SCHEMAEXTENSION_GET), true); |
| 57 | + }); |
| 58 | + |
| 59 | + it('has a description', () => { |
| 60 | + assert.notEqual(command.description, null); |
| 61 | + }); |
| 62 | + it('gets schema extension', (done) => { |
| 63 | + sinon.stub(request, 'get').callsFake((opts) => { |
| 64 | + if (opts.url.indexOf(`schemaExtensions`)> -1) { |
| 65 | + return Promise.resolve({ |
| 66 | + "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity", |
| 67 | + "id": "adatumisv_exo2", |
| 68 | + "description": "sample description", |
| 69 | + "targetTypes": [ |
| 70 | + "Message" |
| 71 | + ], |
| 72 | + "status": "Available", |
| 73 | + "owner": "617720dc-85fc-45d7-a187-cee75eaf239e", |
| 74 | + "properties": [ |
| 75 | + { |
| 76 | + "name": "p1", |
| 77 | + "type": "String" |
| 78 | + }, |
| 79 | + { |
| 80 | + "name": "p2", |
| 81 | + "type": "String" |
| 82 | + } |
| 83 | + ] |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + return Promise.reject('Invalid request'); |
| 88 | + }); |
| 89 | + cmdInstance.action = command.action(); |
| 90 | + cmdInstance.action({ |
| 91 | + options: { |
| 92 | + debug: false, |
| 93 | + id: 'adatumisv_exo2', |
| 94 | + } |
| 95 | + }, () => { |
| 96 | + try { |
| 97 | + assert(cmdInstanceLogSpy.calledWith({ |
| 98 | + "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity", |
| 99 | + "id": "adatumisv_exo2", |
| 100 | + "description": "sample description", |
| 101 | + "targetTypes": [ |
| 102 | + "Message" |
| 103 | + ], |
| 104 | + "status": "Available", |
| 105 | + "owner": "617720dc-85fc-45d7-a187-cee75eaf239e", |
| 106 | + "properties": [ |
| 107 | + { |
| 108 | + "name": "p1", |
| 109 | + "type": "String" |
| 110 | + }, |
| 111 | + { |
| 112 | + "name": "p2", |
| 113 | + "type": "String" |
| 114 | + } |
| 115 | + ] |
| 116 | + })); |
| 117 | + done(); |
| 118 | + } |
| 119 | + catch (e) { |
| 120 | + done(e); |
| 121 | + } |
| 122 | + finally { |
| 123 | + Utils.restore(request.get); |
| 124 | + } |
| 125 | + }); |
| 126 | + }); |
| 127 | + it('gets schema extension(debug)', (done) => { |
| 128 | + sinon.stub(request, 'get').callsFake((opts) => { |
| 129 | + if (opts.url.indexOf(`schemaExtensions`)> -1) { |
| 130 | + return Promise.resolve({ |
| 131 | + "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity", |
| 132 | + "id": "adatumisv_exo2", |
| 133 | + "description": "sample description", |
| 134 | + "targetTypes": [ |
| 135 | + "Message" |
| 136 | + ], |
| 137 | + "status": "Available", |
| 138 | + "owner": "617720dc-85fc-45d7-a187-cee75eaf239e", |
| 139 | + "properties": [ |
| 140 | + { |
| 141 | + "name": "p1", |
| 142 | + "type": "String" |
| 143 | + }, |
| 144 | + { |
| 145 | + "name": "p2", |
| 146 | + "type": "String" |
| 147 | + } |
| 148 | + ] |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + return Promise.reject('Invalid request'); |
| 153 | + }); |
| 154 | + cmdInstance.action = command.action(); |
| 155 | + cmdInstance.action({ |
| 156 | + options: { |
| 157 | + debug: true, |
| 158 | + id: 'adatumisv_exo2', |
| 159 | + } |
| 160 | + }, () => { |
| 161 | + try { |
| 162 | + assert(cmdInstanceLogSpy.calledWith({ |
| 163 | + "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity", |
| 164 | + "id": "adatumisv_exo2", |
| 165 | + "description": "sample description", |
| 166 | + "targetTypes": [ |
| 167 | + "Message" |
| 168 | + ], |
| 169 | + "status": "Available", |
| 170 | + "owner": "617720dc-85fc-45d7-a187-cee75eaf239e", |
| 171 | + "properties": [ |
| 172 | + { |
| 173 | + "name": "p1", |
| 174 | + "type": "String" |
| 175 | + }, |
| 176 | + { |
| 177 | + "name": "p2", |
| 178 | + "type": "String" |
| 179 | + } |
| 180 | + ] |
| 181 | + })); |
| 182 | + done(); |
| 183 | + } |
| 184 | + catch (e) { |
| 185 | + done(e); |
| 186 | + } |
| 187 | + }); |
| 188 | + }); |
| 189 | + it('handles error', (done) => { |
| 190 | + sinon.stub(request, 'get').callsFake((opts) => { |
| 191 | + if (opts.url.indexOf(`schemaExtensions`)> -1) { |
| 192 | + return Promise.reject('An error has occurred'); |
| 193 | + } |
| 194 | + |
| 195 | + return Promise.reject('Invalid request'); |
| 196 | + }); |
| 197 | + cmdInstance.action = command.action(); |
| 198 | + cmdInstance.action({ |
| 199 | + options: { |
| 200 | + debug: true, |
| 201 | + id: 'adatumisv_exo2', |
| 202 | + } |
| 203 | + }, (err?: any) => { |
| 204 | + try { |
| 205 | + assert.equal(JSON.stringify(err), JSON.stringify(new CommandError('An error has occurred'))); |
| 206 | + done(); |
| 207 | + } |
| 208 | + catch (e) { |
| 209 | + done(e); |
| 210 | + } |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | +it('fails validation if the id is not specified', () => { |
| 215 | + const actual = (command.validate() as CommandValidate)({ |
| 216 | + options: { |
| 217 | + debug: false, |
| 218 | + id: null |
| 219 | + } |
| 220 | + }); |
| 221 | + assert.notEqual(actual, true); |
| 222 | + }); |
| 223 | + |
| 224 | + it('passes validation if the id is specified', () => { |
| 225 | + const actual = (command.validate() as CommandValidate)({ |
| 226 | + options: { |
| 227 | + debug: false, |
| 228 | + id: 'adatumisv_exo2' |
| 229 | + } |
| 230 | + }); |
| 231 | + assert.equal(actual, true); |
| 232 | + }); |
| 233 | + |
| 234 | + |
| 235 | + it('supports debug mode', () => { |
| 236 | + const options = (command.options() as CommandOption[]); |
| 237 | + let containsOption = false; |
| 238 | + options.forEach(o => { |
| 239 | + if (o.option === '--debug') { |
| 240 | + containsOption = true; |
| 241 | + } |
| 242 | + }); |
| 243 | + assert(containsOption); |
| 244 | + }); |
| 245 | + |
| 246 | + it('has help referring to the right command', () => { |
| 247 | + const cmd: any = { |
| 248 | + log: (msg: string) => { }, |
| 249 | + prompt: () => { }, |
| 250 | + helpInformation: () => { } |
| 251 | + }; |
| 252 | + const find = sinon.stub(vorpal, 'find').callsFake(() => cmd); |
| 253 | + cmd.help = command.help(); |
| 254 | + cmd.help({}, () => { }); |
| 255 | + assert(find.calledWith(commands.SCHEMAEXTENSION_GET)); |
| 256 | + }); |
| 257 | + |
| 258 | + it('has help with examples', () => { |
| 259 | + const _log: string[] = []; |
| 260 | + const cmd: any = { |
| 261 | + log: (msg: string) => { |
| 262 | + _log.push(msg); |
| 263 | + }, |
| 264 | + prompt: () => { }, |
| 265 | + helpInformation: () => { } |
| 266 | + }; |
| 267 | + sinon.stub(vorpal, 'find').callsFake(() => cmd); |
| 268 | + cmd.help = command.help(); |
| 269 | + cmd.help({}, () => { }); |
| 270 | + let containsExamples: boolean = false; |
| 271 | + _log.forEach(l => { |
| 272 | + if (l && l.indexOf('Examples:') > -1) { |
| 273 | + containsExamples = true; |
| 274 | + } |
| 275 | + }); |
| 276 | + Utils.restore(vorpal.find); |
| 277 | + assert(containsExamples); |
| 278 | + }); |
| 279 | +}); |
0 commit comments