]> source.dussan.org Git - sonarqube.git/blob
d97d31af8ed8d15328cde965bbc5bc91c8898d5a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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;
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.exceptions.ForbiddenException;
30 import org.sonar.server.tester.UserSessionRule;
31 import org.sonar.server.ws.WsActionTester;
32 import org.sonarqube.ws.AlmSettings.AlmSettingGithub;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
36 import static org.junit.rules.ExpectedException.none;
37 import static org.sonar.server.tester.UserSessionRule.standalone;
38 import static org.sonar.test.JsonAssert.assertJson;
39 import static org.sonarqube.ws.AlmSettings.ListDefinitionsWsResponse;
40
41 public class ListDefinitionsActionTest {
42
43   @Rule
44   public ExpectedException expectedException = none();
45   @Rule
46   public UserSessionRule userSession = standalone();
47   @Rule
48   public DbTester db = DbTester.create();
49
50   private WsActionTester ws = new WsActionTester(new ListDefinitionsAction(db.getDbClient(), userSession));
51
52   @Test
53   public void list_github_settings() {
54     UserDto user = db.users().insertUser();
55     userSession.logIn(user).setSystemAdministrator();
56     AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting();
57     AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting();
58
59     ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
60
61     assertThat(wsResponse.getGithubList())
62       .extracting(AlmSettingGithub::getKey, AlmSettingGithub::getUrl, AlmSettingGithub::getAppId, AlmSettingGithub::getPrivateKey)
63       .containsExactlyInAnyOrder(
64         tuple(almSetting1.getKey(), almSetting1.getUrl(), almSetting1.getAppId(), almSetting1.getPrivateKey()),
65         tuple(almSetting2.getKey(), almSetting2.getUrl(), almSetting2.getAppId(), almSetting2.getPrivateKey()));
66   }
67
68   @Test
69   public void return_empty_list_when_no_settings() {
70     UserDto user = db.users().insertUser();
71     userSession.logIn(user).setSystemAdministrator();
72
73     ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
74
75     assertThat(wsResponse.getGithubList()).isEmpty();
76   }
77
78   @Test
79   public void fail_when_user_is_not_system_administrator() {
80     UserDto user = db.users().insertUser();
81     userSession.logIn(user);
82     db.almSettings().insertGitHubAlmSetting();
83
84     expectedException.expect(ForbiddenException.class);
85
86     ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
87   }
88
89   @Test
90   public void json_example() {
91     UserDto user = db.users().insertUser();
92     userSession.logIn(user).setSystemAdministrator();
93     db.almSettings().insertGitHubAlmSetting(
94       almSettingDto -> almSettingDto
95         .setKey("GitHub Server - Dev Team")
96         .setUrl("https://github.enterprise.com")
97         .setAppId("12345")
98         .setPrivateKey("54684654"));
99
100     db.almSettings().insertAzureAlmSetting(
101       a -> a.setKey("Azure Devops Server - Dev Team")
102       .setPersonalAccessToken("12345")
103     );
104     String response = ws.newRequest().execute().getInput();
105
106     assertJson(response).isSimilarTo(getClass().getResource("list_definitions-example.json"));
107   }
108
109   @Test
110   public void definition() {
111     WebService.Action def = ws.getDef();
112
113     assertThat(def.since()).isEqualTo("8.1");
114     assertThat(def.params()).isEmpty();
115     assertThat(def.isPost()).isFalse();
116   }
117 }