UNPKG

@webda/core

Version:

Expose API with Lambda

192 lines 7.08 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 { WebdaError } from "../errors.js"; import { CoreModel } from "../models/coremodel.js"; import { WebdaTest } from "../test.js"; import { BinaryTest } from "./binary.spec.js"; import { CloudBinary, CloudBinaryParameters } from "./cloudbinary.js"; class CloudBinaryFakeService extends CloudBinary { store(object, property, file, metadata) { throw new Error("Method not implemented."); } getUsageCount(hash) { throw new Error("Method not implemented."); } /** * Clean usage for a hash */ async _cleanUsage(hash, uuid) { } async _get() { return undefined; } /** * Retrieve one signed URL to download the file in parameter * @param {GetSignedUrlParams} params * @returns {string} URL in order to download the file */ async getSignedUrlFromMap(map, expires) { return `${map.hash}:${expires}`; } } export class CloudBinaryTest extends BinaryTest { async redirectUrl() { let { user1, ctx } = await this.setupDefault(); // Making sure we are redirected on GET let executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {}); await executor.execute(ctx); assert.ok(ctx.getResponseHeaders().Location !== undefined); } async redirectUrlInfo() { let { user1, ctx } = await this.setupDefault(); // Making sure we are redirected on GET let executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0/url`, {}); await executor.execute(ctx); assert.ok(ctx.getResponseHeaders().Location === undefined); assert.notStrictEqual(JSON.parse(ctx.getResponseBody()).Location, undefined); } } __decorate([ test ], CloudBinaryTest.prototype, "redirectUrl", null); __decorate([ test ], CloudBinaryTest.prototype, "redirectUrlInfo", null); let FakeCloudBinaryTest = class FakeCloudBinaryTest extends WebdaTest { async testDefaults() { let service = new CloudBinaryFakeService(this.webda, "fake", {}); let binaryMap = { hash: "test", size: 10 }; await service.cascadeDelete(binaryMap, "plop"); let stub = sinon.stub(service, "_cleanUsage").callsFake(() => { throw new Error(); }); // Should not fail even with exception await service.cascadeDelete(binaryMap, "plop"); stub.restore(); assert.strictEqual(service._getKey("hash"), "hash/data"); service.getParameters().prefix = "prefix"; assert.strictEqual(service._getKey("hash", "plop"), "prefix/hash/plop"); assert.strictEqual(new CloudBinaryParameters({}, service).prefix, ""); sinon.stub(service, "deleteSuccess").resolves(); let model = new CoreModel(); // @ts-ignore model.load({ plop: [{}, { hash: "fake" }] }, true); await service.delete(model, "plop", 1); // Check called with "fake", 1 } async initRoutes() { let service = new CloudBinaryFakeService(this.webda, "fake", {}); // @ts-ignore addRoute is a protected method of service let stub = sinon.stub(service, "addRoute"); service.initRoutes(); assert.strictEqual(stub.callCount, 0); service.getParameters().expose = { url: "plop", restrict: {} }; service.initRoutes(); assert.strictEqual(stub.callCount, 6); stub.callCount = 0; service.getParameters().expose = { url: "plop", restrict: { get: true } }; service.initRoutes(); assert.strictEqual(stub.callCount, 4); } async routes() { let service = new CloudBinaryFakeService(this.webda, "fake", {}); let wrote; let wroteHead; let context = { parameter: name => { return name === "index" ? 1 : name; }, write: (...arg) => { wrote = arg; }, writeHead: (...arg) => { wroteHead = arg; }, getParameters: () => ({ index: 1, hash: "hash", property: "property" }), getHttpContext: () => ({ getAbsoluteUrl: () => "http://myhost/test", getRelativeUri: () => "/test" }) }; let storeGetResult; // @ts-ignore sinon.stub(service, "_verifyMapAndStore").callsFake(() => { return { get: async () => { return storeGetResult; } }; }); await assert.rejects(() => service.httpGet(context), WebdaError.NotFound); storeGetResult = { property: [1], checkAct: () => { } }; await assert.rejects(() => service.httpGet(context), (err) => err.getResponseCode() === 404); let myEvt; storeGetResult = { property: [ 1, { hash: "myhash" } ], checkAct: (ctx, evt) => { myEvt = evt; }, canAct: (ctx, evt) => { myEvt = evt; } }; let counter = 0; service.on("Binary.Get", () => { counter++; }); await service.httpGet(context); assert.deepStrictEqual(wroteHead, [302, { Location: "myhash:30" }]); // @ts-ignore context.getHttpContext = () => ({ getAbsoluteUrl: () => "http://myhost/test/url", getRelativeUri: () => "/test/url" }); await service.httpGet(context); assert.strictEqual(counter, 2); assert.deepStrictEqual(wrote, [ { Location: "myhash:30", Map: { hash: "myhash" } } ]); assert.strictEqual(myEvt, "get_binary"); } }; __decorate([ test ], FakeCloudBinaryTest.prototype, "testDefaults", null); __decorate([ test ], FakeCloudBinaryTest.prototype, "initRoutes", null); __decorate([ test ], FakeCloudBinaryTest.prototype, "routes", null); FakeCloudBinaryTest = __decorate([ suite ], FakeCloudBinaryTest); export { FakeCloudBinaryTest }; //# sourceMappingURL=cloudbinary.spec.js.map