fix Role creation method and permission checks in tests

This commit is contained in:
dswbx
2025-10-14 16:49:42 +02:00
parent 1b8ce41837
commit 0347efa592
4 changed files with 13 additions and 9 deletions

View File

@@ -201,7 +201,10 @@ describe("mcp auth", async () => {
}, },
return_config: true, return_config: true,
}); });
expect(addGuestRole.config.guest.permissions).toEqual(["read", "write"]); expect(addGuestRole.config.guest.permissions.map((p) => p.permission)).toEqual([
"read",
"write",
]);
// update role // update role
await tool(server, "config_auth_roles_update", { await tool(server, "config_auth_roles_update", {
@@ -210,13 +213,15 @@ describe("mcp auth", async () => {
permissions: ["read"], permissions: ["read"],
}, },
}); });
expect(app.toJSON().auth.roles?.guest?.permissions).toEqual(["read"]); expect(app.toJSON().auth.roles?.guest?.permissions?.map((p) => p.permission)).toEqual([
"read",
]);
// get role // get role
const getGuestRole = await tool(server, "config_auth_roles_get", { const getGuestRole = await tool(server, "config_auth_roles_get", {
key: "guest", key: "guest",
}); });
expect(getGuestRole.value.permissions).toEqual(["read"]); expect(getGuestRole.value.permissions.map((p) => p.permission)).toEqual(["read"]);
// remove role // remove role
await tool(server, "config_auth_roles_remove", { await tool(server, "config_auth_roles_remove", {

View File

@@ -11,7 +11,7 @@ function createGuard(
) { ) {
const _roles = roles const _roles = roles
? objectTransform(roles, ({ permissions = [], is_default, implicit_allow }, name) => { ? objectTransform(roles, ({ permissions = [], is_default, implicit_allow }, name) => {
return Role.create({ name, permissions, is_default, implicit_allow }); return Role.create(name, { permissions, is_default, implicit_allow });
}) })
: {}; : {};
const _permissions = permissionNames.map((name) => new Permission(name)); const _permissions = permissionNames.map((name) => new Permission(name));

View File

@@ -252,7 +252,7 @@ describe("permission middleware", () => {
it("allows if user has (plain) role", async () => { it("allows if user has (plain) role", async () => {
const p = new Permission("test"); const p = new Permission("test");
const r = Role.create({ name: "test", permissions: [p.name] }); const r = Role.create("test", { permissions: [p.name] });
const hono = makeApp([p], [r]) const hono = makeApp([p], [r])
.use(async (c, next) => { .use(async (c, next) => {
// @ts-expect-error // @ts-expect-error
@@ -512,7 +512,7 @@ describe("Role", () => {
true, true,
); );
const json = JSON.parse(JSON.stringify(r.toJSON())); const json = JSON.parse(JSON.stringify(r.toJSON()));
const r2 = Role.create(json); const r2 = Role.create(p.name, json);
expect(r2.toJSON()).toEqual(r.toJSON()); expect(r2.toJSON()).toEqual(r.toJSON());
}); });
}); });

View File

@@ -2,7 +2,7 @@ import type { DB, PrimaryFieldType } from "bknd";
import * as AuthPermissions from "auth/auth-permissions"; import * as AuthPermissions from "auth/auth-permissions";
import type { AuthStrategy } from "auth/authenticate/strategies/Strategy"; import type { AuthStrategy } from "auth/authenticate/strategies/Strategy";
import type { PasswordStrategy } from "auth/authenticate/strategies/PasswordStrategy"; import type { PasswordStrategy } from "auth/authenticate/strategies/PasswordStrategy";
import { $console, secureRandomString, transformObject } from "bknd/utils"; import { $console, secureRandomString, transformObject, pick } from "bknd/utils";
import type { Entity, EntityManager } from "data/entities"; import type { Entity, EntityManager } from "data/entities";
import { em, entity, enumm, type FieldSchema } from "data/prototype"; import { em, entity, enumm, type FieldSchema } from "data/prototype";
import { Module } from "modules/Module"; import { Module } from "modules/Module";
@@ -211,12 +211,11 @@ export class AppAuth extends Module<AppAuthSchema> {
const strategies = this.authenticator.getStrategies(); const strategies = this.authenticator.getStrategies();
const roles = Object.fromEntries(this.ctx.guard.getRoles().map((r) => [r.name, r.toJSON()])); const roles = Object.fromEntries(this.ctx.guard.getRoles().map((r) => [r.name, r.toJSON()]));
console.log("roles", roles);
return { return {
...this.config, ...this.config,
...this.authenticator.toJSON(secrets), ...this.authenticator.toJSON(secrets),
roles: secrets ? roles : undefined, roles,
strategies: transformObject(strategies, (strategy) => ({ strategies: transformObject(strategies, (strategy) => ({
enabled: this.isStrategyEnabled(strategy), enabled: this.isStrategyEnabled(strategy),
...strategy.toJSON(secrets), ...strategy.toJSON(secrets),