diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-09-10 15:55:27 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-09-10 15:55:27 +0100 |
| commit | 1494d3c3a6283a00701ac452b8a9a5f8f2f4e9dd (patch) | |
| tree | 4e4467490b883fe075094a25ba28f05e62d21393 /test | |
| parent | 162324eb530d781aa59acce597997646ee59d1eb (diff) | |
Support disabling external file access
Diffstat (limited to 'test')
| -rw-r--r-- | test/docx/files.tests.js | 21 | ||||
| -rw-r--r-- | test/mammoth.tests.js | 9 |
2 files changed, 26 insertions, 4 deletions
diff --git a/test/docx/files.tests.js b/test/docx/files.tests.js index 9a39acb..5ddda4a 100644 --- a/test/docx/files.tests.js +++ b/test/docx/files.tests.js @@ -13,9 +13,16 @@ var readFile = promises.promisify(fs.readFile.bind(fs)); test("Files", { + "when external file access is disabled then reading file raises error": function() { + var files = new Files({externalFileAccess: false}); + return assertError(files.read("/tmp/image.png", "base64"), function(err) { + assert.strictEqual(err.message, "could not read external image '/tmp/image.png', external file access is disabled"); + }); + }, + "can open files with file URI": function() { var filePath = path.resolve(testing.testPath("tiny-picture.png")); - var files = new Files({}); + var files = new Files({externalFileAccess: true}); return files.read("file:///" + filePath.replace(/^\//, ""), "base64").then(function(contents) { return readFile(filePath, "base64").then(function(expectedContents) { assert.deepEqual(contents, expectedContents); @@ -25,7 +32,10 @@ test("Files", { "can open files with relative URI": function() { var filePath = path.resolve(testing.testPath("tiny-picture.png")); - var files = new Files({relativeToFile: testing.testPath("./document.docx")}); + var files = new Files({ + externalFileAccess: true, + relativeToFile: testing.testPath("./document.docx") + }); return files.read("tiny-picture.png", "base64").then(function(contents) { return readFile(filePath, "base64").then(function(expectedContents) { assert.deepEqual(contents, expectedContents); @@ -34,14 +44,17 @@ test("Files", { }, "given base is not set when opening relative uri then error is raised": function() { - var files = new Files({}); + var files = new Files({externalFileAccess: true}); return assertError(files.read("not-a-real-file.png", "base64"), function(err) { assert.equal(err.message, "could not find external image 'not-a-real-file.png', path of input document is unknown"); }); }, "error if relative uri cannot be opened": function() { - var files = new Files({relativeToFile: "/tmp/document.docx"}); + var files = new Files({ + externalFileAccess: true, + relativeToFile: "/tmp/document.docx" + }); return assertError(files.read("not-a-real-file.png", "base64"), function(err) { assertRegex(err.message, /could not open external image: 'not-a-real-file.png' \(document directory: '\/tmp'\)\nENOENT.*\/tmp\/not-a-real-file.png.*/); }); diff --git a/test/mammoth.tests.js b/test/mammoth.tests.js index 93a4f16..06cf62f 100644 --- a/test/mammoth.tests.js +++ b/test/mammoth.tests.js @@ -327,6 +327,15 @@ test('error if images stored outside of document are specified when passing file }); }); +test('error if images stored outside of document are specified when external file access is disabled', function() { + var docxPath = path.join(__dirname, "test-data/external-picture.docx"); + return mammoth.convertToHtml({path: docxPath}, {externalFileAccess: false}).then(function(result) { + assert.equal(result.value, ''); + assert.equal(result.messages[0].message, "could not read external image 'tiny-picture.png', external file access is disabled"); + assert.equal(result.messages[0].type, "error"); + }); +}); + test('simple list is converted to list elements', function() { var docxPath = path.join(__dirname, "test-data/simple-list.docx"); return mammoth.convertToHtml({path: docxPath}).then(function(result) { |
