Mock PDFSecurity methods in tests to ensure same output accross different runs or systems

This commit is contained in:
Luiz Américo 2018-12-20 13:34:54 -03:00
parent 883983aa3d
commit 5f125f792d
2 changed files with 22 additions and 1 deletions

View File

@ -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)) {

View File

@ -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) {