]> source.dussan.org Git - sonarqube.git/blob
d5f93eeae7035bba6134543c9697366f04c62a1a
[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.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;
36
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;
41
42 public class CreateGithubActionTest {
43
44   @Rule
45   public ExpectedException expectedException = ExpectedException.none();
46   @Rule
47   public UserSessionRule userSession = UserSessionRule.standalone();
48   @Rule
49   public DbTester db = DbTester.create();
50
51   private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
52
53   private WsActionTester ws = new WsActionTester(new CreateGithubAction(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", "GitHub Server - Dev Team")
69       .setParam("url", "https://github.enterprise.com")
70       .setParam("appId", "12345")
71       .setParam("privateKey", "678910")
72       .setParam("clientId", "client_1234")
73       .setParam("clientSecret", "client_so_secret")
74       .execute();
75
76     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
77       .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
78       .containsOnly(tuple("GitHub Server - Dev Team", "https://github.enterprise.com", "12345", "678910", "client_1234", "client_so_secret"));
79   }
80
81   @Test
82   public void remove_trailing_slash() {
83     UserDto user = db.users().insertUser();
84     userSession.logIn(user).setSystemAdministrator();
85
86     ws.newRequest()
87       .setParam("key", "GitHub Server - Dev Team")
88       .setParam("url", "https://github.enterprise.com/")
89       .setParam("appId", "12345")
90       .setParam("privateKey", "678910")
91       .setParam("clientId", "client_1234")
92       .setParam("clientSecret", "client_so_secret")
93       .execute();
94
95     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
96       .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret)
97       .containsOnly(tuple("GitHub Server - Dev Team", "https://github.enterprise.com", "12345", "678910", "client_1234", "client_so_secret"));
98   }
99
100   @Test
101   public void fail_when_key_is_already_used() {
102     when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
103     UserDto user = db.users().insertUser();
104     userSession.logIn(user).setSystemAdministrator();
105     AlmSettingDto gitHubAlmSetting = db.almSettings().insertGitHubAlmSetting();
106
107     expectedException.expect(IllegalArgumentException.class);
108     expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", gitHubAlmSetting.getKey()));
109
110     ws.newRequest()
111       .setParam("key", gitHubAlmSetting.getKey())
112       .setParam("url", "https://github.enterprise.com")
113       .setParam("appId", "12345")
114       .setParam("privateKey", "678910")
115       .setParam("clientId", "client_1234")
116       .setParam("clientSecret", "client_so_secret")
117       .execute();
118   }
119
120   @Test
121   public void fail_when_no_multiple_instance_allowed() {
122     when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
123     UserDto user = db.users().insertUser();
124     userSession.logIn(user).setSystemAdministrator();
125     db.almSettings().insertGitHubAlmSetting();
126
127     expectedException.expect(BadRequestException.class);
128     expectedException.expectMessage("A GITHUB setting is already defined");
129
130     ws.newRequest()
131       .setParam("key", "key")
132       .setParam("url", "https://github.enterprise.com")
133       .setParam("appId", "12345")
134       .setParam("privateKey", "678910")
135       .setParam("clientId", "client_1234")
136       .setParam("clientSecret", "client_so_secret")
137       .execute();
138   }
139
140   @Test
141   public void fail_when_missing_administer_system_permission() {
142     UserDto user = db.users().insertUser();
143     userSession.logIn(user);
144
145     expectedException.expect(ForbiddenException.class);
146
147     ws.newRequest()
148       .setParam("key", "GitHub Server - Dev Team")
149       .setParam("url", "https://github.enterprise.com")
150       .setParam("appId", "12345")
151       .setParam("privateKey", "678910")
152       .setParam("clientId", "client_1234")
153       .setParam("clientSecret", "client_so_secret")
154       .execute();
155   }
156
157   @Test
158   public void definition() {
159     WebService.Action def = ws.getDef();
160
161     assertThat(def.since()).isEqualTo("8.1");
162     assertThat(def.isPost()).isTrue();
163     assertThat(def.params())
164       .extracting(WebService.Param::key, WebService.Param::isRequired)
165       .containsExactlyInAnyOrder(tuple("key", true), tuple("url", true), tuple("appId", true), tuple("privateKey", true), tuple("clientId", true), tuple("clientSecret", true));
166   }
167 }