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