]> source.dussan.org Git - sonarqube.git/blob
769a4db66b04734debf27e35a69f8de9effcf11a
[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.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;
36
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;
41
42 public class CreateBitbucketCloudActionTest {
43
44   @Rule
45   public ExpectedException expectedException = ExpectedException.none();
46   @Rule
47   public UserSessionRule userSession = UserSessionRule.standalone();
48   @Rule
49   public DbTester db = DbTester.create();
50
51   private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
52
53   private WsActionTester ws = new WsActionTester(new CreateBitbucketCloudAction(db.getDbClient(), userSession,
54     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
55       multipleAlmFeatureProvider)));
56
57   @Before
58   public void before() {
59     when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
60   }
61
62   @Test
63   public void create() {
64     UserDto user = db.users().insertUser();
65     userSession.logIn(user).setSystemAdministrator();
66
67     ws.newRequest()
68       .setParam("key", "Bitbucket Server - Dev Team")
69       .setParam("clientId", "id")
70       .setParam("clientSecret", "secret")
71       .setParam("workspace", "workspace1")
72       .execute();
73
74     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
75       .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
76       .containsOnly(tuple("Bitbucket Server - Dev Team", "id", "secret", "workspace1"));
77   }
78
79   @Test
80   public void fail_when_key_is_already_used() {
81     when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
82     UserDto user = db.users().insertUser();
83     userSession.logIn(user).setSystemAdministrator();
84     AlmSettingDto bitbucketAlmSetting = db.almSettings().insertBitbucketAlmSetting();
85
86     expectedException.expect(IllegalArgumentException.class);
87     expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", bitbucketAlmSetting.getKey()));
88
89     ws.newRequest()
90       .setParam("key", bitbucketAlmSetting.getKey())
91       .setParam("workspace", "workspace1")
92       .setParam("clientId", "id")
93       .setParam("clientSecret", "secret")
94       .execute();
95   }
96
97   @Test
98   public void fail_when_no_multiple_instance_allowed() {
99     when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
100     UserDto user = db.users().insertUser();
101     userSession.logIn(user).setSystemAdministrator();
102     db.almSettings().insertBitbucketCloudAlmSetting();
103
104     expectedException.expect(BadRequestException.class);
105     expectedException.expectMessage("A BITBUCKET_CLOUD setting is already defined");
106
107     ws.newRequest()
108       .setParam("key", "otherKey")
109       .setParam("workspace", "workspace1")
110       .setParam("clientId", "id")
111       .setParam("clientSecret", "secret")
112       .execute();
113   }
114
115   @Test
116   public void fail_when_no_multiple_instance_allowed_and_bitbucket_server_exists() {
117     when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
118     UserDto user = db.users().insertUser();
119     userSession.logIn(user).setSystemAdministrator();
120     db.almSettings().insertBitbucketAlmSetting();
121
122     expectedException.expect(BadRequestException.class);
123     expectedException.expectMessage("A BITBUCKET setting is already defined");
124
125     ws.newRequest()
126       .setParam("key", "otherKey")
127       .setParam("workspace", "workspace1")
128       .setParam("clientId", "id")
129       .setParam("clientSecret", "secret")
130       .execute();
131   }
132
133   @Test
134   public void fail_when_missing_administer_system_permission() {
135     UserDto user = db.users().insertUser();
136     userSession.logIn(user);
137
138     expectedException.expect(ForbiddenException.class);
139
140     ws.newRequest()
141       .setParam("key", "Bitbucket Server - Dev Team")
142       .setParam("clientId", "id")
143       .setParam("clientSecret", "secret")
144       .setParam("workspace", "workspace1")
145       .execute();
146   }
147
148   @Test
149   public void definition() {
150     WebService.Action def = ws.getDef();
151
152     assertThat(def.since()).isEqualTo("8.7");
153     assertThat(def.isPost()).isTrue();
154     assertThat(def.params())
155       .extracting(WebService.Param::key, WebService.Param::isRequired)
156       .containsExactlyInAnyOrder(tuple("key", true), tuple("clientId", true), tuple("clientSecret", true), tuple("workspace", true));
157   }
158 }