@webda/core
Version:
Expose API with Lambda
86 lines • 3.36 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 { Core, HttpContext, RoleModel, SimpleUser, WebdaError } from "../index.js";
import { TestApplication } from "../test.js";
import { getCommonJS } from "../utils/esm.js";
const { __dirname } = getCommonJS(import.meta.url);
class RolePolicyModel extends RoleModel {
getRolesMap() {
return {
get: "member",
update: "member",
delete: "admin"
};
}
}
class RolePolicyModelPermissive extends RoleModel {
getRolesMap() {
return {
get: "member",
create: "member"
};
}
isPermissive() {
return true;
}
}
let RolePolicyTest = class RolePolicyTest {
async before() {
let app = new TestApplication(__dirname + "/../../test/config.json");
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 = await this._ctx.newSession();
this._session.login("none", "none");
// @ts-ignore
this._ctx.getCurrentUser = async () => {
return this._user;
};
this.nonPermissive = new RolePolicyModel();
this.permissive = new RolePolicyModelPermissive();
this._user = new SimpleUser();
this._user.addRole("member");
}
async noLogged() {
this._ctx.getSession().logout();
assert.rejects(() => this.permissive.checkAct(this._ctx, "get"), (err) => err.getResponseCode() === 403);
}
async get() {
assert.strictEqual(await this.permissive.canAct(this._ctx, "get"), true);
assert.strictEqual(await this.nonPermissive.canAct(this._ctx, "get"), true);
}
async action() {
assert.strictEqual(await this.permissive.canAct(this._ctx, "action"), true);
await assert.rejects(() => this.nonPermissive.checkAct(this._ctx, "action"), WebdaError.Forbidden);
}
async delete() {
assert.strictEqual(await this.permissive.canAct(this._ctx, "delete"), true);
await assert.rejects(() => this.nonPermissive.checkAct(this._ctx, "delete"));
this._user.addRole("admin");
this._session.roles = undefined;
assert.strictEqual(await this.nonPermissive.canAct(this._ctx, "delete"), true);
}
};
__decorate([
test
], RolePolicyTest.prototype, "noLogged", null);
__decorate([
test
], RolePolicyTest.prototype, "get", null);
__decorate([
test
], RolePolicyTest.prototype, "action", null);
__decorate([
test
], RolePolicyTest.prototype, "delete", null);
RolePolicyTest = __decorate([
suite
], RolePolicyTest);
//# sourceMappingURL=rolemodel.spec.js.map