-
Notifications
You must be signed in to change notification settings - Fork 24
/
validate.js
501 lines (431 loc) · 18.3 KB
/
validate.js
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
* QUIDDITCH
* 3.0.0
*
* This script is for analyzing the wizard's readiness on being a Premium App
* It will check if default values/images have already been replaced with a different one.
* It will also do checks on configuration values to make sure they are valid.
*/
const chalk = require("chalk");
const fs = require('fs').promises;
const fsConstants = require('fs').constants;
const path = require('path');
const HTMLParser = require('node-html-parser');
const css = require('css');
const md5 = require('md5');
// File paths
const configFilePath = path.join(__dirname, 'docs/wizard/config/config.js')
const languageDirPath = path.join(__dirname, 'docs/wizard/assets/languages')
const wizardPath = path.join(__dirname, 'docs/wizard')
// Default config constants to check
const defaultClientId = 'fd2ba742-446f-46c5-bbbc-1cad2f34ac3a';
const defaultIntegrationTypeId = 'premium-app-example';
const defaultViewPermission = 'integration:examplePremiumApp:view';
const defaultPrefix = 'PREMIUM_EXAMPLE_';
// Default language file text to check (en-us)
const defaultLanguage = {
'txt-premium-app-name': 'Premium App',
'txt-greeting-2': 'Welcome to the Premium App Example Application',
'txt-not-available-message': 'We\'re sorry but your Genesys Cloud org does not have the Premium App Sample Product enabled. Please contact Genesys Cloud.',
}
// Default image md5 checksum hashes
const defaulImgHash = {
footerImg: '3b3fc68be4e84a23b52ef2b9fcd359a8',
loadingImg: '862df3c0557cc4e70b524f288eeeb8d9'
}
// Message arrays
const passedMessages = [];
const warningMessages = [];
const criticalMessages = [];
// The validation 'library'
const Validator = {
// Levels of importance
'WARNING': 0,
'CRITICAL': 1,
/**
* Check object if property exists. If it's a string, then make sure that it's not blank.
* @param {Object} obj the object
* @param {String} propertyName property name to check
* @param {String} objectName for message, the name of the object
* @param {String} additionalComment (optional) additional comments
* @returns {Array} [bool, message]
*/
propertyExists(obj, propertyName, objectName, additionalComment) {
if (propertyName in obj) {
// If string, make sure it's not a blank string
if(typeof obj[propertyName] == 'string' && obj[propertyName].trim().length <= 0){
return Promise.resolve([false, `${propertyName} does not exist in ${objectName}. -- ${additionalComment}`])
}
return Promise.resolve([true, `${propertyName} exists in ${objectName}. -- ${additionalComment}`])
}
return Promise.resolve([false, `${propertyName} does not exist in ${objectName}. -- ${additionalComment}`])
},
/**
* Check object if two values are not equal.
* For checking if a default value has already been updated.
* @param {any} value1
* @param {any} value2
* @param {String} value1Name for message name of value 1
* @param {String} additionalComment (optional) additional comments
* @returns {Array} [bool, message]
*/
notEqual(value1, value2, value1Name, additionalComment) {
// value1 should not be null or undefined
if(value1 === null || value1 === undefined){
return [false, `${value1Name} does not exist`]
}
if(value1 !== value2){
return Promise.resolve([true, `${value1Name} is not equal to '${value2.toString()}' -- ${additionalComment}`])
} else {
return Promise.resolve([false, `${value1Name} is equal to '${value2.toString()}' -- ${additionalComment}`])
}
},
/**
* Custom evaluation that returns true or false.
* @param {Function} func fucntion that returns boolean
* @param {String} passMessage
* @param {String} failMessage
* @param {String} additionalComment (optional)
* @returns {Array} [bool, message]
*/
customEvaluation(func, passMessage, failMessage, additionalComment) {
let result = func();
if(typeof result != 'boolean') throw new Error('Func does not return boolean');
if(result){
return Promise.resolve([true, `${passMessage} -- ${additionalComment}`])
} else {
return Promise.resolve([false, `${failMessage} -- ${additionalComment}`])
}
},
/**
* Custom evaluation that returns true or false.
* @param {Function} func async fucntion that returns Promise<boolean>
* @param {String} passMessage
* @param {String} failMessage
* @param {String} additionalComment (optional)
* @returns {Array} [bool, message]
*/
async customEvaluationAsync(func, passMessage, failMessage, additionalComment) {
let result = await func();
if(typeof result != 'boolean') throw new Error('Func resolution does not return boolean');
if(result){
return Promise.resolve([true, `${passMessage} -- ${additionalComment}`])
} else {
return Promise.resolve([false, `${failMessage} -- ${additionalComment}`])
}
},
/**
* Evaluate the contents of the array
* @param {*} importanceLevel Validator.WARNING or Validator.CRITICAL
* @param {Array} evaluationArr array of tests
*/
async evaluateArr(importanceLevel, evaluationArr) {
const evaluationResults = await Promise.all(evaluationArr);
evaluationResults.forEach(evaluation => {
const result = evaluation[0];
const message = evaluation[1];
// If test passed, add message
if(result){
passedMessages.push(message);
return;
}
// For non-pass, determine level
switch(importanceLevel){
case this.WARNING:
warningMessages.push(message);
break;
case this.CRITICAL:
criticalMessages.push(message);
break;
}
});
}
}
// Globals
let config = null; // Config object of the config file
/**
* Print the result messages
*/
function printMessages(){
console.log(chalk.blue(' --------- PASSED ----------'));
if(!passedMessages || passedMessages.length <= 0) console.log(chalk.grey('none'));
passedMessages.forEach((m, i) => console.log(chalk.green(`${i + 1}. ${m}`)));
console.log();
console.log(chalk.blue(' --------- WARNING ----------'));
if(!warningMessages || warningMessages.length <= 0) console.log(chalk.grey('none'));
warningMessages.forEach((m, i) => console.log(chalk.yellow(`${i + 1}. ${m}`)));
console.log();
console.log(chalk.blue(' --------- CRITICAL ----------'));
if(!criticalMessages || criticalMessages.length <= 0) console.log(chalk.grey('none'));
criticalMessages.forEach((m, i) => console.log(chalk.redBright(`${i + 1}. ${m}`)));
console.log();
console.log(chalk.black.bgWhite.underline('NOTE: Some warnings are acceptable especially if the evaluation is done prior to a demo.'));
console.log(chalk.black.bgWhite.underline('For production-ready wizards, every test should pass.'));
console.log(chalk.black.bgWhite.underline('If there are any questions, please contact your Genesys Developer Evangelist POC.'));
console.log();
}
/**
* Print message to validation.log file
*/
function logToFile(){
let log = '';
let ts = Date.now();
let dateTime = (new Date(ts)).toISOString();
log += `${dateTime}\n\n`;
log += ' --------- PASSED ----------\n';
if(passedMessages.length <= 0) log += 'none\n';
passedMessages.forEach((m, i) => log += `${i + 1}. ${m}\n`);
log += '\n';
log += ' --------- WARNING ----------\n';
if(warningMessages.length <= 0) log += 'none\n';
warningMessages.forEach((m, i) => log += `${i + 1}. ${m}\n`);
log += '\n';
log += ' --------- CRITICAL ----------\n';
if(criticalMessages.length <= 0) log += 'none\n';
criticalMessages.forEach((m, i) => log += `${i + 1}. ${m}\n`);
log += '\n';
fs.writeFile('validation.log', log);
}
/**
* Get the config file and return the contents as an object
* @returns {Promise<Object|null>}
*/
async function getConfigObject(){
let configObject = null;
try {
const configData = await fs.readFile(configFilePath);
// remove 'export default'
let configContent = configData.toString()
configContent = configContent.substring(configContent.indexOf('{'));
configObject = Function('return (' + configContent + ')')()
} catch(e) {
console.error(e);
return null ;
}
return configObject;
}
/**
* Validate the config.json
*/
async function validateConfig(){
if(!config) throw new Error('Error on getting the config file.');
// =================== WARNING LEVEL ===============
await Validator.evaluateArr(Validator.WARNING, [
// URLs
Validator.customEvaluation(() => {
let url = new URL(config.wizardUriBase);
return url.hostname == 'localhost' ? false : true;
}, 'wizardUriBase is not localhost', 'wizardUriBase is localhost', 'wizardUriBase should be a publically available URL'),
Validator.customEvaluation(() => {
let url = new URL(config.redirectURLOnWizardCompleted);
return url.hostname == 'localhost' ? false : true;
}, 'redirectURLOnWizardCompleted is not localhost', 'redirectURLOnWizardCompleted is localhost', 'redirectURLOnWizardCompleted should be a publically available URL'),
// Integration Type ID
Validator.notEqual(config.premiumAppIntegrationTypeId, defaultIntegrationTypeId, 'premiumAppIntegrationTypeId', 'Once integration is approved in AppFoundry, premiumAppIntegrationTypeId should match the provided unique ID.'),
// Premium App View Permission
Validator.notEqual(config.premiumAppViewPermission, defaultViewPermission, 'premiumAppViewPermission', 'Once integration is approved in AppFoundry, premiumAppViewPermission should match the new unique permission.'),
])
// =================== CRITICAL LEVEL ===============
await Validator.evaluateArr(Validator.CRITICAL, [
// Client ID
Validator.notEqual(config.clientID, defaultClientId, 'clientID', 'clientID should be replaced with your own client ID.'),
Validator.propertyExists(config, 'clientID', 'config', 'ClientID should exist'),
Validator.propertyExists(config, 'wizardUriBase', 'config', 'wizardUriBase should exist'),
Validator.propertyExists(config, 'redirectURLOnWizardCompleted', 'config', 'redirectURLOnWizardCompleted should exist'),
Validator.propertyExists(config, 'premiumAppIntegrationTypeId', 'config', 'premiumAppIntegrationTypeId should exist'),
Validator.propertyExists(config, 'premiumAppViewPermission', 'config', 'premiumAppViewPermission should exist'),
// checkInstallPermissions
Validator.customEvaluation(() => {
let installPermisison = config.checkInstallPermissions;
if(!installPermisison) return false;
if(['all', 'premium', 'wizard', 'none'].includes(installPermisison)) return true;
return false;
}, `${config.checkInstallPermissions} is valid value for checkInstallPermissions`,
`${config.checkInstallPermissions} is not valid value for checkInstallPermissions`,
`Valid values: all, premium, wizard, none`
),
Validator.propertyExists(config, 'defaultGcEnvironment', 'config', 'defaultGcEnvironment should exist'),
// TODO: Maybe test if pcEnvironment is valid value
Validator.propertyExists(config, 'prefix', 'config', 'prefix should exist'),
Validator.notEqual(config.prefix, defaultPrefix, 'prefix', 'Prefix should be updated to be unique'),
Validator.propertyExists(config, 'provisioningInfo', 'config', 'provisioningInfo should exist'),
Validator.propertyExists(config, 'defaultLanguage', 'config', 'defaultLanguage should exist'),
Validator.propertyExists(config, 'availableLanguageAssets', 'config', 'availableLanguageAssets should exist'),
// Check if defaultLanguage value is valid
Validator.customEvaluation(() => {
if(!config.defaultLanguage) return false;
return Object.keys(config.availableLanguageAssets).includes(config.defaultLanguage)
},
`${config.defaultLanguage} is a valid language value`,
`${config.defaultLanguage} is not available in the availableLanguageAssets`,
`defaultLanguage should be valid`
),
Validator.propertyExists(config, 'installPermissions', 'config', 'installPermissions should exist'),
Validator.propertyExists(config, 'uninstallPermissions', 'config', 'uninstallPermissions should exist'),
Validator.propertyExists(config, 'installScopes', 'config', 'installScopes should exist'),
Validator.propertyExists(config, 'uninstallScopes', 'config', 'uninstallScopes should exist'),
Validator.notEqual(config.enableUninstall, true, 'config.enableUninstall', 'Uninstall should be disabled for production'),
])
}
/**
* Check if language files exist for the available languages in config
*/
async function validateLanguageFiles(){
const toBeEvaluated = [];
Object.keys(config.availableLanguageAssets).forEach(langKey => {
toBeEvaluated.push(Validator.customEvaluationAsync(async () => {
const langFilePath = path.join(languageDirPath, `${langKey}.json`);
try {
await fs.access(langFilePath, fsConstants.F_OK);
return true;
} catch(e) {
return false
}
},
`${langKey}.json exists`,
`${langKey} is declared in config languages but ${langKey}.json does not exist.`,
'Language file should exist'
))
});
await Validator.evaluateArr(Validator.CRITICAL, toBeEvaluated);
}
/**
* Checks the properties of the language JSON file (en-us language only for now)
* and evaluates if they were updated and no longer the default values.
* If en-us does not exist or is not the default language, skip this entire section.
* In that case, we'll assume that because they're using a different default language, that the text would
* already be their own.
*/
async function validateWizardText(){
let langFileObject = null;
// If en-us.json does not exist, skip
try {
const langFileData = await fs.readFile(path.join(languageDirPath, 'en-us.json'));
langFileObject = JSON.parse(langFileData.toString());
} catch(e) {
console.log(e)
return;
}
// If language default is not en-us, skip
if(config.defaultLanguage != 'en-us') return;
const forEvaluation = [];
Object.keys(defaultLanguage).forEach(textKey => {
forEvaluation.push(Validator.notEqual(langFileObject[textKey], defaultLanguage[textKey],
textKey, `text should be personalized`));
})
await Validator.evaluateArr(Validator.CRITICAL, forEvaluation)
}
/**
* Evaluate images if they've been changed
* Uses md5 checksum to check if the same as default image
*/
async function validateImages(){
let htmlString = null;
let htmlValue = null; // Parsed HTML
let forEvaluation = [];
// Footer Image
// NOTE: Only checks in index.html. Pretty safe assumption that if partner updated the image, they'll
// update it on all pages as it would be OBVIOUS if some pages have different logos.
try {
const htmlData = await fs.readFile(path.join(wizardPath, 'index.html'));
htmlString = htmlData.toString();
htmlValue = HTMLParser.parse(htmlString);
} catch(e) {
console.error(`Error in parsing index.html`)
throw e;
}
const footerImgSrc = htmlValue.querySelector('#footer-logo').getAttribute('src');
const footerImgData = await fs.readFile(path.join(wizardPath, footerImgSrc));
forEvaluation.push(Validator.customEvaluation(() => {
return md5(footerImgData) !== defaulImgHash.footerImg;
},
`Footer image has been replaced`,
`Footer image is the default footer image`,
`Personalize UI`
));
// Loading Image
const loaderImgSrc = htmlValue.querySelector('#loading-img').getAttribute('src');
const loaderImgData = await fs.readFile(path.join(wizardPath, loaderImgSrc));
forEvaluation.push(Validator.customEvaluation(() => {
return md5(loaderImgData) !== defaulImgHash.loadingImg;
},
`"Loading" graphic has been replaced`,
`"Loading" graphic is still the default svg`,
`Personalize UI`
));
await Validator.evaluateArr(Validator.CRITICAL, forEvaluation);
}
/**
* Evaluate CSS values
*/
async function validateStyles(){
let forEvaluation = [];
let cssString = null;
let cssValue = null;
try {
const cssData = await fs.readFile(path.join(wizardPath, 'styles/style.css'));
cssString = cssData.toString();
cssValue = css.parse(cssString);
} catch(e) {
console.error(`Error in parsing style.css`)
throw e;
}
const titleColor = getStyleValue(cssValue, '.title', 'color');
if (titleColor){
forEvaluation.push(Validator.notEqual(titleColor, '#3B90AA', '.title CSS', 'Personalize CSS'));
}
const messageTitleColor = getStyleValue(cssValue, '.message-title', 'color');
if (messageTitleColor){
forEvaluation.push(Validator.notEqual(messageTitleColor, '#00AE9E', '.message-title color CSS', 'Personalize CSS'));
}
const buttonColor = getStyleValue(cssValue, 'button', 'background-color');
if (buttonColor){
forEvaluation.push(Validator.notEqual(buttonColor, '#00AE9E', 'button background-color CSS', 'Personalize CSS'));
}
await Validator.evaluateArr(Validator.WARNING, forEvaluation);
}
/**
* Get the value of a selectors's property
* @param {Object} ast Parsed object by 'css'
* @param {String} selector CSS selector (not multiple)
* @param {String} property property of the element
* @returns {String|null} value of the selector's property
* */
function getStyleValue(ast, selector, property) {
const cssRules = ast.stylesheet.rules;
const rule = cssRules.find(rule => {
if(!rule.selectors) return false;
return rule.selectors.includes(selector);
});
if(!rule) return null;
const declaration = rule.declarations.find(declaration => declaration.property == property);
if(!declaration) return null;
const value = declaration.value;
if(!value) return null;
return value;
}
async function validateAll(){
config = await getConfigObject();
const validations = [validateConfig(),
// validateLanguageFiles(),
// validateWizardText(),
validateImages(),
validateStyles()
]
await Promise.all(validations)
printMessages();
logToFile();
}
console.log(chalk.black.bgCyanBright(`
=====================================================================
----------------- QUIDDITCH -----------------
----------------- PREMIUM WIZARD VALIDATION -----------------
----------------- (v3.0.0) -----------------
=====================================================================
`));
validateAll();
/**
* TODO: verify that div ids for 'pages' exist in index.html
* TODO: Make sure wizarduribase hase / at the end of it
* TODO: if post custom setup is enabled make sure translation keys exist.
*/