114 lines
4.1 KiB
TypeScript
114 lines
4.1 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { PermissionFlagsBits, PermissionsBitField } from "discord.js";
|
|
import { isDevOpsOrAdmin, checkDevOpsAuthorization } from "../src/utils/permissions.js";
|
|
|
|
describe("Permissions Utilities", () => {
|
|
const DEVOPS_ROLE_ID = "role-devops-123";
|
|
|
|
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);
|
|
});
|
|
|
|
it("should return true if member has Administrator permission via PermissionsBitField", () => {
|
|
const member = {
|
|
permissions: new PermissionsBitField(PermissionFlagsBits.Administrator),
|
|
roles: { cache: new Map() },
|
|
};
|
|
expect(isDevOpsOrAdmin(member as any, DEVOPS_ROLE_ID)).toBe(true);
|
|
});
|
|
|
|
it("should return true if member has Administrator permission 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);
|
|
});
|
|
|
|
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" });
|
|
|
|
const member = {
|
|
permissions: new PermissionsBitField(),
|
|
roles: {
|
|
cache: {
|
|
has: (id: string) => roleMap.has(id),
|
|
},
|
|
},
|
|
};
|
|
expect(isDevOpsOrAdmin(member as any, DEVOPS_ROLE_ID)).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", () => {
|
|
const member = {
|
|
permissions: new PermissionsBitField(),
|
|
roles: {
|
|
cache: {
|
|
has: (_id: string) => false,
|
|
},
|
|
},
|
|
};
|
|
expect(isDevOpsOrAdmin(member as any, DEVOPS_ROLE_ID)).toBe(false);
|
|
});
|
|
|
|
it("should fail securely (return false) if devopsRoleId is empty or not provided and member is not admin", () => {
|
|
const member = {
|
|
permissions: new PermissionsBitField(),
|
|
roles: ["some-role"],
|
|
};
|
|
expect(isDevOpsOrAdmin(member as any, undefined)).toBe(false);
|
|
expect(isDevOpsOrAdmin(member as any, "")).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);
|
|
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", () => {
|
|
const mockInteraction = {
|
|
guildId: "guild-123",
|
|
member: {
|
|
permissions: new PermissionsBitField(),
|
|
roles: { cache: { has: () => false } },
|
|
},
|
|
};
|
|
const result = checkDevOpsAuthorization(mockInteraction as any, DEVOPS_ROLE_ID);
|
|
expect(result.authorized).toBe(false);
|
|
expect(result.reason).toContain("Permission Denied");
|
|
});
|
|
|
|
it("should grant access when member has DevOps role in a guild", () => {
|
|
const mockInteraction = {
|
|
guildId: "guild-123",
|
|
member: {
|
|
permissions: new PermissionsBitField(),
|
|
roles: { cache: { has: (id: string) => id === DEVOPS_ROLE_ID } },
|
|
},
|
|
};
|
|
const result = checkDevOpsAuthorization(mockInteraction as any, DEVOPS_ROLE_ID);
|
|
expect(result.authorized).toBe(true);
|
|
expect(result.reason).toBeUndefined();
|
|
});
|
|
});
|
|
});
|