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 Server;
|
||||||
private Username;
|
private Username;
|
||||||
private PrivateKey;
|
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();
|
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<{
|
test(): Promise<{
|
||||||
user: string;
|
user: string;
|
||||||
test: true;
|
test: true;
|
||||||
@ -38,6 +41,14 @@ export declare class Unauthorized extends Error {
|
|||||||
type: string;
|
type: string;
|
||||||
constructor();
|
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 {
|
export declare class NotFound extends Error {
|
||||||
type: string;
|
type: string;
|
||||||
constructor();
|
constructor();
|
||||||
|
95
index.js
95
index.js
@ -49,7 +49,8 @@ var rsa = require("node-rsa");
|
|||||||
require("isomorphic-fetch");
|
require("isomorphic-fetch");
|
||||||
var btb = require("blob-to-buffer");
|
var btb = require("blob-to-buffer");
|
||||||
var SecureFile = /** @class */ (function () {
|
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;
|
this.Server = server;
|
||||||
if (this.Server.endsWith("/")) {
|
if (this.Server.endsWith("/")) {
|
||||||
this.Server += "api";
|
this.Server += "api";
|
||||||
@ -59,6 +60,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
}
|
}
|
||||||
this.Username = username;
|
this.Username = username;
|
||||||
this.PrivateKey = private_key;
|
this.PrivateKey = private_key;
|
||||||
|
this.jwt_enabled = jwt;
|
||||||
}
|
}
|
||||||
SecureFile.prototype.getCode = function () {
|
SecureFile.prototype.getCode = function () {
|
||||||
return __awaiter(this, void 0, void 0, 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 () {
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var code, query_str, first, key, myHeaders, res, err_2;
|
var code, query_str, first, key, myHeaders, res, err_2;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
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:
|
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();
|
code = _a.sent();
|
||||||
query.code = code.code;
|
query.code = code.code;
|
||||||
query.signature = code.signature;
|
query.signature = code.signature;
|
||||||
|
_a.label = 5;
|
||||||
|
case 5:
|
||||||
query_str = "?";
|
query_str = "?";
|
||||||
first = true;
|
first = true;
|
||||||
for (key in query) {
|
for (key in query) {
|
||||||
@ -118,20 +151,24 @@ var SecureFile = /** @class */ (function () {
|
|||||||
myHeaders = new Headers();
|
myHeaders = new Headers();
|
||||||
myHeaders.append('pragma', 'no-cache');
|
myHeaders.append('pragma', 'no-cache');
|
||||||
myHeaders.append('cache-control', 'no-cache');
|
myHeaders.append('cache-control', 'no-cache');
|
||||||
_a.label = 2;
|
_a.label = 6;
|
||||||
case 2:
|
case 6:
|
||||||
_a.trys.push([2, 4, , 5]);
|
_a.trys.push([6, 8, , 9]);
|
||||||
return [4 /*yield*/, fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders })];
|
return [4 /*yield*/, fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders })];
|
||||||
case 3:
|
case 7:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
|
if (res.status === 418) {
|
||||||
|
this.JWT = undefined;
|
||||||
|
return [2 /*return*/, this.makeRequest(endpoint, method, query, body, jwt)];
|
||||||
|
}
|
||||||
return [2 /*return*/, res];
|
return [2 /*return*/, res];
|
||||||
case 4:
|
case 8:
|
||||||
err_2 = _a.sent();
|
err_2 = _a.sent();
|
||||||
if (err_2 instanceof TypeError || err_2.errno === "ECONNREFUSED")
|
if (err_2 instanceof TypeError || err_2.errno === "ECONNREFUSED")
|
||||||
throw new NoConnection();
|
throw new NoConnection();
|
||||||
console.log(err_2);
|
console.log(err_2);
|
||||||
throw 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;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
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:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
@ -159,7 +196,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
case 0:
|
case 0:
|
||||||
if (!folder)
|
if (!folder)
|
||||||
folder = "root";
|
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:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
@ -186,7 +223,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
if (!encrypt) {
|
if (!encrypt) {
|
||||||
params.no_encryption = true;
|
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:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
@ -203,11 +240,11 @@ var SecureFile = /** @class */ (function () {
|
|||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0:
|
||||||
if (!(typeof version === "string")) return [3 /*break*/, 2];
|
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:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
return [3 /*break*/, 4];
|
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:
|
case 3:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
_a.label = 4;
|
_a.label = 4;
|
||||||
@ -250,7 +287,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
put = {};
|
put = {};
|
||||||
if (preview)
|
if (preview)
|
||||||
put.preview = 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:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
@ -264,7 +301,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
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:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
@ -278,7 +315,7 @@ var SecureFile = /** @class */ (function () {
|
|||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
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:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
@ -310,6 +347,26 @@ var Unauthorized = /** @class */ (function (_super) {
|
|||||||
return Unauthorized;
|
return Unauthorized;
|
||||||
}(Error));
|
}(Error));
|
||||||
exports.Unauthorized = Unauthorized;
|
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) {
|
var NotFound = /** @class */ (function (_super) {
|
||||||
__extends(NotFound, _super);
|
__extends(NotFound, _super);
|
||||||
function NotFound() {
|
function NotFound() {
|
||||||
@ -338,7 +395,11 @@ function statusParser(res) {
|
|||||||
case 404:
|
case 404:
|
||||||
throw new NotFound();
|
throw new NotFound();
|
||||||
case 403:
|
case 403:
|
||||||
|
throw new NoPermission();
|
||||||
|
case 401:
|
||||||
throw new Unauthorized();
|
throw new Unauthorized();
|
||||||
|
case 418:
|
||||||
|
throw new InvalidJWT();
|
||||||
default:
|
default:
|
||||||
throw new Error(res.statusText);
|
throw new Error(res.statusText);
|
||||||
}
|
}
|
||||||
|
61
index.ts
61
index.ts
@ -21,7 +21,10 @@ export default class SecureFile {
|
|||||||
private Server: string;
|
private Server: string;
|
||||||
private Username: string;
|
private Username: string;
|
||||||
private PrivateKey: 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;
|
this.Server = server;
|
||||||
if (this.Server.endsWith("/")) {
|
if (this.Server.endsWith("/")) {
|
||||||
this.Server += "api";
|
this.Server += "api";
|
||||||
@ -30,6 +33,7 @@ export default class SecureFile {
|
|||||||
}
|
}
|
||||||
this.Username = username;
|
this.Username = username;
|
||||||
this.PrivateKey = private_key;
|
this.PrivateKey = private_key;
|
||||||
|
this.jwt_enabled = jwt;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getCode() {
|
private async getCode() {
|
||||||
@ -54,10 +58,23 @@ export default class SecureFile {
|
|||||||
return { code: code, signature: r.sign(code).toString("base64") };
|
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();
|
let code = await this.getCode();
|
||||||
query.code = code.code;
|
query.code = code.code;
|
||||||
query.signature = code.signature;
|
query.signature = code.signature;
|
||||||
|
}
|
||||||
|
|
||||||
let query_str = "?";
|
let query_str = "?";
|
||||||
let first = true;
|
let first = true;
|
||||||
for (let key in query) {
|
for (let key in query) {
|
||||||
@ -70,6 +87,10 @@ export default class SecureFile {
|
|||||||
myHeaders.append('cache-control', 'no-cache');
|
myHeaders.append('cache-control', 'no-cache');
|
||||||
try {
|
try {
|
||||||
let res = await fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders });
|
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;
|
return res;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof TypeError || err.errno === "ECONNREFUSED")
|
if (err instanceof TypeError || err.errno === "ECONNREFUSED")
|
||||||
@ -80,14 +101,14 @@ export default class SecureFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async test(): Promise<{ user: string, test: true }> {
|
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);
|
statusParser(res);
|
||||||
return await res.json();
|
return await res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
async list(folder?: string): Promise<File[]> {
|
async list(folder?: string): Promise<File[]> {
|
||||||
if (!folder) folder = "root";
|
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);
|
statusParser(res);
|
||||||
return await res.json();
|
return await res.json();
|
||||||
}
|
}
|
||||||
@ -103,7 +124,7 @@ export default class SecureFile {
|
|||||||
if (!encrypt) {
|
if (!encrypt) {
|
||||||
params.no_encryption = true;
|
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);
|
statusParser(res);
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
@ -111,9 +132,9 @@ export default class SecureFile {
|
|||||||
async get(id: string, version?: string): Promise<Buffer> {
|
async get(id: string, version?: string): Promise<Buffer> {
|
||||||
let res: Response;
|
let res: Response;
|
||||||
if (typeof version === "string") {
|
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 {
|
} else {
|
||||||
res = await this.makeRequest("/files/" + id, "GET", {}, undefined);
|
res = await this.makeRequest("/files/" + id, "GET", {}, undefined, this.jwt_enabled);
|
||||||
}
|
}
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
if ((<any>res).buffer) {
|
if ((<any>res).buffer) {
|
||||||
@ -131,19 +152,19 @@ export default class SecureFile {
|
|||||||
async update(id: string, data: Buffer, preview?: Buffer): Promise<File> {
|
async update(id: string, data: Buffer, preview?: Buffer): Promise<File> {
|
||||||
let put: any = {};
|
let put: any = {};
|
||||||
if (preview) put.preview = preview;
|
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);
|
statusParser(res);
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id: string): Promise<boolean> {
|
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);
|
statusParser(res);
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
async history(id: string): Promise<History> {
|
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);
|
statusParser(res);
|
||||||
return res.json();
|
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 {
|
export class NotFound extends Error {
|
||||||
type: string;
|
type: string;
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -189,7 +226,11 @@ function statusParser(res: Response) {
|
|||||||
case 404:
|
case 404:
|
||||||
throw new NotFound();
|
throw new NotFound();
|
||||||
case 403:
|
case 403:
|
||||||
|
throw new NoPermission();
|
||||||
|
case 401:
|
||||||
throw new Unauthorized();
|
throw new Unauthorized();
|
||||||
|
case 418:
|
||||||
|
throw new InvalidJWT();
|
||||||
default:
|
default:
|
||||||
throw new Error(res.statusText);
|
throw new Error(res.statusText);
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,8 @@
|
|||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"test": "nodeunit test.js"
|
"watch": "tsc --watch",
|
||||||
|
"test": "mocha test.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"blob-to-buffer": "^1.2.7",
|
"blob-to-buffer": "^1.2.7",
|
||||||
@ -16,10 +17,14 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/blob-to-buffer": "^1.2.0",
|
"@types/blob-to-buffer": "^1.2.0",
|
||||||
|
"@types/chai": "^4.1.4",
|
||||||
"@types/isomorphic-fetch": "^0.0.34",
|
"@types/isomorphic-fetch": "^0.0.34",
|
||||||
|
"@types/mocha": "^5.2.2",
|
||||||
"@types/node": "^9.4.0",
|
"@types/node": "^9.4.0",
|
||||||
"@types/node-fetch": "^1.6.7",
|
"@types/node-fetch": "^1.6.7",
|
||||||
"@types/node-rsa": "^0.4.1",
|
"@types/node-rsa": "^0.4.1",
|
||||||
|
"chai": "^4.1.2",
|
||||||
|
"mocha": "^5.2.0",
|
||||||
"nodeunit": "^0.11.2",
|
"nodeunit": "^0.11.2",
|
||||||
"typescript": "^2.6.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
|
277
test.js
277
test.js
@ -35,297 +35,212 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
var rsa = require("node-rsa");
|
|
||||||
var index_1 = require("./index");
|
var index_1 = require("./index");
|
||||||
var fs_1 = require("fs");
|
var fs_1 = require("fs");
|
||||||
var buffer_1 = require("buffer");
|
var buffer_1 = require("buffer");
|
||||||
var sf;
|
|
||||||
var testname = "ouiavgbsop687463743";
|
var testname = "ouiavgbsop687463743";
|
||||||
var testdata = new buffer_1.Buffer("Ich bin ein Test");
|
var testdata = new buffer_1.Buffer("Ich bin ein Test");
|
||||||
var newTestData = new buffer_1.Buffer("neue test daten");
|
var newTestData = new buffer_1.Buffer("neue test daten");
|
||||||
var testprev = new buffer_1.Buffer("Ich bin...");
|
var testprev = new buffer_1.Buffer("Ich bin...");
|
||||||
var testid;
|
|
||||||
var testver;
|
|
||||||
var testver2;
|
|
||||||
var testfolder = "iabos";
|
var testfolder = "iabos";
|
||||||
var ftestid;
|
var ftestid;
|
||||||
var e_testid;
|
var e_testid;
|
||||||
var private_key;
|
var private_key;
|
||||||
module.exports = {
|
var chai_1 = require("chai");
|
||||||
setUp: function (finished) {
|
function test(sf) {
|
||||||
var pk = fs_1.readFileSync("./private.pem");
|
var _this = this;
|
||||||
private_key = pk.toString("utf8");
|
var testid;
|
||||||
sf = new index_1.default("http://localhost:3005", "test", private_key);
|
var testver;
|
||||||
finished();
|
var testver2;
|
||||||
},
|
it("create", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
create: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, false, testprev)];
|
||||||
test.expect(2);
|
|
||||||
return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, false, testprev)];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res);
|
chai_1.assert.ok(res, "Res is not set");
|
||||||
test.ok(res._id);
|
chai_1.assert.ok(res._id, "Res has not _id");
|
||||||
testid = res._id;
|
testid = res._id;
|
||||||
testver = res.version;
|
testver = res.version;
|
||||||
test.done();
|
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
it("test", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
test: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.test()];
|
||||||
test.expect(2);
|
|
||||||
return [4 /*yield*/, sf.test()];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res.test);
|
chai_1.assert.ok(res.test, "Test went wrong");
|
||||||
test.equal(res.user, "test");
|
chai_1.assert.equal(res.user, "test", "Wrong user returned");
|
||||||
test.done();
|
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
it("get", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
get: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.get(testid)];
|
||||||
test.expect(2);
|
|
||||||
return [4 /*yield*/, sf.get(testid)];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res);
|
chai_1.assert.ok(res, "No date returned");
|
||||||
test.equal(res.toString(), testdata.toString());
|
chai_1.assert.equal(res.toString(), testdata.toString(), "Returned data not equal to stored");
|
||||||
test.done();
|
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
it("list", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
list: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.list()];
|
||||||
test.expect(4);
|
|
||||||
return [4 /*yield*/, sf.list()];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res);
|
chai_1.assert.ok(res);
|
||||||
test.ok(Array.isArray(res), "Is from type Array");
|
chai_1.assert.ok(Array.isArray(res), "Is not from type Array");
|
||||||
test.ok(res.length > 0, "Do elements exist?");
|
chai_1.assert.ok(res.length > 0, "No elements returned");
|
||||||
res.forEach(function (e) {
|
res.forEach(function (e) {
|
||||||
if (e._id === testid) {
|
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*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
it("update", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
update: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res, res2;
|
var res, res2;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.update(testid, newTestData)];
|
||||||
test.expect(5);
|
|
||||||
return [4 /*yield*/, sf.update(testid, newTestData)];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res);
|
chai_1.assert.ok(res, "No data returned");
|
||||||
test.ok(res._id);
|
chai_1.assert.ok(res._id, "_id missing");
|
||||||
test.notEqual(res.version, testver, "Is new version_id generated?");
|
chai_1.assert.notEqual(res.version, testver, "No new version was created");
|
||||||
testver2 = res.version;
|
testver2 = res.version;
|
||||||
return [4 /*yield*/, sf.get(testid)];
|
return [4 /*yield*/, sf.get(testid)];
|
||||||
case 2:
|
case 2:
|
||||||
res2 = _a.sent();
|
res2 = _a.sent();
|
||||||
test.ok(res2);
|
chai_1.assert.equal(res2.toString(), newTestData.toString(), "Fetched data not updated");
|
||||||
test.equal(res2.toString(), newTestData.toString(), "Is fetched data the updated?");
|
|
||||||
test.done();
|
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
it("history", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
history: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var his, arch;
|
var his, arch;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.history(testid)];
|
||||||
test.expect(7);
|
|
||||||
return [4 /*yield*/, sf.history(testid)];
|
|
||||||
case 1:
|
case 1:
|
||||||
his = _a.sent();
|
his = _a.sent();
|
||||||
test.ok(his);
|
chai_1.assert.ok(his, "no data returned");
|
||||||
test.ok(his.file);
|
chai_1.assert.ok(his.file, "file not set");
|
||||||
test.ok(his.history);
|
chai_1.assert.ok(his.history, "history not set");
|
||||||
test.equal(his.history.length, 1);
|
chai_1.assert.equal(his.history.length, 1, "Not expected history length. Expected 1 got " + his.history.length);
|
||||||
test.equal(his.history[0].version, testver);
|
chai_1.assert.equal(his.history[0].version, testver, "Wrong version on history");
|
||||||
test.equal(his.file.version, testver2, "Is correct version");
|
chai_1.assert.equal(his.file.version, testver2, "Wrong version on file");
|
||||||
return [4 /*yield*/, sf.get(testid, testver)];
|
return [4 /*yield*/, sf.get(testid, testver)];
|
||||||
case 2:
|
case 2:
|
||||||
arch = _a.sent();
|
arch = _a.sent();
|
||||||
test.equal(arch.toString(), testdata.toString(), "Is old version data correct");
|
chai_1.assert.equal(arch.toString(), testdata.toString(), "Old version has wrong data");
|
||||||
test.done();
|
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
it("delete", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
delete: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.delete(testid)];
|
||||||
test.expect(1);
|
|
||||||
return [4 /*yield*/, sf.delete(testid)];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res);
|
chai_1.assert.ok(res, "Res not set");
|
||||||
test.done();
|
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
describe("folder", function () {
|
||||||
folder: {
|
it("create", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
create: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.create(testname, testdata, "text", testfolder, false, testprev)];
|
||||||
test.expect(2);
|
|
||||||
return [4 /*yield*/, sf.create(testname, testdata, "text", testfolder, false, testprev)];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res);
|
chai_1.assert.ok(res, "Res not set");
|
||||||
test.ok(res._id);
|
chai_1.assert.ok(res._id, "No _id field");
|
||||||
ftestid = res._id;
|
ftestid = res._id;
|
||||||
testver = res.version;
|
testver = res.version;
|
||||||
test.done();
|
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
it("list", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
list: function (test) {
|
var res, found;
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res;
|
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.list(testfolder)];
|
||||||
test.expect(4);
|
|
||||||
return [4 /*yield*/, sf.list(testfolder)];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res);
|
chai_1.assert.ok(res);
|
||||||
test.ok(Array.isArray(res), "Is from type Array");
|
chai_1.assert.ok(Array.isArray(res), "Is from type Array");
|
||||||
test.ok(res.length > 0, "Do elements exist?");
|
chai_1.assert.ok(res.length > 0, "Do elements exist?");
|
||||||
|
found = false;
|
||||||
res.forEach(function (e) {
|
res.forEach(function (e) {
|
||||||
if (e._id === ftestid) {
|
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*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}); });
|
||||||
},
|
it("delete", function () { return __awaiter(_this, void 0, void 0, function () {
|
||||||
delete: function (test) {
|
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
|
||||||
var res;
|
var res;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0: return [4 /*yield*/, sf.delete(ftestid)];
|
||||||
test.expect(1);
|
|
||||||
return [4 /*yield*/, sf.delete(ftestid)];
|
|
||||||
case 1:
|
case 1:
|
||||||
res = _a.sent();
|
res = _a.sent();
|
||||||
test.ok(res);
|
chai_1.assert.ok(res, "Res not set");
|
||||||
test.done();
|
|
||||||
return [2 /*return*/];
|
return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}); });
|
||||||
});
|
});
|
||||||
},
|
// describe("encryption", () => {
|
||||||
},
|
// it("create", async () => {
|
||||||
encryption: {
|
// let res = await sf.create(testname, testdata, "text", undefined, true, testprev)
|
||||||
create: function (test) {
|
// assert.ok(res, "Res not set");
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
// assert.ok(res._id, "_id not set");
|
||||||
var res;
|
// e_testid = res._id;
|
||||||
return __generator(this, function (_a) {
|
// testver = res.version;
|
||||||
switch (_a.label) {
|
// })
|
||||||
case 0:
|
// it("get", async () => {
|
||||||
test.expect(2);
|
// let r = new rsa(private_key, "pkcs1-pem");
|
||||||
return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, true, testprev)];
|
// let res = await sf.get(e_testid);
|
||||||
case 1:
|
// res = r.decrypt(res);
|
||||||
res = _a.sent();
|
// assert.ok(res, "Res not set");
|
||||||
test.ok(res);
|
// assert.equal(res.toString(), testdata.toString(), "Unexpected data change");
|
||||||
test.ok(res._id);
|
// })
|
||||||
e_testid = res._id;
|
// it("delete", async () => {
|
||||||
testver = res.version;
|
// let res = await sf.delete(e_testid);
|
||||||
test.done();
|
// assert.ok(res, "Res not set");
|
||||||
return [2 /*return*/];
|
// })
|
||||||
|
// })
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
});
|
});
|
||||||
},
|
|
||||||
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*/];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
226
test.ts
226
test.ts
@ -1,172 +1,146 @@
|
|||||||
import * as rsa from "node-rsa";
|
import * as rsa from "node-rsa";
|
||||||
import SecureFile from "./index";
|
import SecureFile from "./index";
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import { equal, deepEqual } from "assert";
|
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
|
|
||||||
let sf: SecureFile;
|
|
||||||
const testname = "ouiavgbsop687463743"
|
const testname = "ouiavgbsop687463743"
|
||||||
const testdata = new Buffer("Ich bin ein Test");
|
const testdata = new Buffer("Ich bin ein Test");
|
||||||
const newTestData = new Buffer("neue test daten");
|
const newTestData = new Buffer("neue test daten");
|
||||||
const testprev = new Buffer("Ich bin...");
|
const testprev = new Buffer("Ich bin...");
|
||||||
|
|
||||||
let testid: string;
|
|
||||||
let testver;
|
|
||||||
let testver2;
|
|
||||||
|
|
||||||
const testfolder = "iabos";
|
const testfolder = "iabos";
|
||||||
let ftestid;
|
let ftestid;
|
||||||
|
|
||||||
let e_testid;
|
let e_testid;
|
||||||
let private_key;
|
let private_key;
|
||||||
|
|
||||||
interface TestType {
|
import { assert } from "chai"
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
function test(sf: SecureFile) {
|
||||||
setUp: function (finished) {
|
let testid: string;
|
||||||
let pk = readFileSync("./private.pem");
|
let testver: string;
|
||||||
private_key = pk.toString("utf8");
|
let testver2: string;
|
||||||
sf = new SecureFile("http://localhost:3005", "test", private_key);
|
|
||||||
finished();
|
it("create", async () => {
|
||||||
},
|
|
||||||
create: async function (test: TestType) {
|
|
||||||
test.expect(2);
|
|
||||||
let res = await sf.create(testname, testdata, "text", undefined, false, testprev)
|
let res = await sf.create(testname, testdata, "text", undefined, false, testprev)
|
||||||
test.ok(res);
|
assert.ok(res, "Res is not set");
|
||||||
test.ok(res._id);
|
assert.ok(res._id, "Res has not _id");
|
||||||
testid = res._id;
|
testid = res._id;
|
||||||
testver = res.version;
|
testver = res.version;
|
||||||
test.done();
|
})
|
||||||
},
|
|
||||||
|
|
||||||
test: async function (test: TestType) {
|
it("test", async () => {
|
||||||
test.expect(2);
|
|
||||||
let res = await sf.test();
|
let res = await sf.test();
|
||||||
test.ok(res.test);
|
assert.ok(res.test, "Test went wrong");
|
||||||
test.equal(res.user, "test");
|
assert.equal(res.user, "test", "Wrong user returned");
|
||||||
test.done()
|
})
|
||||||
},
|
|
||||||
|
|
||||||
get: async function (test: TestType) {
|
it("get", async () => {
|
||||||
test.expect(2);
|
|
||||||
let res = await sf.get(testid);
|
let res = await sf.get(testid);
|
||||||
test.ok(res);
|
assert.ok(res, "No date returned");
|
||||||
test.equal(res.toString(), testdata.toString());
|
assert.equal(res.toString(), testdata.toString(), "Returned data not equal to stored");
|
||||||
test.done();
|
})
|
||||||
},
|
|
||||||
list: async function (test: TestType) {
|
it("list", async () => {
|
||||||
test.expect(4);
|
|
||||||
let res = await sf.list();
|
let res = await sf.list();
|
||||||
test.ok(res);
|
assert.ok(res);
|
||||||
test.ok(Array.isArray(res), "Is from type Array");
|
assert.ok(Array.isArray(res), "Is not from type Array");
|
||||||
test.ok(res.length > 0, "Do elements exist?")
|
assert.ok(res.length > 0, "No elements returned")
|
||||||
res.forEach(e => {
|
res.forEach(e => {
|
||||||
if (e._id === testid) {
|
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) {
|
it("update", async () => {
|
||||||
test.expect(5);
|
|
||||||
let res = await sf.update(testid, newTestData);
|
let res = await sf.update(testid, newTestData);
|
||||||
test.ok(res);
|
assert.ok(res, "No data returned");
|
||||||
test.ok(res._id);
|
assert.ok(res._id, "_id missing");
|
||||||
test.notEqual(res.version, testver, "Is new version_id generated?")
|
assert.notEqual(res.version, testver, "No new version was created")
|
||||||
testver2 = res.version;
|
testver2 = res.version;
|
||||||
let res2 = await sf.get(testid);
|
let res2 = await sf.get(testid);
|
||||||
test.ok(res2)
|
assert.equal(res2.toString(), newTestData.toString(), "Fetched data not updated")
|
||||||
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)
|
|
||||||
|
|
||||||
test.equal(his.history[0].version, testver);
|
it("history", async () => {
|
||||||
test.equal(his.file.version, testver2, "Is correct version");
|
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);
|
let arch = await sf.get(testid, testver);
|
||||||
test.equal(arch.toString(), testdata.toString(), "Is old version data correct");
|
assert.equal(arch.toString(), testdata.toString(), "Old version has wrong data");
|
||||||
test.done();
|
});
|
||||||
},
|
|
||||||
delete: async function (test: TestType) {
|
it("delete", async () => {
|
||||||
test.expect(1);
|
|
||||||
let res = await sf.delete(testid);
|
let res = await sf.delete(testid);
|
||||||
test.ok(res);
|
assert.ok(res, "Res not set");
|
||||||
test.done();
|
})
|
||||||
},
|
|
||||||
folder: {
|
describe("folder", () => {
|
||||||
create: async function (test: TestType) {
|
it("create", async () => {
|
||||||
test.expect(2);
|
|
||||||
let res = await sf.create(testname, testdata, "text", testfolder, false, testprev)
|
let res = await sf.create(testname, testdata, "text", testfolder, false, testprev)
|
||||||
test.ok(res);
|
assert.ok(res, "Res not set");
|
||||||
test.ok(res._id);
|
assert.ok(res._id, "No _id field");
|
||||||
ftestid = res._id;
|
ftestid = res._id;
|
||||||
testver = res.version;
|
testver = res.version;
|
||||||
test.done();
|
})
|
||||||
},
|
|
||||||
list: async function (test: TestType) {
|
it("list", async () => {
|
||||||
test.expect(4);
|
|
||||||
let res = await sf.list(testfolder);
|
let res = await sf.list(testfolder);
|
||||||
test.ok(res);
|
assert.ok(res);
|
||||||
test.ok(Array.isArray(res), "Is from type Array");
|
assert.ok(Array.isArray(res), "Is from type Array");
|
||||||
test.ok(res.length > 0, "Do elements exist?")
|
assert.ok(res.length > 0, "Do elements exist?")
|
||||||
|
let found = false;
|
||||||
res.forEach(e => {
|
res.forEach(e => {
|
||||||
if (e._id === ftestid) {
|
if (e._id === ftestid) {
|
||||||
test.ok(true, "Element is in List")
|
found = true;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
test.done()
|
assert.ok(found, "Element is not in List")
|
||||||
},
|
})
|
||||||
delete: async function (test: TestType) {
|
|
||||||
test.expect(1);
|
it("delete", async () => {
|
||||||
let res = await sf.delete(ftestid);
|
let res = await sf.delete(ftestid);
|
||||||
test.ok(res);
|
assert.ok(res, "Res not set");
|
||||||
test.done();
|
});
|
||||||
},
|
})
|
||||||
},
|
|
||||||
encryption: {
|
// describe("encryption", () => {
|
||||||
create: async function (test: TestType) {
|
// it("create", async () => {
|
||||||
test.expect(2);
|
// let res = await sf.create(testname, testdata, "text", undefined, true, testprev)
|
||||||
let res = await sf.create(testname, testdata, "text", undefined, true, testprev)
|
// assert.ok(res, "Res not set");
|
||||||
test.ok(res);
|
// assert.ok(res._id, "_id not set");
|
||||||
test.ok(res._id);
|
// e_testid = res._id;
|
||||||
e_testid = res._id;
|
// testver = res.version;
|
||||||
testver = res.version;
|
// })
|
||||||
test.done();
|
// it("get", async () => {
|
||||||
},
|
// let r = new rsa(private_key, "pkcs1-pem");
|
||||||
get: async function (test: TestType) {
|
// let res = await sf.get(e_testid);
|
||||||
let r = new rsa(private_key, "pkcs1-pem");
|
// res = r.decrypt(res);
|
||||||
test.expect(2);
|
// assert.ok(res, "Res not set");
|
||||||
let res = await sf.get(e_testid);
|
// assert.equal(res.toString(), testdata.toString(), "Unexpected data change");
|
||||||
res = r.decrypt(res);
|
// })
|
||||||
test.ok(res);
|
// it("delete", async () => {
|
||||||
test.equal(res.toString(), testdata.toString());
|
// let res = await sf.delete(e_testid);
|
||||||
test.done();
|
// assert.ok(res, "Res not set");
|
||||||
},
|
// })
|
||||||
delete: async function (test: TestType) {
|
// })
|
||||||
test.expect(1);
|
|
||||||
let res = await sf.delete(e_testid);
|
|
||||||
test.ok(res);
|
|
||||||
test.done();
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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