From e1dc4c034402fa2f10bf957caf556490f10b345a Mon Sep 17 00:00:00 2001 From: Krzysztof Wolski Date: Thu, 25 Aug 2022 17:29:32 +0200 Subject: [PATCH] Test withAuthTokenRequired --- .../with-auth-token-required.test.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/middleware/with-auth-token-required.test.ts diff --git a/src/middleware/with-auth-token-required.test.ts b/src/middleware/with-auth-token-required.test.ts new file mode 100644 index 00000000..881c8f52 --- /dev/null +++ b/src/middleware/with-auth-token-required.test.ts @@ -0,0 +1,44 @@ +import { Handler, Request } from "retes"; +import { Response } from "retes/response"; +import { beforeEach, describe, expect, it, vi } from "vitest"; + +import { withAuthTokenRequired } from "./with-auth-token-required"; + +const getMockSuccessResponse = async () => Response.OK({}); + +describe("middleware", () => { + describe("withAuthTokenRequired", () => { + let mockHandlerFn: Handler = vi.fn(getMockSuccessResponse); + + beforeEach(() => { + mockHandlerFn = vi.fn(getMockSuccessResponse); + }); + + it("Pass request when request has token prop", async () => { + const mockRequest = { + context: {}, + headers: {}, + params: { + auth_token: "token", + }, + } as unknown as Request; + + const response = await withAuthTokenRequired(mockHandlerFn)(mockRequest); + + expect(response.status).toBe(200); + expect(mockHandlerFn).toHaveBeenCalledOnce(); + }); + + it("Reject request without auth token", async () => { + const mockRequest = { + context: {}, + headers: {}, + params: {}, + } as unknown as Request; + + const response = await withAuthTokenRequired(mockHandlerFn)(mockRequest); + expect(response.status).eq(400); + expect(mockHandlerFn).toBeCalledTimes(0); + }); + }); +});