feat: SQLite RBAC, granular permissions, 100-VPS seeder, DM pagination
This commit is contained in:
1 parent
41d9e9e8eb
commit
4ccb35a44c
16 files changed
+1690
-327
No files matched your search
+184
-53
@@ -1,113 +1,244 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { PermissionFlagsBits, PermissionsBitField } from "discord.js";
|
||||
import { isDevOpsOrAdmin, checkDevOpsAuthorization } from "../src/utils/permissions.js";
|
||||
import { PermissionRepository } from "../src/repositories/permission.repository.js";
|
||||
import {
|
||||
ALL_VPS_PERMISSIONS,
|
||||
authorizeAction,
|
||||
createDefaultPermissionConfig,
|
||||
getMemberPermissions,
|
||||
hasPermission,
|
||||
RolePermissionConfig,
|
||||
} from "../src/utils/permissions.js";
|
||||
|
||||
describe("Permissions Utilities", () => {
|
||||
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("isDevOpsOrAdmin", () => {
|
||||
it("should return false if member is null or undefined", () => {
|
||||
expect(isDevOpsOrAdmin(null as any, DEVOPS_ROLE_ID)).toBe(false);
|
||||
expect(isDevOpsOrAdmin(undefined as any, DEVOPS_ROLE_ID)).toBe(false);
|
||||
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 return true if member has Administrator permission via PermissionsBitField", () => {
|
||||
it("should grant all 10 granular permissions to Server Administrator", () => {
|
||||
const member = {
|
||||
permissions: new PermissionsBitField(PermissionFlagsBits.Administrator),
|
||||
roles: { cache: new Map() },
|
||||
};
|
||||
expect(isDevOpsOrAdmin(member as any, DEVOPS_ROLE_ID)).toBe(true);
|
||||
|
||||
for (const perm of ALL_VPS_PERMISSIONS) {
|
||||
expect(hasPermission(member as any, perm, defaultConfig)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("should return true if member has Administrator permission via string bitfield", () => {
|
||||
it("should grant all permissions to Administrator via string bitfield", () => {
|
||||
const bitfield = new PermissionsBitField(PermissionFlagsBits.Administrator).bitfield.toString();
|
||||
const member = {
|
||||
permissions: bitfield,
|
||||
roles: [],
|
||||
};
|
||||
expect(isDevOpsOrAdmin(member as any, DEVOPS_ROLE_ID)).toBe(true);
|
||||
|
||||
expect(hasPermission(member as any, "vps:connect", defaultConfig)).toBe(true);
|
||||
expect(hasPermission(member as any, "vps:delete", defaultConfig)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return true if member has the DevOps role via cache.has", () => {
|
||||
const roleMap = new Map();
|
||||
roleMap.set(DEVOPS_ROLE_ID, { id: DEVOPS_ROLE_ID, name: "DevOps" });
|
||||
|
||||
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: {
|
||||
has: (id: string) => roleMap.has(id),
|
||||
},
|
||||
},
|
||||
roles: { cache: roleMap },
|
||||
};
|
||||
expect(isDevOpsOrAdmin(member as any, DEVOPS_ROLE_ID)).toBe(true);
|
||||
|
||||
for (const perm of ALL_VPS_PERMISSIONS) {
|
||||
expect(hasPermission(member as any, perm, defaultConfig)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("should return true if member has the DevOps role via string array (APIInteractionGuildMember)", () => {
|
||||
const member = {
|
||||
permissions: "0",
|
||||
roles: ["other-role", DEVOPS_ROLE_ID],
|
||||
};
|
||||
expect(isDevOpsOrAdmin(member as any, DEVOPS_ROLE_ID)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if member does not have DevOps role or Admin", () => {
|
||||
it("should grant new/regular guild members zero permissions by default (zero-trust)", () => {
|
||||
const member = {
|
||||
permissions: new PermissionsBitField(),
|
||||
roles: {
|
||||
cache: {
|
||||
has: (_id: string) => false,
|
||||
},
|
||||
},
|
||||
roles: ["some-other-role"],
|
||||
};
|
||||
expect(isDevOpsOrAdmin(member as any, DEVOPS_ROLE_ID)).toBe(false);
|
||||
|
||||
for (const perm of ALL_VPS_PERMISSIONS) {
|
||||
expect(hasPermission(member as any, perm, defaultConfig)).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it("should fail securely (return false) if devopsRoleId is empty or not provided and member is not admin", () => {
|
||||
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(isDevOpsOrAdmin(member as any, undefined)).toBe(false);
|
||||
expect(isDevOpsOrAdmin(member as any, "")).toBe(false);
|
||||
|
||||
expect(hasPermission(member as any, "vps:connect", configWithoutDevops)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkDevOpsAuthorization", () => {
|
||||
it("should deny access when command is executed outside a guild (e.g. DM)", () => {
|
||||
const mockInteraction = {
|
||||
guildId: null,
|
||||
member: null,
|
||||
};
|
||||
const result = checkDevOpsAuthorization(mockInteraction as any, DEVOPS_ROLE_ID);
|
||||
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 access when member does not hold DevOps or Admin permissions", () => {
|
||||
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: { has: () => false } },
|
||||
roles: { cache: new Map() },
|
||||
},
|
||||
};
|
||||
const result = checkDevOpsAuthorization(mockInteraction as any, DEVOPS_ROLE_ID);
|
||||
|
||||
const result = authorizeAction(mockInteraction as any, "vps:connect", defaultConfig);
|
||||
expect(result.authorized).toBe(false);
|
||||
expect(result.reason).toContain("Permission Denied");
|
||||
expect(result.reason).toContain("vps:connect");
|
||||
});
|
||||
|
||||
it("should grant access when member has DevOps role in a guild", () => {
|
||||
it("should allow vps:connect for member with DevOps role", () => {
|
||||
const mockInteraction = {
|
||||
guildId: "guild-123",
|
||||
member: {
|
||||
permissions: new PermissionsBitField(),
|
||||
roles: { cache: { has: (id: string) => id === DEVOPS_ROLE_ID } },
|
||||
roles: { cache: new Map([[DEVOPS_ROLE_ID, { id: DEVOPS_ROLE_ID }]]) },
|
||||
},
|
||||
};
|
||||
const result = checkDevOpsAuthorization(mockInteraction as any, 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");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user