]> source.dussan.org Git - sonarqube.git/blob
9814aac7ecc31923d63a7fa3d4e3387affcbbb6a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.sonar.api.config.internal.Encryption;
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.api.Assertions.assertThatThrownBy;
39 import static org.assertj.core.groups.Tuple.tuple;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 public class CreateAzureActionTest {
44
45   @Rule
46   public UserSessionRule userSession = UserSessionRule.standalone();
47   @Rule
48   public DbTester db = DbTester.create();
49
50   private final Encryption encryption = mock(Encryption.class);
51   private final MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
52
53   private WsActionTester ws = new WsActionTester(new CreateAzureAction(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", "Azure Server - Dev Team")
69       .setParam("personalAccessToken", "98765432100")
70       .setParam("url", "https://ado.sonarqube.com/")
71       .execute();
72
73     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
74       .extracting(AlmSettingDto::getKey,
75         s -> s.getDecryptedPersonalAccessToken(encryption),
76         AlmSettingDto::getUrl)
77       .containsOnly(tuple("Azure Server - Dev Team", "98765432100", "https://ado.sonarqube.com/"));
78   }
79
80   @Test
81   public void fail_when_key_is_already_used() {
82     when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
83     UserDto user = db.users().insertUser();
84     userSession.logIn(user).setSystemAdministrator();
85     AlmSettingDto azureAlmSetting = db.almSettings().insertAzureAlmSetting();
86
87     assertThatThrownBy(() -> ws.newRequest()
88       .setParam("key", azureAlmSetting.getKey())
89       .setParam("personalAccessToken", "98765432100")
90       .setParam("url", "https://ado.sonarqube.com/")
91       .execute())
92       .isInstanceOf(IllegalArgumentException.class)
93       .hasMessageContaining(String.format("An ALM setting with key '%s' already exist", azureAlmSetting.getKey()));
94   }
95
96   @Test
97   public void fail_when_no_multiple_instance_allowed() {
98     when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
99     UserDto user = db.users().insertUser();
100     userSession.logIn(user).setSystemAdministrator();
101     db.almSettings().insertAzureAlmSetting();
102
103     assertThatThrownBy(() -> ws.newRequest()
104       .setParam("key", "key")
105       .setParam("personalAccessToken", "98765432100")
106       .setParam("url", "https://ado.sonarqube.com/")
107       .execute())
108       .isInstanceOf(BadRequestException.class)
109       .hasMessageContaining("A AZURE_DEVOPS setting is already defined");
110   }
111
112   @Test
113   public void fail_when_missing_administer_system_permission() {
114     UserDto user = db.users().insertUser();
115     userSession.logIn(user);
116
117     assertThatThrownBy(() -> ws.newRequest()
118       .setParam("key", "Azure Server - Dev Team")
119       .setParam("personalAccessToken", "98765432100")
120       .execute())
121       .isInstanceOf(ForbiddenException.class);
122   }
123
124   @Test
125   public void definition() {
126     WebService.Action def = ws.getDef();
127
128     assertThat(def.since()).isEqualTo("8.1");
129     assertThat(def.isPost()).isTrue();
130     assertThat(def.params())
131       .extracting(WebService.Param::key, WebService.Param::isRequired)
132       .containsExactlyInAnyOrder(tuple("key", true), tuple("personalAccessToken", true), tuple("url", true));
133   }
134 }