@webda/core
Version:
Expose API with Lambda
232 lines • 9.61 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { suite, test } from "@testdeck/mocha";
import * as assert from "assert";
import { existsSync } from "fs";
import pkg from "fs-extra";
import * as sinon from "sinon";
import { FileUtils, ModelMapLoaderImplementation, WebdaError } from "../index.js";
import { HttpContext } from "../utils/httpcontext.js";
import { StoreNotFoundError, UpdateConditionFailError } from "./store.js";
import { StoreTest } from "./store.spec.js";
const { removeSync } = pkg;
let FileStoreTest = class FileStoreTest extends StoreTest {
getUserStore() {
return this.getService("Users");
}
getIdentStore() {
// Need to slow down the _get
let store = this.getService("Idents");
// @ts-ignore
let original = store._get.bind(store);
// @ts-ignore
store._get = async (...args) => {
await this.sleep(1);
return original(...args);
};
return store;
}
async getIndex() {
return this.getService("MemoryAggregators").get("idents-index");
}
async recreateIndex() {
let store = this.getService("MemoryAggregators");
await store.__clean();
await this.getService("IdentsIndexer").createAggregate();
}
async update(delay = 100) {
// Increase the delay for FileStore
return super.update(delay);
}
async cov() {
let identStore = this.getService("Idents");
let userStore = this.getService("Users");
let user = await userStore.save({});
let ident = await identStore.save({
_user: user.getUuid()
});
identStore.getParameters().strict = true;
let res = await identStore.get(user.getUuid());
assert.strictEqual(res, undefined);
const stub = sinon.stub(identStore, "_get").callsFake(async () => user);
try {
res = await identStore.get(user.getUuid());
assert.strictEqual(res, undefined);
await assert.rejects(() => identStore.update({
uuid: user.getUuid(),
plop: true
}), StoreNotFoundError);
await user.refresh();
assert.strictEqual(user.plop, undefined);
user.idents[0] = new ModelMapLoaderImplementation(identStore._model, user.idents[0], user);
userStore["initModel"](user);
await identStore.delete(user.getUuid());
}
finally {
stub.restore();
}
identStore.incrementAttribute("test", "test", 12);
await identStore.create({ uuid: "test_cache" });
await identStore.get("test_cache");
assert.notStrictEqual(await identStore["_cacheStore"].get("test_cache"), undefined);
identStore.emitStoreEvent("Store.PartialUpdated", {
object_id: "test_cache",
partial_update: {},
store: identStore
});
assert.strictEqual(await identStore["_cacheStore"].get("test"), undefined);
// Shoud return directly
await identStore.incrementAttribute("test", "test", 0);
removeSync(identStore.getParameters().folder);
// Should not fail
await identStore.__clean();
// Should recreate folder
identStore.computeParameters();
existsSync(identStore.getParameters().folder);
ident = new identStore._model();
identStore.newModel(ident);
assert.notStrictEqual(ident.getUuid(), undefined);
// Test guard-rails (seems hardly reachable so might be useless)
assert.throws(() => identStore.checkCollectionUpdateCondition(ident, "plops", undefined, 1, null), UpdateConditionFailError);
// @ts-ignore
ident.plops = [];
assert.throws(() => identStore.checkCollectionUpdateCondition(ident, "plops", undefined, 1, null), UpdateConditionFailError);
identStore.checkCollectionUpdateCondition(ident, "plops", undefined, 0, null);
assert.rejects(() => identStore.simulateUpsertItemToCollection(undefined, "__proto__", undefined, new Date()), /Cannot update __proto__: js\/prototype-polluting-assignment/);
// Add a queryMethod test
userStore.getParameters().url = "/users";
userStore.getParameters().expose = {
queryMethod: "PUT",
restrict: {}
};
userStore.initRoutes();
userStore.getUrl("/url", ["GET"]);
}
async configuration() {
let identStore = this.getService("Idents");
assert.strictEqual(identStore.canTriggerConfiguration("plop", () => { }), false);
assert.strictEqual(await identStore.getConfiguration("plop"), undefined);
await identStore.save({ plop: 1, other: true, uuid: "plop" });
assert.deepStrictEqual(await identStore.getConfiguration("plop"), {
other: true,
plop: 1
});
}
async modelActions() {
return super.modelActions();
}
async cacheMishit() {
let identStore = this.getService("Idents");
let ident = await identStore.save({ uuid: "test" });
await identStore._cacheStore.__clean();
// @ts-ignore
await ident.patch({ retest: true });
}
async modelStaticActions() {
let identStore = this.getService("Idents");
let ctx, executor;
let eventFired = 0;
identStore.on("Store.Action", evt => {
eventFired++;
});
ctx = await this.newContext({
type: "CRUD",
uuid: "PLOP"
});
executor = this.getExecutor(ctx, "test.webda.io", "GET", "/idents/index");
assert.notStrictEqual(executor, undefined);
await executor.execute(ctx);
// Our fake index action is just outputing 'indexer'
assert.strictEqual(ctx.getResponseBody(), "indexer");
assert.strictEqual(eventFired, 1);
ctx.resetResponse();
// Return some infos instead of using ctx
// @ts-ignore
identStore._model.index = async () => {
return "vouzouf";
};
await executor.execute(ctx);
// Our fake index action is just outputing 'indexer'
assert.strictEqual(ctx.getResponseBody(), "vouzouf");
}
async httpCRUD() {
return await super.httpCRUD();
}
async getURL() {
//assert.strictEqual((<Store<CoreModel>>this.webda.getService("users")).getUrl(), "/users");
}
async schemaCreate() {
let taskStore = this.webda.getService("Tasks");
let ctx = await this.webda.newWebContext(new HttpContext("webda.io", "GET", "/"));
let executor = this.getExecutor(ctx, "test.webda.io", "POST", "/tasks", {
noname: "Task #1"
});
this.webda.getModules().schemas["WebdaTest/Task"] = FileUtils.load("./test/schemas/task.json");
assert.notStrictEqual(executor, undefined);
ctx.getSession().login("fake_user", "fake_ident");
await assert.rejects(executor.execute(ctx), WebdaError.BadRequest, "Should reject for bad schema");
executor = this.getExecutor(ctx, "test.webda.io", "POST", "/tasks", {
name: "Task #1"
});
await executor.execute(ctx);
let task = JSON.parse(ctx.getResponseBody());
// It is two because the Saved has been called two
assert.notStrictEqual(task.uuid, undefined);
assert.strictEqual(task._autoListener, 2);
task = await taskStore.get(task.uuid);
assert.strictEqual(task._autoListener, 1);
executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/tasks/${task.uuid}`, {
test: "plop"
});
await assert.rejects(() => executor.execute(ctx), (err) => err.getResponseCode() === 400);
executor = this.getExecutor(ctx, "test.webda.io", "PATCH", `/tasks/${task.uuid}`, {
name: 123
});
await assert.rejects(() => executor.execute(ctx), (err) => err.getResponseCode() === 400);
}
computeParams() {
let usersStore = this.getUserStore();
removeSync(usersStore.getParameters().folder);
usersStore.computeParameters();
assert.ok(existsSync(usersStore.getParameters().folder));
}
};
__decorate([
test
], FileStoreTest.prototype, "update", null);
__decorate([
test
], FileStoreTest.prototype, "cov", null);
__decorate([
test
], FileStoreTest.prototype, "configuration", null);
__decorate([
test
], FileStoreTest.prototype, "modelActions", null);
__decorate([
test
], FileStoreTest.prototype, "cacheMishit", null);
__decorate([
test
], FileStoreTest.prototype, "modelStaticActions", null);
__decorate([
test
], FileStoreTest.prototype, "httpCRUD", null);
__decorate([
test
], FileStoreTest.prototype, "getURL", null);
__decorate([
test("JSON Schema - Create")
], FileStoreTest.prototype, "schemaCreate", null);
__decorate([
test
], FileStoreTest.prototype, "computeParams", null);
FileStoreTest = __decorate([
suite
], FileStoreTest);
export { FileStoreTest };
//# sourceMappingURL=file.spec.js.map