forked from hibas123/SecureFileWrapper
Changing Test engine and adding jwt support
This commit is contained in:
parent
1cb87b051c
commit
9284256ae4
15
index.d.ts
vendored
15
index.d.ts
vendored
@ -16,9 +16,12 @@ export default class SecureFile {
|
||||
private Server;
|
||||
private Username;
|
||||
private PrivateKey;
|
||||
constructor(server: string, username: string, private_key: string);
|
||||
private jwt_enabled;
|
||||
private JWT;
|
||||
constructor(server: string, username: string, private_key: string, jwt?: boolean);
|
||||
private getCode();
|
||||
makeRequest(endpoint: string, method: "POST" | "GET" | "PUT" | "DELETE", query: any, body?: Buffer): Promise<Response>;
|
||||
private getJWT();
|
||||
makeRequest(endpoint: string, method: "POST" | "GET" | "PUT" | "DELETE", query: any, body?: Buffer, jwt?: boolean): any;
|
||||
test(): Promise<{
|
||||
user: string;
|
||||
test: true;
|
||||
@ -38,6 +41,14 @@ export declare class Unauthorized extends Error {
|
||||
type: string;
|
||||
constructor();
|
||||
}
|
||||
export declare class NoPermission extends Error {
|
||||
type: string;
|
||||
constructor();
|
||||
}
|
||||
export declare class InvalidJWT extends Error {
|
||||
type: string;
|
||||
constructor();
|
||||
}
|
||||
export declare class NotFound extends Error {
|
||||
type: string;
|
||||
constructor();
|
||||
|
95
index.js
95
index.js
@ -49,7 +49,8 @@ var rsa = require("node-rsa");
|
||||
require("isomorphic-fetch");
|
||||
var btb = require("blob-to-buffer");
|
||||
var SecureFile = /** @class */ (function () {
|
||||
function SecureFile(server, username, private_key) {
|
||||
function SecureFile(server, username, private_key, jwt) {
|
||||
if (jwt === void 0) { jwt = false; }
|
||||
this.Server = server;
|
||||
if (this.Server.endsWith("/")) {
|
||||
this.Server += "api";
|
||||
@ -59,6 +60,7 @@ var SecureFile = /** @class */ (function () {
|
||||
}
|
||||
this.Username = username;
|
||||
this.PrivateKey = private_key;
|
||||
this.jwt_enabled = jwt;
|
||||
}
|
||||
SecureFile.prototype.getCode = function () {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
@ -97,16 +99,47 @@ var SecureFile = /** @class */ (function () {
|
||||
});
|
||||
});
|
||||
};
|
||||
SecureFile.prototype.makeRequest = function (endpoint, method, query, body) {
|
||||
SecureFile.prototype.getJWT = function () {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var res, jwt;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this.makeRequest("/jwt", "GET", {}, undefined, false)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
statusParser(res);
|
||||
return [4 /*yield*/, res.text()];
|
||||
case 2:
|
||||
jwt = _a.sent();
|
||||
this.JWT = jwt;
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
SecureFile.prototype.makeRequest = function (endpoint, method, query, body, jwt) {
|
||||
if (jwt === void 0) { jwt = false; }
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var code, query_str, first, key, myHeaders, res, err_2;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this.getCode()];
|
||||
case 0:
|
||||
if (!(this.jwt_enabled && jwt)) return [3 /*break*/, 3];
|
||||
if (!!this.JWT) return [3 /*break*/, 2];
|
||||
return [4 /*yield*/, this.getJWT()];
|
||||
case 1:
|
||||
_a.sent();
|
||||
_a.label = 2;
|
||||
case 2:
|
||||
query.jwt = this.JWT;
|
||||
return [3 /*break*/, 5];
|
||||
case 3: return [4 /*yield*/, this.getCode()];
|
||||
case 4:
|
||||
code = _a.sent();
|
||||
query.code = code.code;
|
||||
query.signature = code.signature;
|
||||
_a.label = 5;
|
||||
case 5:
|
||||
query_str = "?";
|
||||
first = true;
|
||||
for (key in query) {
|
||||
@ -118,20 +151,24 @@ var SecureFile = /** @class */ (function () {
|
||||
myHeaders = new Headers();
|
||||
myHeaders.append('pragma', 'no-cache');
|
||||
myHeaders.append('cache-control', 'no-cache');
|
||||
_a.label = 2;
|
||||
case 2:
|
||||
_a.trys.push([2, 4, , 5]);
|
||||
_a.label = 6;
|
||||
case 6:
|
||||
_a.trys.push([6, 8, , 9]);
|
||||
return [4 /*yield*/, fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders })];
|
||||
case 3:
|
||||
case 7:
|
||||
res = _a.sent();
|
||||
if (res.status === 418) {
|
||||
this.JWT = undefined;
|
||||
return [2 /*return*/, this.makeRequest(endpoint, method, query, body, jwt)];
|
||||
}
|
||||
return [2 /*return*/, res];
|
||||
case 4:
|
||||
case 8:
|
||||
err_2 = _a.sent();
|
||||
if (err_2 instanceof TypeError || err_2.errno === "ECONNREFUSED")
|
||||
throw new NoConnection();
|
||||
console.log(err_2);
|
||||
throw err_2;
|
||||
case 5: return [2 /*return*/];
|
||||
case 9: return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
@ -141,7 +178,7 @@ var SecureFile = /** @class */ (function () {
|
||||
var res;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this.makeRequest("/test", "GET", {}, undefined)];
|
||||
case 0: return [4 /*yield*/, this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
statusParser(res);
|
||||
@ -159,7 +196,7 @@ var SecureFile = /** @class */ (function () {
|
||||
case 0:
|
||||
if (!folder)
|
||||
folder = "root";
|
||||
return [4 /*yield*/, this.makeRequest("/files", "GET", { folder: folder }, undefined)];
|
||||
return [4 /*yield*/, this.makeRequest("/files", "GET", { folder: folder }, undefined, this.jwt_enabled)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
statusParser(res);
|
||||
@ -186,7 +223,7 @@ var SecureFile = /** @class */ (function () {
|
||||
if (!encrypt) {
|
||||
params.no_encryption = true;
|
||||
}
|
||||
return [4 /*yield*/, this.makeRequest("/files", "POST", params, data)];
|
||||
return [4 /*yield*/, this.makeRequest("/files", "POST", params, data, this.jwt_enabled)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
statusParser(res);
|
||||
@ -203,11 +240,11 @@ var SecureFile = /** @class */ (function () {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
if (!(typeof version === "string")) return [3 /*break*/, 2];
|
||||
return [4 /*yield*/, this.makeRequest("/files/" + id + "/history/" + version, "GET", {}, undefined)];
|
||||
return [4 /*yield*/, this.makeRequest("/files/" + id + "/history/" + version, "GET", {}, undefined, this.jwt_enabled)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
return [3 /*break*/, 4];
|
||||
case 2: return [4 /*yield*/, this.makeRequest("/files/" + id, "GET", {}, undefined)];
|
||||
case 2: return [4 /*yield*/, this.makeRequest("/files/" + id, "GET", {}, undefined, this.jwt_enabled)];
|
||||
case 3:
|
||||
res = _a.sent();
|
||||
_a.label = 4;
|
||||
@ -250,7 +287,7 @@ var SecureFile = /** @class */ (function () {
|
||||
put = {};
|
||||
if (preview)
|
||||
put.preview = preview;
|
||||
return [4 /*yield*/, this.makeRequest("/files/" + id, "PUT", put, data)];
|
||||
return [4 /*yield*/, this.makeRequest("/files/" + id, "PUT", put, data, this.jwt_enabled)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
statusParser(res);
|
||||
@ -264,7 +301,7 @@ var SecureFile = /** @class */ (function () {
|
||||
var res;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this.makeRequest("/files/" + id, "DELETE", {}, undefined)];
|
||||
case 0: return [4 /*yield*/, this.makeRequest("/files/" + id, "DELETE", {}, undefined, this.jwt_enabled)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
statusParser(res);
|
||||
@ -278,7 +315,7 @@ var SecureFile = /** @class */ (function () {
|
||||
var res;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this.makeRequest("/files/" + id + "/history", "GET", {}, undefined)];
|
||||
case 0: return [4 /*yield*/, this.makeRequest("/files/" + id + "/history", "GET", {}, undefined, this.jwt_enabled)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
statusParser(res);
|
||||
@ -310,6 +347,26 @@ var Unauthorized = /** @class */ (function (_super) {
|
||||
return Unauthorized;
|
||||
}(Error));
|
||||
exports.Unauthorized = Unauthorized;
|
||||
var NoPermission = /** @class */ (function (_super) {
|
||||
__extends(NoPermission, _super);
|
||||
function NoPermission() {
|
||||
var _this = _super.call(this, "No permission") || this;
|
||||
_this.type = "nopermission";
|
||||
return _this;
|
||||
}
|
||||
return NoPermission;
|
||||
}(Error));
|
||||
exports.NoPermission = NoPermission;
|
||||
var InvalidJWT = /** @class */ (function (_super) {
|
||||
__extends(InvalidJWT, _super);
|
||||
function InvalidJWT() {
|
||||
var _this = _super.call(this, "Invalid JWT") || this;
|
||||
_this.type = "invalidjwt";
|
||||
return _this;
|
||||
}
|
||||
return InvalidJWT;
|
||||
}(Error));
|
||||
exports.InvalidJWT = InvalidJWT;
|
||||
var NotFound = /** @class */ (function (_super) {
|
||||
__extends(NotFound, _super);
|
||||
function NotFound() {
|
||||
@ -338,7 +395,11 @@ function statusParser(res) {
|
||||
case 404:
|
||||
throw new NotFound();
|
||||
case 403:
|
||||
throw new NoPermission();
|
||||
case 401:
|
||||
throw new Unauthorized();
|
||||
case 418:
|
||||
throw new InvalidJWT();
|
||||
default:
|
||||
throw new Error(res.statusText);
|
||||
}
|
||||
|
61
index.ts
61
index.ts
@ -21,7 +21,10 @@ export default class SecureFile {
|
||||
private Server: string;
|
||||
private Username: string;
|
||||
private PrivateKey: string;
|
||||
constructor(server: string, username: string, private_key: string) {
|
||||
private jwt_enabled: boolean;
|
||||
private JWT: string;
|
||||
|
||||
constructor(server: string, username: string, private_key: string, jwt = false) {
|
||||
this.Server = server;
|
||||
if (this.Server.endsWith("/")) {
|
||||
this.Server += "api";
|
||||
@ -30,6 +33,7 @@ export default class SecureFile {
|
||||
}
|
||||
this.Username = username;
|
||||
this.PrivateKey = private_key;
|
||||
this.jwt_enabled = jwt;
|
||||
}
|
||||
|
||||
private async getCode() {
|
||||
@ -54,10 +58,23 @@ export default class SecureFile {
|
||||
return { code: code, signature: r.sign(code).toString("base64") };
|
||||
}
|
||||
|
||||
public async makeRequest(endpoint: string, method: "POST" | "GET" | "PUT" | "DELETE", query: any, body?: Buffer) {
|
||||
private async getJWT() {
|
||||
let res = await this.makeRequest("/jwt", "GET", {}, undefined, false);
|
||||
statusParser(res);
|
||||
let jwt = await res.text();
|
||||
this.JWT = jwt;
|
||||
}
|
||||
|
||||
public async makeRequest(endpoint: string, method: "POST" | "GET" | "PUT" | "DELETE", query: any, body?: Buffer, jwt = false) {
|
||||
if (this.jwt_enabled && jwt) {
|
||||
if (!this.JWT) await this.getJWT();
|
||||
query.jwt = this.JWT;
|
||||
} else {
|
||||
let code = await this.getCode();
|
||||
query.code = code.code;
|
||||
query.signature = code.signature;
|
||||
}
|
||||
|
||||
let query_str = "?";
|
||||
let first = true;
|
||||
for (let key in query) {
|
||||
@ -70,6 +87,10 @@ export default class SecureFile {
|
||||
myHeaders.append('cache-control', 'no-cache');
|
||||
try {
|
||||
let res = await fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders });
|
||||
if (res.status === 418) { // JWT invalid
|
||||
this.JWT = undefined;
|
||||
return this.makeRequest(endpoint, method, query, body, jwt);
|
||||
}
|
||||
return res;
|
||||
} catch (err) {
|
||||
if (err instanceof TypeError || err.errno === "ECONNREFUSED")
|
||||
@ -80,14 +101,14 @@ export default class SecureFile {
|
||||
}
|
||||
|
||||
async test(): Promise<{ user: string, test: true }> {
|
||||
let res = await this.makeRequest("/test", "GET", {}, undefined);
|
||||
let res = await this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled);
|
||||
statusParser(res);
|
||||
return await res.json();
|
||||
}
|
||||
|
||||
async list(folder?: string): Promise<File[]> {
|
||||
if (!folder) folder = "root";
|
||||
let res = await this.makeRequest("/files", "GET", { folder: folder }, undefined)
|
||||
let res = await this.makeRequest("/files", "GET", { folder: folder }, undefined, this.jwt_enabled)
|
||||
statusParser(res);
|
||||
return await res.json();
|
||||
}
|
||||
@ -103,7 +124,7 @@ export default class SecureFile {
|
||||
if (!encrypt) {
|
||||
params.no_encryption = true;
|
||||
}
|
||||
let res = await this.makeRequest("/files", "POST", params, data)
|
||||
let res = await this.makeRequest("/files", "POST", params, data, this.jwt_enabled)
|
||||
statusParser(res);
|
||||
return res.json();
|
||||
}
|
||||
@ -111,9 +132,9 @@ export default class SecureFile {
|
||||
async get(id: string, version?: string): Promise<Buffer> {
|
||||
let res: Response;
|
||||
if (typeof version === "string") {
|
||||
res = await this.makeRequest(`/files/${id}/history/${version}`, "GET", {}, undefined);
|
||||
res = await this.makeRequest(`/files/${id}/history/${version}`, "GET", {}, undefined, this.jwt_enabled);
|
||||
} else {
|
||||
res = await this.makeRequest("/files/" + id, "GET", {}, undefined);
|
||||
res = await this.makeRequest("/files/" + id, "GET", {}, undefined, this.jwt_enabled);
|
||||
}
|
||||
statusParser(res);
|
||||
if ((<any>res).buffer) {
|
||||
@ -131,19 +152,19 @@ export default class SecureFile {
|
||||
async update(id: string, data: Buffer, preview?: Buffer): Promise<File> {
|
||||
let put: any = {};
|
||||
if (preview) put.preview = preview;
|
||||
let res = await this.makeRequest("/files/" + id, "PUT", put, data);
|
||||
let res = await this.makeRequest("/files/" + id, "PUT", put, data, this.jwt_enabled);
|
||||
statusParser(res);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<boolean> {
|
||||
let res = await this.makeRequest("/files/" + id, "DELETE", {}, undefined);
|
||||
let res = await this.makeRequest("/files/" + id, "DELETE", {}, undefined, this.jwt_enabled);
|
||||
statusParser(res);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async history(id: string): Promise<History> {
|
||||
let res = await this.makeRequest(`/files/${id}/history`, "GET", {}, undefined);
|
||||
let res = await this.makeRequest(`/files/${id}/history`, "GET", {}, undefined, this.jwt_enabled);
|
||||
statusParser(res);
|
||||
return res.json();
|
||||
}
|
||||
@ -165,6 +186,22 @@ export class Unauthorized extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class NoPermission extends Error {
|
||||
type: string;
|
||||
constructor() {
|
||||
super("No permission");
|
||||
this.type = "nopermission"
|
||||
}
|
||||
}
|
||||
|
||||
export class InvalidJWT extends Error {
|
||||
type: string;
|
||||
constructor() {
|
||||
super("Invalid JWT");
|
||||
this.type = "invalidjwt"
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFound extends Error {
|
||||
type: string;
|
||||
constructor() {
|
||||
@ -189,7 +226,11 @@ function statusParser(res: Response) {
|
||||
case 404:
|
||||
throw new NotFound();
|
||||
case 403:
|
||||
throw new NoPermission();
|
||||
case 401:
|
||||
throw new Unauthorized();
|
||||
case 418:
|
||||
throw new InvalidJWT();
|
||||
default:
|
||||
throw new Error(res.statusText);
|
||||
}
|
||||
|
@ -7,7 +7,8 @@
|
||||
"types": "index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "nodeunit test.js"
|
||||
"watch": "tsc --watch",
|
||||
"test": "mocha test.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"blob-to-buffer": "^1.2.7",
|
||||
@ -16,10 +17,14 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/blob-to-buffer": "^1.2.0",
|
||||
"@types/chai": "^4.1.4",
|
||||
"@types/isomorphic-fetch": "^0.0.34",
|
||||
"@types/mocha": "^5.2.2",
|
||||
"@types/node": "^9.4.0",
|
||||
"@types/node-fetch": "^1.6.7",
|
||||
"@types/node-rsa": "^0.4.1",
|
||||
"chai": "^4.1.2",
|
||||
"mocha": "^5.2.0",
|
||||
"nodeunit": "^0.11.2",
|
||||
"typescript": "^2.6.2"
|
||||
}
|
||||
|
9
public.pem
Normal file
9
public.pem
Normal file
@ -0,0 +1,9 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkekKOaeZtnCHr8L8msgg
|
||||
J0vesw4JbJl21YtEH7jnzXks03Gv4r/AeuEiKATFAG8EI+MZLgnDdYTuL787Ic7e
|
||||
KILc+ojtL2H4wtlnjRgcby3f8Qef2EKOE+QjmZxkO66k4PPVdnEgjg+W9nJV6cnW
|
||||
WhiXwg4BsSBHewPuugoacDO7gfZSpUtAW99eEe5dStyb/VoXce56nwmEV82cMbnK
|
||||
8jKYDIHQWtqo+BubZfIHApxAV3YPy0Rp5ewULNK8CXsNrN6QCd8L1R7De/sUFMlV
|
||||
66Tgurh40S32XliZh3eewlpxe4xLD20Z1TCeLeSMRYF8OnEkGSjc2ppBXFk8Azej
|
||||
UwIDAQAB
|
||||
-----END PUBLIC KEY-----
|
1
public.pem.b64
Normal file
1
public.pem.b64
Normal file
@ -0,0 +1 @@
|
||||
LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFrZWtLT2FlWnRuQ0hyOEw4bXNnZwpKMHZlc3c0SmJKbDIxWXRFSDdqbnpYa3MwM0d2NHIvQWV1RWlLQVRGQUc4RUkrTVpMZ25EZFlUdUw3ODdJYzdlCktJTGMrb2p0TDJINHd0bG5qUmdjYnkzZjhRZWYyRUtPRStRam1aeGtPNjZrNFBQVmRuRWdqZytXOW5KVjZjblcKV2hpWHdnNEJzU0JIZXdQdXVnb2FjRE83Z2ZaU3BVdEFXOTllRWU1ZFN0eWIvVm9YY2U1Nm53bUVWODJjTWJuSwo4aktZRElIUVd0cW8rQnViWmZJSEFweEFWM1lQeTBScDVld1VMTks4Q1hzTnJONlFDZDhMMVI3RGUvc1VGTWxWCjY2VGd1cmg0MFMzMlhsaVpoM2Vld2xweGU0eExEMjBaMVRDZUxlU01SWUY4T25Fa0dTamMycHBCWEZrOEF6ZWoKVXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t
|
283
test.js
283
test.js
@ -35,297 +35,212 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
}
|
||||
};
|
||||
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 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:
|
||||
test.expect(2);
|
||||
return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, false, testprev)];
|
||||
case 0: return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, false, testprev)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
test.ok(res);
|
||||
test.ok(res._id);
|
||||
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;
|
||||
test.done();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
test: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
}); });
|
||||
it("test", function () { 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 0: return [4 /*yield*/, sf.test()];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
test.ok(res.test);
|
||||
test.equal(res.user, "test");
|
||||
test.done();
|
||||
chai_1.assert.ok(res.test, "Test went wrong");
|
||||
chai_1.assert.equal(res.user, "test", "Wrong user returned");
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
get: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
}); });
|
||||
it("get", function () { 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 0: return [4 /*yield*/, sf.get(testid)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
test.ok(res);
|
||||
test.equal(res.toString(), testdata.toString());
|
||||
test.done();
|
||||
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*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
list: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
}); });
|
||||
it("list", function () { 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 0: 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?");
|
||||
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) {
|
||||
test.ok(true, "Element is in List");
|
||||
chai_1.assert.ok(true, "Element not in List");
|
||||
}
|
||||
});
|
||||
test.done();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
update: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
}); });
|
||||
it("update", function () { 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 0: 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?");
|
||||
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();
|
||||
test.ok(res2);
|
||||
test.equal(res2.toString(), newTestData.toString(), "Is fetched data the updated?");
|
||||
test.done();
|
||||
chai_1.assert.equal(res2.toString(), newTestData.toString(), "Fetched data not updated");
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
history: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
}); });
|
||||
it("history", function () { 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 0: 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");
|
||||
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();
|
||||
test.equal(arch.toString(), testdata.toString(), "Is old version data correct");
|
||||
test.done();
|
||||
chai_1.assert.equal(arch.toString(), testdata.toString(), "Old version has wrong data");
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
delete: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
}); });
|
||||
it("delete", function () { 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 0: return [4 /*yield*/, sf.delete(testid)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
test.ok(res);
|
||||
test.done();
|
||||
chai_1.assert.ok(res, "Res not set");
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
folder: {
|
||||
create: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
}); });
|
||||
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:
|
||||
test.expect(2);
|
||||
return [4 /*yield*/, sf.create(testname, testdata, "text", testfolder, false, testprev)];
|
||||
case 0: return [4 /*yield*/, sf.create(testname, testdata, "text", testfolder, false, testprev)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
test.ok(res);
|
||||
test.ok(res._id);
|
||||
chai_1.assert.ok(res, "Res not set");
|
||||
chai_1.assert.ok(res._id, "No _id field");
|
||||
ftestid = res._id;
|
||||
testver = res.version;
|
||||
test.done();
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
list: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var res;
|
||||
}); });
|
||||
it("list", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||
var res, found;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
test.expect(4);
|
||||
return [4 /*yield*/, sf.list(testfolder)];
|
||||
case 0: 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?");
|
||||
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) {
|
||||
test.ok(true, "Element is in List");
|
||||
found = true;
|
||||
}
|
||||
});
|
||||
test.done();
|
||||
chai_1.assert.ok(found, "Element is not in List");
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
delete: function (test) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
}); });
|
||||
it("delete", function () { 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 0: return [4 /*yield*/, sf.delete(ftestid)];
|
||||
case 1:
|
||||
res = _a.sent();
|
||||
test.ok(res);
|
||||
test.done();
|
||||
chai_1.assert.ok(res, "Res not set");
|
||||
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*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
}
|
||||
};
|
||||
// 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);
|
||||
});
|
||||
|
226
test.ts
226
test.ts
@ -1,172 +1,146 @@
|
||||
import * as rsa from "node-rsa";
|
||||
import SecureFile from "./index";
|
||||
import { readFileSync } from "fs";
|
||||
import { equal, deepEqual } from "assert";
|
||||
import { Buffer } from "buffer";
|
||||
|
||||
let sf: SecureFile;
|
||||
const testname = "ouiavgbsop687463743"
|
||||
const testdata = new Buffer("Ich bin ein Test");
|
||||
const newTestData = new Buffer("neue test daten");
|
||||
const testprev = new Buffer("Ich bin...");
|
||||
|
||||
let testid: string;
|
||||
let testver;
|
||||
let testver2;
|
||||
|
||||
const testfolder = "iabos";
|
||||
let ftestid;
|
||||
|
||||
let e_testid;
|
||||
let private_key;
|
||||
|
||||
interface TestType {
|
||||
expect(cnt: number): void;
|
||||
ok(value, message?: string): void;
|
||||
equal(actual, expected, message?: string): void;
|
||||
notEqual(actual, expected, message?: string): void;
|
||||
deepEqual(actual, expected, message?: string): void;
|
||||
notDeepEqual(actual, expected, message?: string): void;
|
||||
strictEqual(actual, expected, message?: string): void;
|
||||
notStrictEqual(actual, expected, message?: string): void;
|
||||
throws(block: any, error?, message?: string): void;
|
||||
doesNotThrow(block: any, error?, message?: string): void;
|
||||
ifError(value): void;
|
||||
done(): void;
|
||||
}
|
||||
import { assert } from "chai"
|
||||
|
||||
module.exports = {
|
||||
setUp: function (finished) {
|
||||
let pk = readFileSync("./private.pem");
|
||||
private_key = pk.toString("utf8");
|
||||
sf = new SecureFile("http://localhost:3005", "test", private_key);
|
||||
finished();
|
||||
},
|
||||
create: async function (test: TestType) {
|
||||
test.expect(2);
|
||||
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, false, testprev)
|
||||
test.ok(res);
|
||||
test.ok(res._id);
|
||||
assert.ok(res, "Res is not set");
|
||||
assert.ok(res._id, "Res has not _id");
|
||||
testid = res._id;
|
||||
testver = res.version;
|
||||
test.done();
|
||||
},
|
||||
})
|
||||
|
||||
test: async function (test: TestType) {
|
||||
test.expect(2);
|
||||
it("test", async () => {
|
||||
let res = await sf.test();
|
||||
test.ok(res.test);
|
||||
test.equal(res.user, "test");
|
||||
test.done()
|
||||
},
|
||||
assert.ok(res.test, "Test went wrong");
|
||||
assert.equal(res.user, "test", "Wrong user returned");
|
||||
})
|
||||
|
||||
get: async function (test: TestType) {
|
||||
test.expect(2);
|
||||
it("get", async () => {
|
||||
let res = await sf.get(testid);
|
||||
test.ok(res);
|
||||
test.equal(res.toString(), testdata.toString());
|
||||
test.done();
|
||||
},
|
||||
list: async function (test: TestType) {
|
||||
test.expect(4);
|
||||
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();
|
||||
test.ok(res);
|
||||
test.ok(Array.isArray(res), "Is from type Array");
|
||||
test.ok(res.length > 0, "Do elements exist?")
|
||||
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) {
|
||||
test.ok(true, "Element is in List")
|
||||
assert.ok(true, "Element not in List")
|
||||
}
|
||||
})
|
||||
test.done()
|
||||
},
|
||||
update: async function (test: TestType) {
|
||||
test.expect(5);
|
||||
})
|
||||
|
||||
it("update", async () => {
|
||||
let res = await sf.update(testid, newTestData);
|
||||
test.ok(res);
|
||||
test.ok(res._id);
|
||||
test.notEqual(res.version, testver, "Is new version_id generated?")
|
||||
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);
|
||||
test.ok(res2)
|
||||
test.equal(res2.toString(), newTestData.toString(), "Is fetched data the updated?")
|
||||
test.done();
|
||||
},
|
||||
history: async function (test: TestType) {
|
||||
test.expect(7);
|
||||
let his = await sf.history(testid);
|
||||
test.ok(his)
|
||||
test.ok(his.file)
|
||||
test.ok(his.history);
|
||||
test.equal(his.history.length, 1)
|
||||
assert.equal(res2.toString(), newTestData.toString(), "Fetched data not updated")
|
||||
})
|
||||
|
||||
test.equal(his.history[0].version, testver);
|
||||
test.equal(his.file.version, testver2, "Is correct version");
|
||||
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);
|
||||
test.equal(arch.toString(), testdata.toString(), "Is old version data correct");
|
||||
test.done();
|
||||
},
|
||||
delete: async function (test: TestType) {
|
||||
test.expect(1);
|
||||
assert.equal(arch.toString(), testdata.toString(), "Old version has wrong data");
|
||||
});
|
||||
|
||||
it("delete", async () => {
|
||||
let res = await sf.delete(testid);
|
||||
test.ok(res);
|
||||
test.done();
|
||||
},
|
||||
folder: {
|
||||
create: async function (test: TestType) {
|
||||
test.expect(2);
|
||||
assert.ok(res, "Res not set");
|
||||
})
|
||||
|
||||
describe("folder", () => {
|
||||
it("create", async () => {
|
||||
let res = await sf.create(testname, testdata, "text", testfolder, false, testprev)
|
||||
test.ok(res);
|
||||
test.ok(res._id);
|
||||
assert.ok(res, "Res not set");
|
||||
assert.ok(res._id, "No _id field");
|
||||
ftestid = res._id;
|
||||
testver = res.version;
|
||||
test.done();
|
||||
},
|
||||
list: async function (test: TestType) {
|
||||
test.expect(4);
|
||||
})
|
||||
|
||||
it("list", async () => {
|
||||
let res = await sf.list(testfolder);
|
||||
test.ok(res);
|
||||
test.ok(Array.isArray(res), "Is from type Array");
|
||||
test.ok(res.length > 0, "Do elements exist?")
|
||||
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) {
|
||||
test.ok(true, "Element is in List")
|
||||
found = true;
|
||||
}
|
||||
})
|
||||
test.done()
|
||||
},
|
||||
delete: async function (test: TestType) {
|
||||
test.expect(1);
|
||||
assert.ok(found, "Element is not in List")
|
||||
})
|
||||
|
||||
it("delete", async () => {
|
||||
let res = await sf.delete(ftestid);
|
||||
test.ok(res);
|
||||
test.done();
|
||||
},
|
||||
},
|
||||
encryption: {
|
||||
create: async function (test: TestType) {
|
||||
test.expect(2);
|
||||
let res = await sf.create(testname, testdata, "text", undefined, true, testprev)
|
||||
test.ok(res);
|
||||
test.ok(res._id);
|
||||
e_testid = res._id;
|
||||
testver = res.version;
|
||||
test.done();
|
||||
},
|
||||
get: async function (test: TestType) {
|
||||
let r = new rsa(private_key, "pkcs1-pem");
|
||||
test.expect(2);
|
||||
let res = await sf.get(e_testid);
|
||||
res = r.decrypt(res);
|
||||
test.ok(res);
|
||||
test.equal(res.toString(), testdata.toString());
|
||||
test.done();
|
||||
},
|
||||
delete: async function (test: TestType) {
|
||||
test.expect(1);
|
||||
let res = await sf.delete(e_testid);
|
||||
test.ok(res);
|
||||
test.done();
|
||||
},
|
||||
}
|
||||
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)
|
||||
})
|
Loading…
Reference in New Issue
Block a user