From 0358545ae596dfef75bf7f02b051fc9413861fbf Mon Sep 17 00:00:00 2001 From: indexzero Date: Sun, 20 Sep 2015 00:26:34 -0700 Subject: [PATCH] [test api] Make the format capable of sub-objects. --- lib/nconf/stores/file.js | 15 +++++++++------ test/stores/file-store-test.js | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index d3de098..07e5f93 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -168,11 +168,14 @@ File.prototype.stringify = function () { if (this.secure) { data = Object.keys(data).reduce(function (acc, key) { var value = self.format.stringify(data[key]); - acc[key] = cipherConvert(value, { + acc[key] = { alg: self.secure.alg, - secret: self.secure.secret, - encs: { input: 'utf8', output: 'hex' } - }); + value: cipherConvert(value, { + alg: self.secure.alg, + secret: self.secure.secret, + encs: { input: 'utf8', output: 'hex' } + }) + } return acc; }, {}); @@ -195,8 +198,8 @@ File.prototype.parse = function (contents) { } return Object.keys(parsed).reduce(function (acc, key) { - var decrypted = cipherConvert(parsed[key], { - alg: self.secure.alg, + var decrypted = cipherConvert(parsed[key].value, { + alg: parsed[key].alg || self.secure.alg, secret: self.secure.secret, encs: { input: 'hex', output: 'utf8' } }); diff --git a/test/stores/file-store-test.js b/test/stores/file-store-test.js index 6ce9818..b085442 100644 --- a/test/stores/file-store-test.js +++ b/test/stores/file-store-test.js @@ -226,7 +226,7 @@ vows.describe('nconf/stores/file').addBatch({ "When using the nconf file store": { topic: function () { var secureStore = new nconf.File({ - file: 'mock-file-path.json', + file: path.join(__dirname, '..', 'fixtures', 'secure.json'), secure: 'super-secretzzz' }); @@ -236,13 +236,25 @@ vows.describe('nconf/stores/file').addBatch({ "the stringify() method should encrypt properly": function (store) { var contents = JSON.parse(store.stringify()); Object.keys(data).forEach(function (key) { - assert.isString(contents[key]); + assert.isObject(contents[key]); + assert.isString(contents[key].value); + assert.equal(contents[key].alg, 'aes-256-ctr'); }); }, "the parse() method should decrypt properly": function (store) { var contents = store.stringify(); var parsed = store.parse(contents); assert.deepEqual(parsed, data); + }, + "the load() method should decrypt properly": function (store) { + store.load(function (err, loaded) { + assert.isNull(err); + assert.deepEqual(loaded, data); + }); + }, + "the loadSync() method should decrypt properly": function (store) { + var loaded = store.loadSync() + assert.deepEqual(loaded, data); } } }).export(module);