|
2 | 2 |
|
3 | 3 | var assert = require('assert'); |
4 | 4 | var doT = require('..'); |
| 5 | +var fs = require('fs'); |
5 | 6 |
|
6 | 7 |
|
7 | 8 | describe('doT.process', function() { |
8 | | -describe('polluting object prototype should not affect template compilation', function() { |
9 | | -it('should ignore varname on object prototype', function() { |
10 | | - var currentLog = console.log; |
11 | | - console.log = log; |
12 | | - var logged; |
13 | | - |
14 | | -Object.prototype.templateSettings = {varname: 'it=(console.log("executed"),{})'}; |
15 | | - |
16 | | - try { |
17 | | - const templates = doT.process({path: './test'}); |
18 | | - assert.notEqual(logged, 'executed'); |
19 | | - // injected code can only be executed if undefined is passed to template function |
20 | | - templates.test(); |
21 | | - assert.notEqual(logged, 'executed'); |
22 | | - } finally { |
23 | | - console.log = currentLog; |
24 | | - } |
25 | | - |
26 | | - function log(str) { |
27 | | - logged = str; |
28 | | - } |
29 | | -}) |
30 | | -}); |
| 9 | + beforeEach(function() { |
| 10 | + removeCompiledTemplateFiles(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(function() { |
| 14 | + removeCompiledTemplateFiles(); |
| 15 | + }); |
| 16 | + |
| 17 | + function removeCompiledTemplateFiles() { |
| 18 | + try { fs.unlinkSync('./test/templates/test.js'); } catch(e) {} |
| 19 | + } |
| 20 | + |
| 21 | + it('should compile all templates in folder', function() { |
| 22 | + const templates = doT.process({path: './test/templates'}); |
| 23 | + var str = templates.test({data: 2}); |
| 24 | + assert.equal(str, '21'); |
| 25 | + |
| 26 | + var js = fs.statSync('./test/templates/test.js'); |
| 27 | + assert.ok(js.isFile()); |
| 28 | + |
| 29 | + // code below passes if the test is run without coverage using `npm run test-spec` |
| 30 | + // because source code of doT.encodeHTMLSource is used inside compiled templates |
| 31 | + |
| 32 | + // var fn = require('./templates/test.js'); |
| 33 | + // var str = fn({data: 2}); |
| 34 | + // assert.equal(str, '21'); |
| 35 | + }); |
| 36 | + |
| 37 | + |
| 38 | +it('should ignore varname with polluted object prototype', function() { |
| 39 | + var currentLog = console.log; |
| 40 | + console.log = log; |
| 41 | + var logged; |
| 42 | + |
| 43 | + Object.prototype.templateSettings = {varname: 'it=(console.log("executed"),{})'}; |
| 44 | + |
| 45 | + try { |
| 46 | + const templates = doT.process({path: './test/templates'}); |
| 47 | + assert.notEqual(logged, 'executed'); |
| 48 | + // injected code can only be executed if undefined is passed to template function |
| 49 | + templates.test(); |
| 50 | + assert.notEqual(logged, 'executed'); |
| 51 | + } finally { |
| 52 | + console.log = currentLog; |
| 53 | + } |
| 54 | + |
| 55 | + function log(str) { |
| 56 | + logged = str; |
| 57 | + } |
| 58 | + }); |
31 | 59 | }); |
0 commit comments