From a6ff6301330187d101da9ce5da20ccf90e048457 Mon Sep 17 00:00:00 2001 From: Guillaume Jambet Date: Thu, 2 Nov 2017 17:28:51 +0100 Subject: [PATCH] SONAR-10040 add length validation to Quality Profiles ws --- .../qualityprofile/QProfileFactoryImpl.java | 2 -- .../server/qualityprofile/ws/CreateAction.java | 18 ++++++++++-------- .../server/qualityprofile/ws/RenameAction.java | 14 ++++++++------ .../qualityprofile/ws/RenameActionTest.java | 15 --------------- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactoryImpl.java index 804fd3c7e6a..66f8887f1e5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactoryImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactoryImpl.java @@ -55,8 +55,6 @@ public class QProfileFactoryImpl implements QProfileFactory { this.activeRuleIndexer = activeRuleIndexer; } - // ------------- CREATION - private static OrganizationDto requireNonNull(@Nullable OrganizationDto organization) { Objects.requireNonNull(organization, "Organization is required, when creating a quality profile."); return organization; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java index 63061c405e9..a8c18e70bda 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java @@ -36,12 +36,12 @@ import org.sonar.server.qualityprofile.QProfileName; import org.sonar.server.qualityprofile.QProfileResult; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.user.UserSession; -import org.sonar.server.util.LanguageParamUtils; import org.sonarqube.ws.QualityProfiles.CreateWsResponse; import org.sonarqube.ws.client.qualityprofile.CreateRequest; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; import static org.sonar.server.qualityprofile.ws.QProfileWsSupport.createOrganizationParam; +import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_CREATE; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE; @@ -51,6 +51,7 @@ import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters. public class CreateAction implements QProfileWsAction { private static final String PARAM_BACKUP_FORMAT = "backup_%s"; + static final int NAME_MAXIMUM_LENGTH = 100; private final DbClient dbClient; private final QProfileFactory profileFactory; @@ -81,27 +82,28 @@ public class CreateAction implements QProfileWsAction { @Override public void define(WebService.NewController controller) { NewAction create = controller.createAction(ACTION_CREATE) - .setSince("5.2") - .setDescription("Create a quality profile.
" + - "Requires to be logged in and the 'Administer Quality Profiles' permission.") .setPost(true) + .setDescription("Create a quality profile.
" + + "Requires to be logged in and the 'Administer Quality Profiles' permission.") .setResponseExample(getClass().getResource("create-example.json")) + .setSince("5.2") .setHandler(this); createOrganizationParam(create) .setSince("6.4"); create.createParam(PARAM_NAME) + .setRequired(true) + .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("Quality profile name") .setExampleValue("My Sonar way") - .setDeprecatedKey("profileName", "6.6") - .setRequired(true); + .setDeprecatedKey("profileName", "6.6"); create.createParam(PARAM_LANGUAGE) + .setRequired(true) .setDescription("Quality profile language") .setExampleValue("js") - .setPossibleValues(LanguageParamUtils.getLanguageKeys(languages)) - .setRequired(true); + .setPossibleValues(getLanguageKeys(languages)); for (ProfileImporter importer : importers) { create.createParam(getBackupParamName(importer.getKey())) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RenameAction.java index cbf9ce28d98..e538aafd3b8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RenameAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RenameAction.java @@ -34,6 +34,7 @@ import org.sonar.server.user.UserSession; import static java.lang.String.format; import static java.util.Optional.ofNullable; import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; +import static org.sonar.server.qualityprofile.ws.CreateAction.NAME_MAXIMUM_LENGTH; import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_NAME; @@ -55,25 +56,26 @@ public class RenameAction implements QProfileWsAction { @Override public void define(WebService.NewController controller) { NewAction setDefault = controller.createAction("rename") - .setSince("5.2") + .setPost(true) .setDescription("Rename a quality profile.
" + "Requires one of the following permissions:" + "") - .setPost(true) + .setSince("5.2") .setHandler(this); setDefault.createParam(PARAM_KEY) + .setRequired(true) .setDescription("Quality profile key") - .setExampleValue(UUID_EXAMPLE_01) - .setRequired(true); + .setExampleValue(UUID_EXAMPLE_01); setDefault.createParam(PARAM_NAME) + .setRequired(true) + .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("New quality profile name") - .setExampleValue("My Sonar way") - .setRequired(true); + .setExampleValue("My Sonar way"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java index 4a8cfa693b5..465d8dbe097 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java @@ -136,21 +136,6 @@ public class RenameActionTest { assertThat(reloaded.getName()).isEqualTo("Duplicated name"); } - @Test - public void allow_100_characters_as_name_and_not_more() throws Exception { - logInAsQProfileAdministrator(); - String qualityProfileKey = createNewValidQualityProfileKey(); - - String a100charName = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; - call(qualityProfileKey, a100charName); - - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Name is too long (>100 characters)"); - - String a101charName = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"; - call(qualityProfileKey, a101charName); - } - @Test public void as_qprofile_editor() { QProfileDto qualityProfile = db.qualityProfiles().insert(organization); -- 2.39.5