UNPKG

3.72 kBJavaScriptView Raw
1var assert = require ('assert'),
2 helpers = require ('../lib/helpers'),
3 path = require ('path');
4
5describe ('helpers', function(){
6
7 describe('isPathContainedInRoot', function(){
8 it('should contain root', function(done){
9 assert.ok(helpers.isPathContainedInRoot("/test/wibble", "/test"));
10 assert.ok(!helpers.isPathContainedInRoot("/root/test/wibble", "/test"));
11 done();
12 });
13
14 it('should accept relative paths to the process', function(done){
15 assert.ok(helpers.isPathContainedInRoot("wibble", process.cwd()));
16 assert.ok(!helpers.isPathContainedInRoot("/wibble", process.cwd()));
17 done();
18 });
19
20 it('should handle non string types gracefully', function(done){
21 assert.ok(!helpers.isPathContainedInRoot("/test/wibble", 333));
22 done();
23 });
24
25 });
26
27 describe('areValidPathElements', function(){
28 it('should sanitize string input', function(done){
29 assert.ok(helpers.areValidPathElements("wibble"));
30 assert.ok(!helpers.areValidPathElements("../wibble"));
31 done();
32 });
33
34 it('should sanitize arrays of string input', function(done){
35 assert.ok(helpers.areValidPathElements(["wibble", "other valid"]));
36 assert.ok(!helpers.areValidPathElements(["../wibble", "valid"]));
37 assert.ok(!helpers.areValidPathElements(["../wibble", "../invalid"]));
38 done();
39 });
40
41 it('should sanitize number input', function(done){
42 assert.ok(helpers.areValidPathElements(4444));
43 done();
44 });
45
46 //http://docs.nodejitsu.com/articles/file-system/security/introduction
47 it('should return invalid path if poison null bytes found', function(done){
48 assert.ok(!helpers.areValidPathElements("input\0file"));
49 done();
50 });
51
52 it('should return invalid path if .. found', function(done){
53 assert.ok(!helpers.areValidPathElements("input..file"));
54 done();
55 });
56
57 });
58
59 describe('getJSONFromFile', function(){
60 it('should return default value if file not found', function(done){
61 var defaultValue = {};
62 var filePath = 'invalid_path.txt';
63 assert.equal(defaultValue, helpers.getJSONFromFile(filePath, defaultValue));
64 assert.equal("test", helpers.getJSONFromFile(filePath, "test"));
65 done();
66 });
67
68 it('should call callback if file not found', function(done){
69 var filePath = path.join(process.cwd(), 'test', 'fixtures', 'config', 'DOES-NOT-EXISTS-valid_config.json');
70 var content = helpers.getJSONFromFile(filePath, {}, function(path){
71 assert.equal(filePath, path);
72 done();
73 });
74 });
75
76 it('should return default value if file is not valid JSON', function(done){
77 var defaultValue = {};
78 var filePath = path.join(process.cwd(), 'test', 'fixtures', 'config', 'invalid_config.json');
79 assert.equal(defaultValue, helpers.getJSONFromFile(filePath, defaultValue));
80 assert.equal("test", helpers.getJSONFromFile(filePath, "test"));
81 done();
82 });
83
84 it('should call callback if file is not valid JSON', function(done){
85 var filePath = path.join(process.cwd(), 'test', 'fixtures', 'config', 'invalid_config.json');
86 var content = helpers.getJSONFromFile(filePath, {}, null, function(path, err){
87 assert.equal(filePath, path);
88 done();
89 });
90 });
91
92 it('should return default value if file is not valid JSON', function(done){
93 var filePath = path.join(process.cwd(), 'test', 'fixtures', 'config', 'valid_config.json');
94 var content = helpers.getJSONFromFile(filePath, {});
95 assert.equal("val1", content.key1);
96 done();
97 });
98
99 });
100
101});
\No newline at end of file