mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
- Updated the `Guard` class to improve permission checking by utilizing the new `Permission` class. - Refactored tests in `authorize.spec.ts` to use `Permission` instances instead of strings for better type safety. - Introduced a new `permissions.spec.ts` file to test the functionality of the `Permission` and `Policy` classes. - Enhanced the `recursivelyReplacePlaceholders` utility function to support various object structures and types. - Updated middleware and controller files to align with the new permission handling structure.
90 lines
2.2 KiB
TypeScript
90 lines
2.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { Guard } from "auth/authorize/Guard";
|
|
import { Permission } from "core/security/Permission";
|
|
|
|
describe("authorize", () => {
|
|
const read = new Permission("read");
|
|
const write = new Permission("write");
|
|
|
|
test("basic", async () => {
|
|
const guard = Guard.create(
|
|
["read", "write"],
|
|
{
|
|
admin: {
|
|
permissions: ["read", "write"],
|
|
},
|
|
},
|
|
{ enabled: true },
|
|
);
|
|
const user = {
|
|
role: "admin",
|
|
};
|
|
|
|
expect(guard.granted(read, user)).toBe(true);
|
|
expect(guard.granted(write, user)).toBe(true);
|
|
|
|
expect(() => guard.granted(new Permission("something"))).toThrow();
|
|
});
|
|
|
|
test("with default", async () => {
|
|
const guard = Guard.create(
|
|
["read", "write"],
|
|
{
|
|
admin: {
|
|
permissions: ["read", "write"],
|
|
},
|
|
guest: {
|
|
permissions: ["read"],
|
|
is_default: true,
|
|
},
|
|
},
|
|
{ enabled: true },
|
|
);
|
|
|
|
expect(guard.granted(read)).toBe(true);
|
|
expect(guard.granted(write)).toBe(false);
|
|
|
|
const user = {
|
|
role: "admin",
|
|
};
|
|
|
|
expect(guard.granted(read, user)).toBe(true);
|
|
expect(guard.granted(write, user)).toBe(true);
|
|
});
|
|
|
|
test("guard implicit allow", async () => {
|
|
const guard = Guard.create([], {}, { enabled: false });
|
|
|
|
expect(guard.granted(read)).toBe(true);
|
|
expect(guard.granted(write)).toBe(true);
|
|
});
|
|
|
|
test("role implicit allow", async () => {
|
|
const guard = Guard.create(["read", "write"], {
|
|
admin: {
|
|
implicit_allow: true,
|
|
},
|
|
});
|
|
|
|
const user = {
|
|
role: "admin",
|
|
};
|
|
|
|
expect(guard.granted(read, user)).toBe(true);
|
|
expect(guard.granted(write, user)).toBe(true);
|
|
});
|
|
|
|
test("guard with guest role implicit allow", async () => {
|
|
const guard = Guard.create(["read", "write"], {
|
|
guest: {
|
|
implicit_allow: true,
|
|
is_default: true,
|
|
},
|
|
});
|
|
|
|
expect(guard.getUserRole()?.name).toBe("guest");
|
|
expect(guard.granted(read)).toBe(true);
|
|
expect(guard.granted(write)).toBe(true);
|
|
});
|
|
});
|