From c7ee77ee20f8350a9311add27f8dd80994a3b9f8 Mon Sep 17 00:00:00 2001 From: Anirban Singha <143536290+SinghaAnirban005@users.noreply.github.com> Date: Fri, 3 Apr 2026 23:47:56 +0530 Subject: [PATCH] test: improve matchUserToMatchingOrg coverage (#28721) Co-authored-by: Romit <85230081+romitg2@users.noreply.github.com> --- apps/web/lib/pages/auth/verify-email.test.ts | 88 ++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/apps/web/lib/pages/auth/verify-email.test.ts b/apps/web/lib/pages/auth/verify-email.test.ts index ad568abde0..113102ff8a 100644 --- a/apps/web/lib/pages/auth/verify-email.test.ts +++ b/apps/web/lib/pages/auth/verify-email.test.ts @@ -56,6 +56,75 @@ describe("moveUserToMatchingOrg", () => { ], }; + it("should always invite as MEMBER role, not an elevated role", async() => { + const org = { + id: "org123", + slug: "test-org", + requestedSlug: null + } + + organizationScenarios.organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization( + org, + { email } + ) + + await moveUserToMatchingOrg({ email }) + + const call = vi.mocked(inviteMembersWithNoInviterPermissionCheck).mock.calls[0][0] + expect(call.invitations[0].role).toBe(MembershipRole.MEMBER) + }) + + it("should pass inviterName as null", async () => { + const org = { + id: "org123", + slug: "test-org", + requestedSlug: null, + }; + organizationScenarios.organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization( + org, + { email } + ); + + await moveUserToMatchingOrg({ email }); + + const call = vi.mocked(inviteMembersWithNoInviterPermissionCheck).mock.calls[0][0]; + expect(call.inviterName).toBeNull(); + }); + + it("should pass creationSource as WEBAPP", async () => { + const org = { + id: "org123", + slug: "test-org", + requestedSlug: null, + }; + organizationScenarios.organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization( + org, + { email } + ); + + await moveUserToMatchingOrg({ email }); + + const call = vi.mocked(inviteMembersWithNoInviterPermissionCheck).mock.calls[0][0]; + expect(call.creationSource).toBe(CreationSource.WEBAPP); + }); + + it("should pass exactly one invitation", async () => { + const org = { + id: "org123", + slug: "test-org", + requestedSlug: null, + }; + organizationScenarios.organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization( + org, + { email } + ); + + await moveUserToMatchingOrg({ email }); + + const call = vi.mocked(inviteMembersWithNoInviterPermissionCheck).mock.calls[0][0]; + expect(call.invitations).toHaveLength(1); + }); + it("when organization has a slug and requestedSlug(slug is used)", async () => { const org = { id: "org123", @@ -98,4 +167,23 @@ describe("moveUserToMatchingOrg", () => { }); }); }); + + describe("error handling", () => { + it("should propagate errors thrown by inviteMembersWithNoInviterPermissionCheck", async () => { + const org = { + id: "org123", + slug: "test-org", + requestedSlug: null, + }; + organizationScenarios.organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization( + org, + { email } + ); + + const error = new Error("Invite failed"); + vi.mocked(inviteMembersWithNoInviterPermissionCheck).mockRejectedValue(error); + + await expect(moveUserToMatchingOrg({ email })).rejects.toThrow("Invite failed"); + }); + }) });