Skip to content

Commit c0ab115

Browse files
committed
fix tests
1 parent 1f67177 commit c0ab115

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

packages/features/ee/dsync/lib/handleUserEvents.test.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ async function createMockOrganization({ id, name, slug }: { id: number; name: st
6464
});
6565
}
6666

67-
async function createMockUser({
68-
email,
69-
organizationId,
70-
}: {
71-
email: string;
72-
organizationId: number | null;
73-
}) {
67+
async function createMockUser({ email, organizationId }: { email: string; organizationId: number | null }) {
7468
return prismock.user.create({
7569
data: {
7670
email,
@@ -111,16 +105,14 @@ describe("handleUserEvents", () => {
111105

112106
beforeEach(async () => {
113107
vi.clearAllMocks();
114-
108+
115109
await createMockOrganization({
116110
id: organizationId,
117111
name: organizationName,
118112
slug: organizationSlug,
119113
});
120114

121-
const { getTeamOrThrow } = await import(
122-
"@calcom/trpc/server/routers/viewer/teams/inviteMember/utils"
123-
);
115+
const { getTeamOrThrow } = await import("@calcom/trpc/server/routers/viewer/teams/inviteMember/utils");
124116
vi.mocked(getTeamOrThrow).mockResolvedValue({
125117
id: organizationId,
126118
name: organizationName,
@@ -145,7 +137,7 @@ describe("handleUserEvents", () => {
145137

146138
await createMockUser({
147139
email: userEmail,
148-
organizationId: differentOrgId,
140+
organizationId: 9999,
149141
});
150142

151143
const event: DirectorySyncEvent = {
@@ -203,13 +195,13 @@ describe("handleUserEvents", () => {
203195
() =>
204196
({
205197
isAMemberOfOrganization: vi.fn().mockResolvedValue(true),
206-
}) as unknown as InstanceType<typeof UserRepository>
198+
} as unknown as InstanceType<typeof UserRepository>)
207199
);
208200

209201
await expect(handleUserEvents(event, organizationId)).resolves.not.toThrow();
210202
});
211203

212-
it("should throw error when user has no organizationId (prevents hijacking legacy users)", async () => {
204+
it("should pass when user has no organizationId (allow existing user to be added to an org)", async () => {
213205
const userEmail = "[email protected]";
214206

215207
await createMockUser({
@@ -233,9 +225,7 @@ describe("handleUserEvents", () => {
233225
},
234226
};
235227

236-
await expect(handleUserEvents(event, organizationId)).rejects.toThrow(
237-
"User belongs to another organization."
238-
);
228+
await expect(handleUserEvents(event, organizationId)).resolves.toBeUndefined();
239229
});
240230

241231
it("should succeed when user does not exist yet (new user creation)", async () => {
@@ -306,7 +296,7 @@ describe("handleUserEvents", () => {
306296
() =>
307297
({
308298
isAMemberOfOrganization: vi.fn().mockResolvedValue(false),
309-
}) as unknown as InstanceType<typeof UserRepository>
299+
} as unknown as InstanceType<typeof UserRepository>)
310300
);
311301

312302
const inviteExistingUserToOrg = (await import("./users/inviteExistingUserToOrg")).default;
@@ -391,7 +381,7 @@ describe("handleUserEvents", () => {
391381
() =>
392382
({
393383
isAMemberOfOrganization: vi.fn().mockResolvedValue(true),
394-
}) as unknown as InstanceType<typeof UserRepository>
384+
} as unknown as InstanceType<typeof UserRepository>)
395385
);
396386

397387
const { assignValueToUserInOrgBulk } = await import("./assignValueToUser");
@@ -432,10 +422,10 @@ describe("handleUserEvents", () => {
432422
},
433423
};
434424

435-
const { getTeamOrThrow } = await import(
436-
"@calcom/trpc/server/routers/viewer/teams/inviteMember/utils"
425+
const { getTeamOrThrow } = await import("@calcom/trpc/server/routers/viewer/teams/inviteMember/utils");
426+
vi.mocked(getTeamOrThrow).mockResolvedValue(
427+
null as unknown as Awaited<ReturnType<typeof getTeamOrThrow>>
437428
);
438-
vi.mocked(getTeamOrThrow).mockResolvedValue(null as unknown as Awaited<ReturnType<typeof getTeamOrThrow>>);
439429

440430
await expect(handleUserEvents(event, nonExistentOrgId)).rejects.toThrow("Org not found");
441431
});

0 commit comments

Comments
 (0)