From 5480b5bb9c55090f4db2d68398a8e9ee48aabcdc Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Thu, 12 Nov 2020 12:38:29 -0800 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20plugin-keychain-me?= =?UTF-8?q?mory=20unit=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Peter Somogyvari --- .../unit/plugin-keychain-memory.test.ts | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts diff --git a/packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts b/packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts new file mode 100644 index 0000000000..5844578d15 --- /dev/null +++ b/packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts @@ -0,0 +1,112 @@ +import test, { Test } from "tape-promise/tape"; + +import { v4 as uuidv4 } from "uuid"; +import { + IPluginKeychainMemoryOptions, + PluginKeychainMemory, +} from "../../../main/typescript"; + +test("PluginKeychainMemory", (t1: Test) => { + t1.doesNotThrow( + () => new PluginKeychainMemory({ instanceId: "a", keychainId: "a" }) + ); + + test("Validates constructor arg instanceId", (t: Test) => { + t.throws( + () => + new PluginKeychainMemory({ + instanceId: null as any, + keychainId: "valid-value", + }) + ); + t.throws( + () => + new PluginKeychainMemory({ + instanceId: "", + keychainId: "valid-value", + }) + ); + t.end(); + }); + + test("Validates constructor arg keychainId", (t: Test) => { + t.throws( + () => + new PluginKeychainMemory({ + instanceId: "valid-value", + keychainId: null as any, + }) + ); + t.throws( + () => + new PluginKeychainMemory({ + instanceId: "valid-value", + keychainId: "", + }) + ); + t.end(); + }); + + test("get,set,has,delete alters state as expected", async (t: Test) => { + const options: IPluginKeychainMemoryOptions = { + instanceId: uuidv4(), + keychainId: uuidv4(), + }; + const plugin = new PluginKeychainMemory(options); + t.equal(plugin.getKeychainId(), options.keychainId, "Keychain ID set OK"); + t.equal(plugin.getInstanceId(), options.instanceId, "Instance ID set OK"); + + const key = uuidv4(); + const value = uuidv4(); + + const hasPrior = await plugin.has(key); + t.false(hasPrior, "hasPrior === false OK"); + + await plugin.set(key, value); + + const hasAfter = await plugin.has(key); + t.true(hasAfter, "hasAfter === true OK"); + + const valueAfter = await plugin.get(key); + t.ok(valueAfter, "valueAfter truthy OK"); + t.equal(valueAfter, value, "valueAfter === value OK"); + + await plugin.delete(key); + + const hasAfterDelete = await plugin.has(key); + t.false(hasAfterDelete, "hasAfterDelete === false OK"); + + const valueAfterDelete = await plugin.get(key); + t.notok(valueAfterDelete, "valueAfterDelete falsy OK"); + + t.end(); + }); + + test("rotateEncryptionKeys() fails fast", async (t: Test) => { + const options: IPluginKeychainMemoryOptions = { + instanceId: uuidv4(), + keychainId: uuidv4(), + }; + const plugin = new PluginKeychainMemory(options); + + const promise = plugin.rotateEncryptionKeys(); + const expected = /not implemented/; + await t.rejects(promise, expected, "rotateEncryptionKeys() rejects OK"); + + t.end(); + }); + + test("getEncryptionAlgorithm() returns null", (t: Test) => { + const options: IPluginKeychainMemoryOptions = { + instanceId: uuidv4(), + keychainId: uuidv4(), + }; + const plugin = new PluginKeychainMemory(options); + + t.notok(plugin.getEncryptionAlgorithm(), "encryption algorithm falsy OK"); + + t.end(); + }); + + t1.end(); +});