diff --git a/coral/src/app/features/topics/details/schema/TopicDetailsSchema.test.tsx b/coral/src/app/features/topics/details/schema/TopicDetailsSchema.test.tsx index e2c0e8348a..25545f69ac 100644 --- a/coral/src/app/features/topics/details/schema/TopicDetailsSchema.test.tsx +++ b/coral/src/app/features/topics/details/schema/TopicDetailsSchema.test.tsx @@ -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}` ); }); diff --git a/coral/src/app/features/topics/details/schema/TopicDetailsSchema.tsx b/coral/src/app/features/topics/details/schema/TopicDetailsSchema.tsx index b64954cfa3..51da1c6cb5 100644 --- a/coral/src/app/features/topics/details/schema/TopicDetailsSchema.tsx +++ b/coral/src/app/features/topics/details/schema/TopicDetailsSchema.tsx @@ -181,7 +181,7 @@ function TopicDetailsSchema() { // as it makes it harder to convey the information for assistive technology { 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, ]); @@ -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(); @@ -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, ]); @@ -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(); @@ -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, ]); @@ -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(, { + 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(, { + 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, ]); @@ -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(, { + 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(); }); }); diff --git a/coral/src/app/features/topics/schema-request/TopicSchemaRequest.tsx b/coral/src/app/features/topics/schema-request/TopicSchemaRequest.tsx index 4eddd46e3b..bad1e97452 100644 --- a/coral/src/app/features/topics/schema-request/TopicSchemaRequest.tsx +++ b/coral/src/app/features/topics/schema-request/TopicSchemaRequest.tsx @@ -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"; @@ -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({ schema: topicRequestFormSchema, defaultValues: { topicname: presetTopicName, schemafull: props.schemafullValueForTest || undefined, - environment: presetEnvironment, schemaType: presetSchemaType, }, }); @@ -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; @@ -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: () => { @@ -171,7 +187,7 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) { return ( <> - + {schemaRequestMutation.isError && !isValidationError && ( @@ -179,6 +195,16 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) { )} + {noKafkaEnvForSchemaEnv && ( + + The schema environment " + {presetSchemaEnvironment} + " could not be resolved to a Kafka environment. It may not + exist or have no associated Kafka environment. Please contact your + admin. + + )} +
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 ( @@ -284,7 +312,8 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) { {isValidationError @@ -305,7 +334,7 @@ function TopicSchemaRequest(props: TopicSchemaRequestProps) { -
+ {cancelDialogVisible && (