import SecureFile from "./index"; import { readFileSync } from "fs"; import { equal, deepEqual } from "assert"; import { Buffer } from "buffer"; let sf: SecureFile; const testname = "ouiavgbsop687463743" const testdata = new Buffer("Ich bin ein Test"); const newTestData = new Buffer("neue test daten"); const testprev = new Buffer("Ich bin..."); let testid: string; let testver; let testver2; const testfolder = "iabos"; let ftestid; interface TestType { expect(cnt: number): void; ok(value, message?: string): void; equal(actual, expected, message?: string): void; notEqual(actual, expected, message?: string): void; deepEqual(actual, expected, message?: string): void; notDeepEqual(actual, expected, message?: string): void; strictEqual(actual, expected, message?: string): void; notStrictEqual(actual, expected, message?: string): void; throws(block: any, error?, message?: string): void; doesNotThrow(block: any, error?, message?: string): void; ifError(value): void; done(): void; } module.exports = { setUp: function (finished) { let pk = readFileSync("./private.pem"); sf = new SecureFile("http://localhost:3005/api", "test", pk.toString("utf8")); finished(); }, create: async function (test: TestType) { test.expect(2); let res = await sf.create(testname, testdata, "text", undefined, testprev) test.ok(res); test.ok(res._id); testid = res._id; testver = res.version; test.done(); }, get: async function (test: TestType) { test.expect(2); let res = await sf.get(testid); test.ok(res); test.equal(res.toString(), testdata.toString()); test.done(); }, list: async function (test: TestType) { test.expect(4); let res = await sf.list(); test.ok(res); console.log(res); test.ok(Array.isArray(res), "Is from type Array"); test.ok(res.length > 0, "Do elements exist?") res.forEach(e => { if (e._id === testid) { test.ok(true, "Element is in List") } }) test.done() }, update: async function (test: TestType) { test.expect(5); let res = await sf.update(testid, newTestData); test.ok(res); test.ok(res._id); test.notEqual(res.version, testver, "Is new version_id generated?") testver2 = res.version; let res2 = await sf.get(testid); test.ok(res2) test.equal(res2.toString(), newTestData.toString(), "Is fetched data the updated?") test.done(); }, history: async function (test: TestType) { test.expect(7); let his = await sf.history(testid); test.ok(his) test.ok(his.file) test.ok(his.history); test.equal(his.history.length, 1) test.equal(his.history[0].version, testver); test.equal(his.file.version, testver2, "Is correct version"); let arch = await sf.get(testid, testver); test.equal(arch.toString(), testdata.toString(), "Is old version data correct"); test.done(); }, delete: async function (test: TestType) { test.expect(1); let res = await sf.delete(testid); test.ok(res); test.done(); }, folder: { create: async function (test: TestType) { test.expect(2); let res = await sf.create(testname, testdata, "text", testfolder, testprev) test.ok(res); test.ok(res._id); ftestid = res._id; testver = res.version; test.done(); }, list: async function (test: TestType) { test.expect(4); let res = await sf.list(testfolder); test.ok(res); test.ok(Array.isArray(res), "Is from type Array"); test.ok(res.length > 0, "Do elements exist?") res.forEach(e => { if (e._id === ftestid) { test.ok(true, "Element is in List") } }) test.done() }, delete: async function (test: TestType) { test.expect(1); let res = await sf.delete(ftestid); test.ok(res); test.done(); }, } }