Skip to content

Commit

Permalink
🧪 add test for middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kawamataryo committed Mar 5, 2024
1 parent 547efba commit f8f1591
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/middleware/verifyDiscordInteraction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { InteractionResponseType, verifyKey } from "discord-interactions";
import {
Mock,
VitestUtils,
beforeEach,
describe,
expect,
it,
vi,
} from "vitest";
import { verifyDiscordInteraction } from "./verifyDiscordInteraction";

vi.mock("hono/adapter", () => ({
env: () => ({ DISCORD_PUBLIC_KEY: "test-key" }),
}));
vi.mock("discord-interactions", async (importOriginal) => {
const mod = await importOriginal<typeof import("discord-interactions")>();
return {
...mod,
verifyKey: vi.fn().mockReturnValue(true),
};
});

describe("verifyDiscordInteraction", () => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let ctx: any;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let next: any;

beforeEach(() => {
vi.clearAllMocks();
ctx = {
req: {
header: vi.fn().mockReturnValue("ok"),
raw: {
clone: () => ({ text: () => JSON.stringify({ type: "no-pong" }) }),
},
},
json: vi.fn(),
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
} as any;
next = vi.fn();
});

it("returns 401 if signature or timestamp is missing", async () => {
ctx.req.header.mockReturnValueOnce(null);
await verifyDiscordInteraction(ctx, next);
expect(ctx.json).toHaveBeenCalledWith(
{ message: "invalid request signature" },
401,
);
});

it("returns 401 if request is not valid", async () => {
(verifyKey as Mock).mockReturnValueOnce(false);
await verifyDiscordInteraction(ctx, next);
expect(ctx.json).toHaveBeenCalledWith(
{ message: "invalid request signature" },
401,
);
});

it("returns PONG if interaction type is PONG", async () => {
ctx.req.raw.clone = () => ({
text: () => JSON.stringify({ type: InteractionResponseType.PONG }),
});
await verifyDiscordInteraction(ctx, next);
(verifyKey as Mock).mockReturnValueOnce(true);
expect(ctx.json).toHaveBeenCalledWith({
type: InteractionResponseType.PONG,
});
});

it("calls next middleware if request is valid and interaction type is not PONG", async () => {
ctx.req.raw.clone = () => ({
text: () => JSON.stringify({ type: "no-pong" }),
});
await verifyDiscordInteraction(ctx, next);
expect(next).toHaveBeenCalled();
});
});

0 comments on commit f8f1591

Please sign in to comment.