generated from nhcarrigan/template
refactor: hardcode non-secret Discord IDs as constants
Move guild ID, apotheosis role ID, Discord client ID, and redirect URI from env vars to hardcoded constants — none of these are secrets, and hardcoding removes unnecessary runtime configuration surface. Update tests to use the real IDs and drop now-irrelevant env var scenarios.
This commit is contained in:
@@ -20,42 +20,20 @@ describe("webhook service", () => {
|
||||
describe("grantApotheosisRole", () => {
|
||||
it("does nothing when bot token is missing", async () => {
|
||||
delete process.env["DISCORD_BOT_TOKEN"];
|
||||
process.env["DISCORD_GUILD_ID"] = "guild123";
|
||||
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "role123";
|
||||
const { grantApotheosisRole } = await import("../../src/services/webhook.js");
|
||||
await grantApotheosisRole("user123");
|
||||
expect(mockFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does nothing when guild id is missing", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "token";
|
||||
delete process.env["DISCORD_GUILD_ID"];
|
||||
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "role123";
|
||||
const { grantApotheosisRole } = await import("../../src/services/webhook.js");
|
||||
await grantApotheosisRole("user123");
|
||||
expect(mockFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does nothing when role id is missing", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "token";
|
||||
process.env["DISCORD_GUILD_ID"] = "guild123";
|
||||
delete process.env["DISCORD_APOTHEOSIS_ROLE_ID"];
|
||||
const { grantApotheosisRole } = await import("../../src/services/webhook.js");
|
||||
await grantApotheosisRole("user123");
|
||||
expect(mockFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("calls Discord API with correct URL and auth when env vars are set", async () => {
|
||||
it("calls Discord API with correct URL and auth when bot token is set", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "bot_token";
|
||||
process.env["DISCORD_GUILD_ID"] = "guild123";
|
||||
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "role456";
|
||||
mockFetch.mockResolvedValueOnce({ ok: true });
|
||||
const { grantApotheosisRole } = await import("../../src/services/webhook.js");
|
||||
await grantApotheosisRole("user789");
|
||||
expect(mockFetch).toHaveBeenCalledWith(
|
||||
"https://discord.com/api/v10/guilds/guild123/members/user789/roles/role456",
|
||||
"https://discord.com/api/v10/guilds/1354624415861833870/members/user789/roles/1479966598210129991",
|
||||
expect.objectContaining({
|
||||
method: "PUT",
|
||||
method: "PUT",
|
||||
headers: expect.objectContaining({ Authorization: "Bot bot_token" }),
|
||||
}),
|
||||
);
|
||||
@@ -63,8 +41,6 @@ describe("webhook service", () => {
|
||||
|
||||
it("swallows fetch errors gracefully", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||
process.env["DISCORD_GUILD_ID"] = "g";
|
||||
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "r";
|
||||
mockFetch.mockRejectedValueOnce(new Error("Network error"));
|
||||
const { grantApotheosisRole } = await import("../../src/services/webhook.js");
|
||||
await expect(grantApotheosisRole("user")).resolves.toBeUndefined();
|
||||
@@ -72,8 +48,6 @@ describe("webhook service", () => {
|
||||
|
||||
it("swallows non-Error fetch rejections gracefully", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||
process.env["DISCORD_GUILD_ID"] = "g";
|
||||
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "r";
|
||||
mockFetch.mockRejectedValueOnce("raw string error");
|
||||
const { grantApotheosisRole } = await import("../../src/services/webhook.js");
|
||||
await expect(grantApotheosisRole("user")).resolves.toBeUndefined();
|
||||
@@ -83,18 +57,6 @@ describe("webhook service", () => {
|
||||
describe("grantElysianRole", () => {
|
||||
it("does nothing when bot token is missing", async () => {
|
||||
delete process.env["DISCORD_BOT_TOKEN"];
|
||||
process.env["DISCORD_GUILD_ID"] = "guild123";
|
||||
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "role123";
|
||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||
const result = await grantElysianRole("user123");
|
||||
expect(mockFetch).not.toHaveBeenCalled();
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("does nothing when guild id is missing", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "token";
|
||||
delete process.env["DISCORD_GUILD_ID"];
|
||||
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "role123";
|
||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||
const result = await grantElysianRole("user123");
|
||||
expect(mockFetch).not.toHaveBeenCalled();
|
||||
@@ -103,12 +65,11 @@ describe("webhook service", () => {
|
||||
|
||||
it("returns true when Discord API responds with ok", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "bot_token";
|
||||
process.env["DISCORD_GUILD_ID"] = "guild123";
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, status: 200 });
|
||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||
const result = await grantElysianRole("user789");
|
||||
expect(mockFetch).toHaveBeenCalledWith(
|
||||
"https://discord.com/api/v10/guilds/guild123/members/user789/roles/1486144823684628490",
|
||||
"https://discord.com/api/v10/guilds/1354624415861833870/members/user789/roles/1486144823684628490",
|
||||
expect.objectContaining({
|
||||
method: "PUT",
|
||||
headers: expect.objectContaining({ Authorization: "Bot bot_token" }),
|
||||
@@ -119,7 +80,6 @@ describe("webhook service", () => {
|
||||
|
||||
it("returns true when Discord API responds with 204", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||
process.env["DISCORD_GUILD_ID"] = "g";
|
||||
mockFetch.mockResolvedValueOnce({ ok: false, status: 204 });
|
||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||
const result = await grantElysianRole("user");
|
||||
@@ -128,7 +88,6 @@ describe("webhook service", () => {
|
||||
|
||||
it("returns false when Discord API responds with an error status", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||
process.env["DISCORD_GUILD_ID"] = "g";
|
||||
mockFetch.mockResolvedValueOnce({ ok: false, status: 403 });
|
||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||
const result = await grantElysianRole("user");
|
||||
@@ -137,7 +96,6 @@ describe("webhook service", () => {
|
||||
|
||||
it("returns false and swallows fetch errors gracefully", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||
process.env["DISCORD_GUILD_ID"] = "g";
|
||||
mockFetch.mockRejectedValueOnce(new Error("Network error"));
|
||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||
const result = await grantElysianRole("user");
|
||||
@@ -146,7 +104,6 @@ describe("webhook service", () => {
|
||||
|
||||
it("returns false and swallows non-Error fetch rejections", async () => {
|
||||
process.env["DISCORD_BOT_TOKEN"] = "tok";
|
||||
process.env["DISCORD_GUILD_ID"] = "g";
|
||||
mockFetch.mockRejectedValueOnce("raw string error");
|
||||
const { grantElysianRole } = await import("../../src/services/webhook.js");
|
||||
const result = await grantElysianRole("user");
|
||||
|
||||
Reference in New Issue
Block a user