2022-05-14 03:02:10 +00:00
|
|
|
import type { Page, WorkerInfo } from "@playwright/test";
|
2022-03-28 20:06:41 +00:00
|
|
|
import type Prisma from "@prisma/client";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { Prisma as PrismaType } from "@prisma/client";
|
2023-03-01 20:18:51 +00:00
|
|
|
import { hashSync as hash } from "bcryptjs";
|
2023-08-11 18:56:27 +00:00
|
|
|
import type { API } from "mailhog";
|
2022-03-28 20:06:41 +00:00
|
|
|
|
2022-10-05 14:33:29 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-08-21 09:12:39 +00:00
|
|
|
import stripe from "@calcom/features/ee/payments/server/stripe";
|
2022-08-22 23:53:51 +00:00
|
|
|
import { DEFAULT_SCHEDULE, getAvailabilityFromSchedule } from "@calcom/lib/availability";
|
2022-03-28 20:06:41 +00:00
|
|
|
import { prisma } from "@calcom/prisma";
|
2023-07-31 20:27:22 +00:00
|
|
|
import { MembershipRole, SchedulingType } from "@calcom/prisma/enums";
|
2022-03-28 20:06:41 +00:00
|
|
|
|
2023-07-27 11:48:08 +00:00
|
|
|
import { selectFirstAvailableTimeSlotNextMonth, teamEventSlug, teamEventTitle } from "../lib/testUtils";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { TimeZoneEnum } from "./types";
|
2022-03-28 20:06:41 +00:00
|
|
|
|
2022-08-26 18:44:02 +00:00
|
|
|
// Don't import hashPassword from app as that ends up importing next-auth and initializing it before NEXTAUTH_URL can be updated during tests.
|
2023-03-01 20:18:51 +00:00
|
|
|
export function hashPassword(password: string) {
|
|
|
|
const hashedPassword = hash(password, 12);
|
2022-08-26 18:44:02 +00:00
|
|
|
return hashedPassword;
|
|
|
|
}
|
|
|
|
|
2022-04-06 15:13:09 +00:00
|
|
|
type UserFixture = ReturnType<typeof createUserFixture>;
|
2022-03-28 20:06:41 +00:00
|
|
|
|
2022-05-14 03:02:10 +00:00
|
|
|
const userIncludes = PrismaType.validator<PrismaType.UserInclude>()({
|
|
|
|
eventTypes: true,
|
|
|
|
credentials: true,
|
2022-09-02 19:00:41 +00:00
|
|
|
routingForms: true,
|
2022-05-14 03:02:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const userWithEventTypes = PrismaType.validator<PrismaType.UserArgs>()({
|
|
|
|
include: userIncludes,
|
|
|
|
});
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
const seededForm = {
|
|
|
|
id: "948ae412-d995-4865-875a-48302588de03",
|
|
|
|
name: "Seeded Form - Pro",
|
|
|
|
};
|
|
|
|
|
2022-05-14 03:02:10 +00:00
|
|
|
type UserWithIncludes = PrismaType.UserGetPayload<typeof userWithEventTypes>;
|
|
|
|
|
2023-07-31 20:27:22 +00:00
|
|
|
const createTeamEventType = async (
|
|
|
|
user: { id: number },
|
|
|
|
team: { id: number },
|
|
|
|
scenario?: {
|
|
|
|
schedulingType?: SchedulingType;
|
|
|
|
teamEventTitle?: string;
|
|
|
|
teamEventSlug?: string;
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
return await prisma.eventType.create({
|
|
|
|
data: {
|
|
|
|
team: {
|
|
|
|
connect: {
|
|
|
|
id: team.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
users: {
|
|
|
|
connect: {
|
|
|
|
id: user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
owner: {
|
|
|
|
connect: {
|
|
|
|
id: user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
schedulingType: scenario?.schedulingType ?? SchedulingType.COLLECTIVE,
|
|
|
|
title: scenario?.teamEventTitle ?? `${teamEventTitle}-team-id-${team.id}`,
|
|
|
|
slug: scenario?.teamEventSlug ?? `${teamEventSlug}-team-id-${team.id}`,
|
|
|
|
length: 30,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-09 17:51:38 +00:00
|
|
|
const createTeamAndAddUser = async (
|
2023-07-31 20:27:22 +00:00
|
|
|
{
|
|
|
|
user,
|
|
|
|
isUnpublished,
|
|
|
|
isOrg,
|
|
|
|
hasSubteam,
|
|
|
|
}: {
|
|
|
|
user: { id: number; username: string | null; role?: MembershipRole };
|
|
|
|
isUnpublished?: boolean;
|
|
|
|
isOrg?: boolean;
|
|
|
|
hasSubteam?: true;
|
|
|
|
},
|
2023-02-09 17:51:38 +00:00
|
|
|
workerInfo: WorkerInfo
|
|
|
|
) => {
|
2023-07-31 20:27:22 +00:00
|
|
|
const slug = `${isOrg ? "org" : "team"}-${workerInfo.workerIndex}-${Date.now()}`;
|
|
|
|
const data: PrismaType.TeamCreateInput = {
|
|
|
|
name: `user-id-${user.id}'s Team ${isOrg ? "Org" : "Team"}`,
|
|
|
|
};
|
|
|
|
data.metadata = {
|
|
|
|
...(isUnpublished ? { requestedSlug: slug } : {}),
|
|
|
|
...(isOrg ? { isOrganization: true } : {}),
|
|
|
|
};
|
|
|
|
data.slug = !isUnpublished ? slug : undefined;
|
|
|
|
if (isOrg && hasSubteam) {
|
|
|
|
const team = await createTeamAndAddUser({ user }, workerInfo);
|
|
|
|
await createTeamEventType(user, team);
|
|
|
|
data.children = { connect: [{ id: team.id }] };
|
|
|
|
}
|
|
|
|
data.orgUsers = isOrg ? { connect: [{ id: user.id }] } : undefined;
|
2023-01-23 09:58:41 +00:00
|
|
|
const team = await prisma.team.create({
|
2023-07-31 20:27:22 +00:00
|
|
|
data,
|
2023-01-23 09:58:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const { role = MembershipRole.OWNER, id: userId } = user;
|
|
|
|
await prisma.membership.create({
|
|
|
|
data: {
|
|
|
|
teamId: team.id,
|
|
|
|
userId,
|
|
|
|
role: role,
|
2023-03-07 17:40:47 +00:00
|
|
|
accepted: true,
|
2023-01-23 09:58:41 +00:00
|
|
|
},
|
|
|
|
});
|
2023-03-07 17:40:47 +00:00
|
|
|
return team;
|
2023-01-23 09:58:41 +00:00
|
|
|
};
|
|
|
|
|
2022-03-28 20:06:41 +00:00
|
|
|
// creates a user fixture instance and stores the collection
|
2023-08-11 18:56:27 +00:00
|
|
|
export const createUsersFixture = (page: Page, emails: API, workerInfo: WorkerInfo) => {
|
2022-05-18 16:05:25 +00:00
|
|
|
const store = { users: [], page } as { users: UserFixture[]; page: typeof page };
|
2022-03-28 20:06:41 +00:00
|
|
|
return {
|
2022-09-02 19:00:41 +00:00
|
|
|
create: async (
|
|
|
|
opts?: CustomUserOpts | null,
|
|
|
|
scenario: {
|
|
|
|
seedRoutingForms?: boolean;
|
2023-01-23 09:58:41 +00:00
|
|
|
hasTeam?: true;
|
2023-07-27 11:48:08 +00:00
|
|
|
teammates?: CustomUserOpts[];
|
|
|
|
schedulingType?: SchedulingType;
|
|
|
|
teamEventTitle?: string;
|
|
|
|
teamEventSlug?: string;
|
2023-07-31 20:27:22 +00:00
|
|
|
isOrg?: boolean;
|
|
|
|
hasSubteam?: true;
|
|
|
|
isUnpublished?: true;
|
2022-09-02 19:00:41 +00:00
|
|
|
} = {}
|
|
|
|
) => {
|
2022-05-14 03:02:10 +00:00
|
|
|
const _user = await prisma.user.create({
|
2023-03-14 04:19:05 +00:00
|
|
|
data: createUser(workerInfo, opts),
|
2022-08-05 17:08:47 +00:00
|
|
|
});
|
2023-03-14 04:19:05 +00:00
|
|
|
|
|
|
|
let defaultEventTypes: SupportedTestEventTypes[] = [
|
|
|
|
{ title: "30 min", slug: "30-min", length: 30 },
|
|
|
|
{ title: "Paid", slug: "paid", length: 30, price: 1000 },
|
|
|
|
{ title: "Opt in", slug: "opt-in", requiresConfirmation: true, length: 30 },
|
2023-06-10 03:47:50 +00:00
|
|
|
{ title: "Seated", slug: "seated", seatsPerTimeSlot: 2, length: 30 },
|
2023-03-14 04:19:05 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (opts?.eventTypes) defaultEventTypes = defaultEventTypes.concat(opts.eventTypes);
|
|
|
|
for (const eventTypeData of defaultEventTypes) {
|
|
|
|
eventTypeData.owner = { connect: { id: _user.id } };
|
|
|
|
eventTypeData.users = { connect: { id: _user.id } };
|
|
|
|
await prisma.eventType.create({
|
|
|
|
data: eventTypeData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
if (scenario.seedRoutingForms) {
|
|
|
|
await prisma.app_RoutingForms_Form.create({
|
|
|
|
data: {
|
|
|
|
routes: [
|
|
|
|
{
|
|
|
|
id: "8a898988-89ab-4cde-b012-31823f708642",
|
|
|
|
action: { type: "eventTypeRedirectUrl", value: "pro/30min" },
|
|
|
|
queryValue: {
|
|
|
|
id: "8a898988-89ab-4cde-b012-31823f708642",
|
|
|
|
type: "group",
|
|
|
|
children1: {
|
|
|
|
"8988bbb8-0123-4456-b89a-b1823f70c5ff": {
|
|
|
|
type: "rule",
|
|
|
|
properties: {
|
|
|
|
field: "c4296635-9f12-47b1-8153-c3a854649182",
|
|
|
|
value: ["event-routing"],
|
|
|
|
operator: "equal",
|
|
|
|
valueSrc: ["value"],
|
|
|
|
valueType: ["text"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "aa8aaba9-cdef-4012-b456-71823f70f7ef",
|
|
|
|
action: { type: "customPageMessage", value: "Custom Page Result" },
|
|
|
|
queryValue: {
|
|
|
|
id: "aa8aaba9-cdef-4012-b456-71823f70f7ef",
|
|
|
|
type: "group",
|
|
|
|
children1: {
|
|
|
|
"b99b8a89-89ab-4cde-b012-31823f718ff5": {
|
|
|
|
type: "rule",
|
|
|
|
properties: {
|
|
|
|
field: "c4296635-9f12-47b1-8153-c3a854649182",
|
|
|
|
value: ["custom-page"],
|
|
|
|
operator: "equal",
|
|
|
|
valueSrc: ["value"],
|
|
|
|
valueType: ["text"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "a8ba9aab-4567-489a-bcde-f1823f71b4ad",
|
|
|
|
action: { type: "externalRedirectUrl", value: "https://google.com" },
|
|
|
|
queryValue: {
|
|
|
|
id: "a8ba9aab-4567-489a-bcde-f1823f71b4ad",
|
|
|
|
type: "group",
|
|
|
|
children1: {
|
|
|
|
"998b9b9a-0123-4456-b89a-b1823f7232b9": {
|
|
|
|
type: "rule",
|
|
|
|
properties: {
|
|
|
|
field: "c4296635-9f12-47b1-8153-c3a854649182",
|
|
|
|
value: ["external-redirect"],
|
|
|
|
operator: "equal",
|
|
|
|
valueSrc: ["value"],
|
|
|
|
valueType: ["text"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "aa8ba8b9-0123-4456-b89a-b182623406d8",
|
|
|
|
action: { type: "customPageMessage", value: "Multiselect chosen" },
|
|
|
|
queryValue: {
|
|
|
|
id: "aa8ba8b9-0123-4456-b89a-b182623406d8",
|
|
|
|
type: "group",
|
|
|
|
children1: {
|
|
|
|
"b98a8abb-cdef-4012-b456-718262343d27": {
|
|
|
|
type: "rule",
|
|
|
|
properties: {
|
|
|
|
field: "d4292635-9f12-17b1-9153-c3a854649182",
|
|
|
|
value: [["Option-2"]],
|
|
|
|
operator: "multiselect_equals",
|
|
|
|
valueSrc: ["value"],
|
|
|
|
valueType: ["multiselect"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "898899aa-4567-489a-bcde-f1823f708646",
|
|
|
|
action: { type: "customPageMessage", value: "Fallback Message" },
|
|
|
|
isFallback: true,
|
|
|
|
queryValue: { id: "898899aa-4567-489a-bcde-f1823f708646", type: "group" },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
id: "c4296635-9f12-47b1-8153-c3a854649182",
|
|
|
|
type: "text",
|
|
|
|
label: "Test field",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "d4292635-9f12-17b1-9153-c3a854649182",
|
|
|
|
type: "multiselect",
|
|
|
|
label: "Multi Select",
|
|
|
|
identifier: "multi",
|
|
|
|
selectText: "Option-1\nOption-2",
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
user: {
|
|
|
|
connect: {
|
|
|
|
id: _user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: seededForm.name,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-08-31 19:44:47 +00:00
|
|
|
const user = await prisma.user.findUniqueOrThrow({
|
2022-05-14 03:02:10 +00:00
|
|
|
where: { id: _user.id },
|
|
|
|
include: userIncludes,
|
2022-03-28 20:06:41 +00:00
|
|
|
});
|
2023-01-23 09:58:41 +00:00
|
|
|
if (scenario.hasTeam) {
|
2023-07-31 20:27:22 +00:00
|
|
|
const team = await createTeamAndAddUser(
|
|
|
|
{
|
|
|
|
user: { id: user.id, username: user.username, role: "OWNER" },
|
|
|
|
isUnpublished: scenario.isUnpublished,
|
|
|
|
isOrg: scenario.isOrg,
|
|
|
|
hasSubteam: scenario.hasSubteam,
|
2023-03-07 17:40:47 +00:00
|
|
|
},
|
2023-07-31 20:27:22 +00:00
|
|
|
workerInfo
|
|
|
|
);
|
|
|
|
const teamEvent = await createTeamEventType(user, team, scenario);
|
2023-07-27 11:48:08 +00:00
|
|
|
if (scenario.teammates) {
|
|
|
|
// Create Teammate users
|
|
|
|
for (const teammateObj of scenario.teammates) {
|
|
|
|
const teamUser = await prisma.user.create({
|
|
|
|
data: createUser(workerInfo, teammateObj),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add teammates to the team
|
|
|
|
await prisma.membership.create({
|
|
|
|
data: {
|
|
|
|
teamId: team.id,
|
|
|
|
userId: teamUser.id,
|
|
|
|
role: MembershipRole.MEMBER,
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add teammate to the host list of team event
|
|
|
|
await prisma.host.create({
|
|
|
|
data: {
|
|
|
|
userId: teamUser.id,
|
|
|
|
eventTypeId: teamEvent.id,
|
|
|
|
isFixed: scenario.schedulingType === SchedulingType.COLLECTIVE ? true : false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const teammateFixture = createUserFixture(
|
|
|
|
await prisma.user.findUniqueOrThrow({
|
|
|
|
where: { id: teamUser.id },
|
|
|
|
include: userIncludes,
|
|
|
|
}),
|
|
|
|
store.page
|
|
|
|
);
|
|
|
|
store.users.push(teammateFixture);
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 09:58:41 +00:00
|
|
|
}
|
2023-06-06 11:59:57 +00:00
|
|
|
const userFixture = createUserFixture(user, store.page);
|
2022-03-28 20:06:41 +00:00
|
|
|
store.users.push(userFixture);
|
|
|
|
return userFixture;
|
|
|
|
},
|
|
|
|
get: () => store.users,
|
|
|
|
logout: async () => {
|
2022-09-02 02:00:48 +00:00
|
|
|
await page.goto("/auth/logout");
|
2022-03-28 20:06:41 +00:00
|
|
|
},
|
2022-05-14 03:02:10 +00:00
|
|
|
deleteAll: async () => {
|
2023-08-11 18:56:27 +00:00
|
|
|
const emailMessageIds: string[] = [];
|
2022-05-14 03:02:10 +00:00
|
|
|
const ids = store.users.map((u) => u.id);
|
2023-08-11 18:56:27 +00:00
|
|
|
for (const user of store.users) {
|
|
|
|
const emailMessages = await emails.search(user.email);
|
|
|
|
if (emailMessages && emailMessages.count > 0) {
|
|
|
|
emailMessages.items.forEach((item) => {
|
|
|
|
emailMessageIds.push(item.ID);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const id of emailMessageIds) {
|
|
|
|
await emails.deleteMessage(id);
|
|
|
|
}
|
2022-05-14 03:02:10 +00:00
|
|
|
await prisma.user.deleteMany({ where: { id: { in: ids } } });
|
|
|
|
store.users = [];
|
|
|
|
},
|
|
|
|
delete: async (id: number) => {
|
|
|
|
await prisma.user.delete({ where: { id } });
|
|
|
|
store.users = store.users.filter((b) => b.id !== id);
|
|
|
|
},
|
2022-03-28 20:06:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-06 15:13:09 +00:00
|
|
|
type JSONValue = string | number | boolean | { [x: string]: JSONValue } | Array<JSONValue>;
|
|
|
|
|
2022-03-28 20:06:41 +00:00
|
|
|
// creates the single user fixture
|
2022-05-14 03:02:10 +00:00
|
|
|
const createUserFixture = (user: UserWithIncludes, page: Page) => {
|
2022-03-28 20:06:41 +00:00
|
|
|
const store = { user, page };
|
|
|
|
|
|
|
|
// self is a reflective method that return the Prisma object that references this fixture.
|
2022-05-14 03:02:10 +00:00
|
|
|
const self = async () =>
|
2023-06-06 11:59:57 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2023-06-06 18:02:32 +00:00
|
|
|
(await prisma.user.findUnique({
|
|
|
|
where: { id: store.user.id },
|
|
|
|
include: { eventTypes: true },
|
|
|
|
}))!;
|
2022-03-28 20:06:41 +00:00
|
|
|
return {
|
|
|
|
id: user.id,
|
2023-07-27 11:48:08 +00:00
|
|
|
name: user.name,
|
2022-05-14 03:02:10 +00:00
|
|
|
username: user.username,
|
2023-08-02 06:40:33 +00:00
|
|
|
email: user.email,
|
2022-09-02 19:00:41 +00:00
|
|
|
eventTypes: user.eventTypes,
|
|
|
|
routingForms: user.routingForms,
|
2022-03-28 20:06:41 +00:00
|
|
|
self,
|
2023-06-06 18:02:32 +00:00
|
|
|
apiLogin: async () => apiLogin({ ...(await self()), password: user.username }, store.page),
|
2022-04-06 15:13:09 +00:00
|
|
|
login: async () => login({ ...(await self()), password: user.username }, store.page),
|
2023-04-13 02:10:23 +00:00
|
|
|
logout: async () => {
|
|
|
|
await page.goto("/auth/logout");
|
|
|
|
},
|
2023-07-27 11:48:08 +00:00
|
|
|
getTeam: async () => {
|
|
|
|
return prisma.membership.findFirstOrThrow({
|
|
|
|
where: { userId: user.id },
|
|
|
|
include: { team: true },
|
|
|
|
});
|
|
|
|
},
|
2023-07-31 20:27:22 +00:00
|
|
|
getOrg: async () => {
|
|
|
|
return prisma.membership.findFirstOrThrow({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
team: {
|
|
|
|
metadata: {
|
|
|
|
path: ["isOrganization"],
|
|
|
|
equals: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: { team: { select: { children: true, metadata: true, name: true } } },
|
|
|
|
});
|
|
|
|
},
|
2023-08-11 18:56:27 +00:00
|
|
|
getFirstEventAsOwner: async () =>
|
|
|
|
prisma.eventType.findFirstOrThrow({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
},
|
|
|
|
}),
|
2023-07-28 12:51:15 +00:00
|
|
|
getFirstTeamEvent: async (teamId: number) => {
|
|
|
|
return prisma.eventType.findFirstOrThrow({
|
|
|
|
where: {
|
|
|
|
teamId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2022-05-14 03:02:10 +00:00
|
|
|
getPaymentCredential: async () => getPaymentCredential(store.page),
|
2023-07-01 17:18:27 +00:00
|
|
|
setupEventWithPrice: async (eventType: Pick<Prisma.EventType, "id">) =>
|
|
|
|
setupEventWithPrice(eventType, store.page),
|
2023-08-21 09:12:39 +00:00
|
|
|
bookAndPayEvent: async (eventType: Pick<Prisma.EventType, "slug">) =>
|
|
|
|
bookAndPayEvent(user, eventType, store.page),
|
2023-07-13 00:47:06 +00:00
|
|
|
makePaymentUsingStripe: async () => makePaymentUsingStripe(store.page),
|
2022-03-28 20:06:41 +00:00
|
|
|
// ths is for developemnt only aimed to inject debugging messages in the metadata field of the user
|
2022-04-06 15:13:09 +00:00
|
|
|
debug: async (message: string | Record<string, JSONValue>) => {
|
2023-06-06 18:02:32 +00:00
|
|
|
await prisma.user.update({
|
|
|
|
where: { id: store.user.id },
|
|
|
|
data: { metadata: { debug: message } },
|
|
|
|
});
|
2022-03-28 20:06:41 +00:00
|
|
|
},
|
2023-06-06 11:59:57 +00:00
|
|
|
delete: async () => await prisma.user.delete({ where: { id: store.user.id } }),
|
2023-08-21 09:12:39 +00:00
|
|
|
confirmPendingPayment: async () => confirmPendingPayment(store.page),
|
2022-03-28 20:06:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-03-14 04:19:05 +00:00
|
|
|
type SupportedTestEventTypes = PrismaType.EventTypeCreateInput & {
|
|
|
|
_bookings?: PrismaType.BookingCreateInput[];
|
|
|
|
};
|
2022-12-08 23:20:24 +00:00
|
|
|
type CustomUserOptsKeys = "username" | "password" | "completedOnboarding" | "locale" | "name";
|
2023-03-14 04:19:05 +00:00
|
|
|
type CustomUserOpts = Partial<Pick<Prisma.User, CustomUserOptsKeys>> & {
|
|
|
|
timeZone?: TimeZoneEnum;
|
|
|
|
eventTypes?: SupportedTestEventTypes[];
|
2023-08-22 11:03:00 +00:00
|
|
|
// ignores adding the worker-index after username
|
|
|
|
useExactUsername?: boolean;
|
2023-03-14 04:19:05 +00:00
|
|
|
};
|
2022-03-28 20:06:41 +00:00
|
|
|
|
|
|
|
// creates the actual user in the db.
|
2023-03-01 20:18:51 +00:00
|
|
|
const createUser = (workerInfo: WorkerInfo, opts?: CustomUserOpts | null): PrismaType.UserCreateInput => {
|
2022-03-28 20:06:41 +00:00
|
|
|
// build a unique name for our user
|
2023-08-22 11:03:00 +00:00
|
|
|
const uname =
|
|
|
|
opts?.useExactUsername && opts?.username
|
|
|
|
? opts.username
|
|
|
|
: `${opts?.username || "user"}-${workerInfo.workerIndex}-${Date.now()}`;
|
|
|
|
|
2022-03-28 20:06:41 +00:00
|
|
|
return {
|
|
|
|
username: uname,
|
2022-12-08 23:20:24 +00:00
|
|
|
name: opts?.name,
|
2022-03-28 20:06:41 +00:00
|
|
|
email: `${uname}@example.com`,
|
2023-03-01 20:18:51 +00:00
|
|
|
password: hashPassword(uname),
|
2022-03-28 20:06:41 +00:00
|
|
|
emailVerified: new Date(),
|
|
|
|
completedOnboarding: opts?.completedOnboarding ?? true,
|
2022-10-05 14:33:29 +00:00
|
|
|
timeZone: opts?.timeZone ?? dayjs.tz.guess(),
|
2022-03-28 20:06:41 +00:00
|
|
|
locale: opts?.locale ?? "en",
|
2022-08-22 23:53:51 +00:00
|
|
|
schedules:
|
|
|
|
opts?.completedOnboarding ?? true
|
|
|
|
? {
|
|
|
|
create: {
|
|
|
|
name: "Working Hours",
|
|
|
|
availability: {
|
|
|
|
createMany: {
|
|
|
|
data: getAvailabilityFromSchedule(DEFAULT_SCHEDULE),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: undefined,
|
2022-03-28 20:06:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-08-21 09:12:39 +00:00
|
|
|
async function confirmPendingPayment(page: Page) {
|
|
|
|
await page.waitForURL(new RegExp("/booking/*"));
|
|
|
|
|
|
|
|
const url = page.url();
|
|
|
|
|
|
|
|
const params = new URLSearchParams(url.split("?")[1]);
|
|
|
|
|
|
|
|
const id = params.get("payment_intent");
|
|
|
|
|
|
|
|
if (!id) throw new Error(`Payment intent not found in url ${url}`);
|
|
|
|
|
|
|
|
const payload = JSON.stringify(
|
|
|
|
{ type: "payment_intent.succeeded", data: { object: { id } }, account: "e2e_test" },
|
|
|
|
null,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
|
|
|
const signature = stripe.webhooks.generateTestHeaderString({
|
|
|
|
payload,
|
|
|
|
secret: process.env.STRIPE_WEBHOOK_SECRET as string,
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = await page.request.post("/api/integrations/stripepayment/webhook", {
|
|
|
|
data: payload,
|
|
|
|
headers: { "stripe-signature": signature },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.status() !== 200) throw new Error(`Failed to confirm payment. Response: ${response.text()}`);
|
|
|
|
}
|
|
|
|
|
2022-03-28 20:06:41 +00:00
|
|
|
// login using a replay of an E2E routine.
|
2022-04-06 15:13:09 +00:00
|
|
|
export async function login(
|
|
|
|
user: Pick<Prisma.User, "username"> & Partial<Pick<Prisma.User, "password" | "email">>,
|
|
|
|
page: Page
|
|
|
|
) {
|
|
|
|
// get locators
|
2022-05-14 03:02:10 +00:00
|
|
|
const loginLocator = page.locator("[data-testid=login-form]");
|
|
|
|
const emailLocator = loginLocator.locator("#email");
|
|
|
|
const passwordLocator = loginLocator.locator("#password");
|
|
|
|
const signInLocator = loginLocator.locator('[type="submit"]');
|
2022-04-06 15:13:09 +00:00
|
|
|
|
|
|
|
//login
|
2022-09-02 02:00:48 +00:00
|
|
|
await page.goto("/");
|
2022-04-06 15:13:09 +00:00
|
|
|
await emailLocator.fill(user.email ?? `${user.username}@example.com`);
|
2022-08-26 18:44:02 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2022-04-06 15:13:09 +00:00
|
|
|
await passwordLocator.fill(user.password ?? user.username!);
|
|
|
|
await signInLocator.click();
|
2022-03-28 20:06:41 +00:00
|
|
|
|
2023-04-13 02:10:23 +00:00
|
|
|
// Moving away from waiting 2 seconds, as it is not a reliable way to expect session to be started
|
|
|
|
await page.waitForLoadState("networkidle");
|
2022-03-28 20:06:41 +00:00
|
|
|
}
|
2022-05-14 03:02:10 +00:00
|
|
|
|
2023-06-06 18:02:32 +00:00
|
|
|
export async function apiLogin(
|
|
|
|
user: Pick<Prisma.User, "username"> & Partial<Pick<Prisma.User, "password" | "email">>,
|
|
|
|
page: Page
|
|
|
|
) {
|
|
|
|
const csrfToken = await page
|
|
|
|
.context()
|
|
|
|
.request.get("/api/auth/csrf")
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((json) => json.csrfToken);
|
|
|
|
const data = {
|
|
|
|
email: user.email ?? `${user.username}@example.com`,
|
|
|
|
password: user.password ?? user.username!,
|
|
|
|
callbackURL: "http://localhost:3000/",
|
|
|
|
redirect: "false",
|
|
|
|
json: "true",
|
|
|
|
csrfToken,
|
|
|
|
};
|
|
|
|
return page.context().request.post("/api/auth/callback/credentials", {
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-01 17:18:27 +00:00
|
|
|
export async function setupEventWithPrice(eventType: Pick<Prisma.EventType, "id">, page: Page) {
|
|
|
|
await page.goto(`/event-types/${eventType?.id}?tabName=apps`);
|
|
|
|
await page.locator("div > .ml-auto").first().click();
|
|
|
|
await page.getByPlaceholder("Price").fill("100");
|
|
|
|
await page.getByTestId("update-eventtype").click();
|
|
|
|
}
|
|
|
|
|
2023-08-21 09:12:39 +00:00
|
|
|
export async function bookAndPayEvent(
|
2023-07-01 17:18:27 +00:00
|
|
|
user: Pick<Prisma.User, "username">,
|
|
|
|
eventType: Pick<Prisma.EventType, "slug">,
|
|
|
|
page: Page
|
|
|
|
) {
|
|
|
|
// booking process with stripe integration
|
|
|
|
await page.goto(`${user.username}/${eventType?.slug}`);
|
|
|
|
await selectFirstAvailableTimeSlotNextMonth(page);
|
|
|
|
// --- fill form
|
|
|
|
await page.fill('[name="name"]', "Stripe Stripeson");
|
|
|
|
await page.fill('[name="email"]', "test@example.com");
|
|
|
|
|
|
|
|
await Promise.all([page.waitForURL("/payment/*"), page.press('[name="email"]', "Enter")]);
|
|
|
|
|
2023-07-13 00:47:06 +00:00
|
|
|
await makePaymentUsingStripe(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function makePaymentUsingStripe(page: Page) {
|
|
|
|
const stripeElement = await page.locator(".StripeElement").first();
|
|
|
|
const stripeFrame = stripeElement.frameLocator("iframe").first();
|
2023-07-01 17:18:27 +00:00
|
|
|
await stripeFrame.locator('[name="number"]').fill("4242 4242 4242 4242");
|
|
|
|
const now = new Date();
|
|
|
|
await stripeFrame.locator('[name="expiry"]').fill(`${now.getMonth()} / ${now.getFullYear() + 1}`);
|
|
|
|
await stripeFrame.locator('[name="cvc"]').fill("111");
|
|
|
|
const postcalCodeIsVisible = await stripeFrame.locator('[name="postalCode"]').isVisible();
|
|
|
|
if (postcalCodeIsVisible) {
|
|
|
|
await stripeFrame.locator('[name="postalCode"]').fill("111111");
|
|
|
|
}
|
|
|
|
await page.click('button:has-text("Pay now")');
|
|
|
|
}
|
|
|
|
|
2022-05-14 03:02:10 +00:00
|
|
|
export async function getPaymentCredential(page: Page) {
|
2022-06-01 17:24:41 +00:00
|
|
|
await page.goto("/apps/stripe");
|
2022-05-14 03:02:10 +00:00
|
|
|
|
|
|
|
/** We start the Stripe flow */
|
|
|
|
await Promise.all([
|
2023-05-02 16:58:39 +00:00
|
|
|
page.waitForURL("https://connect.stripe.com/oauth/v2/authorize?*"),
|
2022-06-01 17:24:41 +00:00
|
|
|
page.click('[data-testid="install-app-button"]'),
|
2022-05-14 03:02:10 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
await Promise.all([
|
2023-05-02 16:58:39 +00:00
|
|
|
page.waitForURL("/apps/installed/payment?hl=stripe"),
|
2022-05-14 03:02:10 +00:00
|
|
|
/** We skip filling Stripe forms (testing mode only) */
|
|
|
|
page.click('[id="skip-account-app"]'),
|
|
|
|
]);
|
|
|
|
}
|