-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Error in team members migration during org onboarding (#15349)
* fix: Error in team members migration during org onboarding * Add invitationMemberHandler tests * Add unit tests * Improve tests and refactor * Improve tests and refactor * Fix type issue * Fix createNewUsersConnectToOrgIfExists args
- Loading branch information
1 parent
750676f
commit 6670bbc
Showing
15 changed files
with
1,195 additions
and
436 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
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
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,21 @@ | ||
import { beforeEach, vi, expect } from "vitest"; | ||
import { mockReset, mockDeep } from "vitest-mock-extended"; | ||
|
||
import type * as payments from "@calcom/features/ee/teams/lib/payments"; | ||
|
||
vi.mock("@calcom/features/ee/teams/lib/payments", () => paymentsMock); | ||
|
||
beforeEach(() => { | ||
mockReset(paymentsMock); | ||
}); | ||
|
||
const paymentsMock = mockDeep<typeof payments>(); | ||
|
||
export const paymentsScenarios = {}; | ||
export const paymentsExpects = { | ||
expectQuantitySubscriptionToBeUpdatedForTeam: (teamId: number) => { | ||
expect(paymentsMock.updateQuantitySubscriptionFromStripe).toHaveBeenCalledWith(teamId); | ||
}, | ||
}; | ||
|
||
export default paymentsMock; |
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,28 @@ | ||
import { vi, beforeEach } from "vitest"; | ||
|
||
import type * as constants from "@calcom/lib/constants"; | ||
|
||
const mockedConstants = { | ||
IS_PRODUCTION: false, | ||
IS_TEAM_BILLING_ENABLED: false, | ||
} as typeof constants; | ||
|
||
vi.mock("@calcom/lib/constants", () => { | ||
return mockedConstants; | ||
}); | ||
|
||
beforeEach(() => { | ||
Object.entries(mockedConstants).forEach(([key]) => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
delete mockedConstants[key]; | ||
}); | ||
}); | ||
|
||
export const constantsScenarios = { | ||
enableTeamBilling: () => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
mockedConstants.IS_TEAM_BILLING_ENABLED = true; | ||
}, | ||
}; |
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
122 changes: 122 additions & 0 deletions
122
packages/trpc/server/routers/viewer/teams/inviteMember/__mocks__/inviteMemberUtils.ts
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,122 @@ | ||
import { beforeEach, vi, expect } from "vitest"; | ||
import { mockReset, mockDeep } from "vitest-mock-extended"; | ||
|
||
import type * as inviteMemberUtils from "../utils"; | ||
|
||
vi.mock("../utils", async () => { | ||
return inviteMemberUtilsMock; | ||
}); | ||
|
||
beforeEach(() => { | ||
mockReset(inviteMemberUtilsMock); | ||
}); | ||
const inviteMemberUtilsMock = mockDeep<typeof inviteMemberUtils>(); | ||
|
||
export const inviteMemberutilsScenarios = { | ||
checkPermissions: { | ||
fakePassed: () => | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
inviteMemberUtilsMock.checkPermissions.mockResolvedValue(undefined), | ||
}, | ||
getTeamOrThrow: { | ||
fakeReturnTeam: (team: { id: number } & Record<string, any>, forInput: { teamId: number }) => { | ||
const fakedVal = { | ||
organizationSettings: null, | ||
parent: null, | ||
parentId: null, | ||
...team, | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
inviteMemberUtilsMock.getTeamOrThrow.mockImplementation((teamId) => { | ||
if (forInput.teamId === teamId) { | ||
return fakedVal; | ||
} | ||
throw new Error("Mock Error: Unhandled input"); | ||
}); | ||
return fakedVal; | ||
}, | ||
}, | ||
getOrgState: { | ||
/** | ||
* `getOrgState` completely generates the return value from input without using any outside variable like DB, etc. | ||
* So, it makes sense to let it use the actual implementation instead of mocking the output based on input | ||
*/ | ||
useActual: async function () { | ||
const actualImport = await vi.importActual<typeof inviteMemberUtils>("../utils"); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
return inviteMemberUtilsMock.getOrgState.mockImplementation(actualImport.getOrgState); | ||
}, | ||
}, | ||
getUniqueInvitationsOrThrowIfEmpty: { | ||
useActual: async function () { | ||
const actualImport = await vi.importActual<typeof inviteMemberUtils>("../utils"); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
return inviteMemberUtilsMock.getUniqueInvitationsOrThrowIfEmpty.mockImplementation( | ||
actualImport.getUniqueInvitationsOrThrowIfEmpty | ||
); | ||
}, | ||
}, | ||
findUsersWithInviteStatus: { | ||
useAdvancedMock: function ( | ||
returnVal: Awaited<ReturnType<typeof inviteMemberUtilsMock.findUsersWithInviteStatus>>, | ||
forInput: { | ||
team: any; | ||
invitations: { | ||
usernameOrEmail: string; | ||
}[]; | ||
} | ||
) { | ||
inviteMemberUtilsMock.findUsersWithInviteStatus.mockImplementation(({ invitations, team }) => { | ||
const allInvitationsExist = invitations.every((invitation) => | ||
forInput.invitations.find((i) => i.usernameOrEmail === invitation.usernameOrEmail) | ||
); | ||
if (forInput.team.id == team.id && allInvitationsExist) return returnVal; | ||
}); | ||
return returnVal; | ||
}, | ||
}, | ||
getOrgConnectionInfo: { | ||
useActual: async function () { | ||
const actualImport = await vi.importActual<typeof inviteMemberUtils>("../utils"); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
return inviteMemberUtilsMock.getOrgConnectionInfo.mockImplementation(actualImport.getOrgConnectionInfo); | ||
}, | ||
}, | ||
}; | ||
|
||
export const expects = { | ||
expectSignupEmailsToBeSent: ({ | ||
emails, | ||
team, | ||
inviterName, | ||
isOrg, | ||
teamId, | ||
}: { | ||
emails: string[]; | ||
team; | ||
inviterName: string; | ||
teamId: number; | ||
isOrg: boolean; | ||
}) => { | ||
emails.forEach((email, index) => { | ||
expect(inviteMemberUtilsMock.sendSignupToOrganizationEmail.mock.calls[index][0]).toEqual( | ||
expect.objectContaining({ | ||
usernameOrEmail: email, | ||
team: team, | ||
inviterName: inviterName, | ||
teamId: teamId, | ||
isOrg: isOrg, | ||
}) | ||
); | ||
}); | ||
}, | ||
}; | ||
export default inviteMemberUtilsMock; |
Oops, something went wrong.