forked from hibas123/SecureFileWrapper
Compare commits
2 Commits
a8d5382ac3
...
c63ff06453
Author | SHA1 | Date | |
---|---|---|---|
|
c63ff06453 | ||
|
8e0b859408 |
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,4 +1,4 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
yarn.lock
|
yarn.lock
|
||||||
private.pem
|
private.pem
|
||||||
lib/
|
lib/
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
tsconfig.json
|
tsconfig.json
|
||||||
node_modules/
|
node_modules/
|
||||||
.vscode/
|
.vscode/
|
2506
package-lock.json
generated
2506
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
55
package.json
55
package.json
@ -1,28 +1,29 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/secure-file-wrapper",
|
"name": "@hibas123/secure-file-wrapper",
|
||||||
"version": "2.3.1",
|
"version": "2.3.3",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"types": "lib/index.d.ts",
|
"types": "lib/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"watch": "tsc --watch",
|
"watch": "tsc --watch",
|
||||||
"prepublishOnly": "tsc",
|
"prepublishOnly": "tsc",
|
||||||
"test": "mocha lib/test.js"
|
"test": "mocha lib/test.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cross-fetch": "^3.0.0",
|
"@hibas123/utils": "^1.0.1",
|
||||||
"uuid": "^3.3.2"
|
"cross-fetch": "^3.0.1",
|
||||||
},
|
"uuid": "^3.3.2"
|
||||||
"devDependencies": {
|
},
|
||||||
"@types/chai": "^4.1.4",
|
"devDependencies": {
|
||||||
"@types/mocha": "^5.2.2",
|
"@types/chai": "^4.1.4",
|
||||||
"@types/node": "^10.12.18",
|
"@types/mocha": "^5.2.6",
|
||||||
"@types/node-fetch": "^2.1.4",
|
"@types/node": "^11.10.5",
|
||||||
"@types/uuid": "^3.4.4",
|
"@types/node-fetch": "^2.1.6",
|
||||||
"chai": "^4.1.2",
|
"@types/uuid": "^3.4.4",
|
||||||
"mocha": "^5.2.0",
|
"chai": "^4.1.2",
|
||||||
"typescript": "^3.2.2"
|
"mocha": "^6.0.2",
|
||||||
}
|
"typescript": "^3.3.3333"
|
||||||
|
}
|
||||||
}
|
}
|
468
src/index.ts
468
src/index.ts
@ -1,235 +1,235 @@
|
|||||||
import Observable from "./observable";
|
import { Observable, Lock } from "@hibas123/utils";
|
||||||
import Lock from "./lock";
|
//this references global on node and window in browser
|
||||||
import fetch from "cross-fetch"
|
const fetch = this.fetch ? this.fetch : require("cross-fetch").default;
|
||||||
|
|
||||||
export interface IFileVersion {
|
export interface IFileVersion {
|
||||||
version: string;
|
version: string;
|
||||||
time: Date;
|
time: Date;
|
||||||
preview: string;
|
preview: string;
|
||||||
deleted: boolean;
|
deleted: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IFile {
|
export interface IFile {
|
||||||
_id: string;
|
_id: string;
|
||||||
type: string;
|
type: string;
|
||||||
name: string;
|
name: string;
|
||||||
folder: string;
|
folder: string;
|
||||||
deleted: boolean;
|
deleted: boolean;
|
||||||
active: IFileVersion;
|
active: IFileVersion;
|
||||||
versions: IFileVersion[];
|
versions: IFileVersion[];
|
||||||
user: string;
|
user: string;
|
||||||
application: string;
|
application: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IHistory {
|
export interface IHistory {
|
||||||
file: IFile;
|
file: IFile;
|
||||||
history: IFileVersion[];
|
history: IFileVersion[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export class NoConnection extends Error {
|
export class NoConnection extends Error {
|
||||||
type: string;
|
type: string;
|
||||||
constructor() {
|
constructor() {
|
||||||
super("No connection");
|
super("No connection");
|
||||||
this.type = "noconnection"
|
this.type = "noconnection"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Unauthorized extends Error {
|
export class Unauthorized extends Error {
|
||||||
type: string;
|
type: string;
|
||||||
constructor() {
|
constructor() {
|
||||||
super("Not authorized");
|
super("Not authorized");
|
||||||
this.type = "unauthorized"
|
this.type = "unauthorized"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NoPermission extends Error {
|
export class NoPermission extends Error {
|
||||||
type: string;
|
type: string;
|
||||||
constructor() {
|
constructor() {
|
||||||
super("No permission");
|
super("No permission");
|
||||||
this.type = "nopermission"
|
this.type = "nopermission"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NotFound extends Error {
|
export class NotFound extends Error {
|
||||||
type: string;
|
type: string;
|
||||||
constructor() {
|
constructor() {
|
||||||
super("Not found");
|
super("Not found");
|
||||||
this.type = "notfound"
|
this.type = "notfound"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class BadRequest extends Error {
|
export class BadRequest extends Error {
|
||||||
type: string;
|
type: string;
|
||||||
constructor() {
|
constructor() {
|
||||||
super("Bad request");
|
super("Bad request");
|
||||||
this.type = "badrequest"
|
this.type = "badrequest"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function statusParser(res: Response) {
|
function statusParser(res: Response) {
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
switch (res.status) {
|
switch (res.status) {
|
||||||
case 400:
|
case 400:
|
||||||
throw new BadRequest();
|
throw new BadRequest();
|
||||||
case 404:
|
case 404:
|
||||||
throw new NotFound();
|
throw new NotFound();
|
||||||
case 403:
|
case 403:
|
||||||
throw new NoPermission();
|
throw new NoPermission();
|
||||||
case 401:
|
case 401:
|
||||||
throw new Unauthorized();
|
throw new Unauthorized();
|
||||||
default:
|
default:
|
||||||
throw new Error(res.statusText);
|
throw new Error(res.statusText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class SecureFileWrapper {
|
export default class SecureFileWrapper {
|
||||||
private _jwtObservableServer: Observable<(jwt: string) => void> = new Observable(false);
|
private _jwtObservableServer: Observable<(jwt: string) => void> = new Observable(false);
|
||||||
jwtObservable = this._jwtObservableServer.getPublicApi();
|
jwtObservable = this._jwtObservableServer.getPublicApi();
|
||||||
|
|
||||||
private jwt: string;
|
private jwt: string;
|
||||||
|
|
||||||
private auth_lock = new Lock();
|
private auth_lock = new Lock();
|
||||||
|
|
||||||
constructor(private server: string) {
|
constructor(private server: string) {
|
||||||
if (this.server.endsWith("/")) {
|
if (this.server.endsWith("/")) {
|
||||||
this.server += "api/v1";
|
this.server += "api/v1";
|
||||||
} else {
|
} else {
|
||||||
this.server += "/api/v1";
|
this.server += "/api/v1";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getJWT() {
|
public async getJWT() {
|
||||||
if (!this.auth_lock.locked) {
|
if (!this.auth_lock.locked) {
|
||||||
let lock = await this.auth_lock.getLock();
|
let lock = await this.auth_lock.getLock();
|
||||||
this._jwtObservableServer.send((jwt: string) => {
|
this._jwtObservableServer.send((jwt: string) => {
|
||||||
this.jwt = jwt;
|
this.jwt = jwt;
|
||||||
lock.release();
|
lock.release();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.auth_lock.getLock().then(lock => lock.release())
|
await this.auth_lock.getLock().then(lock => lock.release())
|
||||||
}
|
}
|
||||||
|
|
||||||
public async makeRequest(endpoint: string, method: "POST" | "GET" | "PUT" | "DELETE", query: any, body?: ArrayBuffer | ArrayBufferView, second = false) {
|
public async makeRequest(endpoint: string, method: "POST" | "GET" | "PUT" | "DELETE", query: any, body?: ArrayBuffer | ArrayBufferView, second = false) {
|
||||||
if (!this.jwt || this.jwt === undefined) {
|
if (!this.jwt || this.jwt === undefined) {
|
||||||
await this.getJWT();
|
await this.getJWT();
|
||||||
}
|
}
|
||||||
|
|
||||||
query.jwt = this.jwt;
|
query.jwt = this.jwt;
|
||||||
let query_str = "?";
|
let query_str = "?";
|
||||||
let first = true;
|
let first = true;
|
||||||
for (let key in query) {
|
for (let key in query) {
|
||||||
if (!first) query_str += "&";
|
if (!first) query_str += "&";
|
||||||
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
|
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
var headers = {
|
var headers = {
|
||||||
"pragme": "no-cache",
|
"pragme": "no-cache",
|
||||||
"cache-control": "no-cache"
|
"cache-control": "no-cache"
|
||||||
};
|
};
|
||||||
|
|
||||||
if (body) {
|
if (body) {
|
||||||
headers["Content-Type"] = "application/octet-stream"
|
headers["Content-Type"] = "application/octet-stream"
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
let res = await fetch(this.server + endpoint + query_str, { method, body, headers });
|
let res = await fetch(this.server + endpoint + query_str, { method, body, headers });
|
||||||
if (res.status === 401 && !second) {
|
if (res.status === 401 && !second) {
|
||||||
await this.getJWT();
|
await this.getJWT();
|
||||||
return this.makeRequest(endpoint, method, query, body, true);
|
return this.makeRequest(endpoint, method, query, body, true);
|
||||||
} else {
|
} else {
|
||||||
statusParser(res);
|
statusParser(res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof TypeError || err.errno === "ECONNREFUSED")
|
if (err instanceof TypeError || err.errno === "ECONNREFUSED")
|
||||||
throw new NoConnection();
|
throw new NoConnection();
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fixIFileVersion(version: IFileVersion): IFileVersion {
|
private fixIFileVersion(version: IFileVersion): IFileVersion {
|
||||||
version.time = new Date(version.time)
|
version.time = new Date(version.time)
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
private fixIFile(file: IFile): IFile {
|
private fixIFile(file: IFile): IFile {
|
||||||
file.active.time = new Date(file.active.time)
|
file.active.time = new Date(file.active.time)
|
||||||
if (file.versions) {
|
if (file.versions) {
|
||||||
file.versions = file.versions.map(e => this.fixIFileVersion(e))
|
file.versions = file.versions.map(e => this.fixIFileVersion(e))
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
// async test(jwt): Promise<{ user: string, test: true }> {
|
// async test(jwt): Promise<{ user: string, test: true }> {
|
||||||
// let res = await this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled);
|
// 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<IFile[]> {
|
async list(folder?: string): Promise<IFile[]> {
|
||||||
let query: any = {}
|
let query: any = {}
|
||||||
if (folder) query.folder = folder;
|
if (folder) query.folder = folder;
|
||||||
let res = await this.makeRequest("/files", "GET", query);
|
let res = await this.makeRequest("/files", "GET", query);
|
||||||
let d: { files: IFile[] } = await res.json();
|
let d: { files: IFile[] } = await res.json();
|
||||||
return d.files.map(e => this.fixIFile(e));
|
return d.files.map(e => this.fixIFile(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(name: string, data: ArrayBuffer | ArrayBufferView, type: "text" | "binary", folder?: string, preview?: string, id?: string, date?: Date): Promise<IFile> {
|
async create(name: string, data: ArrayBuffer | ArrayBufferView, type: "text" | "binary", folder?: string, preview?: string, id?: string, date?: Date): Promise<IFile> {
|
||||||
let params: any = { type: type, name: name };
|
let params: any = { type: type, name: name };
|
||||||
if (preview)
|
if (preview)
|
||||||
params.preview = preview;
|
params.preview = preview;
|
||||||
|
|
||||||
if (folder)
|
if (folder)
|
||||||
params.folder = folder;
|
params.folder = folder;
|
||||||
|
|
||||||
if (id)
|
if (id)
|
||||||
params.id = id
|
params.id = id
|
||||||
|
|
||||||
if (date)
|
if (date)
|
||||||
params.date = date.toJSON()
|
params.date = date.toJSON()
|
||||||
|
|
||||||
|
|
||||||
let res = await this.makeRequest("/files", "POST", params, data);
|
let res = await this.makeRequest("/files", "POST", params, data);
|
||||||
return this.fixIFile((await res.json()).file);
|
return this.fixIFile((await res.json()).file);
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(id: string, version?: string): Promise<ArrayBuffer> {
|
async get(id: string, version?: string): Promise<ArrayBuffer> {
|
||||||
let res: Response;
|
let res: Response;
|
||||||
if (typeof version === "string") {
|
if (typeof version === "string") {
|
||||||
res = await this.makeRequest(`/files/${id}/history/${version}`, "GET", {});
|
res = await this.makeRequest(`/files/${id}/history/${version}`, "GET", {});
|
||||||
} else {
|
} else {
|
||||||
res = await this.makeRequest("/files/" + id, "GET", {});
|
res = await this.makeRequest("/files/" + id, "GET", {});
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.arrayBuffer()
|
return res.arrayBuffer()
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string, date?: Date, old = false): Promise<IFile> {
|
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string, date?: Date, old = false): Promise<IFile> {
|
||||||
let params: any = { old };
|
let params: any = { old };
|
||||||
if (preview) params.preview = preview;
|
if (preview) params.preview = preview;
|
||||||
if (date)
|
if (date)
|
||||||
params.date = date.toJSON()
|
params.date = date.toJSON()
|
||||||
let res = await this.makeRequest("/files/" + id, "PUT", params, data);
|
let res = await this.makeRequest("/files/" + id, "PUT", params, data);
|
||||||
|
|
||||||
let json = await res.json()
|
let json = await res.json()
|
||||||
return this.fixIFile(json.file);
|
return this.fixIFile(json.file);
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id: string): Promise<void> {
|
async delete(id: string): Promise<void> {
|
||||||
let res = await this.makeRequest("/files/" + id, "DELETE", {});
|
let res = await this.makeRequest("/files/" + id, "DELETE", {});
|
||||||
}
|
}
|
||||||
|
|
||||||
async history(id: string): Promise<IHistory> {
|
async history(id: string): Promise<IHistory> {
|
||||||
let res = await this.makeRequest(`/files/${id}/history`, "GET", {});
|
let res = await this.makeRequest(`/files/${id}/history`, "GET", {});
|
||||||
let data: IHistory = await res.json();
|
let data: IHistory = await res.json();
|
||||||
data.file = this.fixIFile(data.file)
|
data.file = this.fixIFile(data.file)
|
||||||
data.history = data.history.map(v => this.fixIFileVersion(v));
|
data.history = data.history.map(v => this.fixIFileVersion(v));
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async restore(id: string, version: string) {
|
async restore(id: string, version: string) {
|
||||||
await this.makeRequest(`/files/${id}/history/${version}/restore`, "PUT", {});
|
await this.makeRequest(`/files/${id}/history/${version}/restore`, "PUT", {});
|
||||||
}
|
}
|
||||||
}
|
}
|
36
src/lock.ts
36
src/lock.ts
@ -1,36 +0,0 @@
|
|||||||
export type Release = { release: () => void };
|
|
||||||
export default class Lock {
|
|
||||||
private _locked: boolean = false;
|
|
||||||
get locked() {
|
|
||||||
return this._locked;
|
|
||||||
}
|
|
||||||
private toCome: (() => void)[] = [];
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.release = this.release.bind(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getLock(): Promise<Release> {
|
|
||||||
if (!this._locked) return { release: this.lock() };
|
|
||||||
else {
|
|
||||||
return new Promise<Release>((resolve) => {
|
|
||||||
this.toCome.push(() => {
|
|
||||||
resolve({ release: this.lock() });
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private lock() {
|
|
||||||
this._locked = true;
|
|
||||||
return this.release;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async release() {
|
|
||||||
if (this.toCome.length > 0) {
|
|
||||||
this.toCome.shift()();
|
|
||||||
} else {
|
|
||||||
this._locked = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
export type ObserverCallback<T> = (data: T) => void;
|
|
||||||
|
|
||||||
export default class Observable<T = any> {
|
|
||||||
private subscriber: ObserverCallback<T[]>[] = [];
|
|
||||||
private events: T[] = [];
|
|
||||||
private timeout = undefined;
|
|
||||||
|
|
||||||
constructor(private collect: boolean = true, private collect_intervall: number = 100) { }
|
|
||||||
|
|
||||||
getPublicApi() {
|
|
||||||
return {
|
|
||||||
subscribe: (callback: ObserverCallback<T[]>) => {
|
|
||||||
if (this.subscriber.indexOf(callback) < 0)
|
|
||||||
this.subscriber.push(callback)
|
|
||||||
},
|
|
||||||
unsubscribe: (callback: ObserverCallback<T[]>) => {
|
|
||||||
let idx = this.subscriber.indexOf(callback);
|
|
||||||
if (idx >= 0) {
|
|
||||||
this.subscriber.splice(idx, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
send(data: T) {
|
|
||||||
if (!this.collect)
|
|
||||||
this.subscriber.forEach(e => e([data]));
|
|
||||||
else {
|
|
||||||
this.events.push(data);
|
|
||||||
if (!this.timeout) {
|
|
||||||
this.timeout = setTimeout(() => {
|
|
||||||
this.subscriber.forEach(e => e(this.events));
|
|
||||||
this.timeout = 0;
|
|
||||||
}, this.collect_intervall);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
344
src/test.ts
344
src/test.ts
@ -1,173 +1,173 @@
|
|||||||
import SecureFile, { NotFound } from "./index";
|
import SecureFile, { NotFound } from "./index";
|
||||||
import * as v4 from "uuid/v4"
|
import * as v4 from "uuid/v4"
|
||||||
import { TextEncoder, TextDecoder } from "util";
|
import { TextEncoder, TextDecoder } from "util";
|
||||||
|
|
||||||
const testname = "ouiavgbsop687463743"
|
const testname = "ouiavgbsop687463743"
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
const testdata = encoder.encode("Ich bin ein Test");
|
const testdata = encoder.encode("Ich bin ein Test");
|
||||||
const newTestData = encoder.encode("neue test daten");
|
const newTestData = encoder.encode("neue test daten");
|
||||||
const newTestDataOld = encoder.encode("neue test daten asd");
|
const newTestDataOld = encoder.encode("neue test daten asd");
|
||||||
const testprev = "Ich bin...";
|
const testprev = "Ich bin...";
|
||||||
|
|
||||||
const testfolder = "iabos";
|
const testfolder = "iabos";
|
||||||
let ftestid;
|
let ftestid;
|
||||||
|
|
||||||
import { expect } from "chai"
|
import { expect } from "chai"
|
||||||
|
|
||||||
function test(sf: SecureFile) {
|
function test(sf: SecureFile) {
|
||||||
let testid: string;
|
let testid: string;
|
||||||
let testver: string;
|
let testver: string;
|
||||||
let testver2: string;
|
let testver2: string;
|
||||||
|
|
||||||
it("create", async () => {
|
it("create", async () => {
|
||||||
let res = await sf.create(testname, testdata, "text", undefined, testprev)
|
let res = await sf.create(testname, testdata, "text", undefined, testprev)
|
||||||
expect(res, "Res isnnot set").to.exist;
|
expect(res, "Res isnnot set").to.exist;
|
||||||
expect(res._id, "Res has no _id").to.exist;
|
expect(res._id, "Res has no _id").to.exist;
|
||||||
testid = res._id;
|
testid = res._id;
|
||||||
testver = res.active.version;
|
testver = res.active.version;
|
||||||
})
|
})
|
||||||
|
|
||||||
it("get", async () => {
|
it("get", async () => {
|
||||||
let res = await sf.get(testid);
|
let res = await sf.get(testid);
|
||||||
expect(res, "No data returned").to.exist;
|
expect(res, "No data returned").to.exist;
|
||||||
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
||||||
})
|
})
|
||||||
|
|
||||||
it("get - fail", async () => {
|
it("get - fail", async () => {
|
||||||
const inverr = new Error("Should have failed!");
|
const inverr = new Error("Should have failed!");
|
||||||
try {
|
try {
|
||||||
await sf.get(testid + "asod");
|
await sf.get(testid + "asod");
|
||||||
throw inverr
|
throw inverr
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err !== inverr) {
|
if (err !== inverr) {
|
||||||
expect(err).to.be.instanceOf(NotFound);
|
expect(err).to.be.instanceOf(NotFound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it("list", async () => {
|
it("list", async () => {
|
||||||
let res = await sf.list();
|
let res = await sf.list();
|
||||||
expect(Array.isArray(res), "Is not from type Array").to.be.true;
|
expect(Array.isArray(res), "Is not from type Array").to.be.true;
|
||||||
expect(res.length, "No elements returned").to.greaterThan(0);
|
expect(res.length, "No elements returned").to.greaterThan(0);
|
||||||
let found = !!res.find(e => e._id === testid);
|
let found = !!res.find(e => e._id === testid);
|
||||||
expect(found, "Element not in List").to.be.true;
|
expect(found, "Element not in List").to.be.true;
|
||||||
})
|
})
|
||||||
|
|
||||||
it("update to history", async () => {
|
it("update to history", async () => {
|
||||||
let res = await sf.update(testid, newTestDataOld, undefined, undefined, true);
|
let res = await sf.update(testid, newTestDataOld, undefined, undefined, true);
|
||||||
expect(res, "No data returned").to.exist;
|
expect(res, "No data returned").to.exist;
|
||||||
expect(res._id, "_id missing").to.exist;
|
expect(res._id, "_id missing").to.exist;
|
||||||
expect(res.active.version, "New version was created").to.equal(testver);
|
expect(res.active.version, "New version was created").to.equal(testver);
|
||||||
let res2 = await sf.get(testid);
|
let res2 = await sf.get(testid);
|
||||||
expect(decoder.decode(res2), "Fetched data not updated").to.be.equal(decoder.decode(testdata));
|
expect(decoder.decode(res2), "Fetched data not updated").to.be.equal(decoder.decode(testdata));
|
||||||
})
|
})
|
||||||
|
|
||||||
it("update", async () => {
|
it("update", async () => {
|
||||||
let res = await sf.update(testid, newTestData, undefined);
|
let res = await sf.update(testid, newTestData, undefined);
|
||||||
expect(res, "No data returned").to.exist;
|
expect(res, "No data returned").to.exist;
|
||||||
expect(res._id, "_id missing").to.exist;
|
expect(res._id, "_id missing").to.exist;
|
||||||
expect(res.active.version, "No new version was created").to.not.equal(testver);
|
expect(res.active.version, "No new version was created").to.not.equal(testver);
|
||||||
testver2 = res.active.version;
|
testver2 = res.active.version;
|
||||||
let res2 = await sf.get(testid);
|
let res2 = await sf.get(testid);
|
||||||
expect(decoder.decode(res2), "Fetched data not updated").to.be.equal(decoder.decode(newTestData));
|
expect(decoder.decode(res2), "Fetched data not updated").to.be.equal(decoder.decode(newTestData));
|
||||||
})
|
})
|
||||||
|
|
||||||
it("history", async () => {
|
it("history", async () => {
|
||||||
let his = await sf.history(testid);
|
let his = await sf.history(testid);
|
||||||
expect(his, "no data returned").to.exist;
|
expect(his, "no data returned").to.exist;
|
||||||
expect(his.file, "file not set").to.exist;
|
expect(his.file, "file not set").to.exist;
|
||||||
expect(his.history, "history not set").to.exist;
|
expect(his.history, "history not set").to.exist;
|
||||||
expect(his.history.length, `Not expected history length. Expected 1 got ${his.history.length}`).to.be.equal(2);
|
expect(his.history.length, `Not expected history length. Expected 1 got ${his.history.length}`).to.be.equal(2);
|
||||||
|
|
||||||
expect(his.history[1].version, "Wrong version on history").to.be.equal(testver);
|
expect(his.history[1].version, "Wrong version on history").to.be.equal(testver);
|
||||||
expect(his.file.active.version, "Wrong version on file").to.be.equal(testver2);
|
expect(his.file.active.version, "Wrong version on file").to.be.equal(testver2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("history get old", async () => {
|
it("history get old", async () => {
|
||||||
let arch = await sf.get(testid, testver);
|
let arch = await sf.get(testid, testver);
|
||||||
expect(decoder.decode(arch), "Old version has wrong data").to.be.equal(decoder.decode(testdata));
|
expect(decoder.decode(arch), "Old version has wrong data").to.be.equal(decoder.decode(testdata));
|
||||||
})
|
})
|
||||||
|
|
||||||
it("history restore", async () => {
|
it("history restore", async () => {
|
||||||
await sf.restore(testid, testver);
|
await sf.restore(testid, testver);
|
||||||
|
|
||||||
let res = await sf.get(testid);
|
let res = await sf.get(testid);
|
||||||
expect(res, "No data returned").to.exist;
|
expect(res, "No data returned").to.exist;
|
||||||
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
||||||
})
|
})
|
||||||
|
|
||||||
it("delete", async () => {
|
it("delete", async () => {
|
||||||
await sf.delete(testid);
|
await sf.delete(testid);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
describe("fixed id", () => {
|
describe("fixed id", () => {
|
||||||
let id = v4();
|
let id = v4();
|
||||||
it("create", async () => {
|
it("create", async () => {
|
||||||
let res = await sf.create(testname, testdata, "text", undefined, testprev, id)
|
let res = await sf.create(testname, testdata, "text", undefined, testprev, id)
|
||||||
expect(res, "Res isnnot set").to.exist;
|
expect(res, "Res isnnot set").to.exist;
|
||||||
expect(res._id, "Res has no _id").to.exist;
|
expect(res._id, "Res has no _id").to.exist;
|
||||||
expect(res._id, "Res has invalid _id").to.be.equal(id)
|
expect(res._id, "Res has invalid _id").to.be.equal(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("get", async () => {
|
it("get", async () => {
|
||||||
let res = await sf.get(id);
|
let res = await sf.get(id);
|
||||||
expect(res, "No data returned").to.exist;
|
expect(res, "No data returned").to.exist;
|
||||||
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("predefined date", () => {
|
describe("predefined date", () => {
|
||||||
let id = v4();
|
let id = v4();
|
||||||
it("create", async () => {
|
it("create", async () => {
|
||||||
const date = new Date("2017-01-01T00:00:00.000Z")
|
const date = new Date("2017-01-01T00:00:00.000Z")
|
||||||
let res = await sf.create(testname, testdata, "text", undefined, testprev, id, date)
|
let res = await sf.create(testname, testdata, "text", undefined, testprev, id, date)
|
||||||
expect(res, "Res isnnot set").to.exist;
|
expect(res, "Res isnnot set").to.exist;
|
||||||
expect(res._id, "Res has no _id").to.exist;
|
expect(res._id, "Res has no _id").to.exist;
|
||||||
expect(res._id, "Res has invalid _id").to.be.equal(id)
|
expect(res._id, "Res has invalid _id").to.be.equal(id)
|
||||||
expect(res.active.time.toJSON()).to.be.equal(date.toJSON())
|
expect(res.active.time.toJSON()).to.be.equal(date.toJSON())
|
||||||
})
|
})
|
||||||
|
|
||||||
it("list", async () => {
|
it("list", async () => {
|
||||||
let res = await sf.get(id);
|
let res = await sf.get(id);
|
||||||
expect(res, "No data returned").to.exist;
|
expect(res, "No data returned").to.exist;
|
||||||
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("folder", () => {
|
describe("folder", () => {
|
||||||
it("create", async () => {
|
it("create", async () => {
|
||||||
let res = await sf.create(testname, testdata, "text", testfolder, testprev)
|
let res = await sf.create(testname, testdata, "text", testfolder, testprev)
|
||||||
expect(res, "Res not set").to.exist;
|
expect(res, "Res not set").to.exist;
|
||||||
expect(res._id, "No _id field").to.exist;
|
expect(res._id, "No _id field").to.exist;
|
||||||
ftestid = res._id;
|
ftestid = res._id;
|
||||||
testver = res.active.version;
|
testver = res.active.version;
|
||||||
})
|
})
|
||||||
|
|
||||||
it("list", async () => {
|
it("list", async () => {
|
||||||
let res = await sf.list(testfolder);
|
let res = await sf.list(testfolder);
|
||||||
expect(Array.isArray(res), "Is from type Array").to.be.true;
|
expect(Array.isArray(res), "Is from type Array").to.be.true;
|
||||||
expect(res.length, "Do elements exist?").to.be.greaterThan(0);
|
expect(res.length, "Do elements exist?").to.be.greaterThan(0);
|
||||||
let found = false;
|
let found = false;
|
||||||
res.forEach(e => {
|
res.forEach(e => {
|
||||||
if (e._id === ftestid) {
|
if (e._id === ftestid) {
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
expect(found, "Element is not in List").to.exist;
|
expect(found, "Element is not in List").to.exist;
|
||||||
})
|
})
|
||||||
|
|
||||||
it("delete", async () => {
|
it("delete", async () => {
|
||||||
await sf.delete(ftestid);
|
await sf.delete(ftestid);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("SecureFile Tests", function () {
|
describe("SecureFile Tests", function () {
|
||||||
let sf = new SecureFile("http://localhost:3004");
|
let sf = new SecureFile("http://localhost:3004");
|
||||||
sf.jwtObservable.subscribe((callback) => {
|
sf.jwtObservable.subscribe((callback) => {
|
||||||
callback[0]("TESTJWT");
|
callback[0]("TESTJWT");
|
||||||
})
|
})
|
||||||
test(sf)
|
test(sf)
|
||||||
})
|
})
|
@ -1,17 +1,17 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
/* Basic Options */
|
/* Basic Options */
|
||||||
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
|
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
|
||||||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||||
"lib": [
|
"lib": [
|
||||||
"es6",
|
"es6",
|
||||||
"dom"
|
"dom"
|
||||||
],
|
],
|
||||||
"outDir": "./lib",
|
"outDir": "./lib",
|
||||||
"sourceMap": true
|
"sourceMap": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"./src"
|
"./src"
|
||||||
]
|
]
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user