"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 rsa = require("node-rsa"); var index_1 = require("./index"); var fs_1 = require("fs"); var buffer_1 = require("buffer"); var sf; 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 testid; var testver; var testver2; var testfolder = "iabos"; var ftestid; var e_testid; var private_key; module.exports = { setUp: function (finished) { var pk = fs_1.readFileSync("./private.pem"); private_key = pk.toString("utf8"); sf = new index_1.default("http://localhost:3005", "test", private_key); finished(); }, create: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(2); return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, false, testprev)]; case 1: res = _a.sent(); test.ok(res); test.ok(res._id); testid = res._id; testver = res.version; test.done(); return [2 /*return*/]; } }); }); }, test: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(2); return [4 /*yield*/, sf.test()]; case 1: res = _a.sent(); test.ok(res.test); test.equal(res.user, "test"); test.done(); return [2 /*return*/]; } }); }); }, get: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(2); return [4 /*yield*/, sf.get(testid)]; case 1: res = _a.sent(); test.ok(res); test.equal(res.toString(), testdata.toString()); test.done(); return [2 /*return*/]; } }); }); }, list: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(4); return [4 /*yield*/, sf.list()]; case 1: res = _a.sent(); test.ok(res); test.ok(Array.isArray(res), "Is from type Array"); test.ok(res.length > 0, "Do elements exist?"); res.forEach(function (e) { if (e._id === testid) { test.ok(true, "Element is in List"); } }); test.done(); return [2 /*return*/]; } }); }); }, update: function (test) { return __awaiter(this, void 0, void 0, function () { var res, res2; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(5); return [4 /*yield*/, sf.update(testid, newTestData)]; case 1: res = _a.sent(); test.ok(res); test.ok(res._id); test.notEqual(res.version, testver, "Is new version_id generated?"); testver2 = res.version; return [4 /*yield*/, sf.get(testid)]; case 2: res2 = _a.sent(); test.ok(res2); test.equal(res2.toString(), newTestData.toString(), "Is fetched data the updated?"); test.done(); return [2 /*return*/]; } }); }); }, history: function (test) { return __awaiter(this, void 0, void 0, function () { var his, arch; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(7); return [4 /*yield*/, sf.history(testid)]; case 1: his = _a.sent(); test.ok(his); test.ok(his.file); test.ok(his.history); test.equal(his.history.length, 1); test.equal(his.history[0].version, testver); test.equal(his.file.version, testver2, "Is correct version"); return [4 /*yield*/, sf.get(testid, testver)]; case 2: arch = _a.sent(); test.equal(arch.toString(), testdata.toString(), "Is old version data correct"); test.done(); return [2 /*return*/]; } }); }); }, delete: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(1); return [4 /*yield*/, sf.delete(testid)]; case 1: res = _a.sent(); test.ok(res); test.done(); return [2 /*return*/]; } }); }); }, folder: { create: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(2); return [4 /*yield*/, sf.create(testname, testdata, "text", testfolder, false, testprev)]; case 1: res = _a.sent(); test.ok(res); test.ok(res._id); ftestid = res._id; testver = res.version; test.done(); return [2 /*return*/]; } }); }); }, list: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(4); return [4 /*yield*/, sf.list(testfolder)]; case 1: res = _a.sent(); test.ok(res); test.ok(Array.isArray(res), "Is from type Array"); test.ok(res.length > 0, "Do elements exist?"); res.forEach(function (e) { if (e._id === ftestid) { test.ok(true, "Element is in List"); } }); test.done(); return [2 /*return*/]; } }); }); }, delete: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(1); return [4 /*yield*/, sf.delete(ftestid)]; case 1: res = _a.sent(); test.ok(res); test.done(); return [2 /*return*/]; } }); }); }, }, encryption: { create: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(2); return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, true, testprev)]; case 1: res = _a.sent(); test.ok(res); test.ok(res._id); e_testid = res._id; testver = res.version; test.done(); return [2 /*return*/]; } }); }); }, get: function (test) { return __awaiter(this, void 0, void 0, function () { var r, res; return __generator(this, function (_a) { switch (_a.label) { case 0: r = new rsa(private_key, "pkcs1-pem"); test.expect(2); return [4 /*yield*/, sf.get(e_testid)]; case 1: res = _a.sent(); res = r.decrypt(res); test.ok(res); test.equal(res.toString(), testdata.toString()); test.done(); return [2 /*return*/]; } }); }); }, delete: function (test) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: test.expect(1); return [4 /*yield*/, sf.delete(e_testid)]; case 1: res = _a.sent(); test.ok(res); test.done(); return [2 /*return*/]; } }); }); }, } };