Skip to content

Commit 7209394

Browse files
committed
added unit tests
Signed-off-by: Rocco, Marco <[email protected]>
1 parent 84b9b94 commit 7209394

File tree

8 files changed

+158
-26
lines changed

8 files changed

+158
-26
lines changed

test/field-level-encryption.test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ describe("Field Level Encryption", () => {
109109
}
110110
);
111111
assert.ok(res.header === null);
112+
assert.ok(!res.body.path.to.encryptedData.sensitive);
113+
assert.ok(!res.body.path.to.encryptedData.sensitive2);
112114
assert.ok(res.body.path);
113115
assert.ok(res.body.path.to);
114116
assert.ok(res.body.path.to.encryptedData);
@@ -163,7 +165,9 @@ describe("Field Level Encryption", () => {
163165
it("decrypt response", () => {
164166
let response = require("./mock/response");
165167
let res = decrypt.call(fle, response);
166-
assert.ok(res.elem1.encryptedData.accountNumber === "5123456789012345");
168+
assert.ok(res.foo.accountNumber === "5123456789012345");
169+
assert.ok(!res.foo.elem1);
170+
assert.ok(!res.foo.hasOwnProperty('encryptedData'));
167171
});
168172

169173
it("decrypt response with readme config", () => {
@@ -175,9 +179,8 @@ describe("Field Level Encryption", () => {
175179
assert(res.path);
176180
assert(res.path.to);
177181
assert(res.path.to.foo);
178-
assert(res.path.to.foo.encryptedData);
179-
assert(res.path.to.foo.encryptedData.sensitive);
180-
assert(res.path.to.foo.encryptedData.sensitive2);
182+
assert(res.path.to.foo.sensitive);
183+
assert(res.path.to.foo.sensitive2);
181184
});
182185

183186
it("decrypt response with no valid config", () => {
@@ -189,12 +192,13 @@ describe("Field Level Encryption", () => {
189192

190193
it("decrypt response replacing whole body", () => {
191194
let config = JSON.parse(JSON.stringify(testConfig));
192-
config.paths[0].toDecrypt[0].obj = "";
193-
config.paths[0].toDecrypt[0].element = "encryptedData";
195+
config.paths[0].toDecrypt[0].element = "";
196+
config.paths[0].toDecrypt[0].obj = "encryptedData";
194197
let fle = new FieldLevelEncryption(config);
195198
let response = require("./mock/response-root");
196199
let res = decrypt.call(fle, response);
197200
assert.ok(res.encryptedData.accountNumber === "5123456789012345");
201+
assert.ok(res.notDelete);
198202
});
199203

200204
it("decrypt with header", () => {

test/mock/config-header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = {
99
}],
1010
toDecrypt: [
1111
{
12-
element: "",
13-
obj: "encrypted_payload"
12+
element: "encrypted_payload",
13+
obj: ""
1414
}
1515
]
1616
}

test/mock/config-readme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
}],
1010
toDecrypt: [
1111
{
12-
element: "path.to.encryptedFoo.encryptedData",
12+
element: "path.to.encryptedFoo",
1313
obj: "path.to.foo"
1414
}
1515
]

test/mock/config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = {
99
}],
1010
toDecrypt: [
1111
{
12-
element: "elem1.encryptedData",
13-
obj: "elem1"
12+
element: "foo.elem1",
13+
obj: "foo"
1414
}
1515
]
1616
},
@@ -23,8 +23,8 @@ module.exports = {
2323
}],
2424
toDecrypt: [
2525
{
26-
element: "elem2.encryptedData",
27-
obj: "elem2"
26+
element: "foo.elem1",
27+
obj: "foo"
2828
}
2929
]
3030
}

test/mock/response-root.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
'd4714161898b8bc5c54a63f71ae7c7a40734e4f7c7e27d121ac5e85a3fa47946aa3546027abe0874d751d5ae701491a7f572fc30fa08dd671d358746ffe8709cba36010f97864105b175c51b6f32d36d981287698a3f6f8707aedf980cce19bfe7c5286ddba87b7f3e5abbfa88a980779037c0b7902d340d73201cf3f0b546c2ad9f54e4b71a43504da947a3cb7af54d61717624e636a90069be3c46c19b9ae8b76794321b877544dd03f0ca816288672ef361c3e8f14d4a1ee96ba72d21e3a36c020aa174635a8579b0e9af761d96437e1fa167f00888ff2532292e7a220f5bc948f8159dea2541b8c6df6463213de292b4485076241c90706efad93f9b98ea',
88
publicKeyFingerprint:
99
'80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279',
10-
oaepHashingAlgorithm: 'SHA512'
10+
oaepHashingAlgorithm: 'SHA512',
11+
notDelete: "this field should be deleted"
1112
}
1213
};

test/mock/response.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
module.exports = {
22
request: {url: "/resource"}, body: {
3-
elem1: {
4-
encryptedData:
5-
'3097e36bf8b71637a0273abe69c23752d6157464ce49f6f35120d28bedfb63a1f2c8087be3a3bc9775592db41db87a8c',
6-
iv: '22507f596fffb45b15244356981d7ea1',
7-
encryptedKey:
8-
'd4714161898b8bc5c54a63f71ae7c7a40734e4f7c7e27d121ac5e85a3fa47946aa3546027abe0874d751d5ae701491a7f572fc30fa08dd671d358746ffe8709cba36010f97864105b175c51b6f32d36d981287698a3f6f8707aedf980cce19bfe7c5286ddba87b7f3e5abbfa88a980779037c0b7902d340d73201cf3f0b546c2ad9f54e4b71a43504da947a3cb7af54d61717624e636a90069be3c46c19b9ae8b76794321b877544dd03f0ca816288672ef361c3e8f14d4a1ee96ba72d21e3a36c020aa174635a8579b0e9af761d96437e1fa167f00888ff2532292e7a220f5bc948f8159dea2541b8c6df6463213de292b4485076241c90706efad93f9b98ea',
9-
publicKeyFingerprint:
10-
'80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279',
11-
oaepHashingAlgorithm: 'SHA512'
3+
foo: {
4+
elem1: {
5+
encryptedData:
6+
'3097e36bf8b71637a0273abe69c23752d6157464ce49f6f35120d28bedfb63a1f2c8087be3a3bc9775592db41db87a8c',
7+
iv: '22507f596fffb45b15244356981d7ea1',
8+
encryptedKey:
9+
'd4714161898b8bc5c54a63f71ae7c7a40734e4f7c7e27d121ac5e85a3fa47946aa3546027abe0874d751d5ae701491a7f572fc30fa08dd671d358746ffe8709cba36010f97864105b175c51b6f32d36d981287698a3f6f8707aedf980cce19bfe7c5286ddba87b7f3e5abbfa88a980779037c0b7902d340d73201cf3f0b546c2ad9f54e4b71a43504da947a3cb7af54d61717624e636a90069be3c46c19b9ae8b76794321b877544dd03f0ca816288672ef361c3e8f14d4a1ee96ba72d21e3a36c020aa174635a8579b0e9af761d96437e1fa167f00888ff2532292e7a220f5bc948f8159dea2541b8c6df6463213de292b4485076241c90706efad93f9b98ea',
10+
publicKeyFingerprint:
11+
'80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279',
12+
oaepHashingAlgorithm: 'SHA512'
13+
}
1214
}
1315
}
1416
};

test/payload-encryption.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe("Payload encryption", () => {
1010
config['encryptedValueFieldName'] = "encryptedValue";
1111
let fle = new FieldLevelEncryption(config);
1212
let encryptBody = FieldLevelEncryption.__get__("encryptBody");
13+
let decryptBody = FieldLevelEncryption.__get__("decryptBody");
1314

1415
describe("#encryptBody", () => {
1516

@@ -57,6 +58,90 @@ describe("Payload encryption", () => {
5758
assert.ok(body.encryptedItems.oaepHashingAlgorithm);
5859
});
5960

61+
it("nested object to encrypt", () => {
62+
let body = {
63+
path: {
64+
to: {
65+
encryptedData: {
66+
sensitive: "secret",
67+
sensitive2: "secret 2"
68+
}
69+
}
70+
}
71+
};
72+
encryptBody.call(fle, {
73+
element: "path.to.encryptedData",
74+
obj: "path.to"
75+
}, body);
76+
assert.ok(body.path);
77+
assert.ok(body.path.to);
78+
assert.ok(body.path.to.encryptedValue);
79+
assert.ok(body.path.to.iv);
80+
assert.ok(body.path.to.encryptedKey);
81+
assert.ok(body.path.to.publicKeyFingerprint);
82+
assert.ok(body.path.to.oaepHashingAlgorithm);
83+
assert.ok(!body.path.to.encryptedData);
84+
});
85+
86+
it("nested object, create different nested object and delete it", () => {
87+
let body = {
88+
path: {
89+
to: {
90+
foo: {
91+
sensitive: "secret",
92+
sensitive2: "secret 2"
93+
}
94+
}
95+
}
96+
};
97+
encryptBody.call(fle, {
98+
element: "path.to.foo",
99+
obj: "path.to.encryptedFoo"
100+
}, body);
101+
assert.ok(!body.path.to.foo);
102+
assert.ok(body);
103+
assert.ok(body.path);
104+
assert.ok(body.path.to);
105+
assert.ok(body.path.to.encryptedFoo);
106+
assert.ok(body.path.to.encryptedFoo);
107+
assert.ok(body.path.to.encryptedFoo.iv);
108+
assert.ok(body.path.to.encryptedFoo.encryptedValue);
109+
assert.ok(body.path.to.encryptedFoo.encryptedKey);
110+
assert.ok(body.path.to.encryptedFoo.publicKeyFingerprint);
111+
assert.ok(body.path.to.encryptedFoo.oaepHashingAlgorithm);
112+
});
113+
114+
});
115+
116+
describe("#decryptBody", () => {
117+
it("nested properties, create new obj", () => {
118+
let body = {
119+
path: {
120+
to: {
121+
encryptedFoo: {
122+
encryptedValue:
123+
'3097e36bf8b71637a0273abe69c23752d6157464ce49f6f35120d28bedfb63a1f2c8087be3a3bc9775592db41db87a8c',
124+
iv: '22507f596fffb45b15244356981d7ea1',
125+
encryptedKey:
126+
'd4714161898b8bc5c54a63f71ae7c7a40734e4f7c7e27d121ac5e85a3fa47946aa3546027abe0874d751d5ae701491a7f572fc30fa08dd671d358746ffe8709cba36010f97864105b175c51b6f32d36d981287698a3f6f8707aedf980cce19bfe7c5286ddba87b7f3e5abbfa88a980779037c0b7902d340d73201cf3f0b546c2ad9f54e4b71a43504da947a3cb7af54d61717624e636a90069be3c46c19b9ae8b76794321b877544dd03f0ca816288672ef361c3e8f14d4a1ee96ba72d21e3a36c020aa174635a8579b0e9af761d96437e1fa167f00888ff2532292e7a220f5bc948f8159dea2541b8c6df6463213de292b4485076241c90706efad93f9b98ea',
127+
publicKeyFingerprint:
128+
'80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279',
129+
oaepHashingAlgorithm: 'SHA512'
130+
}
131+
}
132+
}
133+
};
134+
decryptBody.call(fle, {
135+
element: "path.to.encryptedFoo",
136+
obj: "path.to.foo"
137+
}, body);
138+
assert.ok(!body.path.to.encryptedFoo);
139+
assert.ok(body);
140+
assert.ok(body.path);
141+
assert.ok(body.path.to);
142+
assert.ok(body.path.to.foo);
143+
assert.ok(body.path.to.foo.accountNumber === "5123456789012345");
144+
});
60145
});
61146

62147
});

test/utils.test.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe("Utils", () => {
168168
assert.ok(obj.first.second.third === "changed");
169169
});
170170

171-
it("change object value, delete it", () => {
171+
it("change object value, change it but not delete", () => {
172172
let obj = {
173173
first: {
174174
second: {
@@ -179,8 +179,8 @@ describe("Utils", () => {
179179
}
180180
};
181181
let path = "first.second.third";
182-
utils.mutateObjectProperty(path, "changed", obj, "third");
183-
assert.ok(!obj.first.second.third);
182+
utils.mutateObjectProperty(path, "changed", obj);
183+
assert.ok(obj.first.second.third === "changed");
184184
});
185185

186186
it("field not found, create it", () => {
@@ -334,5 +334,45 @@ describe("Utils", () => {
334334

335335
});
336336

337+
describe("#deleteNode", () => {
338+
339+
it("with nulls", () => {
340+
assert.doesNotThrow(() => {
341+
utils.deleteNode(null, null, null);
342+
utils.deleteNode("path.to.foo", null, null);
343+
let body = {};
344+
utils.deleteNode("path.to.foo", body);
345+
assert.ok(JSON.stringify(body) === JSON.stringify({}));
346+
});
347+
});
348+
349+
it("not found path, shouldn't remove it", () => {
350+
let body = {path: {to: {foo: {field: "value"}}}};
351+
let str = JSON.stringify(body);
352+
utils.deleteNode("path.to.notfound", body, null);
353+
assert.ok(str === JSON.stringify(body));
354+
});
355+
356+
it("path found, should remove it", () => {
357+
let body = {path: {to: {foo: {field: "value"}}}};
358+
utils.deleteNode("path.to.foo", body, null);
359+
assert.ok(JSON.stringify({path: {to: {}}}) === JSON.stringify(body));
360+
});
361+
362+
it("root path, without properties, shouldn't remove", () => {
363+
let body = {path: {to: {foo: {field: "value"}}}};
364+
let str = JSON.stringify(body);
365+
utils.deleteNode("", body, null);
366+
assert.ok(str === JSON.stringify(body));
367+
});
368+
369+
it("root path, with properties, should remove the properties", () => {
370+
let body = {path: {to: {foo: {field: "value"}}}, prop: "prop", prop2: "prop2"};
371+
utils.deleteNode("", body, ["prop", "prop2"]);
372+
assert.ok(JSON.stringify({"path": {"to": {"foo": {"field": "value"}}}}) === JSON.stringify(body));
373+
});
374+
375+
});
376+
337377

338378
});

0 commit comments

Comments
 (0)