Skip to content

Commit 0358545

Browse files
committed
[test api] Make the format capable of sub-objects.
1 parent 04c0f3a commit 0358545

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/nconf/stores/file.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,14 @@ File.prototype.stringify = function () {
168168
if (this.secure) {
169169
data = Object.keys(data).reduce(function (acc, key) {
170170
var value = self.format.stringify(data[key]);
171-
acc[key] = cipherConvert(value, {
171+
acc[key] = {
172172
alg: self.secure.alg,
173-
secret: self.secure.secret,
174-
encs: { input: 'utf8', output: 'hex' }
175-
});
173+
value: cipherConvert(value, {
174+
alg: self.secure.alg,
175+
secret: self.secure.secret,
176+
encs: { input: 'utf8', output: 'hex' }
177+
})
178+
}
176179

177180
return acc;
178181
}, {});
@@ -195,8 +198,8 @@ File.prototype.parse = function (contents) {
195198
}
196199

197200
return Object.keys(parsed).reduce(function (acc, key) {
198-
var decrypted = cipherConvert(parsed[key], {
199-
alg: self.secure.alg,
201+
var decrypted = cipherConvert(parsed[key].value, {
202+
alg: parsed[key].alg || self.secure.alg,
200203
secret: self.secure.secret,
201204
encs: { input: 'hex', output: 'utf8' }
202205
});

test/stores/file-store-test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ vows.describe('nconf/stores/file').addBatch({
226226
"When using the nconf file store": {
227227
topic: function () {
228228
var secureStore = new nconf.File({
229-
file: 'mock-file-path.json',
229+
file: path.join(__dirname, '..', 'fixtures', 'secure.json'),
230230
secure: 'super-secretzzz'
231231
});
232232

@@ -236,13 +236,25 @@ vows.describe('nconf/stores/file').addBatch({
236236
"the stringify() method should encrypt properly": function (store) {
237237
var contents = JSON.parse(store.stringify());
238238
Object.keys(data).forEach(function (key) {
239-
assert.isString(contents[key]);
239+
assert.isObject(contents[key]);
240+
assert.isString(contents[key].value);
241+
assert.equal(contents[key].alg, 'aes-256-ctr');
240242
});
241243
},
242244
"the parse() method should decrypt properly": function (store) {
243245
var contents = store.stringify();
244246
var parsed = store.parse(contents);
245247
assert.deepEqual(parsed, data);
248+
},
249+
"the load() method should decrypt properly": function (store) {
250+
store.load(function (err, loaded) {
251+
assert.isNull(err);
252+
assert.deepEqual(loaded, data);
253+
});
254+
},
255+
"the loadSync() method should decrypt properly": function (store) {
256+
var loaded = store.loadSync()
257+
assert.deepEqual(loaded, data);
246258
}
247259
}
248260
}).export(module);

0 commit comments

Comments
 (0)