From 70fb31a42d93ee99b19ffaf2db2a6f930a77a39f Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 19 Jan 2016 18:05:54 +0100 Subject: [PATCH] SONAR-7208 WS api/user_tokens/generate forbid blank token name --- .../org/sonar/server/usertoken/ws/GenerateAction.java | 4 +++- .../org/sonar/server/usertoken/ws/GenerateActionTest.java | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java b/server/sonar-server/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java index fe693a26ccc..542b5de1755 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java @@ -139,11 +139,13 @@ public class GenerateAction implements UserTokensWsAction { private GenerateWsRequest toCreateWsRequest(Request request) { GenerateWsRequest generateWsRequest = new GenerateWsRequest() .setLogin(request.param(PARAM_LOGIN)) - .setName(request.mandatoryParam(PARAM_NAME)); + .setName(request.mandatoryParam(PARAM_NAME).trim()); if (generateWsRequest.getLogin() == null) { generateWsRequest.setLogin(userSession.getLogin()); } + checkRequest(!generateWsRequest.getName().isEmpty(), "The '%s' parameter must not be blank", PARAM_NAME); + return generateWsRequest; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java index 4bccd4fc0f6..942c205eee5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java @@ -111,6 +111,14 @@ public class GenerateActionTest { newRequest("unknown-login", "any-name"); } + @Test + public void fail_if_name_is_blank() { + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("The 'name' parameter must not be blank"); + + newRequest(GRACE_HOPPER, " "); + } + @Test public void fail_if_token_with_same_login_and_name_exists() { newRequest(GRACE_HOPPER, TOKEN_NAME); -- 2.39.5