Files
NODEDC_LAUNCHER/server/device-core-session-access.test.mjs

125 lines
3.5 KiB
JavaScript

import { describe, expect, it } from "vitest";
import { resolveDeviceCoreSessionAccess } from "./device-core-session-access.mjs";
const service = {
id: "service_device_core",
slug: "device-core",
status: "active",
};
const user = {
id: "user_device_admin",
email: "admin@example.test",
name: "Device Admin",
globalStatus: "active",
};
const company = {
id: "client_dctouch",
type: "company",
name: "DC Touch",
status: "active",
};
const membership = {
id: "membership_device_admin",
clientId: company.id,
userId: user.id,
role: "client_admin",
status: "active",
};
function data(overrides = {}) {
return {
services: [service],
users: [user],
clients: [company],
memberships: [membership],
groups: [],
grants: [{
id: "grant_device_core_dctouch",
serviceId: service.id,
targetType: "client",
targetId: company.id,
appRole: "admin",
status: "active",
}],
exceptions: [],
...overrides,
};
}
describe("Device Core trusted session access", () => {
it("projects an administrative company scope only from an active scoped grant", () => {
expect(resolveDeviceCoreSessionAccess(data(), user)).toEqual({
allowed: true,
hubRole: "admin",
ownerScopes: [
{
scopeKind: "company",
ownerRef: "client:client_dctouch",
displayName: "DC Touch",
},
{
scopeKind: "personal",
ownerRef: "user:user_device_admin",
displayName: "Device Admin",
},
],
});
});
it("does not turn company membership alone into a Device Core owner claim", () => {
const access = resolveDeviceCoreSessionAccess(data({ grants: [] }), user);
expect(access).toEqual({ allowed: false, hubRole: "viewer", ownerScopes: [] });
});
it("keeps member and viewer grants out of administrative owner scopes", () => {
for (const appRole of ["member", "viewer"]) {
const access = resolveDeviceCoreSessionAccess(data({
grants: [{
id: `grant_${appRole}`,
serviceId: service.id,
targetType: "client",
targetId: company.id,
appRole,
status: "active",
}],
}), user);
expect(access.allowed).toBe(true);
expect(access.hubRole).toBe(appRole);
expect(access.ownerScopes).toEqual([]);
}
});
it("fails closed on a deny exception even when grants exist", () => {
expect(resolveDeviceCoreSessionAccess(data({
exceptions: [{
id: "deny_device_core",
serviceId: service.id,
userId: user.id,
type: "deny",
}],
}), user)).toEqual({ allowed: false, hubRole: "viewer", ownerScopes: [] });
});
it("keeps an individual allow exception at member privilege", () => {
expect(resolveDeviceCoreSessionAccess(data({
exceptions: [{
id: "allow_device_core",
serviceId: service.id,
userId: user.id,
type: "allow",
}],
}), user)).toEqual({ allowed: true, hubRole: "member", ownerScopes: [] });
});
it("does not leak a grant from one company into another company scope", () => {
const secondCompany = { ...company, id: "client_other", name: "Other Company" };
const secondMembership = { ...membership, id: "membership_other", clientId: secondCompany.id };
const access = resolveDeviceCoreSessionAccess(data({
clients: [company, secondCompany],
memberships: [membership, secondMembership],
}), user);
expect(access.ownerScopes.some((scope) => scope.ownerRef === "client:client_other")).toBe(false);
});
});