import SecureFile, { NotFound } from "./index"; import { TextEncoder, TextDecoder } from "util"; const testname = "ouiavgbsop687463743" const encoder = new TextEncoder(); const decoder = new TextDecoder(); const testdata = encoder.encode("Ich bin ein Test"); const newTestData = encoder.encode("neue test daten"); const testprev = "Ich bin..."; const testfolder = "iabos"; let ftestid; import { expect } from "chai" function test(sf: SecureFile) { let testid: string; let testver: string; let testver2: string; it("create", async () => { let res = await sf.create(testname, testdata, "text", undefined, testprev) expect(res, "Res isnnot set").to.exist; expect(res._id, "Res has no _id").to.exist; testid = res._id; testver = res.active.version; }) it("get", async () => { let res = await sf.get(testid); expect(res, "No data returned").to.exist; expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata)); }) it("get - fail", async () => { const inverr = new Error("Should have failed!"); try { await sf.get(testid + "asod"); throw inverr } catch (err) { if (err !== inverr) { expect(err).to.be.instanceOf(NotFound); } } }) it("list", async () => { let res = await sf.list(); expect(Array.isArray(res), "Is not from type Array").to.be.true; expect(res.length, "No elements returned").to.greaterThan(0); let found = !!res.find(e => e._id === testid); expect(found, "Element not in List").to.be.true; }) it("update", async () => { let res = await sf.update(testid, newTestData, undefined); expect(res, "No data returned").to.exist; expect(res._id, "_id missing").to.exist; expect(res.active.version, "No new version was created").to.not.equal(testver); testver2 = res.active.version; let res2 = await sf.get(testid); expect(decoder.decode(res2), "Fetched data not updated").to.be.equal(decoder.decode(newTestData)); }) it("history", async () => { let his = await sf.history(testid); expect(his, "no data returned").to.exist; expect(his.file, "file not set").to.exist; expect(his.history, "history not set").to.exist; expect(his.history.length, `Not expected history length. Expected 1 got ${his.history.length}`).to.be.equal(1); expect(his.history[0].version, "Wrong version on history").to.be.equal(testver); expect(his.file.active.version, "Wrong version on file").to.be.equal(testver2); }); it("history get old", async () => { let arch = await sf.get(testid, testver); expect(decoder.decode(arch), "Old version has wrong data").to.be.equal(decoder.decode(testdata)); }) it("history restore", async () => { await sf.restore(testid, testver); let res = await sf.get(testid); expect(res, "No data returned").to.exist; expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata)); }) it("delete", async () => { let res = await sf.delete(testid); expect(res, "Res not set").to.exist; }) describe("folder", () => { it("create", async () => { let res = await sf.create(testname, testdata, "text", testfolder, testprev) expect(res, "Res not set").to.exist; expect(res._id, "No _id field").to.exist; ftestid = res._id; testver = res.active.version; }) it("list", async () => { let res = await sf.list(testfolder); expect(Array.isArray(res), "Is from type Array").to.be.true; expect(res.length, "Do elements exist?").to.be.greaterThan(0); let found = false; res.forEach(e => { if (e._id === ftestid) { found = true; } }) expect(found, "Element is not in List").to.exist; }) it("delete", async () => { let res = await sf.delete(ftestid); expect(res, "Res not set").to.exist; }); }) } describe("SecureFile Tests", function () { let sf = new SecureFile("http://localhost:3004"); sf.jwtObservable.subscribe((callback) => { callback[0]("TESTJWT"); }) test(sf) })