]> source.dussan.org Git - sonarqube.git/blob
93265c354fbe5b1d901f3548f8a1f3166d568b4c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.almsettings.ws;
21
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;
35
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;
40
41 public class UpdateGithubActionTest {
42
43   @Rule
44   public ExpectedException expectedException = ExpectedException.none();
45   @Rule
46   public UserSessionRule userSession = UserSessionRule.standalone();
47   @Rule
48   public DbTester db = DbTester.create();
49
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))));
53
54   @Test
55   public void update() {
56     UserDto user = db.users().insertUser();
57     userSession.logIn(user).setSystemAdministrator();
58     AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
59
60     ws.newRequest()
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")
67       .execute();
68
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"));
72   }
73
74   @Test
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();
79
80     ws.newRequest()
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")
87       .execute();
88
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"));
92   }
93
94   @Test
95   public void update_with_new_key() {
96     UserDto user = db.users().insertUser();
97     userSession.logIn(user).setSystemAdministrator();
98     AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting();
99
100     ws.newRequest()
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")
108       .execute();
109
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"));
113   }
114
115   @Test
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();
120
121     ws.newRequest()
122       .setParam("key", almSettingDto.getKey())
123       .setParam("url", "https://github.enterprise-unicorn.com/")
124       .setParam("appId", "54321")
125       .setParam("clientId", "client_1234")
126       .execute();
127
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()));
131   }
132
133   @Test
134   public void fail_when_key_does_not_match_existing_alm_setting() {
135     UserDto user = db.users().insertUser();
136     userSession.logIn(user).setSystemAdministrator();
137
138     expectedException.expect(NotFoundException.class);
139     expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
140
141     ws.newRequest()
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")
149       .execute();
150   }
151
152   @Test
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();
158
159     expectedException.expect(IllegalArgumentException.class);
160     expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
161
162     ws.newRequest()
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")
170       .execute();
171   }
172
173   @Test
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();
178
179     expectedException.expect(ForbiddenException.class);
180
181     ws.newRequest()
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")
189       .execute();
190   }
191
192   @Test
193   public void definition() {
194     WebService.Action def = ws.getDef();
195
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));
202   }
203
204 }