Skip to content

Commit 033bffe

Browse files
committed
Add unit test decryption for post-encrypt
1 parent ee23860 commit 033bffe

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

code/post-encrypt/lambda.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ const lambdaUtils = require("../lambda-utils");
55

66
const console = lambdaUtils.console;
77

8+
function encrypt(clear) {
9+
const cipher = crypto.createCipher("aes192", config.HL_SECRET1);
10+
return cipher.update(clear, "utf8", "base64") + cipher.final("base64");
11+
}
12+
813
function postEncrypt(call, res, next) {
914
console.log("Encrypting payload");
10-
const cipher = crypto.createCipher("aes192", process.env.HL_SECRET1);
11-
cipher.update(JSON.stringify(call.event.body));
12-
call.body = {encrypted: cipher.final("base64")};
15+
const encrypted = encrypt(JSON.stringify(call.event.body));
16+
call.body = {encrypted};
1317
next();
1418
}
1519

code/post-encrypt/unit-tests-tap.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict";
22
const {handler} = require("./lambda");
3+
const config = require("../config");
4+
const crypto = require("crypto");
35
const eventSchema = require("./event-schema");
46
const set = require("lodash.set");
57
const tap = require("tap");
@@ -12,6 +14,11 @@ function mockEvent(path, value) {
1214
return event;
1315
}
1416

17+
function decrypt(encrypted) {
18+
const decipher = crypto.createDecipher("aes192", config.HL_SECRET1);
19+
return decipher.update(encrypted, "base64", "utf8") + decipher.final("utf8");
20+
}
21+
1522
const invalids = [
1623
["body", 0], // not string
1724
["body", false], // not string
@@ -38,6 +45,9 @@ tap.test("handler should work in base case", test => {
3845
const body = JSON.parse(res.body);
3946
test.ok(body.encrypted);
4047
test.same(typeof body.encrypted, "string");
48+
const payloadJson = decrypt(body.encrypted);
49+
const payload = JSON.parse(payloadJson);
50+
test.match({foo: "FOO", bar: "BAR"}, payload);
4151
test.end();
4252
});
4353
});

0 commit comments

Comments
 (0)