3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.almsettings.ws;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.alm.setting.AlmSettingDto;
29 import org.sonar.db.user.UserDto;
30 import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
31 import org.sonar.server.component.ComponentFinder;
32 import org.sonar.server.exceptions.BadRequestException;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.tester.UserSessionRule;
35 import org.sonar.server.ws.WsActionTester;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.groups.Tuple.tuple;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
42 public class CreateGithubActionTest {
45 public ExpectedException expectedException = ExpectedException.none();
47 public UserSessionRule userSession = UserSessionRule.standalone();
49 public DbTester db = DbTester.create();
51 private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
53 private WsActionTester ws = new WsActionTester(new CreateGithubAction(db.getDbClient(), userSession,
54 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
55 multipleAlmFeatureProvider)));
58 public void before() {
59 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
63 public void create() {
64 UserDto user = db.users().insertUser();
65 userSession.logIn(user).setSystemAdministrator();
68 .setParam("key", "GitHub Server - Dev Team")
69 .setParam("url", "https://github.enterprise.com")
70 .setParam("appId", "12345")
71 .setParam("privateKey", "678910")
72 .setParam("clientId", "client_1234")
73 .setParam("clientSecret", "client_so_secret")
76 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
77 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
78 .containsOnly(tuple("GitHub Server - Dev Team", "https://github.enterprise.com", "12345", "678910", "client_1234", "client_so_secret"));
82 public void remove_trailing_slash() {
83 UserDto user = db.users().insertUser();
84 userSession.logIn(user).setSystemAdministrator();
87 .setParam("key", "GitHub Server - Dev Team")
88 .setParam("url", "https://github.enterprise.com/")
89 .setParam("appId", "12345")
90 .setParam("privateKey", "678910")
91 .setParam("clientId", "client_1234")
92 .setParam("clientSecret", "client_so_secret")
95 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
96 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
97 .containsOnly(tuple("GitHub Server - Dev Team", "https://github.enterprise.com", "12345", "678910", "client_1234", "client_so_secret"));
101 public void fail_when_key_is_already_used() {
102 when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
103 UserDto user = db.users().insertUser();
104 userSession.logIn(user).setSystemAdministrator();
105 AlmSettingDto gitHubAlmSetting = db.almSettings().insertGitHubAlmSetting();
107 expectedException.expect(IllegalArgumentException.class);
108 expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", gitHubAlmSetting.getKey()));
111 .setParam("key", gitHubAlmSetting.getKey())
112 .setParam("url", "https://github.enterprise.com")
113 .setParam("appId", "12345")
114 .setParam("privateKey", "678910")
115 .setParam("clientId", "client_1234")
116 .setParam("clientSecret", "client_so_secret")
121 public void fail_when_no_multiple_instance_allowed() {
122 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
123 UserDto user = db.users().insertUser();
124 userSession.logIn(user).setSystemAdministrator();
125 db.almSettings().insertGitHubAlmSetting();
127 expectedException.expect(BadRequestException.class);
128 expectedException.expectMessage("A GITHUB setting is already defined");
131 .setParam("key", "key")
132 .setParam("url", "https://github.enterprise.com")
133 .setParam("appId", "12345")
134 .setParam("privateKey", "678910")
135 .setParam("clientId", "client_1234")
136 .setParam("clientSecret", "client_so_secret")
141 public void fail_when_missing_administer_system_permission() {
142 UserDto user = db.users().insertUser();
143 userSession.logIn(user);
145 expectedException.expect(ForbiddenException.class);
148 .setParam("key", "GitHub Server - Dev Team")
149 .setParam("url", "https://github.enterprise.com")
150 .setParam("appId", "12345")
151 .setParam("privateKey", "678910")
152 .setParam("clientId", "client_1234")
153 .setParam("clientSecret", "client_so_secret")
158 public void definition() {
159 WebService.Action def = ws.getDef();
161 assertThat(def.since()).isEqualTo("8.1");
162 assertThat(def.isPost()).isTrue();
163 assertThat(def.params())
164 .extracting(WebService.Param::key, WebService.Param::isRequired)
165 .containsExactlyInAnyOrder(tuple("key", true), tuple("url", true), tuple("appId", true), tuple("privateKey", true), tuple("clientId", true), tuple("clientSecret", true));