Skip to content

Commit de14eed

Browse files
committed
Add decrypt smoke test
1 parent b3d7894 commit de14eed

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

code/post-encrypt/cipher.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
const config = require("../config");
3+
const crypto = require("crypto");
4+
5+
function encrypt(clear) {
6+
const cipher = crypto.createCipher("aes192", config.HL_SECRET1);
7+
return cipher.update(clear, "utf8", "base64") + cipher.final("base64");
8+
}
9+
10+
function decrypt(encrypted) {
11+
const decipher = crypto.createDecipher("aes192", config.HL_SECRET1);
12+
return decipher.update(encrypted, "base64", "utf8") + decipher.final("utf8");
13+
}
14+
15+
module.exports = {encrypt, decrypt};

code/post-encrypt/lambda.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
"use strict";
2+
const cipher = require("./cipher");
23
const config = require("../config");
3-
const crypto = require("crypto");
44
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-
138
function postEncrypt(call, res, next) {
149
console.log("Encrypting payload");
15-
const encrypted = encrypt(JSON.stringify(call.event.body));
10+
const encrypted = cipher.encrypt(JSON.stringify(call.event.body));
1611
call.body = {encrypted};
1712
next();
1813
}

code/post-encrypt/smoke-tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use strict";
2+
const cipher = require("./cipher");
23
const request = require("request");
34
const tap = require("tap");
45

@@ -17,6 +18,8 @@ tap.test("post-encrypt base case", test => {
1718
test.match(res.statusCode, 200);
1819
test.ok(body.encrypted);
1920
test.same(typeof body.encrypted, "string");
21+
const payload = JSON.parse(cipher.decrypt(body.encrypted));
22+
test.same(payload, {cat: "cat", dog: "dog"});
2023
test.end();
2124
}
2225
);

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22
const {handler} = require("./lambda");
3-
const config = require("../config");
4-
const crypto = require("crypto");
3+
const cipher = require("./cipher");
54
const eventSchema = require("./event-schema");
65
const set = require("lodash.set");
76
const tap = require("tap");
@@ -14,11 +13,6 @@ function mockEvent(path, value) {
1413
return event;
1514
}
1615

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-
2216
const invalids = [
2317
["body", 0], // not string
2418
["body", false], // not string
@@ -45,7 +39,7 @@ tap.test("handler should work in base case", test => {
4539
const body = JSON.parse(res.body);
4640
test.ok(body.encrypted);
4741
test.same(typeof body.encrypted, "string");
48-
const payloadJson = decrypt(body.encrypted);
42+
const payloadJson = cipher.decrypt(body.encrypted);
4943
const payload = JSON.parse(payloadJson);
5044
test.match({foo: "FOO", bar: "BAR"}, payload);
5145
test.end();

0 commit comments

Comments
 (0)