/
hubsite-data-get.spec.ts
316 lines (284 loc) · 9.47 KB
/
hubsite-data-get.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import commands from '../../commands';
import Command, { CommandOption, CommandValidate, CommandError } from '../../../../Command';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
const command: Command = require('./hubsite-data-get');
import * as assert from 'assert';
import request from '../../../../request';
import Utils from '../../../../Utils';
describe(commands.HUBSITE_DATA_GET, () => {
let vorpal: Vorpal;
let log: string[];
let cmdInstance: any;
let cmdInstanceLogSpy: sinon.SinonSpy;
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);
}
};
cmdInstanceLogSpy = sinon.spy(cmdInstance, 'log');
});
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.HUBSITE_DATA_GET), true);
});
it('has a description', () => {
assert.notEqual(command.description, null);
});
it('gets information about the specified hub site', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url.indexOf(`/_api/web/HubSiteData(false)`) > -1) {
return Promise.resolve({
value: JSON.stringify({
"themeKey": null,
"name": "CommunicationSite",
"url": "https://contoso.sharepoint.com/sites/Sales",
"logoUrl": "http://contoso.com/__siteIcon__.jpg",
"usesMetadataNavigation": false,
"navigation": []
})
});
}
return Promise.reject('Invalid request');
});
cmdInstance.action({ options: { debug: false, webUrl: 'https://contoso.sharepoint.com/sites/Project-X' } }, () => {
try {
assert(cmdInstanceLogSpy.calledWith({
"themeKey": null,
"name": "CommunicationSite",
"url": "https://contoso.sharepoint.com/sites/Sales",
"logoUrl": "http://contoso.com/__siteIcon__.jpg",
"usesMetadataNavigation": false,
"navigation": []
}));
done();
}
catch (e) {
done(e);
}
});
});
it('gets information about the specified hub site with forced refresh', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url.indexOf(`/_api/web/HubSiteData(true)`) > -1) {
return Promise.resolve({
value: JSON.stringify({
"themeKey": null,
"name": "CommunicationSite",
"url": "https://contoso.sharepoint.com/sites/Sales",
"logoUrl": "http://contoso.com/__siteIcon__.jpg",
"usesMetadataNavigation": false,
"navigation": []
})
});
}
return Promise.reject('Invalid request');
});
cmdInstance.action({ options: { debug: false, webUrl: 'https://contoso.sharepoint.com/sites/Project-X', forceRefresh: true } }, () => {
try {
assert(cmdInstanceLogSpy.calledWith({
"themeKey": null,
"name": "CommunicationSite",
"url": "https://contoso.sharepoint.com/sites/Sales",
"logoUrl": "http://contoso.com/__siteIcon__.jpg",
"usesMetadataNavigation": false,
"navigation": []
}));
done();
}
catch (e) {
done(e);
}
});
});
it('gets information about the specified hub site (debug)', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url.indexOf(`/_api/web/HubSiteData`) > -1) {
return Promise.resolve({
value: JSON.stringify({
"themeKey": null,
"name": "CommunicationSite",
"url": "https://contoso.sharepoint.com/sites/Sales",
"logoUrl": "http://contoso.com/__siteIcon__.jpg",
"usesMetadataNavigation": false,
"navigation": []
})
});
}
return Promise.reject('Invalid request');
});
cmdInstance.action({ options: { debug: true, webUrl: 'https://contoso.sharepoint.com/sites/Project-X' } }, () => {
try {
assert(cmdInstanceLogSpy.calledWith({
"themeKey": null,
"name": "CommunicationSite",
"url": "https://contoso.sharepoint.com/sites/Sales",
"logoUrl": "http://contoso.com/__siteIcon__.jpg",
"usesMetadataNavigation": false,
"navigation": []
}));
done();
}
catch (e) {
done(e);
}
});
});
it('gets information about the specified hub site', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url.indexOf(`/_api/web/HubSiteData`) > -1) {
return Promise.resolve({ "odata.null": true });
}
return Promise.reject('Invalid request');
});
cmdInstance.action({ options: { debug: false, webUrl: 'https://contoso.sharepoint.com/sites/Project-X' } }, () => {
try {
assert(cmdInstanceLogSpy.notCalled);
done();
}
catch (e) {
done(e);
}
});
});
it('gets information about the specified hub site (debug)', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
if (opts.url.indexOf(`/_api/web/HubSiteData`) > -1) {
return Promise.resolve({ "odata.null": true });
}
return Promise.reject('Invalid request');
});
cmdInstance.action({ options: { debug: true, webUrl: 'https://contoso.sharepoint.com/sites/Project-X' } }, () => {
try {
assert(cmdInstanceLogSpy.calledWith(`https://contoso.sharepoint.com/sites/Project-X is not connected to a hub site and is not a hub site itself`));
done();
}
catch (e) {
done(e);
}
});
});
it('correctly handles error when hub site not found', (done) => {
sinon.stub(request, 'get').callsFake((opts) => {
return Promise.reject({
error: {
"odata.error": {
"code": "-1, Microsoft.SharePoint.Client.ResourceNotFoundException",
"message": {
"lang": "en-US",
"value": "Exception of type 'Microsoft.SharePoint.Client.ResourceNotFoundException' was thrown."
}
}
}
});
});
cmdInstance.action({ options: { debug: false, webUrl: 'https://contoso.sharepoint.com/sites/Project-X' } }, (err?: any) => {
try {
assert.equal(JSON.stringify(err), JSON.stringify(new CommandError("Exception of type 'Microsoft.SharePoint.Client.ResourceNotFoundException' was thrown.")));
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('supports specifying webUrl', () => {
const options = (command.options() as CommandOption[]);
let containsOption = false;
options.forEach(o => {
if (o.option.indexOf('--webUrl') > -1) {
containsOption = true;
}
});
assert(containsOption);
});
it('supports specifying forceRefresh', () => {
const options = (command.options() as CommandOption[]);
let containsOption = false;
options.forEach(o => {
if (o.option.indexOf('--forceRefresh') > -1) {
containsOption = true;
}
});
assert(containsOption);
});
it('fails validation if webUrl is not specified', () => {
const actual = (command.validate() as CommandValidate)({ options: { id: '9b142c22-037f-4a7f-9017-e9d8c0e34b99' } });
assert.notEqual(actual, true);
});
it('fails validation if webUrl is not a valid SharePoint URL', () => {
const actual = (command.validate() as CommandValidate)({ options: { id: '9b142c22-037f-4a7f-9017-e9d8c0e34b99', webUrl: 'Invalid' } });
assert.notEqual(actual, true);
});
it('passed validation if webUrl is a valid SharePoint URL', () => {
const actual = (command.validate() as CommandValidate)({ options: { id: '9b142c22-037f-4a7f-9017-e9d8c0e34b99', webUrl: 'https://contoso.sharepoint.com' } });
assert.equal(actual, true);
});
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.HUBSITE_DATA_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);
});
});