From fc27f22fa01303a39132ae27494b43a41bc7101f Mon Sep 17 00:00:00 2001 From: kumaradityaraj Date: Fri, 20 Sep 2024 17:22:11 +0530 Subject: [PATCH] Added validation for data index url and its tests --- .../src/i18n/AppI18n.ts | 1 + .../src/i18n/locales/de.ts | 1 + .../src/i18n/locales/en.ts | 1 + .../runtimeTools/RuntimeToolsSettings.tsx | 34 +++++++++++++++++-- .../src/url/index.ts | 9 +++++ .../tests/url/dataIndexValidUrl.test.ts | 31 +++++++++++++++++ 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 packages/serverless-logic-web-tools/tests/url/dataIndexValidUrl.test.ts diff --git a/packages/serverless-logic-web-tools/src/i18n/AppI18n.ts b/packages/serverless-logic-web-tools/src/i18n/AppI18n.ts index 9824c624c08..dd19298ac2b 100644 --- a/packages/serverless-logic-web-tools/src/i18n/AppI18n.ts +++ b/packages/serverless-logic-web-tools/src/i18n/AppI18n.ts @@ -71,6 +71,7 @@ interface AppDictionary extends ReferenceDictionary { validationError: string; connectionError: string; configExpiredWarning: string; + validDataIndexURLError: string; }; confirmModal: { title: string; diff --git a/packages/serverless-logic-web-tools/src/i18n/locales/de.ts b/packages/serverless-logic-web-tools/src/i18n/locales/de.ts index f795fce6748..fa0e1c330af 100644 --- a/packages/serverless-logic-web-tools/src/i18n/locales/de.ts +++ b/packages/serverless-logic-web-tools/src/i18n/locales/de.ts @@ -73,6 +73,7 @@ export const de: AppI18n = { validationError: "Sie müssen alle erforderlichen Felder ausfüllen, bevor Sie fortfahren können.", connectionError: "Verbindung abgelehnt. Bitte überprüfen Sie die angegebenen Informationen.", configExpiredWarning: "Token oder Konto ist abgelaufen. Bitte aktualisieren Sie Ihre Konfiguration.", + validDataIndexURLError: "Bitte geben Sie eine gültige Datenindex-URL ein.", }, confirmModal: { title: "Bereitstellen", diff --git a/packages/serverless-logic-web-tools/src/i18n/locales/en.ts b/packages/serverless-logic-web-tools/src/i18n/locales/en.ts index 800745e2565..d896ed0bf76 100644 --- a/packages/serverless-logic-web-tools/src/i18n/locales/en.ts +++ b/packages/serverless-logic-web-tools/src/i18n/locales/en.ts @@ -71,6 +71,7 @@ export const en: AppI18n = { validationError: "You must fill out all required fields before you can proceed.", connectionError: "Connection refused. Please check the information provided.", configExpiredWarning: "Token or account expired. Please update your configuration.", + validDataIndexURLError: "Please enter a valid Data Index URL.", }, confirmModal: { title: "Deploy", diff --git a/packages/serverless-logic-web-tools/src/settings/runtimeTools/RuntimeToolsSettings.tsx b/packages/serverless-logic-web-tools/src/settings/runtimeTools/RuntimeToolsSettings.tsx index ccbdd84723c..b11f4d32fd2 100644 --- a/packages/serverless-logic-web-tools/src/settings/runtimeTools/RuntimeToolsSettings.tsx +++ b/packages/serverless-logic-web-tools/src/settings/runtimeTools/RuntimeToolsSettings.tsx @@ -17,10 +17,10 @@ * under the License. */ -import React from "react"; +import React, { useEffect } from "react"; import { Button, ButtonVariant } from "@patternfly/react-core/dist/js/components/Button"; import { EmptyState, EmptyStateBody, EmptyStateIcon } from "@patternfly/react-core/dist/js/components/EmptyState"; -import { ActionGroup, Form, FormGroup } from "@patternfly/react-core/dist/js/components/Form"; +import { ActionGroup, Form, FormAlert, FormGroup } from "@patternfly/react-core/dist/js/components/Form"; import { InputGroup, InputGroupText } from "@patternfly/react-core/dist/js/components/InputGroup"; import { Modal, ModalVariant } from "@patternfly/react-core/dist/js/components/Modal"; import { PageSection } from "@patternfly/react-core/dist/js/components/Page"; @@ -43,14 +43,28 @@ import { saveConfigCookie, } from "./RuntimeToolsConfig"; import { removeTrailingSlashFromUrl } from "../../url"; +import { validDataIndexUrl } from "../../url"; +import { Alert } from "@patternfly/react-core/dist/js"; +import { useAppI18n } from "../../i18n"; const PAGE_TITLE = "Runtime Tools"; +enum DataIndexValidation { + INITIAL = "INITIAL", + INVALID = "INVALID", +} + export function RuntimeToolsSettings(props: SettingsPageProps) { + const { i18n } = useAppI18n(); const settings = useSettings(); const settingsDispatch = useSettingsDispatch(); const [config, setConfig] = useState(settings.runtimeTools.config); const [isModalOpen, setIsModalOpen] = useState(false); + const [isDataIndexUrlValidated, setDataIndexUrlValidated] = useState(DataIndexValidation.INVALID); + + useEffect(() => { + setDataIndexUrlValidated(DataIndexValidation.INITIAL); + }, [config]); const handleModalToggle = useCallback(() => { setIsModalOpen((prevIsModalOpen) => !prevIsModalOpen); @@ -80,6 +94,11 @@ export function RuntimeToolsSettings(props: SettingsPageProps) { const newConfig: RuntimeToolsSettingsConfig = { dataIndexUrl: removeTrailingSlashFromUrl(config.dataIndexUrl), }; + if (!validDataIndexUrl(config.dataIndexUrl)) { + setDataIndexUrlValidated(DataIndexValidation.INVALID); + return; + } + setConfig(newConfig); settingsDispatch.runtimeTools.setConfig(newConfig); saveConfigCookie(newConfig); @@ -144,6 +163,17 @@ export function RuntimeToolsSettings(props: SettingsPageProps) { appendTo={props.pageContainerRef.current || document.body} >
+ {isDataIndexUrlValidated === DataIndexValidation.INVALID && ( + + + + )} { + it.each([ + ["http://example.com/", true], + ["https://example.com/", true], + ["loremIpsum", false], + ["google.com", false], + ])("should validate the data index URL", (inputUrl, isValidUrl) => { + expect(validDataIndexUrl(inputUrl)).toBe(isValidUrl); + }); +});