import { resolve } from "path"; import { describe, it, beforeEach } from "mocha"; import { expect } from "chai"; import type { Vault } from "../packages/opvault.js/index"; import { OnePassword } from "../packages/opvault.js/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(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; }); }); });