245 lines
9.5 KiB
TypeScript
245 lines
9.5 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
import { PermissionFlagsBits, PermissionsBitField } from "discord.js";
|
|
import { PermissionRepository } from "../src/repositories/permission.repository.js";
|
|
import {
|
|
ALL_VPS_PERMISSIONS,
|
|
authorizeAction,
|
|
createDefaultPermissionConfig,
|
|
getMemberPermissions,
|
|
hasPermission,
|
|
RolePermissionConfig,
|
|
} from "../src/utils/permissions.js";
|
|
|
|
describe("Granular Action-Based Permissions System", () => {
|
|
const DEVOPS_ROLE_ID = "role-devops-123";
|
|
const VIEWER_ROLE_ID = "role-viewer-456";
|
|
const defaultConfig = createDefaultPermissionConfig(DEVOPS_ROLE_ID);
|
|
|
|
describe("getMemberPermissions & hasPermission (in-memory config)", () => {
|
|
it("should return empty set if member is null or undefined", () => {
|
|
expect(getMemberPermissions(null as any, defaultConfig).size).toBe(0);
|
|
expect(hasPermission(undefined as any, "vps:list", defaultConfig)).toBe(false);
|
|
});
|
|
|
|
it("should grant all 10 granular permissions to Server Administrator", () => {
|
|
const member = {
|
|
permissions: new PermissionsBitField(PermissionFlagsBits.Administrator),
|
|
roles: { cache: new Map() },
|
|
};
|
|
|
|
for (const perm of ALL_VPS_PERMISSIONS) {
|
|
expect(hasPermission(member as any, perm, defaultConfig)).toBe(true);
|
|
}
|
|
});
|
|
|
|
it("should grant all permissions to Administrator via string bitfield", () => {
|
|
const bitfield = new PermissionsBitField(PermissionFlagsBits.Administrator).bitfield.toString();
|
|
const member = {
|
|
permissions: bitfield,
|
|
roles: [],
|
|
};
|
|
|
|
expect(hasPermission(member as any, "vps:connect", defaultConfig)).toBe(true);
|
|
expect(hasPermission(member as any, "vps:delete", defaultConfig)).toBe(true);
|
|
});
|
|
|
|
it("should grant DevOps role all 10 granular VPS permissions via role cache", () => {
|
|
const roleMap = new Map([[DEVOPS_ROLE_ID, { id: DEVOPS_ROLE_ID }]]);
|
|
const member = {
|
|
permissions: new PermissionsBitField(),
|
|
roles: { cache: roleMap },
|
|
};
|
|
|
|
for (const perm of ALL_VPS_PERMISSIONS) {
|
|
expect(hasPermission(member as any, perm, defaultConfig)).toBe(true);
|
|
}
|
|
});
|
|
|
|
it("should grant new/regular guild members zero permissions by default (zero-trust)", () => {
|
|
const member = {
|
|
permissions: new PermissionsBitField(),
|
|
roles: ["some-other-role"],
|
|
};
|
|
|
|
for (const perm of ALL_VPS_PERMISSIONS) {
|
|
expect(hasPermission(member as any, perm, defaultConfig)).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("should support per-feature Role -> Permission mappings", () => {
|
|
const granularConfig: RolePermissionConfig = {
|
|
defaultGuildPermissions: [],
|
|
rolePermissions: {
|
|
[VIEWER_ROLE_ID]: ["vps:list", "vps:info"],
|
|
[DEVOPS_ROLE_ID]: [...ALL_VPS_PERMISSIONS],
|
|
},
|
|
adminBypass: true,
|
|
};
|
|
|
|
const noRoleMember = { permissions: "0", roles: [] };
|
|
const viewerMember = { permissions: "0", roles: [VIEWER_ROLE_ID] };
|
|
|
|
expect(hasPermission(noRoleMember as any, "vps:list", granularConfig)).toBe(false);
|
|
expect(hasPermission(viewerMember as any, "vps:list", granularConfig)).toBe(true);
|
|
expect(hasPermission(viewerMember as any, "vps:info", granularConfig)).toBe(true);
|
|
expect(hasPermission(viewerMember as any, "vps:search", granularConfig)).toBe(false);
|
|
expect(hasPermission(viewerMember as any, "vps:filter", granularConfig)).toBe(false);
|
|
expect(hasPermission(viewerMember as any, "vps:stats", granularConfig)).toBe(false);
|
|
expect(hasPermission(viewerMember as any, "vps:connect", granularConfig)).toBe(false);
|
|
});
|
|
|
|
it("should fail securely when devopsRoleId is missing", () => {
|
|
const configWithoutDevops = createDefaultPermissionConfig(undefined);
|
|
const member = {
|
|
permissions: new PermissionsBitField(),
|
|
roles: ["some-role"],
|
|
};
|
|
|
|
expect(hasPermission(member as any, "vps:connect", configWithoutDevops)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("authorizeAction (in-memory config)", () => {
|
|
it("should deny access when executed outside a guild (e.g. DM)", () => {
|
|
const mockInteraction = { guildId: null, member: null };
|
|
const result = authorizeAction(mockInteraction as any, "vps:list", defaultConfig);
|
|
|
|
expect(result.authorized).toBe(false);
|
|
expect(result.reason).toContain("only be executed within an authorized Discord Server");
|
|
});
|
|
|
|
it("should deny vps:connect for unassigned member and include missing permission in reason", () => {
|
|
const mockInteraction = {
|
|
guildId: "guild-123",
|
|
member: {
|
|
permissions: new PermissionsBitField(),
|
|
roles: { cache: new Map() },
|
|
},
|
|
};
|
|
|
|
const result = authorizeAction(mockInteraction as any, "vps:connect", defaultConfig);
|
|
expect(result.authorized).toBe(false);
|
|
expect(result.reason).toContain("vps:connect");
|
|
});
|
|
|
|
it("should allow vps:connect for member with DevOps role", () => {
|
|
const mockInteraction = {
|
|
guildId: "guild-123",
|
|
member: {
|
|
permissions: new PermissionsBitField(),
|
|
roles: { cache: new Map([[DEVOPS_ROLE_ID, { id: DEVOPS_ROLE_ID }]]) },
|
|
},
|
|
};
|
|
|
|
const result = authorizeAction(mockInteraction as any, "vps:connect", defaultConfig);
|
|
expect(result.authorized).toBe(true);
|
|
expect(result.reason).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("SQLite PermissionRepository integration", () => {
|
|
let permRepo: PermissionRepository;
|
|
|
|
beforeEach(() => {
|
|
permRepo = new PermissionRepository(":memory:");
|
|
permRepo.init();
|
|
permRepo.seedIfEmpty(DEVOPS_ROLE_ID);
|
|
});
|
|
|
|
afterEach(() => {
|
|
permRepo.close();
|
|
});
|
|
|
|
it("should enforce zero-trust for new members and grant DevOps role all 10 permissions", () => {
|
|
const newMember = { permissions: "0", roles: ["unassigned-role"] };
|
|
const devopsMember = { permissions: "0", roles: [DEVOPS_ROLE_ID] };
|
|
|
|
expect(hasPermission(newMember as any, "vps:list", permRepo)).toBe(false);
|
|
expect(hasPermission(newMember as any, "vps:connect", permRepo)).toBe(false);
|
|
|
|
for (const perm of ALL_VPS_PERMISSIONS) {
|
|
expect(hasPermission(devopsMember as any, perm, permRepo)).toBe(true);
|
|
}
|
|
});
|
|
|
|
it("should isolate individual feature permissions (list vs info vs search vs add/update/delete)", () => {
|
|
const customRole = "role-custom-999";
|
|
const customMember = { permissions: "0", roles: [customRole] };
|
|
|
|
permRepo.grantRolePermission(customRole, "vps:list");
|
|
permRepo.grantRolePermission(customRole, "vps:add");
|
|
|
|
expect(hasPermission(customMember as any, "vps:list", permRepo)).toBe(true);
|
|
expect(hasPermission(customMember as any, "vps:add", permRepo)).toBe(true);
|
|
expect(hasPermission(customMember as any, "vps:info", permRepo)).toBe(false);
|
|
expect(hasPermission(customMember as any, "vps:search", permRepo)).toBe(false);
|
|
expect(hasPermission(customMember as any, "vps:update", permRepo)).toBe(false);
|
|
expect(hasPermission(customMember as any, "vps:delete", permRepo)).toBe(false);
|
|
|
|
permRepo.revokeRolePermission(customRole, "vps:list");
|
|
expect(hasPermission(customMember as any, "vps:list", permRepo)).toBe(false);
|
|
|
|
permRepo.revokeAllRolePermissions(customRole);
|
|
expect(hasPermission(customMember as any, "vps:add", permRepo)).toBe(false);
|
|
});
|
|
|
|
it("should deny new members and authorize roles assigned in SQLite", () => {
|
|
const newMemberInteraction = {
|
|
guildId: "guild-123",
|
|
member: { permissions: "0", roles: [] },
|
|
};
|
|
const devopsInteraction = {
|
|
guildId: "guild-123",
|
|
member: { permissions: "0", roles: [DEVOPS_ROLE_ID] },
|
|
};
|
|
|
|
expect(authorizeAction(newMemberInteraction as any, "vps:list", permRepo).authorized).toBe(false);
|
|
expect(authorizeAction(newMemberInteraction as any, "vps:connect", permRepo).authorized).toBe(false);
|
|
expect(authorizeAction(devopsInteraction as any, "vps:list", permRepo).authorized).toBe(true);
|
|
expect(authorizeAction(devopsInteraction as any, "vps:connect", permRepo).authorized).toBe(true);
|
|
});
|
|
|
|
it("should enforce strict foreign keys on role_permissions (roles and permissions)", () => {
|
|
const db = permRepo.getDb();
|
|
|
|
expect(() =>
|
|
db
|
|
.prepare("INSERT INTO role_permissions (role_id, permission_name) VALUES (?, ?)")
|
|
.run("non-existent-role", "vps:list")
|
|
).toThrow(/FOREIGN KEY constraint failed/);
|
|
|
|
expect(() =>
|
|
db
|
|
.prepare("INSERT INTO role_permissions (role_id, permission_name) VALUES (?, ?)")
|
|
.run(DEVOPS_ROLE_ID, "invalid:perm")
|
|
).toThrow(/FOREIGN KEY constraint failed/);
|
|
});
|
|
|
|
it("should join role names and record audit logs", () => {
|
|
permRepo.grantRolePermission("role-auditor-777", "vps:info", {
|
|
roleName: "Auditor",
|
|
grantedBy: "admin-1",
|
|
});
|
|
|
|
const entries = permRepo.listRolePermissions();
|
|
const auditorEntry = entries.find((e) => e.roleId === "role-auditor-777");
|
|
expect(auditorEntry).toBeDefined();
|
|
expect(auditorEntry?.roleName).toBe("Auditor");
|
|
expect(auditorEntry?.grantedBy).toBe("admin-1");
|
|
|
|
permRepo.logAudit({
|
|
actorId: "user-1",
|
|
actorTag: "khuong#0001",
|
|
action: "vps:connect",
|
|
target: "shop-prod-01",
|
|
status: "ALLOWED",
|
|
});
|
|
|
|
const logs = permRepo.getRecentAuditLogs(5);
|
|
expect(logs).toHaveLength(1);
|
|
expect(logs[0].action).toBe("vps:connect");
|
|
expect(logs[0].status).toBe("ALLOWED");
|
|
});
|
|
});
|
|
});
|