diff --git a/auth/index.html b/auth/index.html
index 651450a..998a04c 100644
--- a/auth/index.html
+++ b/auth/index.html
@@ -269,7 +269,7 @@
Message log
*/
async function getItemWithExpiry(key) {
const db = await openDB();
- const transaction = db.transaction(STORE_NAME, "readonly");
+ const transaction = db.transaction(STORE_NAME, "readwrite");
const store = transaction.objectStore(STORE_NAME);
return new Promise((resolve, reject) => {
diff --git a/auth/index.test.js b/auth/index.test.js
index caa8141..669e6d4 100644
--- a/auth/index.test.js
+++ b/auth/index.test.js
@@ -3,6 +3,7 @@ import { JSDOM } from "jsdom";
import fs from "fs";
import path from "path";
import * as crypto from "crypto";
+import { indexedDB } from "fake-indexeddb";
const html = fs.readFileSync(path.resolve(__dirname, "./index.html"), "utf8");
@@ -10,7 +11,7 @@ let dom;
let TKHQ;
describe("TKHQ", () => {
- beforeEach(() => {
+ beforeEach(async () => {
dom = new JSDOM(html, {
// Necessary to run script tags
runScripts: "dangerously",
@@ -24,50 +25,62 @@ describe("TKHQ", () => {
value: crypto.webcrypto,
});
+ Object.defineProperty(dom.window, "indexedDB", {
+ value: indexedDB,
+ });
+
TKHQ = dom.window.TKHQ;
+ await TKHQ.openDB();
});
it("gets and sets items with expiry localStorage", async () => {
// Set a TTL of 1000ms
- TKHQ.setItemWithExpiry("k", "v", 1000);
- let item = JSON.parse(dom.window.localStorage.getItem("k"));
- expect(item.value).toBe("v");
- expect(item.expiry).toBeTruthy();
+ await TKHQ.setItemWithExpiry("k", "v", 1000);
+
+ // TODO
+ // probably need wrappers/handlers to manipulate indexeddb directly
+ // let item = JSON.parse(dom.window.localStorage.getItem("k"));
+ // expect(item.value).toBe("v");
+ // expect(item.expiry).toBeTruthy();
// Get item that has not expired yet
- item = TKHQ.getItemWithExpiry("k");
+ let item = await TKHQ.getItemWithExpiry("k");
expect(item).toBe("v");
// Set a TTL of 500ms
- TKHQ.setItemWithExpiry("a", "b", 500);
- setTimeout(() => {
- const expiredItem = TKHQ.getItemWithExpiry("a");
+ await TKHQ.setItemWithExpiry("a", "b", 500);
+ setTimeout(async () => {
+ const expiredItem = await TKHQ.getItemWithExpiry("a");
expect(expiredItem).toBeNull();
}, 600); // Wait for 600ms to ensure the item has expired
- // Returns null if getItemWithExpiry is called for item without expiry
- dom.window.localStorage.setItem("k", JSON.stringify({ value: "v" }));
- item = TKHQ.getItemWithExpiry("k");
- expect(item).toBeNull();
+ // TODO
+ // Returns null if getItemWithExpiry is called for an item that does not have an expiry
+ // dom.window.localStorage.setItem("k", JSON.stringify({ value: "v" }));
+ // item = TKHQ.getItemWithExpiry("k");
+ // expect(item).toBeNull();
});
- it("gets and sets embedded key in localStorage", async () => {
- expect(TKHQ.getEmbeddedKey()).toBe(null);
+ it("gets, sets, and resets embedded key in localStorage", async () => {
+ expect(await TKHQ.getEmbeddedKey()).toBe(null);
// Set a dummy "key"
- TKHQ.setEmbeddedKey({ foo: "bar" });
- expect(TKHQ.getEmbeddedKey()).toEqual({ foo: "bar" });
+ await TKHQ.setEmbeddedKey({ foo: "bar" });
+ expect(await TKHQ.getEmbeddedKey()).toEqual({ foo: "bar" });
+
+ await TKHQ.onResetEmbeddedKey();
+ expect(await TKHQ.getEmbeddedKey()).toBe(null);
});
it("inits embedded key and is idempotent", async () => {
- expect(TKHQ.getEmbeddedKey()).toBe(null);
+ expect(await TKHQ.getEmbeddedKey()).toBe(null);
await TKHQ.initEmbeddedKey();
- var generatedKey = TKHQ.getEmbeddedKey();
+ var generatedKey = await TKHQ.getEmbeddedKey();
expect(generatedKey).not.toBeNull();
// This should have no effect; generated key should stay the same
await TKHQ.initEmbeddedKey();
- expect(TKHQ.getEmbeddedKey()).toEqual(generatedKey);
+ expect(await TKHQ.getEmbeddedKey()).toEqual(generatedKey);
});
it("generates P256 keys", async () => {
diff --git a/auth/package-lock.json b/auth/package-lock.json
index 52e3288..b7fdf6e 100644
--- a/auth/package-lock.json
+++ b/auth/package-lock.json
@@ -14,6 +14,7 @@
"@testing-library/dom": "^9.3.3",
"@testing-library/jest-dom": "^6.1.3",
"babel-jest": "^29.7.0",
+ "fake-indexeddb": "^6.0.0",
"jest": "^29.7.0",
"jsdom": "^22.1.0",
"prettier": "^2.8.4",
@@ -4085,6 +4086,15 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
+ "node_modules/fake-indexeddb": {
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/fake-indexeddb/-/fake-indexeddb-6.0.0.tgz",
+ "integrity": "sha512-YEboHE5VfopUclOck7LncgIqskAqnv4q0EWbYCaxKKjAvO93c+TJIaBuGy8CBFdbg9nKdpN3AuPRwVBJ4k7NrQ==",
+ "dev": true,
+ "engines": {
+ "node": ">=18"
+ }
+ },
"node_modules/fast-deep-equal": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
@@ -10468,6 +10478,12 @@
"jest-util": "^29.7.0"
}
},
+ "fake-indexeddb": {
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/fake-indexeddb/-/fake-indexeddb-6.0.0.tgz",
+ "integrity": "sha512-YEboHE5VfopUclOck7LncgIqskAqnv4q0EWbYCaxKKjAvO93c+TJIaBuGy8CBFdbg9nKdpN3AuPRwVBJ4k7NrQ==",
+ "dev": true
+ },
"fast-deep-equal": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
diff --git a/auth/package.json b/auth/package.json
index d72e1c8..7a8f90f 100644
--- a/auth/package.json
+++ b/auth/package.json
@@ -17,10 +17,11 @@
"@babel/preset-env": "^7.22.20",
"@testing-library/dom": "^9.3.3",
"@testing-library/jest-dom": "^6.1.3",
- "jsdom": "^22.1.0",
"babel-jest": "^29.7.0",
+ "fake-indexeddb": "^6.0.0",
"jest": "^29.7.0",
- "serve": "^14.2.1",
- "prettier": "^2.8.4"
+ "jsdom": "^22.1.0",
+ "prettier": "^2.8.4",
+ "serve": "^14.2.1"
}
}