Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joeauyeung committed Feb 9, 2025
1 parent 268d5e3 commit a14e595
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions apps/web/pages/api/auth/setup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import prismock from "../../../../../tests/libs/__mocks__/prisma";

import type { Request, Response } from "express";
import type { NextApiRequest, NextApiResponse } from "next";
import { createMocks } from "node-mocks-http";
import { describe, test, expect, vi } from "vitest";

import handler from "./setup";

type CustomNextApiRequest = NextApiRequest & Request;
type CustomNextApiResponse = NextApiResponse & Response;

vi.mock("@calcom/lib/server/i18n", () => {
return {
getTranslation: (key: string) => {
return () => key;
},
};
});

describe("setup", () => {
test("should return a 400 error if users already exists", async () => {
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
method: "POST",
body: {
email: "[email protected]",
username: "test",
},
});

await prismock.user.create({
data: {
email: "[email protected]",
},
});

await handler(req, res);

expect(res.statusCode).toBe(400);
});

test("should return 422 if request body is invalid", async () => {
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
method: "POST",
body: {
email: "[email protected]",
},
});

await handler(req, res);

expect(res.statusCode).toBe(422);
});

test("should create a new admin user", async () => {
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
method: "POST",
body: {
username: "test",
full_name: "Test User",
email_address: "[email protected]",
password: "ADMINtestingpassword123!",
},
});

await handler(req, res);

expect(res.statusCode).toBe(200);
});
});

0 comments on commit a14e595

Please sign in to comment.