From: Guillaume Jambet Date: Thu, 2 Nov 2017 15:49:23 +0000 (+0100) Subject: SONAR-10040 add length validation to Organisations ws X-Git-Tag: 7.0-RC1~359 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=418de893b3c055a46517befd24e66cbc77318d23;p=sonarqube.git SONAR-10040 add length validation to Organisations ws --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/CreateAction.java index c19f0d68799..6794c67555e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/CreateAction.java @@ -38,6 +38,7 @@ import org.sonarqube.ws.Organizations.CreateWsResponse; import static com.google.common.base.Preconditions.checkArgument; import static org.sonar.server.organization.OrganizationCreation.NewOrganization.newOrganizationBuilder; +import static org.sonar.server.organization.OrganizationValidation.KEY_MAX_LENGTH; import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_KEY; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -76,6 +77,7 @@ public class CreateAction implements OrganizationsWsAction { action.createParam(PARAM_KEY) .setRequired(false) + .setMaximumLength(KEY_MAX_LENGTH) .setDescription("Key of the organization.
" + "The key is unique to the whole SonarQube.
" + "When not specified, the key is computed from the name.
" + diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsSupport.java index 41131784aa4..7fff042cb82 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsSupport.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsSupport.java @@ -27,6 +27,9 @@ import org.sonar.server.organization.OrganizationValidation; import org.sonarqube.ws.Organizations; import static org.sonar.core.util.Protobuf.setNullable; +import static org.sonar.server.organization.OrganizationValidation.DESCRIPTION_MAX_LENGTH; +import static org.sonar.server.organization.OrganizationValidation.NAME_MAX_LENGTH; +import static org.sonar.server.organization.OrganizationValidation.URL_MAX_LENGTH; /** * Factorizes code and constants between Organization WS's actions. @@ -80,22 +83,26 @@ public class OrganizationsWsSupport { void addOrganizationDetailsParams(WebService.NewAction action, boolean isNameRequired) { action.createParam(PARAM_NAME) .setRequired(isNameRequired) + .setMaximumLength(NAME_MAX_LENGTH) .setDescription("Name of the organization.
" + "It must be between 2 and 64 chars longs.") .setExampleValue("Foo Company"); action.createParam(PARAM_DESCRIPTION) .setRequired(false) + .setMaximumLength(DESCRIPTION_MAX_LENGTH) .setDescription("Description of the organization.
It must be less than 256 chars long.") .setExampleValue("The Foo company produces quality software for Bar."); action.createParam(PARAM_URL) .setRequired(false) + .setMaximumLength(URL_MAX_LENGTH) .setDescription("URL of the organization.
It must be less than 256 chars long.") .setExampleValue("https://www.foo.com"); action.createParam(PARAM_AVATAR_URL) .setRequired(false) + .setMaximumLength(URL_MAX_LENGTH) .setDescription("URL of the organization avatar.
It must be less than 256 chars long.") .setExampleValue("https://www.foo.com/foo.png"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java index 0806889e0ee..6a22e8d4ec1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java @@ -251,7 +251,7 @@ public class CreateActionTest { logInAsSystemAdministrator(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Name '" + STRING_65_CHARS_LONG + "' must be at most 64 chars long"); + expectedException.expectMessage("'name' length (65) is longer than the maximum authorized (64)"); executeRequest(STRING_65_CHARS_LONG); } @@ -283,7 +283,7 @@ public class CreateActionTest { String key = STRING_65_CHARS_LONG.substring(0, 33); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Key '" + key + "' must be at most 32 chars long"); + expectedException.expectMessage("'key' length (33) is longer than the maximum authorized (32)"); executeRequest("foo", key); } @@ -411,7 +411,7 @@ public class CreateActionTest { logInAsSystemAdministrator(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Description '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long"); + expectedException.expectMessage("'description' length (257) is longer than the maximum authorized (256)"); executeRequest("foo", "bar", STRING_257_CHARS_LONG, null, null); } @@ -431,7 +431,7 @@ public class CreateActionTest { logInAsSystemAdministrator(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Url '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long"); + expectedException.expectMessage("'url' length (257) is longer than the maximum authorized (256)"); executeRequest("foo", "bar", null, STRING_257_CHARS_LONG, null); } @@ -451,7 +451,7 @@ public class CreateActionTest { logInAsSystemAdministrator(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Avatar '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long"); + expectedException.expectMessage("'avatar' length (257) is longer than the maximum authorized (256)"); executeRequest("foo", "bar", null, null, STRING_257_CHARS_LONG); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateActionTest.java index 382dd302cf7..dd75504d62c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateActionTest.java @@ -195,7 +195,8 @@ public class UpdateActionTest { userSession.logIn(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Name '" + STRING_65_CHARS_LONG + "' must be at most 64 chars long"); + expectedException.expectMessage("'name' length (65) is longer than the maximum authorized (64)"); + executeKeyRequest(SOME_KEY, STRING_65_CHARS_LONG); } @@ -233,7 +234,7 @@ public class UpdateActionTest { userSession.logIn(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Description '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long"); + expectedException.expectMessage("'description' length (257) is longer than the maximum authorized (256)"); executeKeyRequest(SOME_KEY, "bar", STRING_257_CHARS_LONG, null, null); } @@ -253,7 +254,7 @@ public class UpdateActionTest { userSession.logIn(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Url '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long"); + expectedException.expectMessage("'url' length (257) is longer than the maximum authorized (256)"); executeKeyRequest(SOME_KEY, "bar", null, STRING_257_CHARS_LONG, null); } @@ -273,7 +274,7 @@ public class UpdateActionTest { userSession.logIn(); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Avatar '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long"); + expectedException.expectMessage("'avatar' length (257) is longer than the maximum authorized (256)"); executeKeyRequest(SOME_KEY, "bar", null, null, STRING_257_CHARS_LONG); }