forked from microsoft/ccf-app-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
459 lines (414 loc) · 12.8 KB
/
index.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import Api, { ReportItem, Validator } from "./api.js";
import {
member0DataPart1,
csvDataWrongSchema,
member0DataPart2,
member1Data,
member2Data,
} from "./data.js";
import https from "https";
import fs from "fs";
import inquirer from "inquirer";
const serverUrl = process.env.SERVER!;
const certificateStorePath = process.env.CERTS_FOLDER!;
const interactiveMode = process.env.INTERACTIVE_MODE!;
export interface DemoProps {
ingestUrl: string;
ingestCsvUrl: string;
reportUrl: string;
proposalUrl: string;
}
export interface DemoMemberProps {
id: string;
name: string;
data: unknown;
httpsAgent: https.Agent;
}
class Demo {
//
private static readonly demoProps: DemoProps = {
ingestUrl: `${serverUrl}/app/ingest`,
ingestCsvUrl: `${serverUrl}/app/csv/ingest`,
reportUrl: `${serverUrl}/app/report`,
proposalUrl: `${serverUrl}/gov/proposals`,
};
private static memberDataMap = new Map([
["0", member0DataPart1],
["1", member1Data],
["2", member2Data],
]);
private static memberIds = ["0", "1", "2"];
private static readonly members = Array<DemoMemberProps>();
public static async start() {
/**
* Change working directory to the root of the project
* All paths and process execution will be relative to root
*/
process.chdir("../../");
this.printTestSectionHeader(`🏁 Starting e2e Tests on server ${serverUrl}`);
for (const memberId of this.memberIds) {
const member = this.createMember(memberId);
this.members.push(member);
}
this.printTestSectionHeader("🔬 [TEST]: Data Ingestion Service");
console.log(`📝 Ingestion Service Validations...`);
const dummyMember = this.createMember(this.memberIds[0]);
dummyMember.data = csvDataWrongSchema;
await Validator.validateRequest({
url: this.demoProps.ingestCsvUrl,
method: "POST",
member: dummyMember,
expectedStatus: 400,
testMessage: "CSV data ingest failed (wrong schema)",
});
dummyMember.data = member0DataPart2;
await Validator.validateRequest({
url: this.demoProps.ingestCsvUrl,
method: "POST",
member: dummyMember,
expectedStatus: 400,
testMessage: "CSV data ingest failed (wrong file)",
});
dummyMember.data = [];
await Validator.validateRequest({
url: this.demoProps.ingestUrl,
method: "POST",
member: dummyMember,
expectedStatus: 400,
testMessage: "JSON data ingest failed (data length is zero)",
});
dummyMember.data = null;
await Validator.validateRequest({
url: this.demoProps.ingestUrl,
method: "POST",
member: dummyMember,
expectedStatus: 400,
testMessage: "JSON data ingest failed (data is null)",
});
console.log("---");
// member 0 ingests data through CSV endpoint, members 1 & 2 through JSON
console.log(`📝 Members Ingesting Data...`);
await Api.ingest(this.demoProps.ingestCsvUrl, this.members[0]);
await Api.ingest(this.demoProps.ingestUrl, this.members[1]);
await Api.ingest(this.demoProps.ingestUrl, this.members[2]);
await this.addCheckpoint("Ingestion Stage Complete");
this.printTestSectionHeader(
"🔬 [TEST]: Data Reporting Service (Full Report)"
);
for (const member of this.members) {
await Api.report(this.demoProps, member);
}
await this.addCheckpoint("Full Reports Complete");
this.printTestSectionHeader("🔬 [TEST]:Data Reporting Service (GetById)");
console.log(`📝 Reporting Service Validations...`);
await Validator.validateRequest({
url: `${this.demoProps.reportUrl}/10`,
method: "GET",
member: dummyMember,
expectedStatus: 400,
testMessage: "Getting report by key_not_exist should fail",
});
console.log("---");
let member = this.members[2];
const id_inConsensus = "984500F5BD5BE5767C51";
const id_notEnoughData = "984500BA57A56NBD3A24";
const id_lackOfConsensus = "9845001D460PEJE54159";
// group status for this key changes from LackOfConsensus to InConsensus during the demo
const id_newGroupStatus = id_lackOfConsensus;
console.log(`\n📝 --- IN CONSENSUS Example ---`);
let reportItem = await Api.reportById(
this.demoProps,
member,
id_inConsensus
);
this.assertReportField(
member.name,
reportItem,
"group_status",
"IN_CONSENSUS"
);
await this.addCheckpoint("IN_CONSENSUS DATA");
console.log(`\n📝 --- NOT ENOUGH DATA Example ---`);
reportItem = await Api.reportById(this.demoProps, member, id_notEnoughData);
this.assertReportField(
member.name,
reportItem,
"group_status",
"NOT_ENOUGH_DATA"
);
await this.addCheckpoint("NOT_ENOUGH_DATA");
console.log(`\n📝 --- LACK OF CONSENSUS Example ---`);
reportItem = await Api.reportById(
this.demoProps,
member,
id_lackOfConsensus
);
this.assertReportField(
member.name,
reportItem,
"group_status",
"LACK_OF_CONSENSUS"
);
await this.addCheckpoint("LACK_OF_CONSENSUS DATA");
this.printTestSectionHeader("🔬 [TEST]: Report Changes");
// new Ingestion for member 0 is through JSON
member = this.members[0];
member.data = member0DataPart2;
console.log(`📝 ${member.name} Ingesting new Data...`);
await Api.ingest(this.demoProps.ingestUrl, member);
member = this.members[2];
console.log(
`📝 ${member.name} Data Status changes for id: ${id_newGroupStatus}...`
);
reportItem = await Api.reportById(
this.demoProps,
member,
id_newGroupStatus
);
this.assertReportField(
member.name,
reportItem,
"group_status",
"IN_CONSENSUS"
);
await this.addCheckpoint("Updated Report after New Data Submission");
this.printTestSectionHeader(
"Test Suite - Assertion checks on report fields..."
);
let recordId = "984500F5BD5BE5767C51";
console.log(
`\nChecking ALL fields for ${member.name} and id ${recordId} (In Consensus)\n`
);
reportItem = await Api.reportById(this.demoProps, member, recordId);
this.assertReportField(
member.name,
reportItem,
"group_status",
"IN_CONSENSUS"
);
this.assertReportField(
member.name,
reportItem,
"majority_minority",
"Majority"
);
this.assertReportField(
member.name,
reportItem,
"count_of_unique_values",
1
);
this.assertReportField(member.name, reportItem, "members_in_agreement", 3);
this.assertReportField(member.name, reportItem, "lei", recordId);
this.assertReportField(member.name, reportItem, "nace", "C.18.13");
// member2
recordId = "9845002B6B074505A715";
console.log(
`\nChecking fields for ${member.name} and id ${recordId} (Not Enough Data with Minority of votes)\n`
);
reportItem = await Api.reportById(this.demoProps, member, recordId);
this.assertReportField(
member.name,
reportItem,
"group_status",
"NOT_ENOUGH_DATA"
);
this.assertReportField(
member.name,
reportItem,
"majority_minority",
"Minority"
);
this.assertReportField(
member.name,
reportItem,
"count_of_unique_values",
2
);
this.assertReportField(member.name, reportItem, "members_in_agreement", 1);
// member2
recordId = "984500BA57A56NBD3A24";
console.log(
`\nChecking fields for ${member.name} and id ${recordId} (Not Enough Data with Majority of votes)\n`
);
reportItem = await Api.reportById(this.demoProps, member, recordId);
this.assertReportField(
member.name,
reportItem,
"group_status",
"NOT_ENOUGH_DATA"
);
this.assertReportField(
member.name,
reportItem,
"majority_minority",
"Majority"
);
this.assertReportField(
member.name,
reportItem,
"count_of_unique_values",
1
);
this.assertReportField(member.name, reportItem, "members_in_agreement", 2);
// member2
recordId = "984500E1B2CA1D4EKG67";
console.log(
`\nChecking fields for ${member.name} and id ${recordId} (Lack of Consensus with Majority of votes)\n`
);
reportItem = await Api.reportById(this.demoProps, member, recordId);
this.assertReportField(member.name, reportItem, "lei", recordId);
this.assertReportField(member.name, reportItem, "nace", "A01.1");
this.assertReportField(
member.name,
reportItem,
"group_status",
"LACK_OF_CONSENSUS"
);
this.assertReportField(
member.name,
reportItem,
"majority_minority",
"Majority"
);
this.assertReportField(
member.name,
reportItem,
"count_of_unique_values",
2
);
this.assertReportField(member.name, reportItem, "members_in_agreement", 2);
member = this.members[0];
recordId = "984500E1B2CA1D4EKG67";
console.log(
`\nChecking fields for ${member.name} and id ${recordId} (Lack of Consensus with Minority of votes)\n`
);
reportItem = await Api.reportById(this.demoProps, member, recordId);
this.assertReportField(member.name, reportItem, "lei", recordId);
this.assertReportField(member.name, reportItem, "nace", "A.01.1");
this.assertReportField(
member.name,
reportItem,
"group_status",
"LACK_OF_CONSENSUS"
);
this.assertReportField(
member.name,
reportItem,
"majority_minority",
"Minority"
);
this.assertReportField(
member.name,
reportItem,
"count_of_unique_values",
2
);
this.assertReportField(member.name, reportItem, "members_in_agreement", 1);
// member0
recordId = "984500F5BD5BE5767C51";
console.log(
`\nChecking fields for ${member.name} and id ${recordId} (In Consensus)\n`
);
reportItem = await Api.reportById(this.demoProps, member, recordId);
this.assertReportField(
member.name,
reportItem,
"group_status",
"IN_CONSENSUS"
);
this.assertReportField(
member.name,
reportItem,
"majority_minority",
"Majority"
);
this.assertReportField(
member.name,
reportItem,
"count_of_unique_values",
1
);
this.assertReportField(member.name, reportItem, "members_in_agreement", 3);
member = this.members[1];
recordId = "984500E1B2CA1D4EKG67";
console.log(
`\nChecking fields for ${member.name} and id ${recordId} (Lack of Consensus)\n`
);
reportItem = await Api.reportById(this.demoProps, member, recordId);
this.assertReportField(member.name, reportItem, "lei", recordId);
this.assertReportField(member.name, reportItem, "nace", "A01.1");
this.assertReportField(
member.name,
reportItem,
"group_status",
"LACK_OF_CONSENSUS"
);
// member1
recordId = "984500F5BD5BE5767C51";
console.log(
`\nChecking fields for ${member.name} and id ${recordId} (In Consensus)\n`
);
reportItem = await Api.reportById(this.demoProps, member, recordId);
this.assertReportField(
member.name,
reportItem,
"group_status",
"IN_CONSENSUS"
);
this.printTestSectionHeader("🎉 All Tests Passed...");
}
private static assertReportField(
memberName: string,
reportItem: ReportItem,
fieldName: string,
expectedValue: string | number
) {
const currentValue = reportItem[fieldName];
if (currentValue == expectedValue) {
console.log(
`✅ [PASS] - Assert ${memberName}::${reportItem.lei}.${fieldName} == ${expectedValue}`
);
} else {
throw new Error(
`🛑 [TEST FAILURE]: Unexpected ${fieldName} for ${memberName}::${reportItem.lei} - Current: ${currentValue}. Expected: ${expectedValue}`
);
}
}
private static createMember(memberId: string): DemoMemberProps {
return {
id: memberId,
name: `Member ${memberId}`,
data: this.memberDataMap.get(memberId),
httpsAgent: this.createHttpsAgent(memberId),
};
}
private static createHttpsAgent(memberId: string): https.Agent {
return new https.Agent({
cert: fs.readFileSync(
`${certificateStorePath}/member${memberId}_cert.pem`
),
key: fs.readFileSync(
`${certificateStorePath}/member${memberId}_privk.pem`
),
ca: fs.readFileSync(`${certificateStorePath}/service_cert.pem`),
});
}
private static printTestSectionHeader(title: string) {
console.log("\n===============================================");
console.log(`${title}`);
console.log("===============================================");
}
private static async addCheckpoint(msg: string) {
if (interactiveMode == "1") {
console.log("\n");
await inquirer.prompt([
{
name: msg,
message: `🎬 ${msg}\n - Press return key to continue...`,
},
]);
}
}
}
Demo.start();