Skip to content

Commit

Permalink
Add user _post test
Browse files Browse the repository at this point in the history
  • Loading branch information
joeauyeung committed Feb 7, 2025
1 parent a888781 commit 4e0d12e
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions apps/api/v1/test/lib/users/_post.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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 "../../../pages/api/users/_post";

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

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

vi.stubEnv("CALCOM_LICENSE_KEY", undefined);

describe("POST /api/users", () => {
test("should throw 401 if not system-wide admin", async () => {
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
method: "POST",
body: {
email: "[email protected]",
username: "test",
},
});
req.isSystemWideAdmin = false;

await handler(req, res);

expect(res.statusCode).toBe(401);
});
test("should throw a 400 if no email is provided", async () => {
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
method: "POST",
body: {
username: "test",
},
});
req.isSystemWideAdmin = true;

await handler(req, res);

expect(res.statusCode).toBe(400);
});
test("should throw a 400 if no username is provided", async () => {
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
method: "POST",
body: {
email: "[email protected]",
},
});
req.isSystemWideAdmin = true;

await handler(req, res);

expect(res.statusCode).toBe(400);
});
test("should create user successfully", async () => {
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
method: "POST",
body: {
email: "[email protected]",
username: "test",
},
prisma: prismock,
});
req.isSystemWideAdmin = true;

await handler(req, res);

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

const response = JSON.parse(res._getData());

expect(response.user).toEqual(
expect.objectContaining({
email: "[email protected]",
username: "test",
locked: false,
organizationId: null,
})
);
});
});

0 comments on commit 4e0d12e

Please sign in to comment.