]> source.dussan.org Git - sonarqube.git/blob
948abd11faf1cbc00eedeac9e03f119fce61517e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.config.internal.Encryption;
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.MultipleAlmFeature;
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;
35
36 import static java.lang.String.format;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.assertThatThrownBy;
39 import static org.assertj.core.groups.Tuple.tuple;
40 import static org.mockito.Mockito.mock;
41
42 public class UpdateBitbucketActionIT {
43
44   @Rule
45   public UserSessionRule userSession = UserSessionRule.standalone();
46   @Rule
47   public DbTester db = DbTester.create();
48
49   private final Encryption encryption = mock(Encryption.class);
50
51   private WsActionTester ws = new WsActionTester(new UpdateBitbucketAction(db.getDbClient(), userSession,
52     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
53       mock(MultipleAlmFeature.class))));
54
55   @Test
56   public void update() {
57     UserDto user = db.users().insertUser();
58     userSession.logIn(user).setSystemAdministrator();
59     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
60
61     ws.newRequest()
62       .setParam("key", almSettingDto.getKey())
63       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
64       .setParam("personalAccessToken", "10987654321")
65       .execute();
66
67     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
68       .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
69       .containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", "10987654321"));
70   }
71
72   @Test
73   public void update_with_new_key() {
74     UserDto user = db.users().insertUser();
75     userSession.logIn(user).setSystemAdministrator();
76
77     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
78
79     ws.newRequest()
80       .setParam("key", almSettingDto.getKey())
81       .setParam("newKey", "Bitbucket Server - Infra Team")
82       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
83       .setParam("personalAccessToken", "0123456789")
84       .execute();
85     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
86       .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
87       .containsOnly(tuple("Bitbucket Server - Infra Team", "https://bitbucket.enterprise-unicorn.com", "0123456789"));
88   }
89
90   @Test
91   public void update_without_pat() {
92     UserDto user = db.users().insertUser();
93     userSession.logIn(user).setSystemAdministrator();
94
95     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
96
97     ws.newRequest()
98       .setParam("key", almSettingDto.getKey())
99       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
100       .execute();
101     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
102       .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
103       .containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", almSettingDto.getDecryptedPersonalAccessToken(encryption)));
104   }
105
106   @Test
107   public void fail_when_key_does_not_match_existing_alm_setting() {
108     UserDto user = db.users().insertUser();
109     userSession.logIn(user).setSystemAdministrator();
110     
111     assertThatThrownBy(() -> ws.newRequest()
112       .setParam("key", "unknown")
113       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
114       .setParam("personalAccessToken", "0123456789")
115       .execute())
116       .isInstanceOf(NotFoundException.class)
117       .hasMessageContaining("DevOps Platform setting with key 'unknown' cannot be found");
118   }
119
120   @Test
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();
126
127     assertThatThrownBy(() -> ws.newRequest()
128       .setParam("key", almSetting1.getKey())
129       .setParam("newKey", almSetting2.getKey())
130       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
131       .setParam("personalAccessToken", "0123456789")
132       .execute())
133       .isInstanceOf(IllegalArgumentException.class)
134       .hasMessageContaining(format("An DevOps Platform setting with key '%s' already exists", almSetting2.getKey()));
135   }
136
137   @Test
138   public void fail_when_missing_administer_system_permission() {
139     UserDto user = db.users().insertUser();
140     userSession.logIn(user);
141     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
142
143     assertThatThrownBy(() -> ws.newRequest()
144       .setParam("key", almSettingDto.getKey())
145       .setParam("newKey", "Bitbucket Server - Infra Team")
146       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
147       .setParam("personalAccessToken", "0123456789")
148       .execute())
149       .isInstanceOf(ForbiddenException.class);
150   }
151
152   @Test
153   public void definition() {
154     WebService.Action def = ws.getDef();
155
156     assertThat(def.since()).isEqualTo("8.1");
157     assertThat(def.isPost()).isTrue();
158     assertThat(def.params())
159       .extracting(WebService.Param::key, WebService.Param::isRequired)
160       .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("url", true), tuple("personalAccessToken", false));
161   }
162
163 }