-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
feat: Create UserCreationService
and use in API V1 create user endpoint
#19150
Open
joeauyeung
wants to merge
20
commits into
main
Choose a base branch
from
create-usercreationservice-for-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+434
−31
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a5fff6d
Create `createUser` method in `userCreationService`
joeauyeung 898377b
Refactor `UserRepository` create method to accept prisma input and re…
joeauyeung a3a7583
API use `UserCreationService`
joeauyeung 852dad7
Move slugify to service
joeauyeung ec9bc6c
Use hashPassword instead
joeauyeung d486212
Type fixes in `UserCreationService`
joeauyeung f823030
Add `userCreationService` tests
joeauyeung 01637f4
API accept data object
joeauyeung a888781
Type fixes
joeauyeung 4e0d12e
Add user _post test
joeauyeung 3e18a61
Add test for locked user
joeauyeung a679abf
Add locked param to log
joeauyeung cf319e9
Add user repository tests
joeauyeung 4aa04d4
Do not return locked status
joeauyeung 86200ec
Explicitly pass `locked` prop
joeauyeung ea94100
Fix tests when locked isn't returned
joeauyeung 8e56c25
Fix tests
joeauyeung 1eae797
Pass locked prop
joeauyeung 571ae02
Edit test name
joeauyeung a6bbd69
Use logger
joeauyeung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,134 @@ | ||
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 userQuery = await prismock.user.findFirst({ | ||
where: { | ||
email: "[email protected]", | ||
}, | ||
}); | ||
|
||
expect(userQuery).toEqual( | ||
expect.objectContaining({ | ||
email: "[email protected]", | ||
username: "test", | ||
locked: false, | ||
organizationId: null, | ||
}) | ||
); | ||
}); | ||
|
||
test("should auto lock user if email is in watchlist", async () => { | ||
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ | ||
method: "POST", | ||
body: { | ||
email: "[email protected]", | ||
username: "test", | ||
}, | ||
prisma: prismock, | ||
}); | ||
req.isSystemWideAdmin = true; | ||
|
||
await prismock.watchlist.create({ | ||
data: { | ||
type: "EMAIL", | ||
value: "[email protected]", | ||
severity: "CRITICAL", | ||
createdById: 1, | ||
}, | ||
}); | ||
|
||
await handler(req, res); | ||
|
||
expect(res.statusCode).toBe(200); | ||
|
||
const userQuery = await prismock.user.findFirst({ | ||
where: { | ||
email: "[email protected]", | ||
}, | ||
}); | ||
|
||
expect(userQuery).toEqual( | ||
expect.objectContaining({ | ||
email: "[email protected]", | ||
username: "test", | ||
locked: true, | ||
organizationId: null, | ||
}) | ||
); | ||
}); | ||
}); |
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
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,110 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import prismock from "../../../../tests/libs/__mocks__/prisma"; | ||
|
||
import { describe, test, vi, expect, beforeEach } from "vitest"; | ||
|
||
import { CreationSource } from "@calcom/prisma/enums"; | ||
|
||
import { UserRepository } from "./user"; | ||
|
||
vi.mock("@calcom/lib/server/i18n", () => { | ||
return { | ||
getTranslation: (key: string) => { | ||
return () => key; | ||
}, | ||
}; | ||
}); | ||
|
||
describe("UserRepository", () => { | ||
beforeEach(() => { | ||
prismock; | ||
}); | ||
|
||
describe("create", () => { | ||
test("Should create a user without a password", async () => { | ||
const user = await UserRepository.create({ | ||
username: "test", | ||
email: "[email protected]", | ||
organizationId: null, | ||
creationSource: CreationSource.WEBAPP, | ||
locked: false, | ||
}); | ||
|
||
expect(user).toEqual( | ||
expect.objectContaining({ | ||
username: "test", | ||
email: "[email protected]", | ||
organizationId: null, | ||
creationSource: CreationSource.WEBAPP, | ||
locked: false, | ||
}) | ||
); | ||
|
||
const password = await prismock.userPassword.findUnique({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
}); | ||
|
||
expect(password).toBeNull(); | ||
}); | ||
|
||
test("If locked param is passed, user should be locked", async () => { | ||
const user = await UserRepository.create({ | ||
username: "test", | ||
email: "[email protected]", | ||
organizationId: null, | ||
creationSource: CreationSource.WEBAPP, | ||
locked: true, | ||
}); | ||
|
||
const userQuery = await prismock.user.findUnique({ | ||
where: { | ||
email: "[email protected]", | ||
}, | ||
select: { | ||
locked: true, | ||
}, | ||
}); | ||
|
||
expect(userQuery).toEqual( | ||
expect.objectContaining({ | ||
locked: true, | ||
}) | ||
); | ||
}); | ||
|
||
test("If organizationId is passed, user should be associated with the organization", async () => { | ||
const organizationId = 123; | ||
const username = "test"; | ||
|
||
const user = await UserRepository.create({ | ||
username, | ||
email: "[email protected]", | ||
organizationId, | ||
creationSource: CreationSource.WEBAPP, | ||
locked: true, | ||
}); | ||
|
||
expect(user).toEqual( | ||
expect.objectContaining({ | ||
organizationId, | ||
}) | ||
); | ||
|
||
const profile = await prismock.profile.findFirst({ | ||
where: { | ||
organizationId, | ||
username, | ||
}, | ||
}); | ||
|
||
expect(profile).toEqual( | ||
expect.objectContaining({ | ||
organizationId, | ||
username, | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,7 +1,4 @@ | ||
import { createHash } from "crypto"; | ||
|
||
import { whereClauseForOrgWithSlugOrRequestedSlug } from "@calcom/ee/organizations/lib/orgDomains"; | ||
import { hashPassword } from "@calcom/features/auth/lib/hashPassword"; | ||
import logger from "@calcom/lib/logger"; | ||
import { safeStringify } from "@calcom/lib/safeStringify"; | ||
import { getTranslation } from "@calcom/lib/server/i18n"; | ||
|
@@ -16,7 +13,6 @@ import { userMetadata } from "@calcom/prisma/zod-utils"; | |
import type { UpId, UserProfile } from "@calcom/types/UserProfile"; | ||
|
||
import { DEFAULT_SCHEDULE, getAvailabilityFromSchedule } from "../../availability"; | ||
import slugify from "../../slugify"; | ||
import { ProfileRepository } from "./profile"; | ||
import { getParsedTeam } from "./teamUtils"; | ||
|
||
|
@@ -581,28 +577,27 @@ export class UserRepository { | |
}); | ||
} | ||
|
||
static async create({ | ||
email, | ||
username, | ||
organizationId, | ||
creationSource, | ||
}: { | ||
email: string; | ||
username: string; | ||
organizationId: number | null; | ||
creationSource: CreationSource; | ||
}) { | ||
console.log("create user", { email, username, organizationId }); | ||
const password = createHash("md5").update(`${email}${process.env.CALENDSO_ENCRYPTION_KEY}`).digest("hex"); | ||
const hashedPassword = await hashPassword(password); | ||
static async create( | ||
Comment on lines
-596
to
-597
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving this business logic to the service |
||
data: Omit<Prisma.UserCreateInput, "password" | "organization" | "movedToProfile"> & { | ||
username: string; | ||
hashedPassword?: string; | ||
organizationId: number | null; | ||
creationSource: CreationSource; | ||
locked: boolean; | ||
} | ||
) { | ||
const organizationIdValue = data.organizationId; | ||
const { email, username, creationSource, locked, ...rest } = data; | ||
|
||
logger.info("create user", { email, username, organizationIdValue, locked }); | ||
const t = await getTranslation("en", "common"); | ||
const availability = getAvailabilityFromSchedule(DEFAULT_SCHEDULE); | ||
|
||
return await prisma.user.create({ | ||
const user = await prisma.user.create({ | ||
data: { | ||
username: slugify(username), | ||
username, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also moving this business logic to the service |
||
email: email, | ||
password: { create: { hash: hashedPassword } }, | ||
...(data.hashedPassword && { password: { create: { hash: data.hashedPassword } } }), | ||
// Default schedule | ||
schedules: { | ||
create: { | ||
|
@@ -618,19 +613,25 @@ export class UserRepository { | |
}, | ||
}, | ||
}, | ||
organizationId: organizationId, | ||
profiles: organizationId | ||
creationSource, | ||
locked, | ||
...(organizationIdValue | ||
? { | ||
create: { | ||
username: slugify(username), | ||
organizationId: organizationId, | ||
uid: ProfileRepository.generateProfileUid(), | ||
organizationId: organizationIdValue, | ||
profiles: { | ||
create: { | ||
username, | ||
organizationId: organizationIdValue, | ||
uid: ProfileRepository.generateProfileUid(), | ||
}, | ||
}, | ||
} | ||
: undefined, | ||
creationSource, | ||
: {}), | ||
...rest, | ||
}, | ||
}); | ||
|
||
return user; | ||
} | ||
static async getUserAdminTeams(userId: number) { | ||
return prisma.user.findFirst({ | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll check the new org owner against the watch list in this PR #19201