Skip to content

Commit 8a56d05

Browse files
committed
added tests and error handling
1 parent b47d3cd commit 8a56d05

File tree

2 files changed

+164
-3
lines changed

2 files changed

+164
-3
lines changed

index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,17 @@ SecureStore.prototype.save = function (postfix, key, data, cb) {
3737
assert(typeof key === 'string', 'no hash key specified');
3838
assert(typeof data !== 'undefined', 'no data to save provided');
3939

40-
var encData = dsCrypt.encrypt(JSON.stringify(data), this.secret);
40+
if (typeof data === 'object') {
41+
try {
42+
data = JSON.stringify(data);
43+
} catch (e) {
44+
throw new Error(e);
45+
}
46+
}
4147

42-
this.pool.hset(this.namespace + postfix, shasum(key), encData, function (err, reply) {
48+
data = dsCrypt.encrypt(data, this.secret);
49+
50+
this.pool.hset(this.namespace + postfix, shasum(key), data, function (err, reply) {
4351
cb(err, reply);
4452
});
4553
};
@@ -63,7 +71,20 @@ SecureStore.prototype.get = function (postfix, key, cb) {
6371
} else if (typeof reply !== 'string') {
6472
cb('record not found');
6573
} else {
66-
cb(null, JSON.parse(dsCrypt.decrypt(reply, self.secret)));
74+
75+
var data;
76+
77+
try {
78+
data = dsCrypt.decrypt(reply, self.secret);
79+
} catch (e) {
80+
return cb('unable to decrypt');
81+
}
82+
83+
try {
84+
data = JSON.parse(data);
85+
} catch (e) {}
86+
87+
cb(null, data);
6788
}
6889
});
6990
};

test/basics-suite.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
function getTests() {
2+
return [
3+
{
4+
desc: '# get something that does not exist',
5+
run: function (env, test) {
6+
env.mod.get('blahblah', function (err, data) {
7+
test.assertTypeAnd(data, 'undefined');
8+
test.assert(err, 'record not found');
9+
});
10+
}
11+
},
12+
13+
{
14+
desc: '# save string',
15+
run: function (env, test) {
16+
env.mod.save('foo', 'hallo', function (err, data) {
17+
test.assert(err, null);
18+
});
19+
}
20+
},
21+
22+
{
23+
desc: '# get string',
24+
run: function (env, test) {
25+
env.mod.get('foo', function (err, data) {
26+
test.assertAnd(err, null);
27+
test.assertTypeAnd(data, 'string');
28+
test.assert(data, 'hallo');
29+
});
30+
}
31+
},
32+
33+
{
34+
desc: '# save object',
35+
run: function (env, test) {
36+
env.mod.save('foo', { bar: 'baz', wang: 'bang' }, function (err, data) {
37+
test.assert(err, null);
38+
});
39+
}
40+
},
41+
42+
{
43+
desc: '# get object',
44+
run: function (env, test) {
45+
env.mod.get('foo', function (err, data) {
46+
test.assertAnd(err, null);
47+
test.assertTypeAnd(data, 'object');
48+
test.assert(data, { bar: 'baz', wang: 'bang' });
49+
});
50+
}
51+
},
52+
53+
{
54+
desc: '# new scope',
55+
run: function (env, test) {
56+
env.mod2 = new env.Mod({
57+
namespace: 'secure-store-redis-tests',
58+
secret: 'i dont know the key :(',
59+
redis: {
60+
host: '127.0.0.1',
61+
port: 6379
62+
}
63+
});
64+
test.assertTypeAnd(env.mod2, 'object');
65+
test.assertTypeAnd(env.mod2.get, 'function');
66+
test.assertType(env.mod2.save, 'function');
67+
}
68+
},
69+
70+
71+
{
72+
desc: '# get (wrong secret)',
73+
run: function (env, test) {
74+
env.mod2.get('foo', function (err, data) {
75+
test.assertAnd(err, 'unable to decrypt');
76+
test.assertType(data, 'undefined');
77+
});
78+
}
79+
},
80+
81+
{
82+
desc: '# save complex object',
83+
run: function (env, test) {
84+
env.complexObj = {
85+
foo: 'bar',
86+
bad: 'obj',
87+
this: true,
88+
me: {
89+
o: {
90+
me: {
91+
o: [true, false, true, null, 'this', 'that', true, 9]
92+
}
93+
}
94+
}
95+
};
96+
env.mod2.save('complex', env.complexObj, function (err) {
97+
test.assert(err, null);
98+
});
99+
}
100+
},
101+
102+
{
103+
desc: '# get complex object',
104+
run: function (env, test) {
105+
env.mod2.get('complex', function (err, data) {
106+
test.assertAnd(err, null);
107+
test.assertTypeAnd(data, 'object');
108+
test.assert(data, env.complexObj);
109+
});
110+
}
111+
},
112+
113+
];
114+
}
115+
116+
if (typeof define !== 'function') {
117+
var define = require('amdefine')(module);
118+
}
119+
define(['require'], function (require) {
120+
return [{
121+
desc: "basic tests",
122+
abortOnFail: true,
123+
setup: function (env, test) {
124+
env.Mod = require('./../index');
125+
test.assertTypeAnd(env.Mod, 'function');
126+
env.mod = new env.Mod({
127+
namespace: 'secure-store-redis-tests',
128+
secret: 'sssh dont say nuthin!',
129+
redis: {
130+
host: '127.0.0.1',
131+
port: 6379
132+
}
133+
});
134+
test.assertTypeAnd(env.mod, 'object');
135+
test.assertTypeAnd(env.mod.get, 'function');
136+
test.assertType(env.mod.save, 'function');
137+
},
138+
tests: getTests(),
139+
}];
140+
});

0 commit comments

Comments
 (0)