forked from hibas123/SecureFileWrapper
129 lines
4.0 KiB
TypeScript
129 lines
4.0 KiB
TypeScript
import * as rsa from "node-rsa";
|
|
import SecureFile from "./index";
|
|
import { readFileSync } from "fs";
|
|
import { Buffer } from "buffer";
|
|
|
|
const testname = "ouiavgbsop687463743"
|
|
const testdata = new Buffer("Ich bin ein Test");
|
|
const newTestData = new Buffer("neue test daten");
|
|
const testprev = new Buffer("Ich bin...");
|
|
const testmeta = "testaa";
|
|
|
|
const testfolder = "iabos";
|
|
let ftestid;
|
|
|
|
let e_testid;
|
|
let private_key;
|
|
|
|
import { assert } 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, testmeta)
|
|
assert.ok(res, "Res is not set");
|
|
assert.ok(res._id, "Res has not _id");
|
|
testid = res._id;
|
|
testver = res.version;
|
|
})
|
|
|
|
it("test", async () => {
|
|
let res = await sf.test();
|
|
assert.ok(res.test, "Test went wrong");
|
|
assert.equal(res.user, "test", "Wrong user returned");
|
|
})
|
|
|
|
it("get", async () => {
|
|
let res = await sf.get(testid);
|
|
assert.ok(res, "No date returned");
|
|
assert.equal(res.toString(), testdata.toString(), "Returned data not equal to stored");
|
|
})
|
|
|
|
it("list", async () => {
|
|
let res = await sf.list();
|
|
assert.ok(res);
|
|
assert.ok(Array.isArray(res), "Is not from type Array");
|
|
assert.ok(res.length > 0, "No elements returned")
|
|
let found = false;
|
|
res.forEach(e => {
|
|
if (e._id === testid) {
|
|
found = true;
|
|
assert.equal(e.meta, testmeta, "Meta data dows not fit the expected value!")
|
|
}
|
|
})
|
|
assert.ok(found, "Element not in List")
|
|
})
|
|
|
|
it("update", async () => {
|
|
let res = await sf.update(testid, newTestData, undefined, testmeta + "2");
|
|
assert.ok(res, "No data returned");
|
|
assert.ok(res._id, "_id missing");
|
|
assert.notEqual(res.version, testver, "No new version was created")
|
|
testver2 = res.version;
|
|
let res2 = await sf.get(testid);
|
|
assert.equal(res2.toString(), newTestData.toString(), "Fetched data not updated")
|
|
})
|
|
|
|
it("history", async () => {
|
|
let his = await sf.history(testid);
|
|
assert.ok(his, "no data returned")
|
|
assert.ok(his.file, "file not set")
|
|
assert.ok(his.history, "history not set");
|
|
assert.equal(his.history.length, 1, `Not expected history length. Expected 1 got ${his.history.length}`)
|
|
|
|
assert.equal(his.history[0].version, testver, "Wrong version on history");
|
|
assert.equal(his.file.version, testver2, "Wrong version on file");
|
|
|
|
let arch = await sf.get(testid, testver);
|
|
assert.equal(arch.toString(), testdata.toString(), "Old version has wrong data");
|
|
});
|
|
|
|
it("delete", async () => {
|
|
let res = await sf.delete(testid);
|
|
assert.ok(res, "Res not set");
|
|
})
|
|
|
|
describe("folder", () => {
|
|
it("create", async () => {
|
|
let res = await sf.create(testname, testdata, "text", testfolder, testprev)
|
|
assert.ok(res, "Res not set");
|
|
assert.ok(res._id, "No _id field");
|
|
ftestid = res._id;
|
|
testver = res.version;
|
|
})
|
|
|
|
it("list", async () => {
|
|
let res = await sf.list(testfolder);
|
|
assert.ok(res);
|
|
assert.ok(Array.isArray(res), "Is from type Array");
|
|
assert.ok(res.length > 0, "Do elements exist?")
|
|
let found = false;
|
|
res.forEach(e => {
|
|
if (e._id === ftestid) {
|
|
found = true;
|
|
}
|
|
})
|
|
assert.ok(found, "Element is not in List")
|
|
})
|
|
|
|
it("delete", async () => {
|
|
let res = await sf.delete(ftestid);
|
|
assert.ok(res, "Res not set");
|
|
});
|
|
})
|
|
}
|
|
|
|
private_key = readFileSync("./private.pem").toString("utf8");
|
|
|
|
describe("SecureFile Tests (request based auth)", function () {
|
|
let sf = new SecureFile("http://localhost:3005", "test", private_key);
|
|
test(sf)
|
|
})
|
|
|
|
describe("SecureFile Tests (jwt)", function () {
|
|
let sf = new SecureFile("http://localhost:3005", "test", private_key, true);
|
|
test(sf)
|
|
}) |