fix: Prevent invalid host assignment (#9441)

* Avoids invalid hosts

* Fixed test selector, instead using react-select-2-listbox

* Different way of selecting the managed user

* Convert select-option- to value instead of label

* Replaced all instances of networkidle

* done rewriting test

* Fix prisma is not defined error

* Depend on inner text rather than testid

* Fill of team create is unstable

* Removed console.log

---------

Co-authored-by: Efraín Rochín <[email protected]>
This commit is contained in:
Alex van AndelandEfraín Rochín authored and GitHub committed 2023-06-10 02:39:49 +00:00
1 parent d9a7952467
commit bb31c4ba14
4 files changed
+85 -71

No files matched your search

@@ -463,7 +463,7 @@ async function addAllTypesOfFieldsAndSaveForm(
// Click on the field type dropdown.
await page.locator(".data-testid-field-type").nth(nth).click();
// Click on the dropdown option.
await page.locator(`[data-testid="select-option-${fieldTypeLabel}"]`).click();
await page.locator(`[data-testid^="select-option-"]`).filter({ hasText: fieldTypeLabel }).click();
} else {
// Set the identifier manually for the first field to test out a case when identifier isn't computed from label automatically
// First field type is by default selected. So, no need to choose from dropdown
@@ -45,13 +45,39 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
// Extract this from the input so it doesn't get saved in the db
// eslint-disable-next-line
userId,
// eslint-disable-next-line
teamId,
bookingFields,
offsetStart,
...rest
} = input;
const eventType = await ctx.prisma.eventType.findUniqueOrThrow({
where: { id },
select: {
children: {
select: {
userId: true,
},
},
workflows: {
select: {
workflowId: true,
},
},
team: {
select: {
name: true,
id: true,
},
},
},
});
if (input.teamId && eventType.team?.id && input.teamId !== eventType.team.id) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
const teamId = input.teamId || eventType.team?.id;
ensureUniqueBookingFields(bookingFields);
const data: Prisma.EventTypeUpdateInput = {
@@ -149,7 +175,23 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
};
}
if (hosts) {
if (teamId && hosts) {
// check if all hosts can be assigned (memberships that have accepted invite)
const memberships =
(await ctx.prisma.membership.findMany({
where: {
teamId,
accepted: true,
},
})) || [];
const teamMemberIds = memberships.map((membership) => membership.userId);
// guard against missing IDs, this may mean a member has just been removed
// or this request was forged.
if (!hosts.every((host) => teamMemberIds.includes(host.userId))) {
throw new TRPCError({
code: "FORBIDDEN",
});
}
data.hosts = {
deleteMany: {},
create: hosts.map((host) => ({
@@ -256,48 +298,26 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
});
}
}
const [oldEventType, eventType] = await ctx.prisma.$transaction([
ctx.prisma.eventType.findFirst({
where: { id },
select: {
children: {
select: {
userId: true,
},
},
workflows: {
select: {
workflowId: true,
},
},
team: {
select: {
name: true,
},
},
},
}),
ctx.prisma.eventType.update({
where: { id },
data,
}),
]);
const updatedEventType = await ctx.prisma.eventType.update({
where: { id },
data,
});
// Handling updates to children event types (managed events types)
await updateChildrenEventTypes({
eventTypeId: id,
currentUserId: ctx.user.id,
oldEventType,
oldEventType: eventType,
hashedLink,
connectedLink,
updatedEventType: eventType,
updatedEventType,
children,
prisma: ctx.prisma,
});
const res = ctx.res as NextApiResponse;
if (typeof res?.revalidate !== "undefined") {
try {
await res?.revalidate(`/${ctx.user.username}/${eventType.slug}`);
await res?.revalidate(`/${ctx.user.username}/${updatedEventType.slug}`);
} catch (e) {
// if reach this it is because the event type page has not been created, so it is not possible to revalidate it
logger.debug((e as Error)?.message);
@@ -43,8 +43,8 @@ export const OptionComponent = <
// This gets styled in the select classNames prop now - handles overrides with styles vs className here doesnt
<reactSelectComponents.Option {...props}>
<div className="flex">
<span className="mr-auto" data-testid={`select-option-${props.label}`}>
{props.label}
<span className="mr-auto" data-testid={`select-option-${(props as unknown as ExtendedOption).value}`}>
{props.label || <>&nbsp;</>}
</span>
{(props.data as unknown as ExtendedOption).needsUpgrade && <UpgradeTeamsBadge />}
{props.isSelected && <Check className="ml-2 h-4 w-4" />}