//Azure alm settings,
CreateAzureAction.class,
UpdateAzureAction.class,
+ //Github alm settings
+ CreateGithubAction.class,
+ UpdateGithubAction.class,
//Bitbucket alm settings
CreateBitBucketAction.class,
UpdateBitbucketAction.class
--- /dev/null
+/*
+ * 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.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.GITHUB;
+
+public class CreateGithubAction implements AlmSettingsWsAction {
+
+ private static final String PARAM_KEY = "key";
+ private static final String PARAM_URL = "url";
+ private static final String PARAM_APP_ID = "appId";
+ private static final String PARAM_CLIENT_ID = "clientId";
+ private static final String PARAM_CLIENT_SECRET = "clientSecret";
+ private static final String PARAM_PRIVATE_KEY = "privateKey";
+
+ private final DbClient dbClient;
+ private final UserSession userSession;
+ private final AlmSettingsSupport almSettingsSupport;
+
+ public CreateGithubAction(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_github")
+ .setDescription("Create GitHub ALM instance Setting. <br/>" +
+ "Requires the 'Administer System' permission")
+ .setPost(true)
+ .setSince("8.1")
+ .setHandler(this);
+
+ action.createParam(PARAM_KEY)
+ .setRequired(true)
+ .setMaximumLength(200)
+ .setDescription("Unique key of the GitHub instance setting");
+ action.createParam(PARAM_URL)
+ .setRequired(true)
+ .setMaximumLength(2000)
+ .setDescription("GitHub API URL");
+ action.createParam(PARAM_APP_ID)
+ .setRequired(true)
+ .setMaximumLength(80)
+ .setDescription("GitHub App ID");
+ action.createParam(PARAM_PRIVATE_KEY)
+ .setRequired(true)
+ .setMaximumLength(2000)
+ .setDescription("GitHub App private key");
+ action.createParam(PARAM_CLIENT_ID)
+ .setRequired(true)
+ .setMaximumLength(80)
+ .setDescription("GitHub App Client ID");
+ action.createParam(PARAM_CLIENT_SECRET)
+ .setRequired(true)
+ .setMaximumLength(80)
+ .setDescription("GitHub App Client Secret");
+ }
+
+ @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 = request.mandatoryParam(PARAM_URL);
+ String appId = request.mandatoryParam(PARAM_APP_ID);
+ String clientId = request.mandatoryParam(PARAM_CLIENT_ID);
+ String clientSecret = request.mandatoryParam(PARAM_CLIENT_SECRET);
+ String privateKey = request.mandatoryParam(PARAM_PRIVATE_KEY);
+
+ if (url.endsWith("/")) {
+ url = url.substring(0, url.length() - 1);
+ }
+
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ almSettingsSupport.checkAlmMultipleFeatureEnabled(GITHUB);
+ almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key);
+ dbClient.almSettingDao().insert(dbSession, new AlmSettingDto()
+ .setAlm(GITHUB)
+ .setKey(key)
+ .setUrl(url)
+ .setAppId(appId)
+ .setPrivateKey(privateKey)
+ .setClientId(clientId)
+ .setClientSecret(clientSecret));
+ dbSession.commit();
+ }
+ }
+
+}
--- /dev/null
+/*
+ * 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.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 UpdateGithubAction 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_APP_ID = "appId";
+ private static final String PARAM_CLIENT_ID = "clientId";
+ private static final String PARAM_CLIENT_SECRET = "clientSecret";
+ private static final String PARAM_PRIVATE_KEY = "privateKey";
+
+ private final DbClient dbClient;
+ private final UserSession userSession;
+ private final AlmSettingsSupport almSettingsSupport;
+
+ public UpdateGithubAction(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_github")
+ .setDescription("Update GitHub ALM instance Setting. <br/>" +
+ "Requires the 'Administer System' permission")
+ .setPost(true)
+ .setSince("8.1")
+ .setChangelog(new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_PRIVATE_KEY)),
+ new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_CLIENT_SECRET)))
+ .setHandler(this);
+
+ action.createParam(PARAM_KEY)
+ .setRequired(true)
+ .setMaximumLength(200)
+ .setDescription("Unique key of the GitHub instance setting");
+ action.createParam(PARAM_NEW_KEY)
+ .setRequired(false)
+ .setMaximumLength(200)
+ .setDescription("Optional new value for an unique key of the GitHub instance setting");
+ action.createParam(PARAM_URL)
+ .setRequired(true)
+ .setMaximumLength(2000)
+ .setDescription("GitHub API URL");
+ action.createParam(PARAM_APP_ID)
+ .setRequired(true)
+ .setMaximumLength(80)
+ .setDescription("GitHub API ID");
+ action.createParam(PARAM_PRIVATE_KEY)
+ .setRequired(false)
+ .setMaximumLength(2000)
+ .setDescription("GitHub App private key");
+ action.createParam(PARAM_CLIENT_ID)
+ .setRequired(true)
+ .setMaximumLength(80)
+ .setDescription("GitHub App Client ID");
+ action.createParam(PARAM_CLIENT_SECRET)
+ .setRequired(false)
+ .setMaximumLength(80)
+ .setDescription("GitHub App Client Secret");
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ 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 = request.mandatoryParam(PARAM_URL);
+ String appId = request.mandatoryParam(PARAM_APP_ID);
+ String clientId = request.mandatoryParam(PARAM_CLIENT_ID);
+ String clientSecret = request.param(PARAM_CLIENT_SECRET);
+ String privateKey = request.param(PARAM_PRIVATE_KEY);
+
+ if (url.endsWith("/")) {
+ url = url.substring(0, url.length() - 1);
+ }
+
+ 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)
+ .setAppId(appId)
+ .setPrivateKey(isNotBlank(privateKey) ? privateKey : almSettingDto.getPrivateKey())
+ .setClientId(clientId)
+ .setClientSecret(isNotBlank(clientSecret) ? clientSecret : almSettingDto.getClientSecret()));
+ dbSession.commit();
+ }
+ }
+
+}
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new AlmSettingsWsModule().configure(container);
- assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 9);
+ assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 11);
}
}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.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.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 CreateGithubActionTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
+
+ private WsActionTester ws = new WsActionTester(new CreateGithubAction(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() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+
+ ws.newRequest()
+ .setParam("key", "GitHub Server - Dev Team")
+ .setParam("url", "https://github.enterprise.com")
+ .setParam("appId", "12345")
+ .setParam("privateKey", "678910")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .execute();
+
+ assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+ .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
+ .containsOnly(tuple("GitHub Server - Dev Team", "https://github.enterprise.com", "12345", "678910", "client_1234", "client_so_secret"));
+ }
+
+ @Test
+ public void remove_trailing_slash() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+
+ ws.newRequest()
+ .setParam("key", "GitHub Server - Dev Team")
+ .setParam("url", "https://github.enterprise.com/")
+ .setParam("appId", "12345")
+ .setParam("privateKey", "678910")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .execute();
+
+ assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+ .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
+ .containsOnly(tuple("GitHub Server - Dev Team", "https://github.enterprise.com", "12345", "678910", "client_1234", "client_so_secret"));
+ }
+
+ @Test
+ public void fail_when_key_is_already_used() {
+ when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto gitHubAlmSetting = db.almSettings().insertGitHubAlmSetting();
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", gitHubAlmSetting.getKey()));
+
+ ws.newRequest()
+ .setParam("key", gitHubAlmSetting.getKey())
+ .setParam("url", "https://github.enterprise.com")
+ .setParam("appId", "12345")
+ .setParam("privateKey", "678910")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .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().insertGitHubAlmSetting();
+
+ expectedException.expect(BadRequestException.class);
+ expectedException.expectMessage("A GITHUB setting is already defined");
+
+ ws.newRequest()
+ .setParam("key", "key")
+ .setParam("url", "https://github.enterprise.com")
+ .setParam("appId", "12345")
+ .setParam("privateKey", "678910")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .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", "GitHub Server - Dev Team")
+ .setParam("url", "https://github.enterprise.com")
+ .setParam("appId", "12345")
+ .setParam("privateKey", "678910")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .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("url", true), tuple("appId", true), tuple("privateKey", true), tuple("clientId", true), tuple("clientSecret", true));
+ }
+}
--- /dev/null
+/*
+ * 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.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.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;
+
+public class UpdateGithubActionTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private WsActionTester ws = new WsActionTester(new UpdateGithubAction(db.getDbClient(), userSession,
+ new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
+ mock(MultipleAlmFeatureProvider.class))));
+
+ @Test
+ public void update() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
+
+ ws.newRequest()
+ .setParam("key", almSettingDto.getKey())
+ .setParam("url", "https://github.enterprise-unicorn.com")
+ .setParam("appId", "54321")
+ .setParam("privateKey", "10987654321")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .execute();
+
+ assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+ .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
+ .containsOnly(tuple(almSettingDto.getKey(), "https://github.enterprise-unicorn.com", "54321", "10987654321", "client_1234", "client_so_secret"));
+ }
+
+ @Test
+ public void update_url_with_trailing_slash() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
+
+ ws.newRequest()
+ .setParam("key", almSettingDto.getKey())
+ .setParam("url", "https://github.enterprise-unicorn.com/")
+ .setParam("appId", "54321")
+ .setParam("privateKey", "10987654321")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .execute();
+
+ assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+ .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
+ .containsOnly(tuple(almSettingDto.getKey(), "https://github.enterprise-unicorn.com", "54321", "10987654321", "client_1234", "client_so_secret"));
+ }
+
+ @Test
+ public void update_with_new_key() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
+
+ ws.newRequest()
+ .setParam("key", almSettingDto.getKey())
+ .setParam("newKey", "GitHub Server - Infra Team")
+ .setParam("url", "https://github.enterprise-unicorn.com")
+ .setParam("appId", "54321")
+ .setParam("privateKey", "10987654321")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .execute();
+
+ assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+ .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
+ .containsOnly(tuple("GitHub Server - Infra Team", "https://github.enterprise-unicorn.com", "54321", "10987654321", "client_1234", "client_so_secret"));
+ }
+
+ @Test
+ public void update_without_private_key_nor_client_secret() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user).setSystemAdministrator();
+ AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
+
+ ws.newRequest()
+ .setParam("key", almSettingDto.getKey())
+ .setParam("url", "https://github.enterprise-unicorn.com/")
+ .setParam("appId", "54321")
+ .setParam("clientId", "client_1234")
+ .execute();
+
+ assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+ .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
+ .containsOnly(tuple(almSettingDto.getKey(), "https://github.enterprise-unicorn.com", "54321", almSettingDto.getPrivateKey(), "client_1234", almSettingDto.getClientSecret()));
+ }
+
+ @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("newKey", "GitHub Server - Infra Team")
+ .setParam("url", "https://github.enterprise-unicorn.com")
+ .setParam("appId", "54321")
+ .setParam("privateKey", "10987654321")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .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().insertGitHubAlmSetting();
+ AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting();
+
+ 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("url", "https://github.enterprise-unicorn.com")
+ .setParam("appId", "54321")
+ .setParam("privateKey", "10987654321")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .execute();
+ }
+
+ @Test
+ public void fail_when_missing_administer_system_permission() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user);
+ AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
+
+ expectedException.expect(ForbiddenException.class);
+
+ ws.newRequest()
+ .setParam("key", almSettingDto.getKey())
+ .setParam("newKey", "GitHub Server - Infra Team")
+ .setParam("url", "https://github.enterprise-unicorn.com")
+ .setParam("appId", "54321")
+ .setParam("privateKey", "10987654321")
+ .setParam("clientId", "client_1234")
+ .setParam("clientSecret", "client_so_secret")
+ .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("url", true), tuple("appId", true), tuple("privateKey", false), tuple("clientId", true),
+ tuple("clientSecret", false));
+ }
+
+}