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
No related branches found
No related tags found
1 merge request!19Add readme and improve tests usage
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
const fs = require("fs"); const fs = require("fs");
const crypto = require("crypto");
import MIMEParser from "@vereign/mime-parser"; import MIMEParser from "@vereign/mime-parser";
import { expect, test } from "@jest/globals"; import { expect, test } from "@jest/globals";
import { DOM } from "@vereign/dom"; import { DOM } from "@vereign/dom";
...@@ -10,7 +11,14 @@ import { PlainNormalizer, HTMLNormalizer } from "../src"; ...@@ -10,7 +11,14 @@ import { PlainNormalizer, HTMLNormalizer } from "../src";
const SENT_EML_NAME = "sent.eml"; const SENT_EML_NAME = "sent.eml";
const RECEIVED_EML_NAME = "received.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({ expect.extend({
toEqualWithDiff(target, source) { toEqualWithDiff(target, source) {
...@@ -38,20 +46,26 @@ const getMime = (path: string): MIMEParser => { ...@@ -38,20 +46,26 @@ const getMime = (path: string): MIMEParser => {
return mimeCache[path]; return mimeCache[path];
}; };
export const getNormalizedPlain = ( export const getNormalizedPlain = async (
testCasePath: string testCasePath: string
): { ): Promise<{
sentPlain: string; sentPlain: string;
receivedPlain: string; receivedPlain: string;
} => { }> => {
const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
const sentNormalizedPlain = PlainNormalizer.normalizePlain( const sentNormalizedPlain = PlainNormalizer.normalizePlain(
sentMime.getPlain(populateAttachments) await sentMime.getPlain({
populateAttachments,
hashAttachment,
})
); );
const receivedNormalizedPlain = PlainNormalizer.normalizePlain( const receivedNormalizedPlain = PlainNormalizer.normalizePlain(
receivedMime.getPlain(populateAttachments) await receivedMime.getPlain({
populateAttachments,
hashAttachment,
})
); );
return { return {
...@@ -66,18 +80,24 @@ export const getTestCasesDirs = (testCasesPath: string): Array<string> => { ...@@ -66,18 +80,24 @@ export const getTestCasesDirs = (testCasesPath: string): Array<string> => {
}); });
}; };
export const getNormalizedHtml = ( export const getNormalizedHtml = async (
testCasePath: string, testCasePath: string,
vendor: string vendor: string
): { ): Promise<{
sentHtml: string; sentHtml: string;
receivedHtml: string; receivedHtml: string;
} => { }> => {
const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
const sentHtml = sentMime.getHTML(populateAttachments); const sentHtml = await sentMime.getHTML({
const receivedHtml = receivedMime.getHTML(populateAttachments); populateAttachments,
hashAttachment,
});
const receivedHtml = await receivedMime.getHTML({
populateAttachments,
hashAttachment,
});
const sentDOM = new DOM(sentHtml); const sentDOM = new DOM(sentHtml);
const receivedDOM = new JSDOM(receivedHtml); const receivedDOM = new JSDOM(receivedHtml);
...@@ -126,9 +146,9 @@ export const createDescribeHtmlTestCases = ( ...@@ -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 testCasePath = testsCasesPath + "/" + dirName;
const normalizedHtmls = getNormalizedHtml(testCasePath, vendor); const normalizedHtmls = await getNormalizedHtml(testCasePath, vendor);
const { sentHtml, receivedHtml } = normalizedHtmls; const { sentHtml, receivedHtml } = normalizedHtmls;
// expect(receivedHtml.length).toBeGreaterThan(0); // expect(receivedHtml.length).toBeGreaterThan(0);
...@@ -159,9 +179,9 @@ export const createDescribePlainTestCases = (testsPath: string) => ( ...@@ -159,9 +179,9 @@ export const createDescribePlainTestCases = (testsPath: string) => (
testCasesDirs = testCasesDirs.filter((dir) => !failingCases.includes(dir)); 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 testCasePath = testsCasesPath + "/" + dirName;
const normalizedPlain = getNormalizedPlain(testCasePath); const normalizedPlain = await getNormalizedPlain(testCasePath);
const { sentPlain, receivedPlain } = normalizedPlain; const { sentPlain, receivedPlain } = normalizedPlain;
// expect(sentPlain.length).toBeGreaterThan(0); // expect(sentPlain.length).toBeGreaterThan(0);
...@@ -201,9 +221,9 @@ export const createDescribePseudoPlainTestCases = ( ...@@ -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 testCasePath = testsCasesPath + "/" + dirName;
const { sentHtmlDocument, receivedHtmlDocument } = getDOMDocuments( const { sentHtmlDocument, receivedHtmlDocument } = await getDOMDocuments(
testCasePath testCasePath
); );
...@@ -226,17 +246,21 @@ export const createDescribePseudoPlainTestCases = ( ...@@ -226,17 +246,21 @@ export const createDescribePseudoPlainTestCases = (
}); });
}; };
export const getDOMDocuments = ( export const getDOMDocuments = async (
testCasePath: string testCasePath: string
): { ): Promise<{
sentHtmlDocument: HTMLDocument; sentHtmlDocument: HTMLDocument;
receivedHtmlDocument: HTMLDocument; receivedHtmlDocument: HTMLDocument;
} => { }> => {
const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`); const sentMime = getMime(`${testCasePath}/${SENT_EML_NAME}`);
const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`); const receivedMime = getMime(`${testCasePath}/${RECEIVED_EML_NAME}`);
const sentDOM = new DOM(sentMime.getHTML(populateAttachments)); const sentDOM = new DOM(
const receivedDOM = new JSDOM(receivedMime.getHTML(populateAttachments)); await sentMime.getHTML({ populateAttachments, hashAttachment })
);
const receivedDOM = new JSDOM(
await receivedMime.getHTML({ populateAttachments, hashAttachment })
);
return { return {
sentHtmlDocument: sentDOM.window.document, sentHtmlDocument: sentDOM.window.document,
......
...@@ -1264,7 +1264,7 @@ ...@@ -1264,7 +1264,7 @@
"@vereign/mime-parser@git+ssh://git@code.vereign.com:code/js-toolbox/mime-parser.git": "@vereign/mime-parser@git+ssh://git@code.vereign.com:code/js-toolbox/mime-parser.git":
version "1.0.0" 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: dependencies:
libmime "^5.0.0" libmime "^5.0.0"
libqp "^1.1.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