-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a888781
commit 4e0d12e
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
); | ||
}); | ||
}); |