Skip to content

Commit

Permalink
Test withAuthTokenRequired
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofwolski committed Aug 25, 2022
1 parent 0882c7e commit e1dc4c0
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/middleware/with-auth-token-required.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit e1dc4c0

Please sign in to comment.