]> source.dussan.org Git - sonarqube.git/blob
bb37976d64f4790c11fd2082429adc908a0617c0
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.config.internal.Encryption;
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.MultipleAlmFeature;
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.TestRequest;
36 import org.sonar.server.ws.WsActionTester;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.assertThatNoException;
40 import static org.assertj.core.api.Assertions.assertThatThrownBy;
41 import static org.assertj.core.groups.Tuple.tuple;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44
45 public class CreateBitbucketCloudActionIT {
46
47   @Rule
48   public UserSessionRule userSession = UserSessionRule.standalone();
49   @Rule
50   public DbTester db = DbTester.create();
51
52   private final Encryption encryption = mock(Encryption.class);
53   private final MultipleAlmFeature multipleAlmFeature = mock(MultipleAlmFeature.class);
54
55   private WsActionTester ws = new WsActionTester(new CreateBitbucketCloudAction(db.getDbClient(), userSession,
56     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
57       multipleAlmFeature)));
58
59   @Before
60   public void before() {
61     when(multipleAlmFeature.isEnabled()).thenReturn(false);
62   }
63
64   @Test
65   public void create() {
66     UserDto user = db.users().insertUser();
67     userSession.logIn(user).setSystemAdministrator();
68
69     ws.newRequest()
70       .setParam("key", "Bitbucket Server - Dev Team")
71       .setParam("clientId", "id")
72       .setParam("clientSecret", "secret")
73       .setParam("workspace", "workspace1")
74       .execute();
75
76     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
77       .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, s -> s.getDecryptedClientSecret(encryption), AlmSettingDto::getAppId)
78       .containsOnly(tuple("Bitbucket Server - Dev Team", "id", "secret", "workspace1"));
79   }
80
81   @Test
82   public void fail_when_key_is_already_used() {
83     when(multipleAlmFeature.isEnabled()).thenReturn(true);
84     UserDto user = db.users().insertUser();
85     userSession.logIn(user).setSystemAdministrator();
86     AlmSettingDto bitbucketAlmSetting = db.almSettings().insertBitbucketAlmSetting();
87
88     assertThatThrownBy(() -> ws.newRequest()
89       .setParam("key", bitbucketAlmSetting.getKey())
90       .setParam("workspace", "workspace1")
91       .setParam("clientId", "id")
92       .setParam("clientSecret", "secret")
93       .execute())
94       .isInstanceOf(IllegalArgumentException.class)
95       .hasMessageContaining(String.format("An DevOps Platform setting with key '%s' already exist", bitbucketAlmSetting.getKey()));
96   }
97
98   @Test
99   public void fail_when_no_multiple_instance_allowed() {
100     when(multipleAlmFeature.isEnabled()).thenReturn(false);
101     UserDto user = db.users().insertUser();
102     userSession.logIn(user).setSystemAdministrator();
103     db.almSettings().insertBitbucketCloudAlmSetting();
104
105     assertThatThrownBy(() -> ws.newRequest()
106       .setParam("key", "otherKey")
107       .setParam("workspace", "workspace1")
108       .setParam("clientId", "id")
109       .setParam("clientSecret", "secret")
110       .execute())
111       .isInstanceOf(BadRequestException.class)
112       .hasMessageContaining("A BITBUCKET_CLOUD setting is already defined");
113   }
114
115   @Test
116   public void fail_when_no_multiple_instance_allowed_and_bitbucket_server_exists() {
117     when(multipleAlmFeature.isEnabled()).thenReturn(false);
118     UserDto user = db.users().insertUser();
119     userSession.logIn(user).setSystemAdministrator();
120     db.almSettings().insertBitbucketAlmSetting();
121
122     assertThatThrownBy(() -> ws.newRequest()
123       .setParam("key", "otherKey")
124       .setParam("workspace", "workspace1")
125       .setParam("clientId", "id")
126       .setParam("clientSecret", "secret")
127       .execute())
128       .isInstanceOf(BadRequestException.class)
129       .hasMessageContaining("A BITBUCKET setting is already defined");
130   }
131
132   @Test
133   public void fail_when_missing_administer_system_permission() {
134     UserDto user = db.users().insertUser();
135     userSession.logIn(user);
136
137     assertThatThrownBy(() -> ws.newRequest()
138       .setParam("key", "Bitbucket Server - Dev Team")
139       .setParam("clientId", "id")
140       .setParam("clientSecret", "secret")
141       .setParam("workspace", "workspace1")
142       .execute())
143       .isInstanceOf(ForbiddenException.class);
144   }
145
146   @Test
147   public void fail_when_workspace_id_format_is_incorrect() {
148     when(multipleAlmFeature.isEnabled()).thenReturn(false);
149     String workspace = "workspace/name";
150     UserDto user = db.users().insertUser();
151     userSession.logIn(user).setSystemAdministrator();
152
153     TestRequest request = ws.newRequest()
154       .setParam("key", "another new key")
155       .setParam("workspace", workspace)
156       .setParam("clientId", "id")
157       .setParam("clientSecret", "secret");
158
159     assertThatThrownBy(request::execute)
160       .isInstanceOf(BadRequestException.class)
161       .hasMessageContaining(String.format(
162         "Workspace ID '%s' has an incorrect format. Should only contain lowercase letters, numbers, dashes, and underscores.",
163         workspace
164       ));
165   }
166
167   @Test
168   public void do_not_fail_when_workspace_id_format_is_correct() {
169     when(multipleAlmFeature.isEnabled()).thenReturn(false);
170     String workspace = "work-space_123";
171     UserDto user = db.users().insertUser();
172     userSession.logIn(user).setSystemAdministrator();
173
174     TestRequest request = ws.newRequest()
175       .setParam("key", "yet another new key")
176       .setParam("workspace", workspace)
177       .setParam("clientId", "id")
178       .setParam("clientSecret", "secret");
179
180     assertThatNoException().isThrownBy(request::execute);
181   }
182
183   @Test
184   public void definition() {
185     WebService.Action def = ws.getDef();
186
187     assertThat(def.since()).isEqualTo("8.7");
188     assertThat(def.isPost()).isTrue();
189     assertThat(def.params())
190       .extracting(WebService.Param::key, WebService.Param::isRequired)
191       .containsExactlyInAnyOrder(tuple("key", true), tuple("clientId", true), tuple("clientSecret", true), tuple("workspace", true));
192   }
193 }