UNPKG

@webda/core

Version:

Expose API with Lambda

187 lines 7.67 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 { AclModel, Core, HttpContext, SimpleUser, User } from "../index.js"; import { TestApplication } from "../test.js"; import { getCommonJS } from "../utils/esm.js"; const { __filename, __dirname } = getCommonJS(import.meta.url); let AclModelTest = class AclModelTest { async before() { let app = new TestApplication(); await app.load(); this._webda = new Core(app); await this._webda.init(); this._ctx = await this._webda.newWebContext(new HttpContext("test.webda.io", "GET", "/")); this._session = this._ctx.getSession(); this._session.login("user-uid", "none"); this.model = new AclModel().getProxy(); this._user = new SimpleUser(); this._user.setUuid("user-uid"); this._user.addGroup("gip-123"); // @ts-ignore this._ctx.getCurrentUser = async () => { return this._user; }; } cov() { assert.deepStrictEqual(this.model.getGroups(undefined, undefined), []); } async get() { await assert.rejects(() => this.model.checkAct(this._ctx, "get")); this.model.__acl["gip-123"] = "get"; assert.strictEqual(await this.model.canAct(this._ctx, "get"), true); } async multipermissions() { await assert.rejects(() => this.model.checkAct(this._ctx, "action")); this.model.__acl["gip-123"] = "get,action"; assert.strictEqual(await this.model.canAct(this._ctx, "get"), true); assert.strictEqual(await this.model.canAct(this._ctx, "action"), true); assert.deepStrictEqual(await this.model.getPermissions(this._ctx), ["get", "action"]); await this.model._onGet(); // @ts-ignore assert.deepStrictEqual(this.model._permissions, []); this.model.setContext(this._ctx); await this.model._onGet(); // @ts-ignore assert.deepStrictEqual(this.model._permissions, ["get", "action"]); } async multigroups() { await assert.rejects(() => this.model.checkAct(this._ctx, "action")); this.model.__acl["gip-124"] = "get,action"; this.model.__acl["gip-123"] = "get"; this.model.__acl["gip-122"] = "get,action"; assert.strictEqual(await this.model.canAct(this._ctx, "get"), true); await assert.rejects(() => this.model.checkAct(this._ctx, "action")); // @ts-ignore this._user._groups = undefined; await assert.rejects(() => this.model.checkAct(this._ctx, "get")); } async userid() { await assert.rejects(() => this.model.checkAct(this._ctx, "action")); this.model.__acl["user-uid"] = "get"; this.model.__acl["gip-122"] = "get,action"; assert.strictEqual(await this.model.canAct(this._ctx, "get"), true); await assert.rejects(() => this.model.checkAct(this._ctx, "action")); this._ctx.getCurrentUserId = () => { return undefined; }; await assert.rejects(() => this.model.checkAct(this._ctx, "get"), (err) => err.getResponseCode() === 403); this._ctx.getCurrentUserId = () => { return "123"; }; this.model.setAcl(undefined); await assert.rejects(() => this.model.checkAct(this._ctx, "get"), (err) => err.getResponseCode() === 403); } async all() { await assert.rejects(() => this.model.checkAct(this._ctx, "action")); this.model.__acl["gip-124"] = "get,action"; this.model.__acl["user-uid"] = "all"; this.model.__acl["gip-122"] = "get,action"; assert.strictEqual(await this.model.canAct(this._ctx, "get"), true); assert.strictEqual(await this.model.canAct(this._ctx, "action"), true); assert.strictEqual(await this.model.canAct(this._ctx, "delete"), true); assert.strictEqual(await this.model.canAct(this._ctx, "whatever"), true); } async httpAcls() { // @ts-ignore this.model.__store = { save: async () => this.model, patch: async () => this.model, // @ts-ignore getService: () => { return { get: async () => { return { toPublicEntry: () => { return { displayName: "Plopi" }; } }; } }; } }; let actions = AclModel.getActions(); assert.notStrictEqual(actions.acl, undefined); this._ctx.setHttpContext(new HttpContext("test.webda.io", "PUT", "/")); this._ctx.getHttpContext().setBody({ acl: "mine" }); await this.model.acl(this._ctx); // @ts-ignore assert.strictEqual(this.model.getAcl().acl, "mine"); this._ctx.reinit(); this._ctx.getHttpContext().setBody({ raw: { acl: "mine" } }); await assert.rejects(() => this.model.acl(this._ctx)); this._ctx.reinit(); this._ctx.setHttpContext(new HttpContext("test.webda.io", "GET", "/")); // Action return their result directly await User.ref("acl").create({ displayName: "Plopi" }); let res = await this.model.acl(this._ctx); assert.deepStrictEqual(res, { raw: { acl: "mine" }, resolved: [ { actor: { avatar: undefined, displayName: "Plopi", email: undefined, uuid: "acl" }, permission: "mine" } ] }); } async canActCreate() { this._ctx.getSession().userId = undefined; assert.strictEqual(await this.model.canAct(this._ctx, "create"), "No ACL or user"); this._ctx.getSession().login("user-uid", "ident-uid"); assert.strictEqual(await this.model.canAct(this._ctx, "create"), true); } async onSave() { this.model.setContext(this._ctx); await this.model._onSave(); assert.deepStrictEqual(this.model.__acl, { "user-uid": "all" }); assert.strictEqual(this.model._creator, "user-uid"); } }; __decorate([ test ], AclModelTest.prototype, "cov", null); __decorate([ test ], AclModelTest.prototype, "get", null); __decorate([ test ], AclModelTest.prototype, "multipermissions", null); __decorate([ test ], AclModelTest.prototype, "multigroups", null); __decorate([ test ], AclModelTest.prototype, "userid", null); __decorate([ test ], AclModelTest.prototype, "all", null); __decorate([ test ], AclModelTest.prototype, "httpAcls", null); __decorate([ test ], AclModelTest.prototype, "canActCreate", null); __decorate([ test ], AclModelTest.prototype, "onSave", null); AclModelTest = __decorate([ suite ], AclModelTest); //# sourceMappingURL=aclmodel.spec.js.map