]> source.dussan.org Git - sonarqube.git/blob
3b9f38757e1734272f812b5b4174e76b65b30abf
[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.platform.monitoring;
21
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.sonar.api.CoreProperties;
30 import org.sonar.api.SonarEdition;
31 import org.sonar.api.SonarRuntime;
32 import org.sonar.api.config.internal.MapSettings;
33 import org.sonar.api.platform.Server;
34 import org.sonar.api.security.SecurityRealm;
35 import org.sonar.api.utils.log.LoggerLevel;
36 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
37 import org.sonar.server.authentication.IdentityProviderRepositoryRule;
38 import org.sonar.server.authentication.TestIdentityProvider;
39 import org.sonar.server.log.ServerLogging;
40 import org.sonar.server.platform.DockerSupport;
41 import org.sonar.server.platform.OfficialDistribution;
42 import org.sonar.server.user.SecurityRealmFactory;
43
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.sonar.api.SonarEdition.COMMUNITY;
48 import static org.sonar.api.SonarEdition.DATACENTER;
49 import static org.sonar.api.SonarEdition.DEVELOPER;
50 import static org.sonar.api.SonarEdition.ENTERPRISE;
51 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
52 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
53
54 @RunWith(DataProviderRunner.class)
55 public class StandaloneSystemSectionTest {
56
57   @Rule
58   public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
59
60   private MapSettings settings = new MapSettings();
61   private Server server = mock(Server.class);
62   private ServerLogging serverLogging = mock(ServerLogging.class);
63   private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
64   private OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
65   private DockerSupport dockerSupport = mock(DockerSupport.class);
66
67   private SonarRuntime sonarRuntime = mock(SonarRuntime.class);
68
69   private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server,
70     serverLogging, officialDistribution, dockerSupport, sonarRuntime);
71
72   @Before
73   public void setUp() {
74     when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
75     when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
76   }
77
78   @Test
79   public void name_is_not_empty() {
80     assertThat(underTest.name()).isNotEmpty();
81   }
82
83   @Test
84   public void test_getServerId() {
85     when(server.getId()).thenReturn("ABC");
86     assertThat(underTest.getServerId()).isEqualTo("ABC");
87   }
88
89   @Test
90   public void official_distribution() {
91     when(officialDistribution.check()).thenReturn(true);
92
93     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
94     assertThatAttributeIs(protobuf, "Official Distribution", true);
95   }
96
97   @Test
98   public void not_an_official_distribution() {
99     when(officialDistribution.check()).thenReturn(false);
100
101     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
102     assertThatAttributeIs(protobuf, "Official Distribution", false);
103   }
104
105   @Test
106   public void get_realm() {
107     SecurityRealm realm = mock(SecurityRealm.class);
108     when(realm.getName()).thenReturn("LDAP");
109     when(securityRealmFactory.getRealm()).thenReturn(realm);
110
111     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
112     assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
113   }
114
115   @Test
116   public void no_realm() {
117     when(securityRealmFactory.getRealm()).thenReturn(null);
118
119     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
120     assertThat(attribute(protobuf, "External User Authentication")).isNull();
121   }
122
123   @Test
124   public void get_enabled_identity_providers() {
125     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
126       .setKey("github")
127       .setName("GitHub")
128       .setEnabled(true));
129     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
130       .setKey("bitbucket")
131       .setName("Bitbucket")
132       .setEnabled(true));
133     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
134       .setKey("disabled")
135       .setName("Disabled")
136       .setEnabled(false));
137
138     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
139     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
140   }
141
142   @Test
143   public void get_enabled_identity_providers_allowing_users_to_signup() {
144     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
145       .setKey("github")
146       .setName("GitHub")
147       .setEnabled(true)
148       .setAllowsUsersToSignUp(true));
149     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
150       .setKey("bitbucket")
151       .setName("Bitbucket")
152       .setEnabled(true)
153       .setAllowsUsersToSignUp(false));
154     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
155       .setKey("disabled")
156       .setName("Disabled")
157       .setEnabled(false)
158       .setAllowsUsersToSignUp(true));
159
160     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
161     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
162   }
163
164   @Test
165   public void return_nb_of_processors() {
166     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
167     assertThat(attribute(protobuf, "Processors").getLongValue()).isPositive();
168   }
169
170   @Test
171   public void get_force_authentication_defaults_to_true() {
172     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
173     assertThatAttributeIs(protobuf, "Force authentication", true);
174   }
175
176   @Test
177   public void get_force_authentication() {
178     settings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false);
179     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
180     assertThatAttributeIs(protobuf, "Force authentication", false);
181   }
182
183   @Test
184   @UseDataProvider("trueOrFalse")
185   public void return_docker_flag_from_DockerSupport(boolean flag) {
186     when(dockerSupport.isRunningInDocker()).thenReturn(flag);
187     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
188     assertThat(attribute(protobuf, "Docker").getBooleanValue()).isEqualTo(flag);
189   }
190
191   @Test
192   @UseDataProvider("editions")
193   public void get_edition(SonarEdition sonarEdition, String editionLabel) {
194     when(sonarRuntime.getEdition()).thenReturn(sonarEdition);
195     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
196     assertThatAttributeIs(protobuf, "Edition", editionLabel);
197   }
198
199   @DataProvider
200   public static Object[][] trueOrFalse() {
201     return new Object[][] {
202       {true},
203       {false}
204     };
205   }
206
207   @DataProvider
208   public static Object[][] editions() {
209     return new Object[][] {
210       {COMMUNITY, COMMUNITY.getLabel()},
211       {DEVELOPER, DEVELOPER.getLabel()},
212       {ENTERPRISE, ENTERPRISE.getLabel()},
213       {DATACENTER, DATACENTER.getLabel()},
214     };
215   }
216 }