OSSecureFileWrapper/test.ts

146 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-02-18 13:03:23 +00:00
import * as rsa from "node-rsa";
2018-02-01 15:27:01 +00:00
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...");
2018-02-05 09:32:48 +00:00
const testfolder = "iabos";
let ftestid;
2018-02-18 13:03:23 +00:00
let e_testid;
let private_key;
import { assert } from "chai"
function test(sf: SecureFile) {
let testid: string;
let testver: string;
let testver2: string;
2018-02-01 15:27:01 +00:00
it("create", async () => {
2018-02-18 13:03:23 +00:00
let res = await sf.create(testname, testdata, "text", undefined, false, testprev)
assert.ok(res, "Res is not set");
assert.ok(res._id, "Res has not _id");
2018-02-01 15:27:01 +00:00
testid = res._id;
testver = res.version;
})
2018-02-25 10:39:01 +00:00
it("test", async () => {
2018-02-25 10:39:01 +00:00
let res = await sf.test();
assert.ok(res.test, "Test went wrong");
assert.equal(res.user, "test", "Wrong user returned");
})
2018-02-25 10:39:01 +00:00
it("get", async () => {
2018-02-01 15:27:01 +00:00
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 () => {
2018-02-01 15:27:01 +00:00
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")
2018-02-01 15:27:01 +00:00
res.forEach(e => {
if (e._id === testid) {
assert.ok(true, "Element not in List")
2018-02-01 15:27:01 +00:00
}
})
})
it("update", async () => {
2018-02-01 15:27:01 +00:00
let res = await sf.update(testid, newTestData);
assert.ok(res, "No data returned");
assert.ok(res._id, "_id missing");
assert.notEqual(res.version, testver, "No new version was created")
2018-02-01 15:27:01 +00:00
testver2 = res.version;
let res2 = await sf.get(testid);
assert.equal(res2.toString(), newTestData.toString(), "Fetched data not updated")
})
it("history", async () => {
2018-02-01 15:27:01 +00:00
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}`)
2018-02-01 15:27:01 +00:00
assert.equal(his.history[0].version, testver, "Wrong version on history");
assert.equal(his.file.version, testver2, "Wrong version on file");
2018-02-01 15:27:01 +00:00
let arch = await sf.get(testid, testver);
assert.equal(arch.toString(), testdata.toString(), "Old version has wrong data");
});
it("delete", async () => {
2018-02-01 15:27:01 +00:00
let res = await sf.delete(testid);
assert.ok(res, "Res not set");
})
describe("folder", () => {
it("create", async () => {
2018-02-18 13:03:23 +00:00
let res = await sf.create(testname, testdata, "text", testfolder, false, testprev)
assert.ok(res, "Res not set");
assert.ok(res._id, "No _id field");
2018-02-05 09:32:48 +00:00
ftestid = res._id;
testver = res.version;
})
it("list", async () => {
2018-02-05 09:32:48 +00:00
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;
2018-02-05 09:32:48 +00:00
res.forEach(e => {
if (e._id === ftestid) {
found = true;
2018-02-05 09:32:48 +00:00
}
})
assert.ok(found, "Element is not in List")
})
it("delete", async () => {
2018-02-05 09:32:48 +00:00
let res = await sf.delete(ftestid);
assert.ok(res, "Res not set");
});
})
// describe("encryption", () => {
// it("create", async () => {
// let res = await sf.create(testname, testdata, "text", undefined, true, testprev)
// assert.ok(res, "Res not set");
// assert.ok(res._id, "_id not set");
// e_testid = res._id;
// testver = res.version;
// })
// it("get", async () => {
// let r = new rsa(private_key, "pkcs1-pem");
// let res = await sf.get(e_testid);
// res = r.decrypt(res);
// assert.ok(res, "Res not set");
// assert.equal(res.toString(), testdata.toString(), "Unexpected data change");
// })
// it("delete", async () => {
// let res = await sf.delete(e_testid);
// 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)
})