-
Notifications
You must be signed in to change notification settings - Fork 299
/
main.ts
276 lines (219 loc) · 7.2 KB
/
main.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
import { Logger, LogLevel, ConsoleListener } from "@pnp/logging";
import { getGUID, combine, assign } from "@pnp/common";
// import { graph } from "@pnp/graph";
import { SPFetchClient } from "@pnp/nodejs";
import { sp } from "@pnp/sp";
import "@pnp/sp/webs";
import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
import "mocha";
import * as findup from "findup-sync";
import { Web } from "@pnp/sp/webs";
chai.use(chaiAsPromised);
declare var process: any;
export interface ISettingsTestingPart {
enableWebTests: boolean;
graph?: {
id: string;
secret: string;
tenant: string;
};
sp?: {
webUrl?: string;
id: string;
notificationUrl: string | null;
secret: string;
url: string;
};
}
export interface ISettings {
testing: ISettingsTestingPart;
}
// we need to load up the appropriate settings based on where we are running
let settings: ISettings = null;
let mode = "cmd";
let site: string = null;
let skipWeb = false;
let deleteWeb = false;
let logging = false;
const deleteAllWebs = false;
for (let i = 0; i < process.argv.length; i++) {
const arg = process.argv[i];
if (/^--mode/i.test(arg)) {
switch (process.argv[++i]) {
case "pr":
mode = "travis-noweb";
break;
case "push":
mode = "travis";
}
}
if (/^--site/i.test(arg)) {
site = process.argv[++i];
}
if (/^--skip-web/i.test(arg)) {
skipWeb = true;
}
if (/^--cleanup/i.test(arg)) {
deleteWeb = true;
}
if (/^--logging/i.test(arg)) {
logging = true;
Logger.activeLogLevel = LogLevel.Info;
Logger.subscribe(new ConsoleListener());
}
}
console.log(`*****************************`);
console.log("Testing command args:");
console.log(`mode: ${mode}`);
console.log(`site: ${site}`);
console.log(`skipWeb: ${skipWeb}`);
console.log(`deleteWeb: ${deleteWeb}`);
console.log(`logging: ${logging}`);
console.log(`*****************************`);
switch (mode) {
case "travis":
settings = {
testing: {
enableWebTests: true,
graph: {
id: "",
secret: "",
tenant: "",
},
sp: {
id: process.env.PnPTesting_ClientId,
notificationUrl: process.env.PnPTesting_NotificationUrl || null,
secret: process.env.PnPTesting_ClientSecret,
url: process.env.PnPTesting_SiteUrl,
},
},
};
break;
case "travis-noweb":
settings = {
testing: {
enableWebTests: false,
},
};
break;
default:
settings = require(findup("settings.js"));
if (skipWeb) {
settings.testing.enableWebTests = false;
}
break;
}
function spTestSetup(ts: ISettingsTestingPart): Promise<void> {
return new Promise((resolve, reject) => {
if (site && site.length > 0) {
// we have a site url provided, we'll use that
sp.setup({
sp: {
fetchClientFactory: () => {
return new SPFetchClient(site, ts.sp.id, ts.sp.secret);
},
},
});
ts.sp.webUrl = site;
return resolve();
}
sp.setup({
sp: {
fetchClientFactory: () => {
return new SPFetchClient(ts.sp.url, ts.sp.id, ts.sp.secret);
},
},
});
// create the web in which we will test
const d = new Date();
const g = getGUID();
sp.web.webs.add(`PnP-JS-Core Testing ${d.toDateString()}`, g).then(() => {
const url = combine(ts.sp.url, g);
// set the testing web url so our tests have access if needed
ts.sp.webUrl = url;
// re-setup the node client to use the new web
sp.setup({
sp: {
// headers: {
// "Accept": "application/json;odata=verbose",
// },
fetchClientFactory: () => {
return new SPFetchClient(url, ts.sp.id, ts.sp.secret);
},
},
});
resolve();
}).catch(e => reject(e));
});
}
// function graphTestSetup(ts: ISettingsTestingPart): Promise<void> {
// return new Promise((resolve) => {
// graph.setup({
// graph: {
// fetchClientFactory: () => {
// return new AdalFetchClient(ts.graph.tenant, ts.graph.id, ts.graph.secret);
// },
// },
// });
// resolve();
// });
// }
export let testSettings: ISettingsTestingPart = assign(settings.testing, { webUrl: "" });
before(async function (): Promise<void> {
// this may take some time, don't timeout early
this.timeout(90000);
// establish the connection to sharepoint
if (testSettings.enableWebTests) {
console.log(`Setting up SharePoint tests...`);
const s = Date.now();
await spTestSetup(testSettings);
const e = Date.now();
console.log(`Setup SharePoint tests in ${((e - s) / 1000).toFixed(4)} seconds.`);
// console.log(`Setting up Graph tests...`);
// s = Date.now();
// await graphTestSetup(testSettings);
// e = Date.now();
// console.log(`Setup Graph tests in ${((e - s) / 1000).toFixed(4)} seconds.`);
}
});
after(async () => {
console.log();
console.log();
console.log();
console.log("Ending...");
console.log();
if (deleteAllWebs) {
await cleanUpAllSubsites();
} else if (deleteWeb && testSettings.enableWebTests) {
console.log(`Deleting web ${testSettings.sp.webUrl} created during testing.`);
const w = Web(testSettings.sp.webUrl);
const children = await w.webs.select("Title")();
await Promise.all(children.map((value) => {
const web2 = Web(value["odata.id"], "");
console.log(`Deleting: ${value["odata.id"]}`);
return web2.delete();
}));
await w.delete();
console.log(`Deleted web ${testSettings.sp.webUrl} created during testing.`);
} else if (testSettings.enableWebTests) {
console.log(`Leaving ${testSettings.sp.webUrl} alone.`);
}
console.log("All done. Have a nice day :)");
});
// Function deletes all test subsites
async function cleanUpAllSubsites(): Promise<void> {
const w = await sp.site.rootWeb.webs.select("Title")();
w.forEach(async (e: any) => {
const web = Web(e["odata.id"], "");
console.log(`Deleting: ${e["odata.id"]}`);
const children = await web.webs.select("Title")();
await Promise.all(children.map(async (value) => {
const web2 = Web(value["odata.id"], "");
console.log(`Deleting: ${value["odata.id"]}`);
return web2.delete();
}));
await web.delete();
console.log(`Deleted: ${e["odata.id"]}`);
});
}