Skip to content
Snippets Groups Projects
Commit 8f57a3f2 authored by Igor Markin's avatar Igor Markin
Browse files

Add option to hash attachment

parent 7b6f8b60
Branches
Tags
1 merge request!19Add readme and improve tests usage
import { JSDOM } from "jsdom";
const fs = require("fs");
const crypto = require("crypto");
import MIMEParser from "@vereign/mime-parser";
import { expect, test } from "@jest/globals";
import { DOM } from "@vereign/dom";
......@@ -10,7 +11,14 @@ import { PlainNormalizer, HTMLNormalizer } from "../src";
const SENT_EML_NAME = "sent.eml";
const RECEIVED_EML_NAME = "received.eml";
const populateAttachments = false;
const populateAttachments = true;
const hashAttachment = (base64: string) => {
return crypto
.createHash("sha256")
.update(Buffer.from(base64, "base64"))
.digest()
.toString("base64");
};
expect.extend({
toEqualWithDiff(target, source) {
......@@ -38,20 +46,26 @@ const getMime = (path: string): MIMEParser => {
return mimeCache[path];
};
export const getNormalizedPlain = (
export const getNormalizedPlain = async (
testCasePath: string
): {
): Promise<{
sentPlain: string;
receivedPlain: string;
} => {
}> => {
const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
const sentNormalizedPlain = PlainNormalizer.normalizePlain(
sentMime.getPlain(populateAttachments)
await sentMime.getPlain({
populateAttachments,
hashAttachment,
})
);
const receivedNormalizedPlain = PlainNormalizer.normalizePlain(
receivedMime.getPlain(populateAttachments)
await receivedMime.getPlain({
populateAttachments,
hashAttachment,
})
);
return {
......@@ -66,18 +80,24 @@ export const getTestCasesDirs = (testCasesPath: string): Array<string> => {
});
};
export const getNormalizedHtml = (
export const getNormalizedHtml = async (
testCasePath: string,
vendor: string
): {
): Promise<{
sentHtml: string;
receivedHtml: string;
} => {
}> => {
const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
const sentHtml = sentMime.getHTML(populateAttachments);
const receivedHtml = receivedMime.getHTML(populateAttachments);
const sentHtml = await sentMime.getHTML({
populateAttachments,
hashAttachment,
});
const receivedHtml = await receivedMime.getHTML({
populateAttachments,
hashAttachment,
});
const sentDOM = new DOM(sentHtml);
const receivedDOM = new JSDOM(receivedHtml);
......@@ -126,9 +146,9 @@ export const createDescribeHtmlTestCases = (
);
}
test.each(testCasesDirs)("Case %s", (dirName: string) => {
test.each(testCasesDirs)("Case %s", async (dirName: string) => {
const testCasePath = testsCasesPath + "/" + dirName;
const normalizedHtmls = getNormalizedHtml(testCasePath, vendor);
const normalizedHtmls = await getNormalizedHtml(testCasePath, vendor);
const { sentHtml, receivedHtml } = normalizedHtmls;
// expect(receivedHtml.length).toBeGreaterThan(0);
......@@ -159,9 +179,9 @@ export const createDescribePlainTestCases = (testsPath: string) => (
testCasesDirs = testCasesDirs.filter((dir) => !failingCases.includes(dir));
}
test.each(testCasesDirs)("Case %s", (dirName: string) => {
test.each(testCasesDirs)("Case %s", async (dirName: string) => {
const testCasePath = testsCasesPath + "/" + dirName;
const normalizedPlain = getNormalizedPlain(testCasePath);
const normalizedPlain = await getNormalizedPlain(testCasePath);
const { sentPlain, receivedPlain } = normalizedPlain;
// expect(sentPlain.length).toBeGreaterThan(0);
......@@ -201,9 +221,9 @@ export const createDescribePseudoPlainTestCases = (
);
}
test.each(testCasesDirs)("Case %s", (dirName: string) => {
test.each(testCasesDirs)("Case %s", async (dirName: string) => {
const testCasePath = testsCasesPath + "/" + dirName;
const { sentHtmlDocument, receivedHtmlDocument } = getDOMDocuments(
const { sentHtmlDocument, receivedHtmlDocument } = await getDOMDocuments(
testCasePath
);
......@@ -226,17 +246,21 @@ export const createDescribePseudoPlainTestCases = (
});
};
export const getDOMDocuments = (
export const getDOMDocuments = async (
testCasePath: string
): {
): Promise<{
sentHtmlDocument: HTMLDocument;
receivedHtmlDocument: HTMLDocument;
} => {
}> => {
const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
const sentDOM = new DOM(sentMime.getHTML(populateAttachments));
const receivedDOM = new JSDOM(receivedMime.getHTML(populateAttachments));
const sentDOM = new DOM(
await sentMime.getHTML({ populateAttachments, hashAttachment })
);
const receivedDOM = new JSDOM(
await receivedMime.getHTML({ populateAttachments, hashAttachment })
);
return {
sentHtmlDocument: sentDOM.window.document,
......
......@@ -1264,7 +1264,7 @@
"@vereign/mime-parser@git+ssh://git@code.vereign.com:code/js-toolbox/mime-parser.git":
version "1.0.0"
resolved "git+ssh://git@code.vereign.com:code/js-toolbox/mime-parser.git#07fa6f0d595416f12baa601b9258acedf91c97c7"
resolved "git+ssh://git@code.vereign.com:code/js-toolbox/mime-parser.git#4b509038b448d0ce2f4e679beb5535e62ed7d6ad"
dependencies:
libmime "^5.0.0"
libqp "^1.1.0"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment