From 5f125f792dad08db1365c25ac2c84532cb7d1fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Am=C3=A9rico?= Date: Thu, 20 Dec 2018 13:34:54 -0300 Subject: [PATCH] Mock PDFSecurity methods in tests to ensure same output accross different runs or systems --- tests/helpers.js | 15 +++++++++++++++ tests/unit/trailer.spec.js | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/helpers.js b/tests/helpers.js index d73daa0..889ef87 100644 --- a/tests/helpers.js +++ b/tests/helpers.js @@ -1,7 +1,22 @@ var PDFDocument = require('../lib/document').default; +var PDFSecurity = require('../lib/security').default; +var CryptoJS = require('crypto-js'); var path = require('path'); var fs = require('fs'); +// manual mock for PDFSecurity to ensure stored id will be the same accross different systems +PDFSecurity.generateFileID = () => { + return new Buffer('mocked-pdf-id'); +} + +PDFSecurity.generateRandomWordArray = (bytes) => { + const words = []; + for (let i = 0; i < bytes; i++) { + words.push(0x00010203); + } + return new CryptoJS.lib.WordArray.init(words, bytes); +} + function updatePdf (pdfData, testState, snapshotChanges) { const pdfDir = path.join(path.dirname(testState.testPath), '__pdfs__'); if (!fs.existsSync(pdfDir)) { diff --git a/tests/unit/trailer.spec.js b/tests/unit/trailer.spec.js index 775eeb6..f350238 100644 --- a/tests/unit/trailer.spec.js +++ b/tests/unit/trailer.spec.js @@ -1,4 +1,10 @@ const PDFDocument = require('../../lib/document').default; +const PDFSecurity = require('../../lib/security').default; + +// manual mock for PDFSecurity to ensure stored id will be the same accross different systems +PDFSecurity.generateFileID = () => { + return new Buffer('mocked-pdf-id'); +} describe('Document trailer', () => { let document; @@ -28,7 +34,7 @@ describe('Document trailer', () => { ], [ 'trailer', - `<<\n/Size 11\n/Root 2 0 R\n/Info 7 0 R\n/ID [<8c72cf48ff87daac57e26bf1550e6979> <8c72cf48ff87daac57e26bf1550e6979>]\n>>` + `<<\n/Size 11\n/Root 2 0 R\n/Info 7 0 R\n/ID [<6d6f636b65642d7064662d6964> <6d6f636b65642d7064662d6964>]\n>>` ] ]; document._write = function(data) {