]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14372 move Gitlab alm settings WS to CE
authorZipeng WU <zipeng.wu@sonarsource.com>
Mon, 1 Feb 2021 09:06:56 +0000 (10:06 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Feb 2021 20:07:08 +0000 (20:07 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CreateGitlabAction.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateGitlabAction.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CreateGitlabActionTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateGitlabActionTest.java [new file with mode: 0644]

index 3e8239b22ae39daf14ef0d6f3ab82ebd7f37d51e..9f6196b233356ca22d0ca12274187fbafaf641bc 100644 (file)
@@ -36,6 +36,9 @@ public class AlmSettingsWsModule extends Module {
       //Github alm settings
       CreateGithubAction.class,
       UpdateGithubAction.class,
+      //Gitlab alm settings
+      CreateGitlabAction.class,
+      UpdateGitlabAction.class,
       //Bitbucket alm settings
       CreateBitBucketAction.class,
       UpdateBitbucketAction.class
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CreateGitlabAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CreateGitlabAction.java
new file mode 100644 (file)
index 0000000..a4993e8
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.almsettings.ws;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.server.ws.Change;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.server.user.UserSession;
+
+import static org.sonar.db.alm.setting.ALM.GITLAB;
+
+public class CreateGitlabAction implements AlmSettingsWsAction {
+
+  private static final String PARAM_KEY = "key";
+  private static final String PARAM_URL = "url";
+  private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
+
+  private final DbClient dbClient;
+  private UserSession userSession;
+  private final AlmSettingsSupport almSettingsSupport;
+
+  public CreateGitlabAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
+    this.dbClient = dbClient;
+    this.userSession = userSession;
+    this.almSettingsSupport = almSettingsSupport;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction("create_gitlab")
+      .setDescription("Create GitLab ALM instance Setting. <br/>" +
+        "Requires the 'Administer System' permission")
+      .setPost(true)
+      .setSince("8.1")
+      .setChangelog(new Change("8.2", "Parameter 'URL' was added"))
+      .setHandler(this);
+
+    action.createParam(PARAM_KEY)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("Unique key of the GitLab instance setting");
+    action.createParam(PARAM_URL)
+      .setRequired(true)
+      .setMaximumLength(2000)
+      .setDescription("GitLab API URL");
+    action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
+      .setRequired(true)
+      .setMaximumLength(2000)
+      .setDescription("GitLab personal access token");
+  }
+
+  @Override
+  public void handle(Request request, Response response) {
+    userSession.checkIsSystemAdministrator();
+    doHandle(request);
+    response.noContent();
+  }
+
+  private void doHandle(Request request) {
+    String key = request.mandatoryParam(PARAM_KEY);
+    String url = StringUtils.trim(request.mandatoryParam(PARAM_URL));
+    String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN);
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      almSettingsSupport.checkAlmMultipleFeatureEnabled(GITLAB);
+      almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key);
+      dbClient.almSettingDao().insert(dbSession, new AlmSettingDto()
+        .setAlm(GITLAB)
+        .setUrl(url)
+        .setKey(key)
+        .setPersonalAccessToken(pat));
+      dbSession.commit();
+    }
+  }
+
+}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateGitlabAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateGitlabAction.java
new file mode 100644 (file)
index 0000000..09ced8e
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.almsettings.ws;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.server.ws.Change;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.server.user.UserSession;
+
+import static org.apache.commons.lang.StringUtils.isNotBlank;
+
+public class UpdateGitlabAction implements AlmSettingsWsAction {
+
+  private static final String PARAM_KEY = "key";
+  private static final String PARAM_NEW_KEY = "newKey";
+  private static final String PARAM_URL = "url";
+  private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
+
+  private final DbClient dbClient;
+  private UserSession userSession;
+  private final AlmSettingsSupport almSettingsSupport;
+
+  public UpdateGitlabAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
+    this.dbClient = dbClient;
+    this.userSession = userSession;
+    this.almSettingsSupport = almSettingsSupport;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction("update_gitlab")
+      .setDescription("Update GitLab ALM instance Setting. <br/>" +
+        "Requires the 'Administer System' permission")
+      .setPost(true)
+      .setSince("8.1")
+      .setChangelog(new Change("8.2", "Parameter 'URL' was added"),
+        new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_PERSONAL_ACCESS_TOKEN)))
+      .setHandler(this);
+
+    action.createParam(PARAM_KEY)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("Unique key of the GitLab instance setting");
+    action.createParam(PARAM_NEW_KEY)
+      .setRequired(false)
+      .setMaximumLength(200)
+      .setDescription("Optional new value for an unique key of the GitLab instance setting");
+    action.createParam(PARAM_URL)
+      .setRequired(true)
+      .setMaximumLength(2000)
+      .setDescription("GitLab API URL");
+    action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
+      .setRequired(false)
+      .setMaximumLength(2000)
+      .setDescription("GitLab personal access token");
+  }
+
+  @Override
+  public void handle(Request request, Response response) {
+    userSession.checkIsSystemAdministrator();
+    doHandle(request);
+    response.noContent();
+  }
+
+  private void doHandle(Request request) {
+    String key = request.mandatoryParam(PARAM_KEY);
+    String newKey = request.param(PARAM_NEW_KEY);
+    String url = StringUtils.trim(request.mandatoryParam(PARAM_URL));
+    String pat = request.param(PARAM_PERSONAL_ACCESS_TOKEN);
+
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
+      if (isNotBlank(newKey) && !newKey.equals(key)) {
+        almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
+      }
+      dbClient.almSettingDao().update(dbSession, almSettingDto
+        .setKey(isNotBlank(newKey) ? newKey : key)
+        .setUrl(url)
+        .setPersonalAccessToken(isNotBlank(pat) ? pat : almSettingDto.getPersonalAccessToken()));
+      dbSession.commit();
+    }
+  }
+
+}
index a72b6ddd5da7cc7ae753952ee928a7c81ca46975..bab08fc152d5c8b175bc91448adea6663a5bc0b5 100644 (file)
@@ -31,7 +31,7 @@ public class AlmSettingsWsModuleTest {
   public void verify_count_of_added_components() {
     ComponentContainer container = new ComponentContainer();
     new AlmSettingsWsModule().configure(container);
-    assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 11);
+    assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 13);
   }
 
 }
\ No newline at end of file
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CreateGitlabActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CreateGitlabActionTest.java
new file mode 100644 (file)
index 0000000..7323ec7
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.almsettings.ws;
+
+import org.assertj.core.api.Assertions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
+import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class CreateGitlabActionTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create();
+
+  private static String GITLAB_URL = "gitlab.com/api/v4";
+
+  private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
+
+  private WsActionTester ws = new WsActionTester(new CreateGitlabAction(db.getDbClient(), userSession,
+    new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null), multipleAlmFeatureProvider)));
+
+  @Before
+  public void before() {
+    when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
+  }
+
+  @Test
+  public void create_without_url() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    TestRequest request = ws.newRequest()
+      .setParam("key", "Gitlab - Dev Team")
+      .setParam("personalAccessToken", "98765432100");
+
+    Assertions.assertThatThrownBy(() -> request.execute())
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("The 'url' parameter is missing");
+  }
+
+  @Test
+  public void create_with_url() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    ws.newRequest()
+      .setParam("key", "Gitlab - Dev Team")
+      .setParam("url", GITLAB_URL)
+      .setParam("personalAccessToken", "98765432100")
+      .execute();
+
+    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+      .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
+      .containsOnly(tuple("Gitlab - Dev Team", GITLAB_URL, "98765432100"));
+  }
+
+  @Test
+  public void create_with_empty_url() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    TestRequest request = ws.newRequest()
+      .setParam("key", "Gitlab - Dev Team")
+      .setParam("url", "")
+      .setParam("personalAccessToken", "98765432100");
+
+    Assertions.assertThatThrownBy(() -> request.execute())
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("The 'url' parameter is missing");
+  }
+
+  @Test
+  public void fail_when_key_is_already_used() {
+    when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+    AlmSettingDto gitlabAlmSetting = db.almSettings().insertGitlabAlmSetting();
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", gitlabAlmSetting.getKey()));
+
+    ws.newRequest()
+      .setParam("key", gitlabAlmSetting.getKey())
+      .setParam("personalAccessToken", "98765432100")
+      .setParam("url", GITLAB_URL)
+      .execute();
+  }
+
+  @Test
+  public void fail_when_no_multiple_instance_allowed() {
+    when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+    db.almSettings().insertGitlabAlmSetting();
+
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("A GITLAB setting is already defined");
+
+    ws.newRequest()
+      .setParam("key", "anotherKey")
+      .setParam("personalAccessToken", "98765432100")
+      .setParam("url", GITLAB_URL)
+      .execute();
+  }
+
+  @Test
+  public void fail_when_missing_administer_system_permission() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user);
+
+    expectedException.expect(ForbiddenException.class);
+
+    ws.newRequest()
+      .setParam("key", "Gitlab - Dev Team")
+      .setParam("personalAccessToken", "98765432100")
+      .execute();
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action def = ws.getDef();
+
+    assertThat(def.since()).isEqualTo("8.1");
+    assertThat(def.changelog()).hasSize(1);
+    assertThat(def.isPost()).isTrue();
+    assertThat(def.params())
+      .extracting(WebService.Param::key, WebService.Param::isRequired)
+      .containsExactlyInAnyOrder(tuple("key", true), tuple("personalAccessToken", true), tuple("url", true));
+  }
+}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateGitlabActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateGitlabActionTest.java
new file mode 100644 (file)
index 0000000..132d8d2
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.almsettings.ws;
+
+import org.assertj.core.api.Assertions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
+import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.WsActionTester;
+
+import static java.lang.String.format;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class UpdateGitlabActionTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create();
+
+  private static String GITLAB_URL = "gitlab.com/api/v4";
+
+  private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
+
+  private WsActionTester ws = new WsActionTester(new UpdateGitlabAction(db.getDbClient(), userSession,
+    new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null), multipleAlmFeatureProvider)));
+
+  @Before
+  public void before() {
+    when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
+  }
+
+  @Test
+  public void update_without_url() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    TestRequest request = ws.newRequest()
+      .setParam("key", "Gitlab - Dev Team")
+      .setParam("personalAccessToken", "98765432100");
+
+    Assertions.assertThatThrownBy(() -> request.execute())
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("The 'url' parameter is missing");
+  }
+
+  @Test
+  public void update_with_url() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
+
+    ws.newRequest()
+      .setParam("key", almSettingDto.getKey())
+      .setParam("url", GITLAB_URL)
+      .setParam("personalAccessToken", "10987654321")
+      .execute();
+    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+      .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
+      .containsOnly(tuple(almSettingDto.getKey(), GITLAB_URL, "10987654321"));
+  }
+
+  @Test
+  public void update_with_new_key() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
+
+    ws.newRequest()
+      .setParam("key", almSettingDto.getKey())
+      .setParam("newKey", "Gitlab - Infra Team")
+      .setParam("personalAccessToken", "0123456789")
+      .setParam("url", GITLAB_URL)
+      .execute();
+
+    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+      .extracting(AlmSettingDto::getKey, AlmSettingDto::getPersonalAccessToken, AlmSettingDto::getUrl)
+      .containsOnly(tuple("Gitlab - Infra Team", "0123456789", GITLAB_URL));
+  }
+
+  @Test
+  public void update_without_pat() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
+
+    ws.newRequest()
+      .setParam("key", almSettingDto.getKey())
+      .setParam("url", GITLAB_URL)
+      .execute();
+    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+      .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
+      .containsOnly(tuple(almSettingDto.getKey(), GITLAB_URL, almSettingDto.getPersonalAccessToken()));
+  }
+
+  @Test
+  public void fail_when_key_does_not_match_existing_alm_setting() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
+
+    ws.newRequest()
+      .setParam("key", "unknown")
+      .setParam("personalAccessToken", "0123456789")
+      .setParam("url", GITLAB_URL)
+      .execute();
+  }
+
+  @Test
+  public void fail_when_new_key_matches_existing_alm_setting() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+    AlmSettingDto almSetting1 = db.almSettings().insertGitlabAlmSetting();
+    AlmSettingDto almSetting2 = db.almSettings().insertGitlabAlmSetting();
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
+
+    ws.newRequest()
+      .setParam("key", almSetting1.getKey())
+      .setParam("newKey", almSetting2.getKey())
+      .setParam("personalAccessToken", "0123456789")
+      .setParam("url", GITLAB_URL)
+      .execute();
+  }
+
+  @Test
+  public void fail_when_missing_administer_system_permission() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user);
+    AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
+
+    expectedException.expect(ForbiddenException.class);
+
+    ws.newRequest()
+      .setParam("key", almSettingDto.getKey())
+      .setParam("newKey", "Gitlab - Infra Team")
+      .setParam("personalAccessToken", "0123456789")
+      .setParam("url", GITLAB_URL)
+      .execute();
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action def = ws.getDef();
+
+    assertThat(def.since()).isEqualTo("8.1");
+    assertThat(def.isPost()).isTrue();
+    assertThat(def.params())
+      .extracting(WebService.Param::key, WebService.Param::isRequired)
+      .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("personalAccessToken", false), tuple("url", true));
+  }
+
+}