UNPKG

@webda/core

Version:

Expose API with Lambda

268 lines 10.4 kB
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 * as sinon from "sinon"; import { Core, Inject, Operation, Service } from "...js"; import { WebdaTest } from "../test.js"; import { OperationContext } from "../utils/context.js"; import { RegExpStringValidator, ServiceParameters } from "./service.js"; class FakeServiceParameters extends ServiceParameters { } class FakeService extends Service { // Set to undefined to ensure fallback on method name async myOperation(ctx) { ctx.write((await ctx.getInput()).output); } myOperation2(ctx) { ctx.write("plop2"); } } __decorate([ Inject("Authentication2", true) ], FakeService.prototype, "serv", void 0); __decorate([ Inject("bean", "Authentication", true) ], FakeService.prototype, "serv2", void 0); __decorate([ Inject("params:bean", undefined, true) ], FakeService.prototype, "serv3", void 0); __decorate([ Inject("params:bean", undefined, false) ], FakeService.prototype, "serv4", void 0); __decorate([ Operation({}, { url: "/operation/plop" }) ], FakeService.prototype, "myOperation", null); __decorate([ Operation({ id: "myOperation" }) ], FakeService.prototype, "myOperation2", null); class FakeService2 extends Service { } __decorate([ Inject("Authentication2") ], FakeService2.prototype, "serv", void 0); class FakeOperationContext extends OperationContext { constructor(webda) { super(webda); this.input = ""; } setInput(input) { this.input = input; } /** * By default empty * @returns */ async getRawInputAsString(_limit = 1024 * 1024 * 10, _timeout = 60000, _encoding) { return this.input; } /** * @override */ async getRawInput(limit = 1024 * 1024 * 10, timeout = 60000) { return Buffer.from(this.input); } } let RegExpStringValidatorTest = class RegExpStringValidatorTest { async validate() { let validator = new RegExpStringValidator(["test1", "regex:test[2-3]+", "regex:^itest[4-5]b$", "test[1-9]+"]); assert.ok(validator.validate("test1")); assert.ok(validator.validate("test[1-9]+")); assert.ok(validator.validate("test2")); assert.ok(validator.validate("test23")); assert.ok(validator.validate("itest4b")); assert.ok(validator.validate("itest5b")); // ^ should be added to the regex assert.ok(!validator.validate("test")); assert.ok(!validator.validate("stest2")); assert.ok(!validator.validate("test2b")); assert.ok(!validator.validate("test6")); } }; __decorate([ test ], RegExpStringValidatorTest.prototype, "validate", null); RegExpStringValidatorTest = __decorate([ suite ], RegExpStringValidatorTest); let ServiceTest = class ServiceTest extends WebdaTest { async buildWebda() { await super.buildWebda(); this.webda.getBeans = () => { }; } async injector() { let service = new FakeService(this.webda, "plop"); assert.throws(() => service.resolve(), /Injector did not found bean 'undefined'\(parameter:bean\) for 'plop'/); service = new FakeService(this.webda, "plop", { bean: "Authentication" }); service.resolve(); assert.strictEqual(service.serv, undefined); assert.throws(() => new FakeService2(this.webda, "kf").resolve(), /Injector did not found bean 'Authentication2' for 'kf'/); } async operation() { let ctx = new FakeOperationContext(this.webda); await ctx.init(); ctx.setInput(JSON.stringify({ output: "plop" })); assert.rejects(() => this.webda.callOperation(ctx, "myOperation2")); // @ts-ignore const schemaRegistry = this.webda.getApplication().baseConfiguration.cachedModules.schemas; schemaRegistry["plop.myoperation.input"] = { properties: { output: { type: "string", minLength: 8 } }, required: ["output"] }; // @ts-ignore schemaRegistry["plop.myOperation.input"] = schemaRegistry["plop.myoperation.input"]; let service = new FakeService(this.webda, "plop", { bean: "Authentication" }); service.resolve(); this.registerService(service); // @ts-ignore this.webda.operations["plop.myOperation"].input = "plop.myoperation.input"; await assert.rejects(() => this.webda.callOperation(ctx, "plop.myOperation"), /Operation plop.myOperation InvalidInput/); ctx.getInput = () => undefined; await assert.rejects(() => this.webda.callOperation(ctx, "plop.myOperation"), /Operation plop.myOperation InvalidInput/); ctx = new FakeOperationContext(this.webda); await ctx.init(); ctx.setInput(JSON.stringify({ output: "longerOutput" })); await this.webda.callOperation(ctx, "plop.myOperation"); await ctx.end(); assert.strictEqual(ctx.getOutput(), "longerOutput"); // Check the list assert.deepStrictEqual(this.webda.listOperations(), { "plop.myOperation": { id: "plop.myOperation", input: "plop.myoperation.input" } }); // Check with permission // @ts-ignore this.webda.operations["plop.myOperation"].permission = "role = 'test'"; await assert.rejects(() => this.webda.callOperation(ctx, "plop.myOperation"), /Operation plop.myOperation PermissionDenied/); await ctx.newSession(); ctx.getSession().role = "test"; await this.webda.callOperation(ctx, "plop.myOperation"); // Test input detection this.webda.registerOperation("plop.myOperation", { id: "plop.myOperation", input: "plop.myOperation.input", service: "plop", method: "myOperation" }); assert.deepStrictEqual(this.webda.listOperations(), { "plop.myOperation": { id: "plop.myOperation", input: "plop.myOperation.input" } }); // Test input detection this.webda.registerOperation("plop.myOperation", { id: "plop.myOperation", input: "plop.myOperation.input2", service: "plop", method: "myOperation" }); assert.deepStrictEqual(this.webda.listOperations(), { "plop.myOperation": { id: "plop.myOperation" } }); } async clean() { let service = new FakeService(this.webda, "plop"); // @ts-ignore const origin = global.it; // @ts-ignore global.it = undefined; assert.rejects(() => service.__clean(), /Only for test purpose/); // @ts-ignore global.it = origin; await service.__clean(); } toPublicJSON() { let service = new FakeService(this.webda, "plop", {}); let stub = sinon.stub(this.webda, "toPublicJSON").callsFake(() => "plop"); try { assert.strictEqual(service.toPublicJSON({ l: "p" }), "plop"); } finally { stub.restore(); } } toStringMethod() { let service = new FakeService(this.webda, "plop", { type: "FakeService" }); assert.strictEqual(service.toString(), "FakeService[plop]"); } publicEvents() { let service = new FakeService(this.webda, "plop", { type: "FakeService" }); assert.deepStrictEqual(service.getClientEvents(), []); assert.deepStrictEqual(service.authorizeClientEvent("plop", undefined), false); } getUrl() { let service = new FakeService(this.webda, "plop", { type: "FakeService" }); assert.strictEqual(service.getUrl("/plop", ["GET"]), "/plop"); assert.strictEqual(service.getUrl("./plop", ["GET"]), undefined); assert.strictEqual(service.getUrl("plop", ["GET"]), undefined); service.getParameters().url = "/re"; assert.strictEqual(service.getUrl("./plop", ["GET"]), "/re/plop"); assert.strictEqual(service.getUrl("plop", ["GET"]), "plop"); service.getParameters().url = "/"; assert.strictEqual(service.getUrl("./plop", ["GET"]), "/plop"); assert.strictEqual(service.getUrl("plop", ["GET"]), "plop"); assert.strictEqual(service.getUrl(".", ["GET"]), "/"); } /** * Ensure a message is displayed if listener is long * Ensure error in listener are catched */ async longListener() { let service = new FakeService(this.webda, "plop", {}); let logs = []; Core.get().log = (...args) => { logs.push(args); }; service.on("test", async () => { await new Promise(resolve => setTimeout(resolve, 140)); throw new Error("My error"); }); await service.emitSync("test", undefined); assert.strictEqual(logs.length, 2); assert.deepStrictEqual(logs.map(l => `${l[0]}_${l[1]}`), ["ERROR_Listener error", "INFO_Long listener"]); } }; __decorate([ test ], ServiceTest.prototype, "injector", null); __decorate([ test ], ServiceTest.prototype, "operation", null); __decorate([ test ], ServiceTest.prototype, "clean", null); __decorate([ test ], ServiceTest.prototype, "toPublicJSON", null); __decorate([ test ], ServiceTest.prototype, "toStringMethod", null); __decorate([ test ], ServiceTest.prototype, "publicEvents", null); __decorate([ test ], ServiceTest.prototype, "getUrl", null); __decorate([ test ], ServiceTest.prototype, "longListener", null); ServiceTest = __decorate([ suite ], ServiceTest); //# sourceMappingURL=service.spec.js.map