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.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.db.DbTester;
27 import org.sonar.db.alm.setting.AlmSettingDto;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
30 import org.sonar.server.component.ComponentFinder;
31 import org.sonar.server.exceptions.ForbiddenException;
32 import org.sonar.server.exceptions.NotFoundException;
33 import org.sonar.server.tester.UserSessionRule;
34 import org.sonar.server.ws.WsActionTester;
36 import static java.lang.String.format;
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;
41 public class UpdateGithubActionTest {
44 public ExpectedException expectedException = ExpectedException.none();
46 public UserSessionRule userSession = UserSessionRule.standalone();
48 public DbTester db = DbTester.create();
50 private WsActionTester ws = new WsActionTester(new UpdateGithubAction(db.getDbClient(), userSession,
51 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
52 mock(MultipleAlmFeatureProvider.class))));
55 public void update() {
56 UserDto user = db.users().insertUser();
57 userSession.logIn(user).setSystemAdministrator();
58 AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
61 .setParam("key", almSettingDto.getKey())
62 .setParam("url", "https://github.enterprise-unicorn.com")
63 .setParam("appId", "54321")
64 .setParam("privateKey", "10987654321")
65 .setParam("clientId", "client_1234")
66 .setParam("clientSecret", "client_so_secret")
69 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
70 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
71 .containsOnly(tuple(almSettingDto.getKey(), "https://github.enterprise-unicorn.com", "54321", "10987654321", "client_1234", "client_so_secret"));
75 public void update_url_with_trailing_slash() {
76 UserDto user = db.users().insertUser();
77 userSession.logIn(user).setSystemAdministrator();
78 AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
81 .setParam("key", almSettingDto.getKey())
82 .setParam("url", "https://github.enterprise-unicorn.com/")
83 .setParam("appId", "54321")
84 .setParam("privateKey", "10987654321")
85 .setParam("clientId", "client_1234")
86 .setParam("clientSecret", "client_so_secret")
89 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
90 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
91 .containsOnly(tuple(almSettingDto.getKey(), "https://github.enterprise-unicorn.com", "54321", "10987654321", "client_1234", "client_so_secret"));
95 public void update_with_new_key() {
96 UserDto user = db.users().insertUser();
97 userSession.logIn(user).setSystemAdministrator();
98 AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
101 .setParam("key", almSettingDto.getKey())
102 .setParam("newKey", "GitHub Server - Infra Team")
103 .setParam("url", "https://github.enterprise-unicorn.com")
104 .setParam("appId", "54321")
105 .setParam("privateKey", "10987654321")
106 .setParam("clientId", "client_1234")
107 .setParam("clientSecret", "client_so_secret")
110 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
111 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
112 .containsOnly(tuple("GitHub Server - Infra Team", "https://github.enterprise-unicorn.com", "54321", "10987654321", "client_1234", "client_so_secret"));
116 public void update_without_private_key_nor_client_secret() {
117 UserDto user = db.users().insertUser();
118 userSession.logIn(user).setSystemAdministrator();
119 AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
122 .setParam("key", almSettingDto.getKey())
123 .setParam("url", "https://github.enterprise-unicorn.com/")
124 .setParam("appId", "54321")
125 .setParam("clientId", "client_1234")
128 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
129 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
130 .containsOnly(tuple(almSettingDto.getKey(), "https://github.enterprise-unicorn.com", "54321", almSettingDto.getPrivateKey(), "client_1234", almSettingDto.getClientSecret()));
134 public void fail_when_key_does_not_match_existing_alm_setting() {
135 UserDto user = db.users().insertUser();
136 userSession.logIn(user).setSystemAdministrator();
138 expectedException.expect(NotFoundException.class);
139 expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
142 .setParam("key", "unknown")
143 .setParam("newKey", "GitHub Server - Infra Team")
144 .setParam("url", "https://github.enterprise-unicorn.com")
145 .setParam("appId", "54321")
146 .setParam("privateKey", "10987654321")
147 .setParam("clientId", "client_1234")
148 .setParam("clientSecret", "client_so_secret")
153 public void fail_when_new_key_matches_existing_alm_setting() {
154 UserDto user = db.users().insertUser();
155 userSession.logIn(user).setSystemAdministrator();
156 AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting();
157 AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting();
159 expectedException.expect(IllegalArgumentException.class);
160 expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
163 .setParam("key", almSetting1.getKey())
164 .setParam("newKey", almSetting2.getKey())
165 .setParam("url", "https://github.enterprise-unicorn.com")
166 .setParam("appId", "54321")
167 .setParam("privateKey", "10987654321")
168 .setParam("clientId", "client_1234")
169 .setParam("clientSecret", "client_so_secret")
174 public void fail_when_missing_administer_system_permission() {
175 UserDto user = db.users().insertUser();
176 userSession.logIn(user);
177 AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
179 expectedException.expect(ForbiddenException.class);
182 .setParam("key", almSettingDto.getKey())
183 .setParam("newKey", "GitHub Server - Infra Team")
184 .setParam("url", "https://github.enterprise-unicorn.com")
185 .setParam("appId", "54321")
186 .setParam("privateKey", "10987654321")
187 .setParam("clientId", "client_1234")
188 .setParam("clientSecret", "client_so_secret")
193 public void definition() {
194 WebService.Action def = ws.getDef();
196 assertThat(def.since()).isEqualTo("8.1");
197 assertThat(def.isPost()).isTrue();
198 assertThat(def.params())
199 .extracting(WebService.Param::key, WebService.Param::isRequired)
200 .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("url", true), tuple("appId", true), tuple("privateKey", false), tuple("clientId", true),
201 tuple("clientSecret", false));