Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe("TopicDetailsSchema", () => {
expect(link).toBeVisible();
expect(link).toHaveAttribute(
"href",
`/topic/${testTopicName}/request-schema?env=${env}&schemaType=${schemaType}`
`/topic/${testTopicName}/request-schema?schemaEnv=${env}&schemaType=${schemaType}`
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function TopicDetailsSchema() {
// as it makes it harder to convey the information for assistive technology
<Box alignSelf={"top"} aria-hidden={!createSchemaAllowed}>
<InternalLinkButton
href={`/topic/${topicName}/request-schema?env=${schemaDetailsPerEnv.env}&schemaType=${schemaDetailsPerEnv.schemaType || "AVRO"}`}
href={`/topic/${topicName}/request-schema?schemaEnv=${schemaDetailsPerEnv.env}&schemaType=${schemaDetailsPerEnv.schemaType || "AVRO"}`}
// user can not promote a schema when it it's forbidden, when there is an open request
// or an open promotion request

Expand Down
132 changes: 122 additions & 10 deletions coral/src/app/features/topics/schema-request/TopicSchemaRequest.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe("TopicSchemaRequest", () => {
useQuerySpy.mockRestore();
});

it("does not redirect user if env ID query is part of list of environments", async () => {
it("does not redirect user if schemaEnv ID query matches an associated schema registry env", async () => {
mockGetAllEnvironmentsForTopicAndAcl.mockResolvedValue([
...mockedGetAllEnvironmentsForTopicAndAclResponse,
]);
Expand All @@ -146,7 +146,8 @@ describe("TopicSchemaRequest", () => {
queryClient: true,
memoryRouter: true,
aquariumContext: true,
customRoutePath: "/topic/testtopic/request-schema?env=1",
// schema registry env id "3" = DEV_SCH, associated with Kafka env DEV (id "1")
customRoutePath: "/topic/testtopic/request-schema?schemaEnv=3",
});

const form = getForm();
Expand All @@ -172,7 +173,7 @@ describe("TopicSchemaRequest", () => {
expect(mockedUsedNavigate).not.toHaveBeenCalled();
});

it("does not redirect user if env name query is part of list of environments", async () => {
it("does not redirect user if schemaEnv name query matches an associated schema registry env", async () => {
mockGetAllEnvironmentsForTopicAndAcl.mockResolvedValue([
...mockedGetAllEnvironmentsForTopicAndAclResponse,
]);
Expand All @@ -181,7 +182,8 @@ describe("TopicSchemaRequest", () => {
queryClient: true,
memoryRouter: true,
aquariumContext: true,
customRoutePath: "/topic/testtopic/request-schema?env=INFRA",
// schema registry env name INFRA_SCH, associated with Kafka env INFRA (id "3")
customRoutePath: "/topic/testtopic/request-schema?schemaEnv=INFRA_SCH",
});

const form = getForm();
Expand All @@ -207,7 +209,7 @@ describe("TopicSchemaRequest", () => {
expect(mockedUsedNavigate).not.toHaveBeenCalled();
});

it("redirects user if env id query does not exist in list of environments", async () => {
it("does not redirect user if env query is the name of the associated schema registry env (as passed from topic details schema tab)", async () => {
mockGetAllEnvironmentsForTopicAndAcl.mockResolvedValue([
...mockedGetAllEnvironmentsForTopicAndAclResponse,
]);
Expand All @@ -216,15 +218,86 @@ describe("TopicSchemaRequest", () => {
queryClient: true,
memoryRouter: true,
aquariumContext: true,
customRoutePath: "/topic/testtopic/request-schema?env=999",
customRoutePath: "/topic/testtopic/request-schema?schemaEnv=DEV_SCH",
});

const form = getForm();
expect(form).toBeVisible();

await waitForElementToBeRemoved(
screen.getAllByTestId(/async-select-loading/)
);

const select = within(form).getByRole("combobox", {
name: /Environment/i,
});

// the form preselects the Kafka env (DEV, id "1") that has DEV_SCH as
// its associated schema registry env
await waitFor(() => expect(select).toHaveValue("1"));
await waitFor(() => expect(select).toHaveDisplayValue("DEV"));

expect(mockedUsedNavigate).not.toHaveBeenCalled();
});

it("does not redirect user if env query is the id of the associated schema registry env", async () => {
mockGetAllEnvironmentsForTopicAndAcl.mockResolvedValue([
...mockedGetAllEnvironmentsForTopicAndAclResponse,
]);

customRender(<TopicSchemaRequest presetTopicName={testTopicName} />, {
queryClient: true,
memoryRouter: true,
aquariumContext: true,
customRoutePath: "/topic/testtopic/request-schema?schemaEnv=9",
});

const form = getForm();
expect(form).toBeVisible();

await waitForElementToBeRemoved(
screen.getAllByTestId(/async-select-loading/)
);

const select = within(form).getByRole("combobox", {
name: /Environment/i,
});

// env "9" is the schema registry env of TST (Kafka env id "2")
await waitFor(() => expect(select).toHaveValue("2"));
await waitFor(() => expect(select).toHaveDisplayValue("TST"));

expect(mockedUsedNavigate).not.toHaveBeenCalled();
});

// When the schemaEnv param cannot be resolved to a Kafka env (unknown value, or a
// schema registry env whose Kafka association was removed), the form still renders
// but a warning is shown and submitting is disabled.
it("warns and disables submit when schemaEnv id matches no env (e.g. association removed)", async () => {
mockGetAllEnvironmentsForTopicAndAcl.mockResolvedValue([
...mockedGetAllEnvironmentsForTopicAndAclResponse,
]);

customRender(<TopicSchemaRequest presetTopicName={testTopicName} />, {
queryClient: true,
memoryRouter: true,
aquariumContext: true,
customRoutePath: "/topic/testtopic/request-schema?schemaEnv=999",
});

await waitFor(() => {
expect(mockedUsedNavigate).toHaveBeenCalledWith(-1);
expect(
screen.getByText(/could not be resolved to a Kafka environment/)
).toBeVisible();
});
expect(mockedUsedNavigate).not.toHaveBeenCalled();
expect(getForm()).toBeVisible();
expect(
within(getForm()).getByRole("button", { name: /Submit request/i })
).toBeDisabled();
});

it("redirects user if env name query does not exist in list of environments", async () => {
it("warns and disables submit when schemaEnv name matches no env", async () => {
mockGetAllEnvironmentsForTopicAndAcl.mockResolvedValue([
...mockedGetAllEnvironmentsForTopicAndAclResponse,
]);
Expand All @@ -233,12 +306,51 @@ describe("TopicSchemaRequest", () => {
queryClient: true,
memoryRouter: true,
aquariumContext: true,
customRoutePath: "/topic/testtopic/request-schema?env=HELLO",
customRoutePath: "/topic/testtopic/request-schema?schemaEnv=HELLO",
});

await waitFor(() => {
expect(mockedUsedNavigate).toHaveBeenCalledWith(-1);
expect(
screen.getByText(/could not be resolved to a Kafka environment/)
).toBeVisible();
});
expect(mockedUsedNavigate).not.toHaveBeenCalled();
expect(getForm()).toBeVisible();
expect(
within(getForm()).getByRole("button", { name: /Submit request/i })
).toBeDisabled();
});

it("accepts a Kafka env id as schemaEnv (legacy links) and preselects it without warning", async () => {
mockGetAllEnvironmentsForTopicAndAcl.mockResolvedValue([
...mockedGetAllEnvironmentsForTopicAndAclResponse,
]);

customRender(<TopicSchemaRequest presetTopicName={testTopicName} />, {
queryClient: true,
memoryRouter: true,
aquariumContext: true,
// "1" is the Kafka env id of DEV
customRoutePath: "/topic/testtopic/request-schema?schemaEnv=1",
});

const form = getForm();
expect(form).toBeVisible();

await waitForElementToBeRemoved(
screen.getAllByTestId(/async-select-loading/)
);

const select = within(form).getByRole("combobox", {
name: /Environment/i,
});
await waitFor(() => expect(select).toHaveValue("1"));
await waitFor(() => expect(select).toHaveDisplayValue("DEV"));

expect(
screen.queryByText(/could not be resolved to a Kafka environment/)
).not.toBeInTheDocument();
expect(mockedUsedNavigate).not.toHaveBeenCalled();
});
});

Expand Down
87 changes: 58 additions & 29 deletions coral/src/app/features/topics/schema-request/TopicSchemaRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
Box,
Button,
useToast,
Spacing,
} from "@aivenio/aquarium";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import { AsyncNativeSelectWrapper } from "src/app/components/AsyncNativeSelectWrapper";
import { Dialog } from "src/app/components/Dialog";
Expand Down Expand Up @@ -52,28 +53,29 @@ type TopicSchemaRequestProps = {
function TopicSchemaRequest(props: TopicSchemaRequestProps) {
const { presetTopicName } = props;
const [searchParams] = useSearchParams();
const presetEnvironment = searchParams.get("env") || undefined;
const presetSchemaEnvironment = searchParams.get("schemaEnv") || undefined;
const presetSchemaType = (searchParams.get("schemaType") ??
"AVRO") as SchemaRequest["schemaType"];

const [cancelDialogVisible, setCancelDialogVisible] = useState(false);
const [isValidationError, setIsValidationError] = useState(false);
// set when a "schemaEnv" param was given but no Kafka env has it as associated env
const [noKafkaEnvForSchemaEnv, setNoKafkaEnvForSchemaEnv] = useState(false);

const navigate = useNavigate();
const toast = useToast();

const hasPresetTopicName = presetTopicName !== undefined;
const hasPresetEnvironment =
presetEnvironment !== null &&
presetEnvironment !== undefined &&
presetEnvironment.length > 0;
const hasPresetSchemaEnvironment =
presetSchemaEnvironment !== null &&
presetSchemaEnvironment !== undefined &&
presetSchemaEnvironment.length > 0;

const form = useForm<TopicRequestFormSchema>({
schema: topicRequestFormSchema,
defaultValues: {
topicname: presetTopicName,
schemafull: props.schemafullValueForTest || undefined,
environment: presetEnvironment,
schemaType: presetSchemaType,
},
});
Expand All @@ -90,6 +92,7 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) {
}),
});

//Topic guard: redirect back when the preset topic is not one of the users team topics
useEffect(() => {
if (presetTopicName === undefined || topicNames === undefined) {
return;
Expand All @@ -110,29 +113,42 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) {
queryKey: ["getEnvs"],
queryFn: () => getAllEnvironmentsForTopicAndAcl(),
// not every Kafka Environment has an associated env for schemas,
// so we only want to show those who do.
// We use name and ID related to the Kafka Environment in form and mutation.
// we only want to show those who do.
select: (kafkaEnvironments) =>
kafkaEnvironments.filter((env) => env.associatedEnv),
});

useEffect(() => {
if (presetEnvironment !== undefined && environments !== undefined) {
const validEnv = environments.find(
(env) => presetEnvironment === env.id || presetEnvironment === env.name
);
// Get the associated kafka env for the "schemaEnv" search param
const matchedKafkaEnv = useMemo(() => {
if (presetSchemaEnvironment === undefined || environments === undefined) {
return undefined;
}
return environments.find(
(env) =>
presetSchemaEnvironment === env.id ||
presetSchemaEnvironment === env.name ||
presetSchemaEnvironment === env.associatedEnv?.id ||
presetSchemaEnvironment === env.associatedEnv?.name
);
}, [environments, presetSchemaEnvironment]);

// Allows to pass environment name as well as environment id as search param
if (validEnv && isNaN(Number(presetEnvironment))) {
form.setValue("environment", validEnv.id);
return;
}
//Schema env guard: redirect back when the preset schema env is not in the list
useEffect(() => {
if (
presetSchemaEnvironment !== undefined &&
environments !== undefined &&
matchedKafkaEnv === undefined
) {
setNoKafkaEnvForSchemaEnv(true);
}
}, [environments, presetSchemaEnvironment, matchedKafkaEnv]);

if (validEnv === undefined) {
navigate(-1);
}
// The form expect the Kafka env id
useEffect(() => {
if (matchedKafkaEnv !== undefined) {
form.setValue("environment", matchedKafkaEnv.id);
}
}, [environments, presetEnvironment, form]);
}, [matchedKafkaEnv, form]);

const schemaRequestMutation = useMutation(requestSchemaCreation, {
onSuccess: () => {
Expand Down Expand Up @@ -171,14 +187,24 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) {

return (
<>
<Box>
<Spacing gap={"l2"}>
{schemaRequestMutation.isError && !isValidationError && (
<Box marginBottom={"l1"}>
<Alert type="error">
{parseErrorMsg(schemaRequestMutation.error)}
</Alert>
</Box>
)}
{noKafkaEnvForSchemaEnv && (
<Alert type="warning">
The schema environment &quot;
{presetSchemaEnvironment}
&quot; could not be resolved to a Kafka environment. It may not
exist or have no associated Kafka environment. Please contact your
admin.
</Alert>
)}

<Form
{...form}
ariaLabel={"Request a new schema"}
Expand Down Expand Up @@ -217,11 +243,13 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) {
<NativeSelect<TopicRequestFormSchema>
name={"environment"}
labelText={
hasPresetEnvironment ? "Environment (read-only)" : "Environment"
hasPresetSchemaEnvironment
? "Environment (read-only)"
: "Environment"
}
placeholder={"-- Please select --"}
readOnly={hasPresetEnvironment}
required={!presetEnvironment}
readOnly={hasPresetSchemaEnvironment}
required={!presetSchemaEnvironment}
>
{environments?.map((env) => {
return (
Expand Down Expand Up @@ -284,7 +312,8 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) {
<SubmitButton
disabled={
(isValidationError && !form.watch("forceRegister")) ||
schemaRequestMutation.isLoading
schemaRequestMutation.isLoading ||
noKafkaEnvForSchemaEnv
}
>
{isValidationError
Expand All @@ -305,7 +334,7 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) {
</Button>
</Box>
</Form>
</Box>
</Spacing>
{cancelDialogVisible && (
<Dialog
title={"Cancel schema request?"}
Expand Down
Loading