161 lines
4.7 KiB
JavaScript
161 lines
4.7 KiB
JavaScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
filterPermissionRecords,
|
|
getPermissionDelta,
|
|
groupPermissionRecords,
|
|
humanizeEndpoint,
|
|
humanizePermissionKey,
|
|
normalizePermissionCatalog,
|
|
sanitizePermissionDomId,
|
|
} from "../../src/views/dashboards/superUserDashboard/roles/rolePermissionCatalog.js";
|
|
|
|
const catalog = {
|
|
"/orders": {
|
|
GET: {
|
|
list_orders: "List orders",
|
|
},
|
|
POST: {
|
|
add_order: "Add an order",
|
|
},
|
|
},
|
|
"/admin/bookings": {
|
|
POST: {
|
|
create_bookings: "Create bookings",
|
|
},
|
|
},
|
|
"/superuser/system/replication": {
|
|
GET: {
|
|
superuser_replication_view: "View replication status",
|
|
},
|
|
},
|
|
};
|
|
|
|
const departments = [
|
|
{
|
|
id: 7,
|
|
name: "Copenhagen",
|
|
},
|
|
];
|
|
|
|
const permissionOverrides = {
|
|
user: {
|
|
label: "User",
|
|
description: "Allows access to user dashboard pages.",
|
|
groupKey: "access",
|
|
},
|
|
admin: {
|
|
label: "Admin",
|
|
description: "Allows access to admin dashboard pages.",
|
|
groupKey: "access",
|
|
},
|
|
superuser: {
|
|
label: "Superuser",
|
|
description: "Allows access to superuser dashboard pages.",
|
|
groupKey: "access",
|
|
},
|
|
department_access_7: {
|
|
label: "Copenhagen",
|
|
description: "Allows access to Copenhagen.",
|
|
groupKey: "access",
|
|
departmentId: 7,
|
|
},
|
|
};
|
|
|
|
describe("role permission catalog helpers", () => {
|
|
it("normalizes route permissions and virtual access permissions", () => {
|
|
const records = normalizePermissionCatalog(catalog, {
|
|
departments,
|
|
permissionOverrides,
|
|
});
|
|
|
|
expect(records.map((record) => record.permission)).toEqual(
|
|
expect.arrayContaining([
|
|
"user",
|
|
"admin",
|
|
"superuser",
|
|
"department_access_7",
|
|
"list_orders",
|
|
"add_order",
|
|
"create_bookings",
|
|
"superuser_replication_view",
|
|
])
|
|
);
|
|
expect(records.find((record) => record.permission === "department_access_7")).toMatchObject({
|
|
groupKey: "access",
|
|
label: "Copenhagen",
|
|
description: "Allows access to Copenhagen.",
|
|
isVirtual: true,
|
|
});
|
|
expect(records.find((record) => record.permission === "list_orders")).toMatchObject({
|
|
groupKey: "orders_pos",
|
|
label: "List orders",
|
|
description: "List orders",
|
|
endpoint: "/orders",
|
|
endpointLabel: "Orders",
|
|
method: "GET",
|
|
});
|
|
expect(records.find((record) => record.permission === "create_bookings")).toMatchObject({
|
|
groupKey: "bookings",
|
|
});
|
|
expect(records.find((record) => record.permission === "superuser_replication_view")).toMatchObject({
|
|
groupKey: "system_config",
|
|
});
|
|
});
|
|
|
|
it("searches labels, descriptions, keys, endpoints, and methods", () => {
|
|
const records = normalizePermissionCatalog(catalog, {
|
|
departments,
|
|
permissionOverrides,
|
|
});
|
|
|
|
expect(filterPermissionRecords(records, "copenhagen").map((record) => record.permission)).toEqual([
|
|
"department_access_7",
|
|
]);
|
|
expect(filterPermissionRecords(records, "POST").map((record) => record.permission)).toEqual(
|
|
expect.arrayContaining(["add_order", "create_bookings"])
|
|
);
|
|
expect(filterPermissionRecords(records, "replication status").map((record) => record.permission)).toEqual([
|
|
"superuser_replication_view",
|
|
]);
|
|
});
|
|
|
|
it("groups records by stable group key", () => {
|
|
const grouped = groupPermissionRecords(
|
|
normalizePermissionCatalog(catalog, {
|
|
departments,
|
|
permissionOverrides,
|
|
})
|
|
);
|
|
|
|
expect(grouped.get("access").map((record) => record.permission)).toEqual(
|
|
expect.arrayContaining(["user", "admin", "superuser", "department_access_7"])
|
|
);
|
|
expect(grouped.get("orders_pos").map((record) => record.permission)).toEqual(
|
|
expect.arrayContaining(["list_orders", "add_order"])
|
|
);
|
|
expect(grouped.get("bookings").map((record) => record.permission)).toEqual(["create_bookings"]);
|
|
});
|
|
|
|
it("calculates bulk add and remove deltas without no-op requests", () => {
|
|
const records = normalizePermissionCatalog(catalog, {
|
|
departments,
|
|
permissionOverrides,
|
|
}).filter((record) => ["list_orders", "add_order"].includes(record.permission));
|
|
|
|
expect(getPermissionDelta(records, new Set(["list_orders"]), true)).toEqual({
|
|
add: ["add_order"],
|
|
remove: [],
|
|
});
|
|
expect(getPermissionDelta(records, new Set(["list_orders"]), false)).toEqual({
|
|
add: [],
|
|
remove: ["list_orders"],
|
|
});
|
|
});
|
|
|
|
it("humanizes and sanitizes permission display values", () => {
|
|
expect(humanizePermissionKey("superuser_replication_view")).toBe("Superuser replication view");
|
|
expect(humanizeEndpoint("/superuser/system/replication")).toBe("Superuser system replication");
|
|
expect(sanitizePermissionDomId("department/access 7")).toBe("department-access-7");
|
|
});
|
|
});
|