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.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;
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;
41 public class UpdateBitbucketActionTest {
44 public ExpectedException expectedException = ExpectedException.none();
46 public UserSessionRule userSession = UserSessionRule.standalone();
48 public DbTester db = DbTester.create();
50 private WsActionTester ws = new WsActionTester(new UpdateBitbucketAction(db.getDbClient(), userSession,
51 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
52 mock(MultipleAlmFeatureProvider.class))));
55 public void update() {
56 UserDto user = db.users().insertUser();
57 userSession.logIn(user).setSystemAdministrator();
58 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
61 .setParam("key", almSettingDto.getKey())
62 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
63 .setParam("personalAccessToken", "10987654321")
66 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
67 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
68 .containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", "10987654321"));
72 public void update_with_new_key() {
73 UserDto user = db.users().insertUser();
74 userSession.logIn(user).setSystemAdministrator();
76 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
79 .setParam("key", almSettingDto.getKey())
80 .setParam("newKey", "Bitbucket Server - Infra Team")
81 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
82 .setParam("personalAccessToken", "0123456789")
84 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
85 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
86 .containsOnly(tuple("Bitbucket Server - Infra Team", "https://bitbucket.enterprise-unicorn.com", "0123456789"));
90 public void update_without_pat() {
91 UserDto user = db.users().insertUser();
92 userSession.logIn(user).setSystemAdministrator();
94 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
97 .setParam("key", almSettingDto.getKey())
98 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
100 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
101 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
102 .containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", almSettingDto.getPersonalAccessToken()));
106 public void fail_when_key_does_not_match_existing_alm_setting() {
107 UserDto user = db.users().insertUser();
108 userSession.logIn(user).setSystemAdministrator();
110 expectedException.expect(NotFoundException.class);
111 expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
114 .setParam("key", "unknown")
115 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
116 .setParam("personalAccessToken", "0123456789")
121 public void fail_when_new_key_matches_existing_alm_setting() {
122 UserDto user = db.users().insertUser();
123 userSession.logIn(user).setSystemAdministrator();
124 AlmSettingDto almSetting1 = db.almSettings().insertBitbucketAlmSetting();
125 AlmSettingDto almSetting2 = db.almSettings().insertBitbucketAlmSetting();
127 expectedException.expect(IllegalArgumentException.class);
128 expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
131 .setParam("key", almSetting1.getKey())
132 .setParam("newKey", almSetting2.getKey())
133 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
134 .setParam("personalAccessToken", "0123456789")
139 public void fail_when_missing_administer_system_permission() {
140 UserDto user = db.users().insertUser();
141 userSession.logIn(user);
142 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
144 expectedException.expect(ForbiddenException.class);
147 .setParam("key", almSettingDto.getKey())
148 .setParam("newKey", "Bitbucket Server - Infra Team")
149 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
150 .setParam("personalAccessToken", "0123456789")
155 public void definition() {
156 WebService.Action def = ws.getDef();
158 assertThat(def.since()).isEqualTo("8.1");
159 assertThat(def.isPost()).isTrue();
160 assertThat(def.params())
161 .extracting(WebService.Param::key, WebService.Param::isRequired)
162 .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("url", true), tuple("personalAccessToken", false));