This repository has been archived on 2019-08-30. You can view files and clone it, but cannot push or open issues or pull requests.
SecureFileWrapper/test.ts
2018-06-14 16:42:30 +02:00

125 lines
3.8 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 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)
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")
res.forEach(e => {
if (e._id === testid) {
assert.ok(true, "Element not in List")
}
})
})
it("update", async () => {
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")
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)
})