Skip to content
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

fix: team and org invite e2e tests #12566

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions apps/web/playwright/fixtures/clipboard.ts

This file was deleted.

6 changes: 0 additions & 6 deletions apps/web/playwright/lib/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import prisma from "@calcom/prisma";

import type { ExpectedUrlDetails } from "../../../../playwright.config";
import { createBookingsFixture } from "../fixtures/bookings";
import { createClipboardFixture } from "../fixtures/clipboard";
import { createEmbedsFixture } from "../fixtures/embeds";
import { createOrgsFixture } from "../fixtures/orgs";
import { createPaymentsFixture } from "../fixtures/payments";
Expand All @@ -30,7 +29,6 @@ export interface Fixtures {
emails?: API;
routingForms: ReturnType<typeof createRoutingFormsFixture>;
bookingPage: ReturnType<typeof createBookingPageFixture>;
clipboard: ReturnType<typeof createClipboardFixture>;
}

declare global {
Expand Down Expand Up @@ -97,8 +95,4 @@ export const test = base.extend<Fixtures>({
const bookingPage = createBookingPageFixture(page);
await use(bookingPage);
},
clipboard: async ({ page }, use) => {
const clipboard = createClipboardFixture(page);
await use(clipboard);
},
});
6 changes: 6 additions & 0 deletions apps/web/playwright/lib/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ export async function installAppleCalendar(page: Page) {
await page.click('[data-testid="install-app-button"]');
}

export async function getInviteLink(page: Page) {
const response = await page.waitForResponse("**/api/trpc/teams/createInvite?batch=1");
const json = await response.json();
return json[0].result.data.json.inviteLink as string;
}

export async function getEmailsReceivedByUser({
emails,
userEmail,
Expand Down
13 changes: 6 additions & 7 deletions apps/web/playwright/organization/organization-invitation.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { expect } from "@playwright/test";

import { test } from "../lib/fixtures";
import { getInviteLink } from "../lib/testUtils";
import { expectInvitationEmailToBeReceived } from "./expects";

test.describe.configure({ mode: "parallel" });

test.afterEach(async ({ users, emails, clipboard }) => {
clipboard.reset();
test.afterEach(async ({ users, emails }) => {
await users.deleteAll();
emails?.deleteAll();
});

test.describe("Organization", () => {
test("Invitation (non verified)", async ({ browser, page, users, emails, clipboard }) => {
test("Invitation (non verified)", async ({ browser, page, users, emails }) => {
const orgOwner = await users.create(undefined, { hasTeam: true, isOrg: true });
const { team: org } = await orgOwner.getOrgMembership();
await orgOwner.apiLogin();
Expand Down Expand Up @@ -69,9 +69,8 @@ test.describe("Organization", () => {
// Get the invite link
await page.locator('button:text("Add")').click();
await page.locator(`[data-testid="copy-invite-link-button"]`).click();
const inviteLink = await clipboard.get();
await page.waitForLoadState("networkidle");

const inviteLink = await getInviteLink(page);
// Follow invite link in new window
const context = await browser.newContext();
const inviteLinkPage = await context.newPage();
Expand All @@ -98,7 +97,7 @@ test.describe("Organization", () => {
await page.waitForLoadState("networkidle");

await test.step("To the organization by email (internal user)", async () => {
const invitedUserEmail = `[email protected]`;
const invitedUserEmail = `rick-${Date.now()}@example.com`;
await page.locator('button:text("Add")').click();
await page.locator('input[name="inviteUser"]').fill(invitedUserEmail);
await page.locator('button:text("Send invite")').click();
Expand All @@ -110,7 +109,7 @@ test.describe("Organization", () => {
`${org.name}'s admin invited you to join the organization ${org.name} on Cal.com`
);

// Check newly invited member exists and is pending
// Check newly invited member exists and is not pending
await expect(
page.locator(`[data-testid="email-${invitedUserEmail.replace("@", "")}-pending"]`)
).toHaveCount(0);
Expand Down
10 changes: 4 additions & 6 deletions apps/web/playwright/team/team-invitation.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ import { expect } from "@playwright/test";
import { WEBAPP_URL } from "@calcom/lib/constants";

import { test } from "../lib/fixtures";
import { localize } from "../lib/testUtils";
import { localize, getInviteLink } from "../lib/testUtils";
import { expectInvitationEmailToBeReceived } from "./expects";

test.describe.configure({ mode: "parallel" });

test.afterEach(async ({ users, emails, clipboard }) => {
clipboard.reset();
test.afterEach(async ({ users, emails }) => {
await users.deleteAll();
emails?.deleteAll();
});

test.describe("Team", () => {
test("Invitation (non verified)", async ({ browser, page, users, emails, clipboard }) => {
test("Invitation (non verified)", async ({ browser, page, users, emails }) => {
const t = await localize("en");
const teamOwner = await users.create(undefined, { hasTeam: true });
const { team } = await teamOwner.getFirstTeam();
Expand Down Expand Up @@ -76,8 +75,7 @@ test.describe("Team", () => {
});
await page.locator(`button:text("${t("add")}")`).click();
await page.locator(`[data-testid="copy-invite-link-button"]`).click();
const inviteLink = await clipboard.get();
await page.waitForLoadState("networkidle");
const inviteLink = await getInviteLink(page);

const context = await browser.newContext();
const inviteLinkPage = await context.newPage();
Expand Down
26 changes: 7 additions & 19 deletions packages/features/ee/teams/components/MemberInvitationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Controller, useForm } from "react-hook-form";

import TeamInviteFromOrg from "@calcom/ee/organizations/components/TeamInviteFromOrg";
import { classNames } from "@calcom/lib";
import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { MembershipRole } from "@calcom/prisma/enums";
import type { RouterOutputs } from "@calcom/trpc";
Expand All @@ -25,7 +25,6 @@ import {
TextAreaField,
} from "@calcom/ui";
import { Link } from "@calcom/ui/components/icon";
import type { Window as WindowWithClipboardValue } from "@calcom/web/playwright/fixtures/clipboard";

import type { PendingMember } from "../lib/types";
import { GoogleWorkspaceInviteButton } from "./GoogleWorkspaceInviteButton";
Expand Down Expand Up @@ -76,8 +75,8 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)
);

const createInviteMutation = trpc.viewer.teams.createInvite.useMutation({
onSuccess(token) {
copyInviteLinkToClipboard(token);
async onSuccess({ inviteLink }) {
await copyInviteLinkToClipboard(inviteLink);
trpcContext.viewer.teams.get.invalidate();
trpcContext.viewer.teams.list.invalidate();
},
Expand All @@ -86,20 +85,11 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)
},
});

const copyInviteLinkToClipboard = async (token: string) => {
const isOrgInvite = isOrg;
const teamInviteLink = `${WEBAPP_URL}/teams?token=${token}`;
const orgInviteLink = `${WEBAPP_URL}/signup?token=${token}&callbackUrl=/getting-started`;

const inviteLink =
isOrgInvite || (props?.orgMembers && props.orgMembers?.length > 0) ? orgInviteLink : teamInviteLink;
const copyInviteLinkToClipboard = async (inviteLink: string) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We send the fully formed invite link from the server

try {
await navigator.clipboard.writeText(inviteLink);
showToast(t("invite_link_copied"), "success");
} catch (e) {
if (process.env.NEXT_PUBLIC_IS_E2E) {
(window as WindowWithClipboardValue).E2E_CLIPBOARD_VALUE = inviteLink;
}
console.error(e);
}
};
Expand Down Expand Up @@ -373,11 +363,9 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)
type="button"
color="minimal"
variant="icon"
onClick={() =>
props.token
? copyInviteLinkToClipboard(props.token)
: createInviteMutation.mutate({ teamId: props.teamId })
}
onClick={() => {
createInviteMutation.mutate({ teamId: props.teamId, token: props.token });
}}
className={classNames("gap-2", props.token && "opacity-50")}
data-testid="copy-invite-link-button">
<Link className="text-default h-4 w-4" aria-hidden="true" />
Expand Down
21 changes: 11 additions & 10 deletions packages/lib/server/queries/teams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,17 @@ export async function getTeamWithMembers(args: {

// also returns team
export async function isTeamAdmin(userId: number, teamId: number) {
return (
(await prisma.membership.findFirst({
where: {
userId,
teamId,
accepted: true,
OR: [{ role: "ADMIN" }, { role: "OWNER" }],
},
})) || false
);
const team = await prisma.membership.findFirst({
where: {
userId,
teamId,
accepted: true,
OR: [{ role: "ADMIN" }, { role: "OWNER" }],
},
include: { team: true },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we don't double query and can reuse the response

});
if (!team) return false;
return team;
}

export async function isTeamOwner(userId: number, teamId: number) {
Expand Down
33 changes: 31 additions & 2 deletions packages/trpc/server/routers/viewer/teams/createInvite.handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { randomBytes } from "crypto";

import { WEBAPP_URL } from "@calcom/lib/constants";
import { isTeamAdmin } from "@calcom/lib/server/queries/teams";
import { prisma } from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { TRPCError } from "@calcom/trpc/server";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";

import { getMembersHandler } from "../organizations/getMembers.handler";
import type { TCreateInviteInputSchema } from "./createInvite.schema";

type CreateInviteOptions = {
Expand All @@ -16,8 +19,26 @@ type CreateInviteOptions = {

export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) => {
const { teamId } = input;
const membership = await isTeamAdmin(ctx.user.id, teamId);

if (!(await isTeamAdmin(ctx.user.id, teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
if (!membership || !membership?.team) throw new TRPCError({ code: "UNAUTHORIZED" });
const teamMetadata = teamMetadataSchema.parse(membership.team.metadata);
const isOrg = !!(membership.team?.parentId === null && teamMetadata?.isOrganization);
const orgMembers = await getMembersHandler({
ctx,
input: { teamIdToExclude: teamId, distinctUser: true },
});

if (input.token) {
const existingToken = await prisma.verificationToken.findFirst({
where: { token: input.token, identifier: `invite-link-for-teamId-${teamId}`, teamId },
});
if (!existingToken) throw new TRPCError({ code: "NOT_FOUND" });
return {
token: existingToken.token,
inviteLink: await getInviteLink(existingToken.token, isOrg, orgMembers?.length),
};
}

const token = randomBytes(32).toString("hex");
await prisma.verificationToken.create({
Expand All @@ -28,5 +49,13 @@ export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) =
teamId,
},
});
return token;

return { token, inviteLink: await getInviteLink(token, isOrg, orgMembers?.length) };
};

async function getInviteLink(token = "", isOrg = false, orgMembers = 0) {
const teamInviteLink = `${WEBAPP_URL}/teams?token=${token}`;
const orgInviteLink = `${WEBAPP_URL}/signup?token=${token}&callbackUrl=/getting-started`;
if (isOrg || orgMembers > 0) return orgInviteLink;
return teamInviteLink;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from "zod";

export const ZCreateInviteInputSchema = z.object({
teamId: z.number(),
token: z.string().optional(),
});

export type TCreateInviteInputSchema = z.infer<typeof ZCreateInviteInputSchema>;
Loading