* wip * restore lock * remove debug * add tests + extract util * Update packages/features/pbac/services/permission.service.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix parse permission string * use correct permission strings in tests * use eventType instead of event * fix import --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
84 lines
3.0 KiB
TypeScript
84 lines
3.0 KiB
TypeScript
import type { RolePermission } from "@prisma/client";
|
|
import { describe, it, expect } from "vitest";
|
|
|
|
import type { PermissionString } from "../../domain/types/permission-registry";
|
|
import { PermissionDiffService } from "../permission-diff.service";
|
|
|
|
describe("PermissionDiffService", () => {
|
|
const service = new PermissionDiffService();
|
|
|
|
describe("calculateDiff", () => {
|
|
it("should calculate permissions to add and remove", () => {
|
|
const existingPermissions = [
|
|
{ id: "1", roleId: "role1", resource: "booking", action: "create" },
|
|
{ id: "2", roleId: "role1", resource: "booking", action: "read" },
|
|
] as RolePermission[];
|
|
|
|
const newPermissions = ["booking.read", "booking.update", "eventType.create"] as PermissionString[];
|
|
|
|
const result = service.calculateDiff(newPermissions, existingPermissions);
|
|
|
|
expect(result.toAdd).toEqual([
|
|
{ resource: "booking", action: "update" },
|
|
{ resource: "eventType", action: "create" },
|
|
]);
|
|
|
|
expect(result.toRemove).toEqual([{ id: "1", roleId: "role1", resource: "booking", action: "create" }]);
|
|
});
|
|
|
|
it("should handle empty new permissions", () => {
|
|
const existingPermissions = [
|
|
{ id: "1", roleId: "role1", resource: "booking", action: "create" },
|
|
] as RolePermission[];
|
|
|
|
const result = service.calculateDiff([], existingPermissions);
|
|
|
|
expect(result.toAdd).toEqual([]);
|
|
expect(result.toRemove).toEqual(existingPermissions);
|
|
});
|
|
|
|
it("should handle empty existing permissions", () => {
|
|
const newPermissions = ["booking.create", "booking.read"] as PermissionString[];
|
|
|
|
const result = service.calculateDiff(newPermissions, []);
|
|
|
|
expect(result.toAdd).toEqual([
|
|
{ resource: "booking", action: "create" },
|
|
{ resource: "booking", action: "read" },
|
|
]);
|
|
expect(result.toRemove).toEqual([]);
|
|
});
|
|
|
|
it("should filter out internal _resource permissions", () => {
|
|
const existingPermissions = [
|
|
{ id: "1", roleId: "role1", resource: "booking", action: "create" },
|
|
] as RolePermission[];
|
|
|
|
const newPermissions = [
|
|
"booking.create",
|
|
"booking._resource",
|
|
"eventType.create",
|
|
] as PermissionString[];
|
|
|
|
const result = service.calculateDiff(newPermissions, existingPermissions);
|
|
|
|
expect(result.toAdd).toEqual([{ resource: "eventType", action: "create" }]);
|
|
expect(result.toRemove).toEqual([]);
|
|
});
|
|
|
|
it("should handle identical permissions with no changes needed", () => {
|
|
const existingPermissions = [
|
|
{ id: "1", roleId: "role1", resource: "booking", action: "create" },
|
|
{ id: "2", roleId: "role1", resource: "booking", action: "read" },
|
|
] as RolePermission[];
|
|
|
|
const newPermissions = ["booking.create", "booking.read"] as PermissionString[];
|
|
|
|
const result = service.calculateDiff(newPermissions, existingPermissions);
|
|
|
|
expect(result.toAdd).toEqual([]);
|
|
expect(result.toRemove).toEqual([]);
|
|
});
|
|
});
|
|
});
|