From: Jacek <52388493+jacek-poreda-sonarsource@users.noreply.github.com> Date: Wed, 28 Aug 2019 06:06:51 +0000 (+0200) Subject: SONAR-12376 - increase project key param limit to 400 in api/webhook/create (#2027) X-Git-Tag: 8.0~194 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=90970dd0e1c0858d328bf535d17e83e1580574af;p=sonarqube.git SONAR-12376 - increase project key param limit to 400 in api/webhook/create (#2027) --- diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhooksWsParameters.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhooksWsParameters.java index ec847a6d3e3..36501327bfc 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhooksWsParameters.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhooksWsParameters.java @@ -33,7 +33,7 @@ class WebhooksWsParameters { static final String ORGANIZATION_KEY_PARAM = "organization"; static final int ORGANIZATION_KEY_PARAM_MAXIMUM_LENGTH = 255; static final String PROJECT_KEY_PARAM = "project"; - static final int PROJECT_KEY_PARAM_MAXIMUM_LENGTH = 100; + static final int PROJECT_KEY_PARAM_MAXIMUM_LENGTH = 400; static final String NAME_PARAM = "name"; static final int NAME_PARAM_MAXIMUM_LENGTH = 100; static final String URL_PARAM = "url"; diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/webhook/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/webhook/ws/CreateActionTest.java index 06fe6016c0f..502ed2284dd 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/webhook/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/webhook/ws/CreateActionTest.java @@ -97,6 +97,27 @@ public class CreateActionTest { } + @Test + public void create_a_webhook_with_400_length_project_key() { + String longProjectKey = generateStringWithLength(400); + ComponentDto project = componentDbTester.insertPrivateProject(componentDto -> componentDto.setDbKey(longProjectKey)); + + userSession.logIn().addProjectPermission(ADMIN, project); + + CreateWsResponse response = wsActionTester.newRequest() + .setParam("project", longProjectKey) + .setParam("name", NAME_WEBHOOK_EXAMPLE_001) + .setParam("url", URL_WEBHOOK_EXAMPLE_001) + .setParam("secret", "a_secret") + .executeProtobuf(CreateWsResponse.class); + + assertThat(response.getWebhook()).isNotNull(); + assertThat(response.getWebhook().getKey()).isNotNull(); + assertThat(response.getWebhook().getName()).isEqualTo(NAME_WEBHOOK_EXAMPLE_001); + assertThat(response.getWebhook().getUrl()).isEqualTo(URL_WEBHOOK_EXAMPLE_001); + assertThat(response.getWebhook().getSecret()).isEqualTo("a_secret"); + } + @Test public void create_a_webhook_with_secret() { userSession.logIn().addPermission(ADMINISTER, defaultOrganizationProvider.get().getUuid()); @@ -351,4 +372,30 @@ public class CreateActionTest { .execute(); } + + @Test + public void throw_IllegalArgumentException_if_project_key_greater_than_400() { + String longProjectKey = generateStringWithLength(401); + userSession.logIn().addPermission(ADMINISTER, defaultOrganizationProvider.get().getUuid()); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("'project' length (401) is longer than the maximum authorized (400)"); + + wsActionTester.newRequest() + .setParam("project", longProjectKey) + .setParam("name", NAME_WEBHOOK_EXAMPLE_001) + .setParam("url", URL_WEBHOOK_EXAMPLE_001) + .setParam("secret", "a_secret") + .executeProtobuf(CreateWsResponse.class); + } + + + private static String generateStringWithLength(int length) { + StringBuilder sb = new StringBuilder(length); + for (int i = 0; i < length; i++) { + sb.append("x"); + } + return sb.toString(); + } + }