From 8f57a3f20c81fcb10db13fef3f15b4e3d789d59a Mon Sep 17 00:00:00 2001 From: igor <igor.markin@vereign.com> Date: Mon, 28 Dec 2020 13:08:37 +0300 Subject: [PATCH] Add option to hash attachment --- __tests__/utils.ts | 68 +++++++++++++++++++++++++++++++--------------- yarn.lock | 2 +- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/__tests__/utils.ts b/__tests__/utils.ts index e0e51ef..1ec1507 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -1,5 +1,6 @@ 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, diff --git a/yarn.lock b/yarn.lock index f785607..a048b46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" -- GitLab