|
| 1 | +import test, { Test } from "tape-promise/tape"; |
| 2 | + |
| 3 | +import { v4 as uuidv4 } from "uuid"; |
| 4 | +import { |
| 5 | + IPluginKeychainMemoryOptions, |
| 6 | + PluginKeychainMemory, |
| 7 | +} from "../../../main/typescript"; |
| 8 | + |
| 9 | +test("PluginKeychainMemory", (t1: Test) => { |
| 10 | + t1.doesNotThrow( |
| 11 | + () => new PluginKeychainMemory({ instanceId: "a", keychainId: "a" }) |
| 12 | + ); |
| 13 | + |
| 14 | + test("Validates constructor arg instanceId", (t: Test) => { |
| 15 | + t.throws( |
| 16 | + () => |
| 17 | + new PluginKeychainMemory({ |
| 18 | + instanceId: null as any, |
| 19 | + keychainId: "valid-value", |
| 20 | + }) |
| 21 | + ); |
| 22 | + t.throws( |
| 23 | + () => |
| 24 | + new PluginKeychainMemory({ |
| 25 | + instanceId: "", |
| 26 | + keychainId: "valid-value", |
| 27 | + }) |
| 28 | + ); |
| 29 | + t.end(); |
| 30 | + }); |
| 31 | + |
| 32 | + test("Validates constructor arg keychainId", (t: Test) => { |
| 33 | + t.throws( |
| 34 | + () => |
| 35 | + new PluginKeychainMemory({ |
| 36 | + instanceId: "valid-value", |
| 37 | + keychainId: null as any, |
| 38 | + }) |
| 39 | + ); |
| 40 | + t.throws( |
| 41 | + () => |
| 42 | + new PluginKeychainMemory({ |
| 43 | + instanceId: "valid-value", |
| 44 | + keychainId: "", |
| 45 | + }) |
| 46 | + ); |
| 47 | + t.end(); |
| 48 | + }); |
| 49 | + |
| 50 | + test("get,set,has,delete alters state as expected", async (t: Test) => { |
| 51 | + const options: IPluginKeychainMemoryOptions = { |
| 52 | + instanceId: uuidv4(), |
| 53 | + keychainId: uuidv4(), |
| 54 | + }; |
| 55 | + const plugin = new PluginKeychainMemory(options); |
| 56 | + t.equal(plugin.getKeychainId(), options.keychainId, "Keychain ID set OK"); |
| 57 | + t.equal(plugin.getInstanceId(), options.instanceId, "Instance ID set OK"); |
| 58 | + |
| 59 | + const key = uuidv4(); |
| 60 | + const value = uuidv4(); |
| 61 | + |
| 62 | + const hasPrior = await plugin.has(key); |
| 63 | + t.false(hasPrior, "hasPrior === false OK"); |
| 64 | + |
| 65 | + await plugin.set(key, value); |
| 66 | + |
| 67 | + const hasAfter = await plugin.has(key); |
| 68 | + t.true(hasAfter, "hasAfter === true OK"); |
| 69 | + |
| 70 | + const valueAfter = await plugin.get(key); |
| 71 | + t.ok(valueAfter, "valueAfter truthy OK"); |
| 72 | + t.equal(valueAfter, value, "valueAfter === value OK"); |
| 73 | + |
| 74 | + await plugin.delete(key); |
| 75 | + |
| 76 | + const hasAfterDelete = await plugin.has(key); |
| 77 | + t.false(hasAfterDelete, "hasAfterDelete === false OK"); |
| 78 | + |
| 79 | + const valueAfterDelete = await plugin.get(key); |
| 80 | + t.notok(valueAfterDelete, "valueAfterDelete falsy OK"); |
| 81 | + |
| 82 | + t.end(); |
| 83 | + }); |
| 84 | + |
| 85 | + test("rotateEncryptionKeys() fails fast", async (t: Test) => { |
| 86 | + const options: IPluginKeychainMemoryOptions = { |
| 87 | + instanceId: uuidv4(), |
| 88 | + keychainId: uuidv4(), |
| 89 | + }; |
| 90 | + const plugin = new PluginKeychainMemory(options); |
| 91 | + |
| 92 | + const promise = plugin.rotateEncryptionKeys(); |
| 93 | + const expected = /not implemented/; |
| 94 | + await t.rejects(promise, expected, "rotateEncryptionKeys() rejects OK"); |
| 95 | + |
| 96 | + t.end(); |
| 97 | + }); |
| 98 | + |
| 99 | + test("getEncryptionAlgorithm() returns null", (t: Test) => { |
| 100 | + const options: IPluginKeychainMemoryOptions = { |
| 101 | + instanceId: uuidv4(), |
| 102 | + keychainId: uuidv4(), |
| 103 | + }; |
| 104 | + const plugin = new PluginKeychainMemory(options); |
| 105 | + |
| 106 | + t.notok(plugin.getEncryptionAlgorithm(), "encryption algorithm falsy OK"); |
| 107 | + |
| 108 | + t.end(); |
| 109 | + }); |
| 110 | + |
| 111 | + t1.end(); |
| 112 | +}); |
0 commit comments