Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: User UserCreationService in self serve admin creation #19200

Open
wants to merge 9 commits into
base: create-usercreationservice-for-api
Choose a base branch
from
90 changes: 90 additions & 0 deletions apps/web/pages/api/auth/setup.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 { UserPermissionRole, CreationSource, IdentityProvider } from "@calcom/prisma/enums";

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);

const user = await prismock.user.findFirst({
where: {
email: "[email protected]",
},
});

expect(user).toEqual(
expect.objectContaining({
email: "[email protected]",
username: "test",
locked: false,
organizationId: null,
identityProvider: IdentityProvider.CAL,
role: UserPermissionRole.ADMIN,
creationSource: CreationSource.SELF_SERVE_ADMIN,
})
);
});
});
14 changes: 5 additions & 9 deletions apps/web/pages/api/auth/setup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { NextApiRequest } from "next";
import z from "zod";

import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
import { isPasswordValid } from "@calcom/features/auth/lib/isPasswordValid";
import { emailRegex } from "@calcom/lib/emailSchema";
import { HttpError } from "@calcom/lib/http-error";
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
import slugify from "@calcom/lib/slugify";
import { UserCreationService } from "@calcom/lib/server/service/userCreationService";
import prisma from "@calcom/prisma";
import { IdentityProvider } from "@calcom/prisma/enums";
import { CreationSource } from "@calcom/prisma/enums";
Expand Down Expand Up @@ -34,22 +33,19 @@ async function handler(req: NextApiRequest) {
throw new HttpError({ statusCode: 422, message: parsedQuery.error.message });
}

const username = slugify(parsedQuery.data.username.trim());
const userEmail = parsedQuery.data.email_address.toLowerCase();

const hashedPassword = await hashPassword(parsedQuery.data.password);
Comment on lines -37 to -40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these two calls to the UserCreationService


await prisma.user.create({
await UserCreationService.createUser({
data: {
username,
username: parsedQuery.data.username.trim(),
email: userEmail,
password: { create: { hash: hashedPassword } },
password: parsedQuery.data.password,
role: "ADMIN",
name: parsedQuery.data.full_name,
emailVerified: new Date(),
locale: "en", // TODO: We should revisit this
identityProvider: IdentityProvider.CAL,
creationSource: CreationSource.WEBAPP,
creationSource: CreationSource.SELF_SERVE_ADMIN,
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterEnum
ALTER TYPE "CreationSource" ADD VALUE 'self_serve_admin';
7 changes: 4 additions & 3 deletions packages/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ enum PeriodType {
}

enum CreationSource {
API_V1 @map("api_v1")
API_V2 @map("api_v2")
WEBAPP @map("webapp")
API_V1 @map("api_v1")
API_V2 @map("api_v2")
WEBAPP @map("webapp")
SELF_SERVE_ADMIN @map("self_serve_admin")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should expand on the CreationSource to pinpoint where watchlisted users are created

}

model Host {
Expand Down