]> source.dussan.org Git - sonarqube.git/blob
47d2868bdbd75054f88c1b722e263a8c2abda60f
[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.sonar.api.resources.ResourceTypes;
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.TestRequest;
35 import org.sonar.server.ws.WsActionTester;
36
37 import static java.lang.String.format;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.assertThatThrownBy;
40 import static org.assertj.core.groups.Tuple.tuple;
41 import static org.mockito.Mockito.mock;
42
43 public class UpdateBitbucketCloudActionTest {
44   @Rule
45   public UserSessionRule userSession = UserSessionRule.standalone();
46   @Rule
47   public DbTester db = DbTester.create();
48
49   private final WsActionTester ws = new WsActionTester(new UpdateBitbucketCloudAction(db.getDbClient(), userSession,
50     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), mock(ResourceTypes.class)),
51       mock(MultipleAlmFeatureProvider.class))));
52
53   @Test
54   public void update() {
55     UserDto user = db.users().insertUser();
56     userSession.logIn(user).setSystemAdministrator();
57     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
58
59     ws.newRequest()
60       .setParam("key", almSettingDto.getKey())
61       .setParam("workspace", "workspace")
62       .setParam("clientId", "id")
63       .setParam("clientSecret", "secret")
64       .execute();
65
66     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
67       .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
68       .containsOnly(tuple(almSettingDto.getKey(), "id", "secret", "workspace"));
69   }
70
71   @Test
72   public void update_with_new_key() {
73     UserDto user = db.users().insertUser();
74     userSession.logIn(user).setSystemAdministrator();
75
76     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
77
78     ws.newRequest()
79       .setParam("key", almSettingDto.getKey())
80       .setParam("newKey", "Bitbucket Server - Infra Team")
81       .setParam("workspace", "workspace")
82       .setParam("clientId", "id")
83       .setParam("clientSecret", "secret")
84       .execute();
85     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
86       .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
87       .containsOnly(tuple("Bitbucket Server - Infra Team", "id", "secret", "workspace"));
88   }
89
90   @Test
91   public void update_binding_without_changing_the_key() {
92     UserDto user = db.users().insertUser();
93     userSession.logIn(user).setSystemAdministrator();
94     AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
95
96     ws.newRequest()
97       .setParam("key", almSetting.getKey())
98       .setParam("newKey", almSetting.getKey())
99       .setParam("workspace", "workspace")
100       .setParam("clientId", "id")
101       .setParam("clientSecret", "secret")
102       .execute();
103
104     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
105       .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
106       .containsOnly(tuple(almSetting.getKey(), "id", "secret", "workspace"));
107   }
108
109   @Test
110   public void update_without_secret() {
111     UserDto user = db.users().insertUser();
112     userSession.logIn(user).setSystemAdministrator();
113
114     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
115
116     ws.newRequest()
117       .setParam("key", almSettingDto.getKey())
118       .setParam("workspace", "workspace")
119       .setParam("clientId", "id")
120       .execute();
121     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
122       .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
123       .containsOnly(tuple(almSettingDto.getKey(), "id", almSettingDto.getClientSecret(), "workspace"));
124   }
125
126   @Test
127   public void fail_when_key_does_not_match_existing_alm_setting() {
128     UserDto user = db.users().insertUser();
129     userSession.logIn(user).setSystemAdministrator();
130     TestRequest request = ws.newRequest()
131       .setParam("key", "unknown")
132       .setParam("workspace", "workspace")
133       .setParam("clientId", "id")
134       .setParam("clientSecret", "secret");
135
136     assertThatThrownBy(request::execute)
137       .isInstanceOf(NotFoundException.class)
138       .hasMessage("ALM setting with key 'unknown' cannot be found");
139   }
140
141   @Test
142   public void fail_when_new_key_matches_existing_alm_setting() {
143     UserDto user = db.users().insertUser();
144     userSession.logIn(user).setSystemAdministrator();
145     AlmSettingDto almSetting1 = db.almSettings().insertBitbucketAlmSetting();
146     AlmSettingDto almSetting2 = db.almSettings().insertBitbucketAlmSetting();
147     TestRequest request = ws.newRequest()
148       .setParam("key", almSetting1.getKey())
149       .setParam("newKey", almSetting2.getKey())
150       .setParam("workspace", "workspace")
151       .setParam("clientId", "id")
152       .setParam("clientSecret", "secret");
153
154     assertThatThrownBy(request::execute)
155       .isInstanceOf(IllegalArgumentException.class)
156       .hasMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
157   }
158
159   @Test
160   public void fail_when_missing_administer_system_permission() {
161     UserDto user = db.users().insertUser();
162     userSession.logIn(user);
163     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
164     TestRequest request = ws.newRequest()
165       .setParam("key", almSettingDto.getKey())
166       .setParam("newKey", "Bitbucket Server - Infra Team")
167       .setParam("workspace", "workspace")
168       .setParam("clientId", "id")
169       .setParam("clientSecret", "secret");
170
171     assertThatThrownBy(request::execute)
172       .isInstanceOf(ForbiddenException.class);
173   }
174
175   @Test
176   public void definition() {
177     WebService.Action def = ws.getDef();
178
179     assertThat(def.since()).isEqualTo("8.7");
180     assertThat(def.isPost()).isTrue();
181     assertThat(def.params())
182       .extracting(WebService.Param::key, WebService.Param::isRequired)
183       .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("workspace", true),
184         tuple("clientId", true), tuple("clientSecret", false));
185   }
186
187 }