Skip to content

Open source alternative to Semrush and Ahrefs

3.4 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import { resolveSignInHostedOrganization } from "@/server/auth/default-hosted-organization";

const mocks = vi.hoisted(() => ({
  getLastActiveOrganizationId: vi.fn(),
  getMembership: vi.fn(),
  findNewestMembershipForUser: vi.fn(),
  findFirstOrganizationIdForUser: vi.fn(),
  getHostedUser: vi.fn(),
  hasPendingInvitationForEmail: vi.fn(),
}));

vi.mock("@/server/auth/repositories/AuthRepository", () => ({
  AuthRepository: mocks,
}));

// Referral pin repair is fire-and-forget KV bookkeeping; mocking it keeps
// `cloudflare:workers` out of this module graph.
vi.mock("@/server/referrals/dub", () => ({
  markDubReferredOrganization: vi.fn(),
}));

describe("resolveSignInHostedOrganization", () => {
  beforeEach(() => {
    mocks.getLastActiveOrganizationId.mockResolvedValue(null);
    mocks.findNewestMembershipForUser.mockResolvedValue(null);
    mocks.getHostedUser.mockResolvedValue({
      id: "user-1",
      email: "[email protected]",
      name: "Invitee",
    });
    mocks.getMembership.mockResolvedValue({ role: "owner" });
  });

  // The invite-signup invariant: a brand-new user with a pending invitation
  // must NOT get a personal organization auto-minted at sign-in — accepting the
  // invite should leave them in exactly the inviter's org.
  it("defers organization creation while an invitation is pending", async () => {
    const createOrganization = vi.fn().mockResolvedValue({ id: "org-new" });

    mocks.hasPendingInvitationForEmail.mockResolvedValue(true);
    await expect(
      resolveSignInHostedOrganization("user-1", createOrganization),
    ).resolves.toBeNull();
    expect(createOrganization).not.toHaveBeenCalled();

    mocks.hasPendingInvitationForEmail.mockResolvedValue(false);
    await expect(
      resolveSignInHostedOrganization("user-1", createOrganization),
    ).resolves.toMatchObject({ organizationId: "org-new" });
  });

  // Billing invariant: each default org is a new billing customer with a
  // free-plan grant, and the unique slug is what stops a second one. A user
  // who left their org and renamed themselves must still collide.
  it("keys the default organization slug on the user, not their name", async () => {
    mocks.hasPendingInvitationForEmail.mockResolvedValue(false);
    const createOrganization = vi.fn(async (_input: { slug: string }) => ({
      id: "org-new",
    }));

    await resolveSignInHostedOrganization("user-1", createOrganization);
    mocks.getHostedUser.mockResolvedValue({
      id: "user-1",
      email: "[email protected]",
      name: "Renamed",
    });
    await resolveSignInHostedOrganization("user-1", createOrganization);

    const [first, second] = createOrganization.mock.calls.map(
      ([input]) => input.slug,
    );
    expect(second).toBe(first);
  });

  // An existing membership always wins — the pending-invite check only
  // applies to membership-less users, so inviting an existing user never
  // hides their current organization.
  it("returns the existing membership without checking invitations", async () => {
    mocks.findNewestMembershipForUser.mockResolvedValue({
      organizationId: "org-existing",
      role: "admin",
    });

    await expect(
      resolveSignInHostedOrganization("user-1", vi.fn()),
    ).resolves.toEqual({ organizationId: "org-existing", role: "admin" });

    expect(mocks.hasPendingInvitationForEmail).not.toHaveBeenCalled();
  });
});