]> source.dussan.org Git - sonarqube.git/blob
f05b53c083f15e6264f5e70385fb26d637a1545d
[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.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;
35
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;
40
41 public class UpdateBitbucketActionTest {
42
43   @Rule
44   public ExpectedException expectedException = ExpectedException.none();
45   @Rule
46   public UserSessionRule userSession = UserSessionRule.standalone();
47   @Rule
48   public DbTester db = DbTester.create();
49
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))));
53
54   @Test
55   public void update() {
56     UserDto user = db.users().insertUser();
57     userSession.logIn(user).setSystemAdministrator();
58     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
59
60     ws.newRequest()
61       .setParam("key", almSettingDto.getKey())
62       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
63       .setParam("personalAccessToken", "10987654321")
64       .execute();
65
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"));
69   }
70
71   @Test
72   public void update_with_new_key() {
73     UserDto user = db.users().insertUser();
74     userSession.logIn(user).setSystemAdministrator();
75
76     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
77
78     ws.newRequest()
79       .setParam("key", almSettingDto.getKey())
80       .setParam("newKey", "Bitbucket Server - Infra Team")
81       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
82       .setParam("personalAccessToken", "0123456789")
83       .execute();
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"));
87   }
88
89   @Test
90   public void update_without_pat() {
91     UserDto user = db.users().insertUser();
92     userSession.logIn(user).setSystemAdministrator();
93
94     AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
95
96     ws.newRequest()
97       .setParam("key", almSettingDto.getKey())
98       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
99       .execute();
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()));
103   }
104
105   @Test
106   public void fail_when_key_does_not_match_existing_alm_setting() {
107     UserDto user = db.users().insertUser();
108     userSession.logIn(user).setSystemAdministrator();
109
110     expectedException.expect(NotFoundException.class);
111     expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
112
113     ws.newRequest()
114       .setParam("key", "unknown")
115       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
116       .setParam("personalAccessToken", "0123456789")
117       .execute();
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     expectedException.expect(IllegalArgumentException.class);
128     expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
129
130     ws.newRequest()
131       .setParam("key", almSetting1.getKey())
132       .setParam("newKey", almSetting2.getKey())
133       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
134       .setParam("personalAccessToken", "0123456789")
135       .execute();
136   }
137
138   @Test
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();
143
144     expectedException.expect(ForbiddenException.class);
145
146     ws.newRequest()
147       .setParam("key", almSettingDto.getKey())
148       .setParam("newKey", "Bitbucket Server - Infra Team")
149       .setParam("url", "https://bitbucket.enterprise-unicorn.com")
150       .setParam("personalAccessToken", "0123456789")
151       .execute();
152   }
153
154   @Test
155   public void definition() {
156     WebService.Action def = ws.getDef();
157
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));
163   }
164
165 }