The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Hide Regenerate Key button if form field is readonly (#72105)
Nick Fitzpatrick53a13043ae4e
diff --git a/frontend/src/metabase/forms/components/FormSecretKey/FormSecretKey.tsx b/frontend/src/metabase/forms/components/FormSecretKey/FormSecretKey.tsxindex 2c372b64c8e..6eb2e6e4502 100644--- a/frontend/src/metabase/forms/components/FormSecretKey/FormSecretKey.tsx+++ b/frontend/src/metabase/forms/components/FormSecretKey/FormSecretKey.tsx@@ -22,12 +22,28 @@ export interface FormSecretKeyProps}export const FormSecretKey = forwardRef(function FormSecretKey(- { name, nullable, onChange, onBlur, ...props }: FormSecretKeyProps,+ { name, nullable, onChange, onBlur, readOnly, ...props }: FormSecretKeyProps,ref: Ref<HTMLInputElement>,) {const [showModal, { open: openModal, close: closeModal }] = useDisclosure();const [{ value }, { error }, { setValue }] = useField(name);+ const generateSecretButtonProps = readOnly+ ? null+ : {+ rightSection: (+ <Button+ className={S.generateButton}+ miw={value ? undefined : "10rem"}+ onClick={openModal}+ variant="filled"+ >+ {value ? t`Regenerate key` : t`Set up key`}+ </Button>+ ),+ rightSectionProps: { className: S.rightSection },+ };+return (<><Stack gap="sm">@@ -43,17 +59,7 @@ export const FormSecretKey = forwardRef(function FormSecretKey([S.unset]: !value, // Just show the 'Set up key' button when no key is set yet}),}}- rightSection={- <Button- className={S.generateButton}- miw={value ? undefined : "10rem"}- onClick={openModal}- variant="filled"- >- {value ? t`Regenerate key` : t`Set up key`}- </Button>- }- rightSectionProps={{ className: S.rightSection }}+ {...generateSecretButtonProps}/>{!!error && <Text c="error">{error}</Text>}</Stack>diff --git a/frontend/src/metabase/forms/components/FormSecretKey/FormSecretKey.unit.spec.tsx b/frontend/src/metabase/forms/components/FormSecretKey/FormSecretKey.unit.spec.tsxindex 4b64a8ed5d2..452d82a0e16 100644--- a/frontend/src/metabase/forms/components/FormSecretKey/FormSecretKey.unit.spec.tsx+++ b/frontend/src/metabase/forms/components/FormSecretKey/FormSecretKey.unit.spec.tsx@@ -13,6 +13,7 @@ interface FormValues {interface SetupOpts {initialValues?: FormValues;+ readOnly?: boolean;}const GENERATED_TOKEN = "newly-generated-token-xyz";@@ -21,7 +22,10 @@ const EXISTING_VALUE = "my-super-secret-token-abc123";const OBFUSCATED_EXISTING = "**********23";const OBFUSCATED_GENERATED = "**********yz";-const setup = ({ initialValues = { secret: undefined } }: SetupOpts = {}) => {+const setup = ({+ initialValues = { secret: undefined },+ readOnly = false,+}: SetupOpts = {}) => {const onSubmit = jest.fn();setupGenerateRandomTokenEndpoint(GENERATED_TOKEN);@@ -29,7 +33,14 @@ const setup = ({ initialValues = { secret: undefined } }: SetupOpts = {}) => {renderWithProviders(<FormProvider initialValues={initialValues} onSubmit={onSubmit}><Form>- <FormSecretKey name="secret" label="Signing Key" />+ <FormSecretKey+ name="secret"+ label="Signing Key"+ readOnly={readOnly}+ wrapperProps={{+ "data-testid": "inputWrapper",+ }}+ /><FormSubmitButton /></Form></FormProvider>,@@ -166,7 +177,9 @@ describe("FormSecretKey", () => {setup({ initialValues: { secret: EXISTING_VALUE } });await userEvent.click(- screen.getByRole("button", { name: "Regenerate key" }),+ within(screen.getByTestId("inputWrapper")).getByRole("button", {+ name: "Regenerate key",+ }),);expect(@@ -189,6 +202,15 @@ describe("FormSecretKey", () => {});});+ describe("when readOnly is true (env var controlled)", () => {+ it("does not show 'Regenerate key' or 'Set up key' buttons", () => {+ setup({ initialValues: { secret: EXISTING_VALUE }, readOnly: true });+ expect(+ within(screen.getByTestId("inputWrapper")).queryByRole("button"),+ ).not.toBeInTheDocument();+ });+ });+describe("token length validation", () => {it("disables 'Done' button when the token is too short", async () => {setup({ initialValues: { secret: undefined } });