Add tests to workbook metadata validator (#1652)

* Add tests to workbook metadata validator

* empty dependency format test file removed and is done using in json schema
This commit is contained in:
roabadie-microsoft 2021-02-02 11:31:13 +02:00 коммит произвёл GitHub
Родитель f93d10838f
Коммит fdbe1e300f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
24 изменённых файлов: 148 добавлений и 28 удалений

Просмотреть файл

@ -1,11 +1,11 @@
[
{
"workbookKey": "MyTestWorkbook",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a test workbook",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "SomeImageWhite.png", "SomeImageBlack.png" ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",
@ -14,11 +14,11 @@
},
{
"workbookKey": "MyTestWorkbook",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a workbook with a duplicate key",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "SomeImageWhite.png", "SomeImageBlack.png" ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",

Просмотреть файл

@ -0,0 +1,15 @@
[
{
"workbookKey": "MyTestWorkbook",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a workbook with an invalid format of data connectors dependencies",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [""],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",
"subtitle": "",
"provider": "Microsoft"
}
]

Просмотреть файл

@ -0,0 +1,15 @@
[
{
"workbookKey": "MyTestWorkbook",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a workbook with an invalid format of data types dependencies",
"dataTypesDependencies": ["", ""],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",
"subtitle": "",
"provider": "Microsoft"
}
]

Просмотреть файл

@ -1,11 +1,11 @@
[
{
"workbookKey": "MyTestWorkbook",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a test workbook with an illegal field",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "SomeImageWhite.png", "SomeImageBlack.png" ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",

Просмотреть файл

@ -0,0 +1,15 @@
[
{
"workbookKey": "MyValidWorkbook1",
"logoFileName": "non_existing_logo.svg",
"description": "This is a test workbook with logo image which does not exist",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",
"subtitle": "",
"provider": "Microsoft"
}
]

Просмотреть файл

@ -1,11 +1,11 @@
[
{
"workbookKey": "onlyWhitePreviewImagesWorkbook",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a test workbook with only white preview image filenames",
"dataTypesDependencies": [ "CommonSecurityLog", "SecurityEvent", "Syslog"],
"dataConnectorsDependencies": [ "SomeConnector", "SomeOtherConnector" ],
"previewImagesFileNames": [ "Image1White.png", "Image2White.png" ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",

Просмотреть файл

@ -1,10 +1,10 @@
[
{
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a test workbook with a missing required property (workbookKey)",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "SomeImageWhite.png", "SomeImageBlack.png" ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",

Просмотреть файл

@ -1,11 +1,11 @@
[
{
"workbookKey": "onlyBlackPreviewImagesWorkbook",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a test workbook with only black preview image filenames",
"dataTypesDependencies": [ "CommonSecurityLog", "SecurityEvent", "Syslog"],
"dataConnectorsDependencies": [ "SomeConnector", "SomeOtherConnector" ],
"previewImagesFileNames": [ "Image1Black.png", "Image2Black.png" ],
"previewImagesFileNames": [ "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",

Просмотреть файл

@ -1,11 +1,11 @@
[
{
"workbookKey": "MyTestWorkbook",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a test workbook with an invalid preview image (no color)",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "SomeImage.png" ],
"previewImagesFileNames": [ "TestPreviewImages/SomeImage.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",

Просмотреть файл

@ -1,11 +1,11 @@
[
{
"workbookKey": "MyTestWorkbook",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is an invalid workbook with a non PNG preview image",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "SomeImage-Black.jpeg" ],
"previewImagesFileNames": [ "TestPreviewImages/SomeImage-Black.jpeg" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",

Просмотреть файл

@ -0,0 +1,15 @@
[
{
"workbookKey": "MyWorkbook",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a test workbook with preview images which do not exist",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "NotExistImageBlack.png", "NotExistImageWhite.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",
"subtitle": "",
"provider": "Microsoft"
}
]

Просмотреть файл

@ -1,11 +1,11 @@
[
{
"workbookKey": "MyValidWorkbook1",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is a valid test workbook",
"dataTypesDependencies": [ "CommonSecurityLog"],
"dataConnectorsDependencies": [ ],
"previewImagesFileNames": [ "SomeImageWhite.png", "SomeImageBlack.png" ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",
@ -14,11 +14,11 @@
},
{
"workbookKey": "MyValidWorkbook2",
"logoFileName": "some_logo.svg",
"logoFileName": "TestLogos/some_logo.svg",
"description": "This is another valid test workbook",
"dataTypesDependencies": [ "CommonSecurityLog", "SecurityEvent", "Syslog"],
"dataConnectorsDependencies": [ "SomeConnector", "SomeOtherConnector" ],
"previewImagesFileNames": [ "Image1White.png", "Image1Black.png", "Image2White.png", "Image2Black.png" ],
"previewImagesFileNames": [ "TestPreviewImages/Image1White.png", "TestPreviewImages/Image1Black.png" ],
"version": "1.0",
"title": "My Test Workbook",
"templateRelativePath": "MyTest.json",

Просмотреть файл

@ -38,6 +38,22 @@ describe("workbooksValidator", () => {
await checkInvalid(".script/tests/workbooksValidatorTest/testFiles/missingBlackPreviewImageWorkbooksMetadata.json", "WorkbookValidationError");
});
it("should throw an exception when empty data connectors dependency is in the wrong format", async () => {
await checkInvalid(".script/tests/workbooksValidatorTest/testFiles/emptyDataConnectorsDependenciesWithInvalidFormat.json", "SchemaError");
});
it("should throw an exception when empty data types dependency is in the wrong format", async () => {
await checkInvalid(".script/tests/workbooksValidatorTest/testFiles/emptyDataTypesDependenciesWithInvalidFormat.json", "SchemaError");
});
it("should throw an exception when logo image file does not exist", async () => {
await checkInvalid(".script/tests/workbooksValidatorTest/testFiles/logoImageFileDoesNotExist.json", "WorkbookValidationError");
});
it("should throw an exception when preview image file does not exist", async () => {
await checkInvalid(".script/tests/workbooksValidatorTest/testFiles/previewImageFilesDoNotExist.json", "WorkbookValidationError");
});
async function checkInvalid(filePath: string, expectedError: string): Promise<Chai.PromisedAssertion> {
await expect(IsValidWorkbookMetadata(filePath)).eventually.rejectedWith(Error).and.have.property("name", expectedError);
}

Просмотреть файл

@ -1,9 +1,16 @@
import { SchemaError, Validator } from "jsonschema";
import { Schema, SchemaError, ValidationError, Validator } from "jsonschema";
export function isValidSchema(json: object, schema: object) {
var validationResult = new Validator().validate(json, schema);
if (!validationResult.valid) {
let errorMsg = `Invalid Schema. Validation errors: ${validationResult.errors.map((err) => err.message).join(", ")}`;
throw new SchemaError(errorMsg, schema);
let errorMessage = `Invalid Schema. Validation errors: ${validationResult.errors.map((err) => buildErrorMessage(err)).join(", ")}`;
throw new SchemaError(errorMessage, schema);
}
}
function buildErrorMessage(err: ValidationError){
let errorMessage = err.stack;
let description = (<Schema>err.schema).description;
errorMessage += description ? `. Description: ${description}` : "";
return errorMessage;
}

Просмотреть файл

@ -17,7 +17,9 @@
"type": "array",
"items": [
{
"type": "string"
"type": "string",
"minLength": 1,
"description": "A value of a data type dependency can't be an empty string."
}
]
},
@ -25,7 +27,9 @@
"type": "array",
"items": [
{
"type": "string"
"type": "string",
"minLength": 1,
"description": "A value of a data connector dependency can't be an empty string."
}
]
},

Просмотреть файл

@ -0,0 +1,30 @@
import fs from "fs";
import { WorkbookValidationError } from "../validationError";
import { WorkbookMetadata } from "../workbookMetadata";
const logoImagesFolderPath: string = "Workbooks/Images/Logos";
const previewImagesFolderPath: string = "Workbooks/Images/Preview";
// This function checks if the defined logo image files exist
export function doDefinedLogoImageFilesExist(items: Array<WorkbookMetadata>) {
items.forEach((workbookMetadata: WorkbookMetadata) => {
if(workbookMetadata.logoFileName !== ""){
if(!fs.existsSync(`${logoImagesFolderPath}/${workbookMetadata.logoFileName}`)){
throw new WorkbookValidationError(`Can't locate logo image file ${workbookMetadata.logoFileName} under the ${logoImagesFolderPath} directory`);
}
}
});
}
// This function checks if the defined preview image files exist
export function doDefinedPreviewImageFilesExist(items: Array<WorkbookMetadata>) {
items.forEach((workbookMetadata: WorkbookMetadata) => {
if(workbookMetadata.previewImagesFileNames.length > 0){
workbookMetadata.previewImagesFileNames.forEach((previewImageFileName: string) => {
if(previewImageFileName !== "" && !fs.existsSync(`${previewImagesFolderPath}/${previewImageFileName}`)){
throw new WorkbookValidationError(`Can't locate preview image file ${previewImageFileName} under the ${previewImagesFolderPath} directory`);
}
});
}
});
}

Просмотреть файл

@ -1,5 +1,5 @@
import { WorkbookTemplatesValidationError } from "../validationError";
import { WorkbookTemplate } from "../workbookTemplate";
import { WorkbookTemplatesValidationError } from "../../validationError";
import { WorkbookTemplate } from "../../workbookTemplate";
// This function checks if the value of the "fromTemplateId" key is not "sentinel-UserWorkbook" (which is the default given value).
export function isFromTemplateIdNotSentinelUserWorkbook(workbookTemplate: WorkbookTemplate) {

Просмотреть файл

@ -3,6 +3,7 @@ import { runCheckOverChangedFiles } from "./utils/changedFilesValidator";
import { ExitCode } from "./utils/exitCode";
import { isValidSchema } from "./utils/jsonSchemaChecker";
import * as logger from "./utils/logger";
import { doDefinedLogoImageFilesExist, doDefinedPreviewImageFilesExist } from "./utils/workbookCheckers/imageExistChecker";
import { isValidPreviewImageFileNames } from "./utils/workbookCheckers/previewImageChecker";
import { isUniqueKeys } from "./utils/workbookCheckers/uniqueWorkbookKeyChecker";
@ -13,6 +14,8 @@ export async function IsValidWorkbookMetadata(filePath: string): Promise<ExitCod
isValidSchema(workbooksMetadata, schema);
isUniqueKeys(workbooksMetadata);
isValidPreviewImageFileNames(workbooksMetadata);
doDefinedLogoImageFilesExist(workbooksMetadata);
doDefinedPreviewImageFilesExist(workbooksMetadata);
return ExitCode.SUCCESS;
}

Просмотреть файл

@ -2,7 +2,7 @@ import fs from "fs";
import { runCheckOverChangedFiles } from "./utils/changedFilesValidator";
import { ExitCode } from "./utils/exitCode";
import * as logger from "./utils/logger";
import { isFromTemplateIdNotSentinelUserWorkbook } from "./utils/workbookCheckers/fromTemplateIdChecker";
import { isFromTemplateIdNotSentinelUserWorkbook } from "./utils/workbookCheckers/workbookTemplateCheckers/fromTemplateIdChecker";
import { WorkbookTemplate } from "./utils/workbookTemplate";
export async function IsValidWorkbookTemplate(filePath: string): Promise<ExitCode> {

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл