import * as rsa from "node-rsa"; 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; let e_testid; let private_key; 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"); private_key = pk.toString("utf8"); sf = new SecureFile("http://localhost:3005", "test", private_key); finished(); }, create: async function (test: TestType) { test.expect(2); let res = await sf.create(testname, testdata, "text", undefined, false, testprev) test.ok(res); test.ok(res._id); testid = res._id; testver = res.version; test.done(); }, test: async function (test: TestType) { test.expect(2); let res = await sf.test(); test.ok(res.test); test.equal(res.user, "test"); 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); 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, false, 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(); }, }, encryption: { create: async function (test: TestType) { test.expect(2); let res = await sf.create(testname, testdata, "text", undefined, true, testprev) test.ok(res); test.ok(res._id); e_testid = res._id; testver = res.version; test.done(); }, get: async function (test: TestType) { let r = new rsa(private_key, "pkcs1-pem"); test.expect(2); let res = await sf.get(e_testid); res = r.decrypt(res); test.ok(res); test.equal(res.toString(), testdata.toString()); test.done(); }, delete: async function (test: TestType) { test.expect(1); let res = await sf.delete(e_testid); test.ok(res); test.done(); }, } }