commit eb728ad8f5a701342d8d338cff99cb2f6ad82f0e Author: Patrick Hiesel Date: Tue Oct 6 09:37:34 2020 +0200 PostWatchedProjects: Throw BRE in case of empty input Change-Id: I6e278da313b0dcfee808a1d5e0d08a92cae08379 diff --git a/java/com/google/gerrit/server/restapi/account/PostWatchedProjects.java b/java/com/google/gerrit/server/restapi/account/PostWatchedProjects.java index c80bf57..5979b2a 100644 --- a/java/com/google/gerrit/server/restapi/account/PostWatchedProjects.java +++ b/java/com/google/gerrit/server/restapi/account/PostWatchedProjects.java @@ -91,7 +91,7 @@ public class PostWatchedProjects throws RestApiException, IOException, PermissionBackendException { Map> m = new HashMap<>(); for (ProjectWatchInfo info : input) { - if (info.project == null) { + if (info.project == null || info.project.trim().isEmpty()) { throw new BadRequestException("project name must be specified"); } diff --git a/javatests/com/google/gerrit/acceptance/rest/account/WatchedProjectsIT.java b/javatests/com/google/gerrit/acceptance/rest/account/WatchedProjectsIT.java index 2c9107c..b70cab8 100644 --- a/javatests/com/google/gerrit/acceptance/rest/account/WatchedProjectsIT.java +++ b/javatests/com/google/gerrit/acceptance/rest/account/WatchedProjectsIT.java @@ -249,4 +249,30 @@ public class WatchedProjectsIT extends AbstractDaemonTest { public void postWithoutBody() throws Exception { adminRestSession.post("/accounts/" + admin.username() + "/watched.projects").assertOK(); } + + @Test + public void nullProjectThrowsBadRequestException() { + List projectsToWatch = new ArrayList<>(); + ProjectWatchInfo pwi = new ProjectWatchInfo(); + pwi.project = null; + projectsToWatch.add(pwi); + Throwable t = + assertThrows( + BadRequestException.class, + () -> gApi.accounts().self().setWatchedProjects(projectsToWatch)); + assertThat(t.getMessage()).isEqualTo("project name must be specified"); + } + + @Test + public void emptyProjectThrowsBadRequestException() { + List projectsToWatch = new ArrayList<>(); + ProjectWatchInfo pwi = new ProjectWatchInfo(); + pwi.project = " "; + projectsToWatch.add(pwi); + Throwable t = + assertThrows( + BadRequestException.class, + () -> gApi.accounts().self().setWatchedProjects(projectsToWatch)); + assertThat(t.getMessage()).isEqualTo("project name must be specified"); + } }