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