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.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;
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;
42 public class CreateBitbucketActionTest {
45 public ExpectedException expectedException = ExpectedException.none();
47 public UserSessionRule userSession = UserSessionRule.standalone();
49 public DbTester db = DbTester.create();
51 private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
53 private WsActionTester ws = new WsActionTester(new CreateBitBucketAction(db.getDbClient(), userSession,
54 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
55 multipleAlmFeatureProvider)));
59 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
63 public void create() {
64 UserDto user = db.users().insertUser();
65 userSession.logIn(user).setSystemAdministrator();
68 .setParam("key", "Bitbucket Server - Dev Team")
69 .setParam("url", "https://bitbucket.enterprise.com")
70 .setParam("personalAccessToken", "98765432100")
73 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
74 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
75 .containsOnly(tuple("Bitbucket Server - Dev Team", "https://bitbucket.enterprise.com", "98765432100"));
79 public void fail_when_key_is_already_used() {
80 when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
81 UserDto user = db.users().insertUser();
82 userSession.logIn(user).setSystemAdministrator();
83 AlmSettingDto bitbucketAlmSetting = db.almSettings().insertBitbucketAlmSetting();
85 expectedException.expect(IllegalArgumentException.class);
86 expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", bitbucketAlmSetting.getKey()));
89 .setParam("key", bitbucketAlmSetting.getKey())
90 .setParam("url", "https://bitbucket.enterprise.com")
91 .setParam("personalAccessToken", "98765432100")
96 public void fail_when_no_multiple_instance_allowed() {
97 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
98 UserDto user = db.users().insertUser();
99 userSession.logIn(user).setSystemAdministrator();
100 db.almSettings().insertBitbucketAlmSetting();
102 expectedException.expect(BadRequestException.class);
103 expectedException.expectMessage("A BITBUCKET setting is already defined");
106 .setParam("key", "otherKey")
107 .setParam("url", "https://bitbucket.enterprise.com")
108 .setParam("personalAccessToken", "98765432100")
113 public void fail_when_no_multiple_instance_allowed_and_bitbucket_cloud_exists() {
114 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
115 UserDto user = db.users().insertUser();
116 userSession.logIn(user).setSystemAdministrator();
117 db.almSettings().insertBitbucketCloudAlmSetting();
119 expectedException.expect(BadRequestException.class);
120 expectedException.expectMessage("A BITBUCKET_CLOUD setting is already defined");
123 .setParam("key", "otherKey")
124 .setParam("url", "https://bitbucket.enterprise.com")
125 .setParam("personalAccessToken", "98765432100")
130 public void fail_when_missing_administer_system_permission() {
131 UserDto user = db.users().insertUser();
132 userSession.logIn(user);
134 expectedException.expect(ForbiddenException.class);
137 .setParam("key", "Bitbucket Server - Dev Team")
138 .setParam("url", "https://bitbucket.enterprise.com")
139 .setParam("personalAccessToken", "98765432100")
144 public void definition() {
145 WebService.Action def = ws.getDef();
147 assertThat(def.since()).isEqualTo("8.1");
148 assertThat(def.isPost()).isTrue();
149 assertThat(def.params())
150 .extracting(WebService.Param::key, WebService.Param::isRequired)
151 .containsExactlyInAnyOrder(tuple("key", true), tuple("url", true), tuple("personalAccessToken", true));