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;
|
|
|
|
|
2018-06-14 08:02:05 +00:00
|
|
|
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
|
|
|
|
2018-06-14 08:02:05 +00:00
|
|
|
it("create", async () => {
|
2018-06-14 14:42:30 +00:00
|
|
|
let res = await sf.create(testname, testdata, "text", undefined, testprev)
|
2018-06-14 08:02:05 +00:00
|
|
|
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-06-14 08:02:05 +00:00
|
|
|
})
|
2018-02-25 10:39:01 +00:00
|
|
|
|
2018-06-14 08:02:05 +00:00
|
|
|
it("test", async () => {
|
2018-02-25 10:39:01 +00:00
|
|
|
let res = await sf.test();
|
2018-06-14 08:02:05 +00:00
|
|
|
assert.ok(res.test, "Test went wrong");
|
|
|
|
assert.equal(res.user, "test", "Wrong user returned");
|
|
|
|
})
|
2018-02-25 10:39:01 +00:00
|
|
|
|
2018-06-14 08:02:05 +00:00
|
|
|
it("get", async () => {
|
2018-02-01 15:27:01 +00:00
|
|
|
let res = await sf.get(testid);
|
2018-06-14 08:02:05 +00:00
|
|
|
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();
|
2018-06-14 08:02:05 +00:00
|
|
|
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) {
|
2018-06-14 08:02:05 +00:00
|
|
|
assert.ok(true, "Element not in List")
|
2018-02-01 15:27:01 +00:00
|
|
|
}
|
|
|
|
})
|
2018-06-14 08:02:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("update", async () => {
|
2018-02-01 15:27:01 +00:00
|
|
|
let res = await sf.update(testid, newTestData);
|
2018-06-14 08:02:05 +00:00
|
|
|
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);
|
2018-06-14 08:02:05 +00:00
|
|
|
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);
|
2018-06-14 08:02:05 +00:00
|
|
|
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
|
|
|
|
2018-06-14 08:02:05 +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);
|
2018-06-14 08:02:05 +00:00
|
|
|
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);
|
2018-06-14 08:02:05 +00:00
|
|
|
assert.ok(res, "Res not set");
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("folder", () => {
|
|
|
|
it("create", async () => {
|
2018-06-14 14:42:30 +00:00
|
|
|
let res = await sf.create(testname, testdata, "text", testfolder, testprev)
|
2018-06-14 08:02:05 +00:00
|
|
|
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;
|
2018-06-14 08:02:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("list", async () => {
|
2018-02-05 09:32:48 +00:00
|
|
|
let res = await sf.list(testfolder);
|
2018-06-14 08:02:05 +00:00
|
|
|
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) {
|
2018-06-14 08:02:05 +00:00
|
|
|
found = true;
|
2018-02-05 09:32:48 +00:00
|
|
|
}
|
|
|
|
})
|
2018-06-14 08:02:05 +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);
|
2018-06-14 08:02:05 +00:00
|
|
|
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)
|
|
|
|
})
|