From 6060ade54f7c19bc0b51c74a8f15e86dc22f3f6d Mon Sep 17 00:00:00 2001 From: PiyushPushkar02 <118709669+PiyushPushkar02@users.noreply.github.com> Date: Thu, 11 May 2023 17:09:05 +0530 Subject: [PATCH] fix: CURL empty command error msg fix (#23119) Changed the error message returned in case of empty input from the user for CURL import Fixes #12008 --- .../server/exceptions/AppsmithError.java | 1 + .../server/exceptions/AppsmithErrorCode.java | 1 + .../ce/CurlImporterServiceCEImpl.java | 5 +- .../services/CurlImporterServiceTest.java | 46 ++++++++++++++++++- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithError.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithError.java index c0300105d2..de2195e559 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithError.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithError.java @@ -12,6 +12,7 @@ import java.text.MessageFormat; public enum AppsmithError { // Ref syntax for message templates: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/MessageFormat.html INVALID_PARAMETER(400, AppsmithErrorCode.INVALID_PARAMETER.getCode(), "Please enter a valid parameter {0}.", AppsmithErrorAction.DEFAULT, "Invalid parameter", ErrorType.ARGUMENT_ERROR, null), + EMPTY_CURL_INPUT_STATEMENT(400, AppsmithErrorCode.EMPTY_CURL_INPUT_STATEMENT.getCode(), "Input CURL statement is empty / null. Please edit the input box to provide a valid CURL statement.", AppsmithErrorAction.DEFAULT, "Invalid parameter", ErrorType.ARGUMENT_ERROR, null), PLUGIN_NOT_INSTALLED(400, AppsmithErrorCode.PLUGIN_NOT_INSTALLED.getCode(), "Plugin {0} not installed", AppsmithErrorAction.DEFAULT, "Plugin not installed", ErrorType.INTERNAL_ERROR, null), PLUGIN_ID_NOT_GIVEN(400, AppsmithErrorCode.PLUGIN_ID_NOT_GIVEN.getCode(), "Missing plugin id. Please enter one.", AppsmithErrorAction.DEFAULT, "Missing plugin id", ErrorType.INTERNAL_ERROR, null), DATASOURCE_NOT_GIVEN(400, AppsmithErrorCode.DATASOURCE_NOT_GIVEN.getCode(), "Missing datasource. Add/enter/connect a datasource to create a valid action.", diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithErrorCode.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithErrorCode.java index fd99dd570c..39bcfa5a73 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithErrorCode.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithErrorCode.java @@ -66,6 +66,7 @@ public enum AppsmithErrorCode { GENERIC_BAD_REQUEST("AE-BAD-4000", "Generic bad request"), GOOGLE_RECAPTCHA_FAILED("AE-CAP-4035", "Google recaptcha failed"), INVALID_CRUD_PAGE_REQUEST("AE-CRD-4039", "Invalid crud page request"), + EMPTY_CURL_INPUT_STATEMENT("AE-CRL-4054","Invalid CURL input statement"), INVALID_CURL_COMMAND("AE-CRL-4029", "Invalid curl command"), INVALID_CURL_METHOD("AE-CRL-4032", "Invalid curl method"), INVALID_CURL_HEADER("AE-CRL-4036", "Invalid curl header"), diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/CurlImporterServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/CurlImporterServiceCEImpl.java index 340f2a9340..e959d78e1c 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/CurlImporterServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/CurlImporterServiceCEImpl.java @@ -37,6 +37,7 @@ import java.util.Base64; import java.util.List; import java.util.Map; import java.util.regex.Pattern; +import static org.apache.commons.lang3.StringUtils.isBlank; @Slf4j public class CurlImporterServiceCEImpl extends BaseApiImporter implements CurlImporterServiceCE { @@ -80,8 +81,8 @@ public class CurlImporterServiceCEImpl extends BaseApiImporter implements CurlIm ActionDTO action; try { - if (input == null) { - throw new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.CURL_CODE); + if (isBlank((String) input)) { + throw new AppsmithException(AppsmithError.EMPTY_CURL_INPUT_STATEMENT, FieldName.CURL_CODE); } action = curlToAction((String) input, name); } catch (AppsmithException e) { diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/CurlImporterServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/CurlImporterServiceTest.java index 7ce1819050..ec0601d2f6 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/CurlImporterServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/CurlImporterServiceTest.java @@ -160,7 +160,7 @@ public class CurlImporterServiceTest { @Test @WithUserDetails(value = "api_user") - public void testImportAction_EmptyLex() { + public void testImportActionOnInvalidInput() { setup(); // Set up the application & page for which this import curl action would be added Application app = new Application(); @@ -180,6 +180,50 @@ public class CurlImporterServiceTest { .verify(); } + @Test + @WithUserDetails(value = "api_user") + public void testImportActionOnNullInput() { + setup(); + // Set up the application & page for which this import curl action would be added + Application app = new Application(); + app.setName("curlTest Incorrect Command"); + + Application application = applicationPageService.createApplication(app, workspaceId).block(); + assert application != null; + PageDTO page = newPageService.findPageById(application.getPages().get(0).getId(), AclPermission.MANAGE_PAGES, false).block(); + + assert page != null; + Mono action = curlImporterService.importAction(null, page.getId(), "actionName", workspaceId, null); + + StepVerifier + .create(action) + .expectErrorMatches(throwable -> throwable instanceof AppsmithException && + throwable.getMessage().equals(AppsmithError.EMPTY_CURL_INPUT_STATEMENT.getMessage())) + .verify(); + } + + @Test + @WithUserDetails(value = "api_user") + public void testImportActionOnEmptyInput() { + setup(); + // Set up the application & page for which this import curl action would be added + Application app = new Application(); + app.setName("curlTest Incorrect Command"); + + Application application = applicationPageService.createApplication(app, workspaceId).block(); + assert application != null; + PageDTO page = newPageService.findPageById(application.getPages().get(0).getId(), AclPermission.MANAGE_PAGES, false).block(); + + assert page != null; + Mono action = curlImporterService.importAction("", page.getId(), "actionName", workspaceId, null); + + StepVerifier + .create(action) + .expectErrorMatches(throwable -> throwable instanceof AppsmithException && + throwable.getMessage().equals(AppsmithError.EMPTY_CURL_INPUT_STATEMENT.getMessage())) + .verify(); + } + @Test @WithUserDetails(value = "api_user") public void importValidCurlCommand() {