Refactor
This commit is contained in:
File diff suppressed because one or more lines are too long
@ -1,104 +0,0 @@
|
||||
import { resolve } from "path";
|
||||
import { describe, it, beforeEach } from "mocha";
|
||||
import { expect } from "chai";
|
||||
|
||||
import type { Vault } from "../packages/opvault.js/src/index";
|
||||
import { OnePassword } from "../packages/opvault.js/src/index";
|
||||
|
||||
describe("OnePassword", () => {
|
||||
const freddy = resolve(__dirname, "../freddy-2013-12-04.opvault");
|
||||
|
||||
describe("getProfileNames", () => {
|
||||
it("freddy", async () => {
|
||||
const instance = new OnePassword({ path: freddy });
|
||||
expect(await instance.getProfileNames()).to.deep.equal(["default"]);
|
||||
});
|
||||
|
||||
it.skip("ignores faulty folders", async () => {});
|
||||
});
|
||||
|
||||
describe("unlock", () => {
|
||||
let vault: Vault;
|
||||
|
||||
beforeEach(async () => {
|
||||
vault = await new OnePassword({ path: freddy }).getProfile("default");
|
||||
});
|
||||
|
||||
it("accepts correct password", async () => {
|
||||
await expect(vault.unlock("freddy")).to.be.fulfilled;
|
||||
expect(vault.isLocked).to.be.false;
|
||||
});
|
||||
|
||||
it("rejects wrong password", () => {
|
||||
["Freddy", "_freddy", ""].forEach(async (password) => {
|
||||
await expect(vault.unlock(password)).to.be.rejectedWith(
|
||||
"Invalid password"
|
||||
);
|
||||
expect(vault.isLocked).to.be.true;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("content", () => {
|
||||
let vault: Vault;
|
||||
|
||||
beforeEach(async () => {
|
||||
vault = await new OnePassword({ path: freddy }).getProfile("default");
|
||||
await vault.unlock("freddy");
|
||||
});
|
||||
|
||||
it("reads notes", async () => {
|
||||
const item = (await vault.getItem({
|
||||
title: "A note with some attachments",
|
||||
}))!;
|
||||
expect(item).to.exist;
|
||||
expect(item.uuid).to.equal("F2DB5DA3FCA64372A751E0E85C67A538");
|
||||
expect(item.attachments).to.have.lengthOf(2);
|
||||
expect(item.details).to.deep.equal({
|
||||
notesPlain: "This note has two attachments.",
|
||||
});
|
||||
expect(item.overview).to.deep.equal({
|
||||
title: "A note with some attachments",
|
||||
ps: 0,
|
||||
ainfo: "This note has two attachments.",
|
||||
});
|
||||
});
|
||||
|
||||
it("decrypts items", async () => {
|
||||
const decrypted = require("./decrypted.json");
|
||||
expect(vault.isLocked).to.be.false;
|
||||
for (const [uuid, item] of Object.entries<any>(decrypted)) {
|
||||
const actual = await vault.getItem(uuid);
|
||||
expect(actual).to.exist;
|
||||
expect(actual!.overview).to.deep.equal(item.overview);
|
||||
expect(actual!.details).to.deep.equal(item.itemDetails);
|
||||
expect(actual!.attachments).to.have.lengthOf(item.attachments.length);
|
||||
for (const [i, attachment] of actual!.attachments.entries()) {
|
||||
const expected = item.attachments[i];
|
||||
await attachment.unlock();
|
||||
expect(attachment.metadata).to.deep.equal(expected.metadata);
|
||||
expect(attachment.file.toString("base64")).to.deep.equal(
|
||||
expected.file
|
||||
);
|
||||
expect(attachment.icon.toString("base64")).to.deep.equal(
|
||||
expected.icon
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("lock", () => {
|
||||
it("locks", async () => {
|
||||
const instance = new OnePassword({ path: freddy });
|
||||
const vault = await instance.getProfile("default");
|
||||
await vault.unlock("freddy");
|
||||
expect(vault.isLocked).to.be.false;
|
||||
|
||||
vault.lock();
|
||||
expect(vault.isLocked).to.be.true;
|
||||
expect(vault.getItem("F2DB5DA3FCA64372A751E0E85C67A538")).to.eventually
|
||||
.throw;
|
||||
});
|
||||
});
|
||||
});
|
@ -1,43 +0,0 @@
|
||||
import { describe, it } from "mocha";
|
||||
import { expect } from "chai";
|
||||
|
||||
import { WeakValueMap } from "../packages/opvault.js/src/weakMap";
|
||||
|
||||
declare const gc: () => void;
|
||||
|
||||
describe("WeakValueMap", () => {
|
||||
interface Value {
|
||||
value: number;
|
||||
}
|
||||
|
||||
it("covers base use cases", () => {
|
||||
const map = new WeakValueMap<string, Value>();
|
||||
const object = { value: 1 };
|
||||
map.set("key", object);
|
||||
expect(map.get("key")!.value).to.equal(1);
|
||||
expect(map.delete("key")).to.be.true;
|
||||
expect(!map.delete("key")).to.be.true;
|
||||
});
|
||||
|
||||
it("overrides previous value", () => {
|
||||
const map = new WeakValueMap<string, Value>();
|
||||
map.set("key", { value: 2 });
|
||||
map.set("key", { value: 3 });
|
||||
expect(map.get("key")!.value).to.equal(3);
|
||||
});
|
||||
|
||||
it("deletes garbage collected values", (done) => {
|
||||
const map = new WeakValueMap<string, Value>();
|
||||
map.set("key", { value: 1 });
|
||||
setTimeout(() => {
|
||||
gc();
|
||||
expect(map.has("key")).to.be.false;
|
||||
map.set("key", { value: 2 });
|
||||
|
||||
setTimeout(() => {
|
||||
gc();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user