"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [0, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var index_1 = require("./index"); var fs_1 = require("fs"); var buffer_1 = require("buffer"); var testname = "ouiavgbsop687463743"; var testdata = new buffer_1.Buffer("Ich bin ein Test"); var newTestData = new buffer_1.Buffer("neue test daten"); var testprev = new buffer_1.Buffer("Ich bin..."); var testfolder = "iabos"; var ftestid; var e_testid; var private_key; var chai_1 = require("chai"); function test(sf) { var _this = this; var testid; var testver; var testver2; it("create", function () { return __awaiter(_this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, false, testprev)]; case 1: res = _a.sent(); chai_1.assert.ok(res, "Res is not set"); chai_1.assert.ok(res._id, "Res has not _id"); testid = res._id; testver = res.version; return [2 /*return*/]; } }); }); }); it("test", function () { return __awaiter(_this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.test()]; case 1: res = _a.sent(); chai_1.assert.ok(res.test, "Test went wrong"); chai_1.assert.equal(res.user, "test", "Wrong user returned"); return [2 /*return*/]; } }); }); }); it("get", function () { return __awaiter(_this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.get(testid)]; case 1: res = _a.sent(); chai_1.assert.ok(res, "No date returned"); chai_1.assert.equal(res.toString(), testdata.toString(), "Returned data not equal to stored"); return [2 /*return*/]; } }); }); }); it("list", function () { return __awaiter(_this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.list()]; case 1: res = _a.sent(); chai_1.assert.ok(res); chai_1.assert.ok(Array.isArray(res), "Is not from type Array"); chai_1.assert.ok(res.length > 0, "No elements returned"); res.forEach(function (e) { if (e._id === testid) { chai_1.assert.ok(true, "Element not in List"); } }); return [2 /*return*/]; } }); }); }); it("update", function () { return __awaiter(_this, void 0, void 0, function () { var res, res2; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.update(testid, newTestData)]; case 1: res = _a.sent(); chai_1.assert.ok(res, "No data returned"); chai_1.assert.ok(res._id, "_id missing"); chai_1.assert.notEqual(res.version, testver, "No new version was created"); testver2 = res.version; return [4 /*yield*/, sf.get(testid)]; case 2: res2 = _a.sent(); chai_1.assert.equal(res2.toString(), newTestData.toString(), "Fetched data not updated"); return [2 /*return*/]; } }); }); }); it("history", function () { return __awaiter(_this, void 0, void 0, function () { var his, arch; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.history(testid)]; case 1: his = _a.sent(); chai_1.assert.ok(his, "no data returned"); chai_1.assert.ok(his.file, "file not set"); chai_1.assert.ok(his.history, "history not set"); chai_1.assert.equal(his.history.length, 1, "Not expected history length. Expected 1 got " + his.history.length); chai_1.assert.equal(his.history[0].version, testver, "Wrong version on history"); chai_1.assert.equal(his.file.version, testver2, "Wrong version on file"); return [4 /*yield*/, sf.get(testid, testver)]; case 2: arch = _a.sent(); chai_1.assert.equal(arch.toString(), testdata.toString(), "Old version has wrong data"); return [2 /*return*/]; } }); }); }); it("delete", function () { return __awaiter(_this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.delete(testid)]; case 1: res = _a.sent(); chai_1.assert.ok(res, "Res not set"); return [2 /*return*/]; } }); }); }); describe("folder", function () { it("create", function () { return __awaiter(_this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.create(testname, testdata, "text", testfolder, false, testprev)]; case 1: res = _a.sent(); chai_1.assert.ok(res, "Res not set"); chai_1.assert.ok(res._id, "No _id field"); ftestid = res._id; testver = res.version; return [2 /*return*/]; } }); }); }); it("list", function () { return __awaiter(_this, void 0, void 0, function () { var res, found; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.list(testfolder)]; case 1: res = _a.sent(); chai_1.assert.ok(res); chai_1.assert.ok(Array.isArray(res), "Is from type Array"); chai_1.assert.ok(res.length > 0, "Do elements exist?"); found = false; res.forEach(function (e) { if (e._id === ftestid) { found = true; } }); chai_1.assert.ok(found, "Element is not in List"); return [2 /*return*/]; } }); }); }); it("delete", function () { return __awaiter(_this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, sf.delete(ftestid)]; case 1: res = _a.sent(); chai_1.assert.ok(res, "Res not set"); return [2 /*return*/]; } }); }); }); }); // 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 = fs_1.readFileSync("./private.pem").toString("utf8"); describe("SecureFile Tests (request based auth)", function () { var sf = new index_1.default("http://localhost:3005", "test", private_key); test(sf); }); describe("SecureFile Tests (jwt)", function () { var sf = new index_1.default("http://localhost:3005", "test", private_key, true); test(sf); });