Skip to content

Commit 203a728

Browse files
ManthawarManthawar
authored andcommitted
added unit tests to test using certificate content instead of file path
1 parent 15e585e commit 203a728

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

test/field-level-crypto.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ const testConfigHeader = require("./mock/config-header");
88

99
const iv = "6f38f3ecd8b92c2fd2537a7235deb9a8";
1010
const secretKey = "bab78b5ec588274a4dd2a60834efcf60";
11+
const encryptionCertificateText = '-----BEGIN CERTIFICATE-----'+
12+
'MIIDITCCAgmgAwIBAgIJANLIazc8xI4iMA0GCSqGSIb3DQEBBQUAMCcxJTAjBgNV'+
13+
'BAMMHHd3dy5qZWFuLWFsZXhpcy1hdWZhdXZyZS5jb20wHhcNMTkwMjIxMDg1MTM1'+
14+
'WhcNMjkwMjE4MDg1MTM1WjAnMSUwIwYDVQQDDBx3d3cuamVhbi1hbGV4aXMtYXVm'+
15+
'YXV2cmUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9Mp6gEFp'+
16+
'9E+/1SS5XrUyYKMbE7eU0dyJCfmJPz8YOkOYV7ohqwXQvjlaP/YazZ6bbmYfa2WC'+
17+
'raOpW0o2BYijHgQ7z2a2Az87rKdAtCpZSKFW82Ijnsw++lx7EABI3tFF282ZV7LT'+
18+
'13n9m4th5Kldukk9euy+TuJqCvPu4xzE/NE+l4LFMr8rfD47EPQkrun5w/TXwkmJ'+
19+
'rdnG9ejl3BLQO06Ns6Bs516geiYZ7RYxtI8Xnu0ZC0fpqDqjCPZBTORkiFeLocEP'+
20+
'RbTgo1H+0xQFNdsMH1/0F1BI+hvdxlbc3+kHZFZFoeBMkR3jC8jDXOXNCMNWb13T'+
21+
'in6HqPReO0KW8wIDAQABo1AwTjAdBgNVHQ4EFgQUDtqNZacrC6wR53kCpw/BfG2C'+
22+
't3AwHwYDVR0jBBgwFoAUDtqNZacrC6wR53kCpw/BfG2Ct3AwDAYDVR0TBAUwAwEB'+
23+
'/zANBgkqhkiG9w0BAQUFAAOCAQEAJ09tz2BDzSgNOArYtF4lgRtjViKpV7gHVqtc'+
24+
'3xQT9ujbaxEgaZFPbf7/zYfWZfJggX9T54NTGqo5AXM0l/fz9AZ0bOm03rnF2I/F'+
25+
'/ewhSlHYzvKiPM+YaswaRo1M1UPPgKpLlRDMO0u5LYiU5ICgCNm13TWgjBlzLpP6'+
26+
'U4z2iBNq/RWBgYxypi/8NMYZ1RcCrAVSt3QnW6Gp+vW/HrE7KIlAp1gFdme3Xcx1'+
27+
'vDRpA+MeeEyrnc4UNIqT/4bHGkKlIMKdcjZgrFfEJVFav3eJ4CZ7ZSV6Bx+9yRCL'+
28+
'DPGlRJLISxgwsOTuUmLOxjotRxO8TdR5e1V+skEtfEctMuSVYA=='+
29+
'-----END CERTIFICATE-----'
1130

1231
describe("Field Level Crypto", () => {
1332
describe("#new Crypto", () => {
@@ -169,6 +188,33 @@ describe("Field Level Crypto", () => {
169188
/Config not valid: found multiple configurations encrypt\/decrypt with root mapping/
170189
);
171190
});
191+
192+
it("With useCertificateContent enabled, without valid encryption certificate content", () => {
193+
const config = JSON.parse(JSON.stringify(testConfig));
194+
config.useCertificateContent = true;
195+
assert.throws(
196+
() => new Crypto(config),
197+
/Invalid PEM formatted message./
198+
);
199+
});
200+
201+
it("With useCertificateContent enabled, with valid encryption certificate content and without private key", () => {
202+
const config = JSON.parse(JSON.stringify(testConfig));
203+
config.useCertificateContent = true;
204+
config.encryptionCertificate = encryptionCertificateText;
205+
delete config["privateKey"];
206+
assert.doesNotThrow(() => new Crypto(config));
207+
});
208+
209+
it("With useCertificateContent enabled, without encryptionCertificate", () => {
210+
const config = JSON.parse(JSON.stringify(testConfig));
211+
config.useCertificateContent = true;
212+
config.encryptionCertificate = null;
213+
assert.throws(
214+
() => new Crypto(config),
215+
/Config not valid: please check that all the properties are defined/
216+
);
217+
});
172218
});
173219

174220
describe("#encryptData()", () => {
@@ -238,6 +284,27 @@ describe("Field Level Crypto", () => {
238284
assert.ok(!resp.iv);
239285
assert.ok(resp.oaepHashingAlgorithm);
240286
});
287+
288+
it("with useCertificateContent", () => {
289+
const config = JSON.parse(JSON.stringify(testConfig));
290+
config.ivFieldName = 'IvCustomName';
291+
config.useCertificateContent = true;
292+
config.encryptionCertificate = encryptionCertificateText;
293+
delete config["privateKey"];
294+
const crypto = new Crypto(config);
295+
const data = JSON.stringify({ text: "message with custom ivFieldName" });
296+
const resp = crypto.encryptData({
297+
data: data,
298+
});
299+
assert.ok(resp);
300+
assert.ok(resp.encryptedKey);
301+
assert.ok(resp.encryptedData);
302+
assert.ok(resp.IvCustomName);
303+
assert.ok(!resp.iv);
304+
assert.ok(resp.oaepHashingAlgorithm);
305+
});
306+
307+
241308
});
242309

243310

0 commit comments

Comments
 (0)