first commit
This commit is contained in:
commit
41d9e9e8eb
21 files changed
+3538
No files matched your search
@@ -0,0 +1,113 @@
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,115 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { generateSSHCommand, isValidHost, isValidUsername, isValidPort } from "../src/utils/ssh.js";
|
||||
import { VPS } from "../src/types/vps.js";
|
||||
|
||||
describe("SSH Utility", () => {
|
||||
const baseVPS: VPS = {
|
||||
id: "vps-test",
|
||||
name: "test-node",
|
||||
provider: "Vultr",
|
||||
region: "Singapore",
|
||||
environment: "Production",
|
||||
cpu: 4,
|
||||
ramGB: 8,
|
||||
diskGB: 160,
|
||||
};
|
||||
|
||||
describe("Validation functions", () => {
|
||||
it("should correctly validate IPv4 and hostnames", () => {
|
||||
expect(isValidHost("203.0.113.10")).toBe(true);
|
||||
expect(isValidHost("192.168.1.1")).toBe(true);
|
||||
expect(isValidHost("vps.example.com")).toBe(true);
|
||||
|
||||
// Malicious or invalid hosts
|
||||
expect(isValidHost("203.0.113.10; rm -rf /")).toBe(false);
|
||||
expect(isValidHost("`whoami`.example.com")).toBe(false);
|
||||
expect(isValidHost("-oProxyCommand=calc.exe")).toBe(false);
|
||||
expect(isValidHost("256.300.1.1")).toBe(false);
|
||||
});
|
||||
|
||||
it("should correctly validate SSH usernames", () => {
|
||||
expect(isValidUsername("root")).toBe(true);
|
||||
expect(isValidUsername("deploy")).toBe(true);
|
||||
expect(isValidUsername("admin_user-01")).toBe(true);
|
||||
|
||||
// Malicious usernames
|
||||
expect(isValidUsername("deploy; ls")).toBe(false);
|
||||
expect(isValidUsername("user$(whoami)")).toBe(false);
|
||||
expect(isValidUsername("user`id`")).toBe(false);
|
||||
expect(isValidUsername("user with spaces")).toBe(false);
|
||||
});
|
||||
|
||||
it("should correctly validate ports", () => {
|
||||
expect(isValidPort(22)).toBe(true);
|
||||
expect(isValidPort(2222)).toBe(true);
|
||||
expect(isValidPort(65535)).toBe(true);
|
||||
|
||||
expect(isValidPort(0)).toBe(false);
|
||||
expect(isValidPort(-1)).toBe(false);
|
||||
expect(isValidPort(70000)).toBe(false);
|
||||
expect(isValidPort(22.5)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("generateSSHCommand", () => {
|
||||
it("should fail gracefully if IP is missing", () => {
|
||||
const vpsWithoutIP: VPS = { ...baseVPS, sshUser: "deploy" };
|
||||
const result = generateSSHCommand(vpsWithoutIP);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe("SSH connection information has not been configured for this VPS.");
|
||||
expect(result.command).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should fail gracefully if sshUser is missing", () => {
|
||||
const vpsWithoutUser: VPS = { ...baseVPS, ip: "203.0.113.10" };
|
||||
const result = generateSSHCommand(vpsWithoutUser);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe("SSH connection information has not been configured for this VPS.");
|
||||
});
|
||||
|
||||
it("should default to port 22 when sshPort is not specified", () => {
|
||||
const vps: VPS = {
|
||||
...baseVPS,
|
||||
ip: "203.0.113.10",
|
||||
sshUser: "deploy",
|
||||
};
|
||||
const result = generateSSHCommand(vps);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.command).toBe("ssh -p 22 deploy@203.0.113.10");
|
||||
});
|
||||
|
||||
it("should use custom port when sshPort is specified", () => {
|
||||
const vps: VPS = {
|
||||
...baseVPS,
|
||||
ip: "198.51.100.25",
|
||||
sshUser: "ubuntu",
|
||||
sshPort: 22022,
|
||||
};
|
||||
const result = generateSSHCommand(vps);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.command).toBe("ssh -p 22022 ubuntu@198.51.100.25");
|
||||
});
|
||||
|
||||
it("should reject malicious IP input", () => {
|
||||
const vps: VPS = {
|
||||
...baseVPS,
|
||||
ip: "203.0.113.10; id",
|
||||
sshUser: "deploy",
|
||||
};
|
||||
const result = generateSSHCommand(vps);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain("invalid or unsafe");
|
||||
});
|
||||
|
||||
it("should reject malicious username input", () => {
|
||||
const vps: VPS = {
|
||||
...baseVPS,
|
||||
ip: "203.0.113.10",
|
||||
sshUser: "deploy&&reboot",
|
||||
};
|
||||
const result = generateSSHCommand(vps);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain("invalid or unsafe");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,197 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import path from "node:path";
|
||||
import { VPSRepository } from "../src/repositories/vps.repository.js";
|
||||
import { VPSService } from "../src/services/vps.service.js";
|
||||
import { VPS } from "../src/types/vps.js";
|
||||
|
||||
describe("VPS Repository and Service", () => {
|
||||
let repository: VPSRepository;
|
||||
let service: VPSService;
|
||||
|
||||
beforeEach(() => {
|
||||
// Point to data/vps.json
|
||||
const seedPath = path.resolve(process.cwd(), "data/vps.json");
|
||||
repository = new VPSRepository(seedPath);
|
||||
service = new VPSService(repository);
|
||||
});
|
||||
|
||||
describe("Repository loading and validation", () => {
|
||||
it("should load exactly the 3 seed VPS records", () => {
|
||||
const all = repository.getAll();
|
||||
expect(all).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("should throw a descriptive error when file is missing", () => {
|
||||
const invalidRepo = new VPSRepository("non-existent-path.json");
|
||||
expect(() => invalidRepo.getAll()).toThrow("Inventory data file not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("listAll", () => {
|
||||
it("should return all three VPS with correct properties", () => {
|
||||
const list = service.listAll();
|
||||
expect(list).toHaveLength(3);
|
||||
|
||||
const names = list.map((v) => v.name);
|
||||
expect(names).toEqual(["shop-prod-01", "staging-01", "db-prod-01"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getByName", () => {
|
||||
it("should return the exact VPS when name matches", () => {
|
||||
const vps = service.getByName("shop-prod-01");
|
||||
expect(vps).toBeDefined();
|
||||
expect(vps?.name).toBe("shop-prod-01");
|
||||
expect(vps?.provider).toBe("Vultr");
|
||||
expect(vps?.region).toBe("Singapore");
|
||||
expect(vps?.environment).toBe("Production");
|
||||
expect(vps?.cpu).toBe(4);
|
||||
expect(vps?.ramGB).toBe(8);
|
||||
expect(vps?.diskGB).toBe(160);
|
||||
});
|
||||
|
||||
it("should support case-insensitive name lookup", () => {
|
||||
const vps1 = service.getByName("SHOP-PROD-01");
|
||||
const vps2 = service.getByName("sHoP-pRoD-01");
|
||||
expect(vps1).toBeDefined();
|
||||
expect(vps1?.id).toBe("vps-001");
|
||||
expect(vps2?.id).toBe("vps-001");
|
||||
});
|
||||
|
||||
it("should trim surrounding whitespace in name lookup", () => {
|
||||
const vps = service.getByName(" staging-01 ");
|
||||
expect(vps).toBeDefined();
|
||||
expect(vps?.name).toBe("staging-01");
|
||||
});
|
||||
|
||||
it("should return undefined for non-existent VPS", () => {
|
||||
const vps = service.getByName("non-existent-vps");
|
||||
expect(vps).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for empty string or whitespace-only name", () => {
|
||||
expect(service.getByName("")).toBeUndefined();
|
||||
expect(service.getByName(" ")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("search", () => {
|
||||
it("should search by partial name case-insensitively", () => {
|
||||
const results = service.search("prod");
|
||||
expect(results).toHaveLength(2);
|
||||
expect(results.map((v) => v.name)).toEqual(["shop-prod-01", "db-prod-01"]);
|
||||
});
|
||||
|
||||
it("should return staging-01 when searching 'stag'", () => {
|
||||
const results = service.search("STAG");
|
||||
expect(results).toHaveLength(1);
|
||||
expect(results[0].name).toBe("staging-01");
|
||||
});
|
||||
|
||||
it("should search by IP address when IP is configured", () => {
|
||||
// Mock repository with an IP configured
|
||||
const mockRepo = new VPSRepository();
|
||||
const mockVPSList: VPS[] = [
|
||||
{
|
||||
id: "vps-test",
|
||||
name: "test-vps",
|
||||
provider: "AWS",
|
||||
region: "Tokyo",
|
||||
environment: "Development",
|
||||
cpu: 2,
|
||||
ramGB: 4,
|
||||
diskGB: 40,
|
||||
ip: "192.0.2.15",
|
||||
},
|
||||
];
|
||||
mockRepo.getAll = () => mockVPSList;
|
||||
const testService = new VPSService(mockRepo);
|
||||
|
||||
const byIp = testService.search("192.0.2", true);
|
||||
expect(byIp).toHaveLength(1);
|
||||
expect(byIp[0].name).toBe("test-vps");
|
||||
|
||||
// When IP search is not permitted (e.g. unauthenticated)
|
||||
const noIp = testService.search("192.0.2", false);
|
||||
expect(noIp).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should return empty array for empty search query", () => {
|
||||
const results = service.search(" ");
|
||||
expect(results).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return empty array when no VPS matches", () => {
|
||||
const results = service.search("unknown-query-xyz");
|
||||
expect(results).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("filter", () => {
|
||||
it("should filter by Provider case-insensitively", () => {
|
||||
const vultr = service.filter({ provider: "vultr" });
|
||||
expect(vultr).toHaveLength(2);
|
||||
expect(vultr.map((v) => v.name)).toEqual(["shop-prod-01", "db-prod-01"]);
|
||||
|
||||
const doServers = service.filter({ provider: "DigitalOcean" });
|
||||
expect(doServers).toHaveLength(1);
|
||||
expect(doServers[0].name).toBe("staging-01");
|
||||
});
|
||||
|
||||
it("should filter by Environment", () => {
|
||||
const prod = service.filter({ environment: "Production" });
|
||||
expect(prod).toHaveLength(2);
|
||||
|
||||
const staging = service.filter({ environment: "Staging" });
|
||||
expect(staging).toHaveLength(1);
|
||||
expect(staging[0].name).toBe("staging-01");
|
||||
|
||||
const dev = service.filter({ environment: "Development" });
|
||||
expect(dev).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should support combining both filters using AND logic", () => {
|
||||
const result1 = service.filter({ provider: "Vultr", environment: "Production" });
|
||||
expect(result1).toHaveLength(2);
|
||||
|
||||
const result2 = service.filter({ provider: "DigitalOcean", environment: "Production" });
|
||||
expect(result2).toHaveLength(0);
|
||||
|
||||
const result3 = service.filter({ provider: "DigitalOcean", environment: "Staging" });
|
||||
expect(result3).toHaveLength(1);
|
||||
expect(result3[0].name).toBe("staging-01");
|
||||
});
|
||||
|
||||
it("should return empty array when no records match filter criteria", () => {
|
||||
const none = service.filter({ provider: "NonExistentProvider" });
|
||||
expect(none).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getStats", () => {
|
||||
it("should accurately compute all aggregate statistics for seed data", () => {
|
||||
const stats = service.getStats();
|
||||
|
||||
// Verified exact calculations:
|
||||
// shop-prod-01: 4 CPU, 8 RAM, 160 Disk
|
||||
// staging-01: 2 CPU, 4 RAM, 80 Disk
|
||||
// db-prod-01: 4 CPU, 16 RAM, 320 Disk
|
||||
expect(stats.totalVPS).toBe(3);
|
||||
expect(stats.totalVCPU).toBe(10);
|
||||
expect(stats.totalRamGB).toBe(28);
|
||||
expect(stats.totalDiskGB).toBe(560);
|
||||
|
||||
// Environment breakdown
|
||||
expect(stats.byEnvironment).toEqual({
|
||||
Production: 2,
|
||||
Staging: 1,
|
||||
});
|
||||
|
||||
// Provider breakdown
|
||||
expect(stats.byProvider).toEqual({
|
||||
Vultr: 2,
|
||||
DigitalOcean: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user