]> source.dussan.org Git - sonarqube.git/blob
91c2b435aa65dceb4bf870020985d22681315d00
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.cluster;
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.SonarRuntime;
31 import org.sonar.api.config.internal.MapSettings;
32 import org.sonar.api.platform.Server;
33 import org.sonar.api.security.SecurityRealm;
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.platform.DockerSupport;
38 import org.sonar.server.platform.StatisticsSupport;
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.api.SonarEdition.COMMUNITY;
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 GlobalSystemSectionTest {
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 SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
57
58   private DockerSupport dockerSupport = mock(DockerSupport.class);
59   private StatisticsSupport statisticsSupport = mock(StatisticsSupport.class);
60
61   private SonarRuntime sonarRuntime = mock(SonarRuntime.class);
62
63   private GlobalSystemSection underTest = new GlobalSystemSection(settings.asConfig(),
64     server, securityRealmFactory, identityProviderRepository, dockerSupport, statisticsSupport, sonarRuntime);
65
66   @Before
67   public void setUp() {
68     when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
69   }
70
71   @Test
72   public void name_is_not_empty() {
73     assertThat(underTest.toProtobuf().getName()).isEqualTo("System");
74   }
75
76   @Test
77   public void get_realm() {
78     SecurityRealm realm = mock(SecurityRealm.class);
79     when(realm.getName()).thenReturn("LDAP");
80     when(securityRealmFactory.getRealm()).thenReturn(realm);
81
82     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
83     assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
84   }
85
86   @Test
87   public void no_realm() {
88     when(securityRealmFactory.getRealm()).thenReturn(null);
89
90     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
91     assertThat(attribute(protobuf, "External User Authentication")).isNull();
92   }
93
94   @Test
95   public void get_enabled_identity_providers() {
96     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
97       .setKey("github")
98       .setName("GitHub")
99       .setEnabled(true));
100     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
101       .setKey("bitbucket")
102       .setName("Bitbucket")
103       .setEnabled(true));
104     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
105       .setKey("disabled")
106       .setName("Disabled")
107       .setEnabled(false));
108
109     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
110     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
111   }
112
113   @Test
114   public void get_enabled_identity_providers_allowing_users_to_signup() {
115     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
116       .setKey("github")
117       .setName("GitHub")
118       .setEnabled(true)
119       .setAllowsUsersToSignUp(true));
120     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
121       .setKey("bitbucket")
122       .setName("Bitbucket")
123       .setEnabled(true)
124       .setAllowsUsersToSignUp(false));
125     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
126       .setKey("disabled")
127       .setName("Disabled")
128       .setEnabled(false)
129       .setAllowsUsersToSignUp(true));
130
131     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
132     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
133   }
134
135   @Test
136   public void get_force_authentication_defaults_to_true() {
137     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
138     assertThatAttributeIs(protobuf, "Force authentication", true);
139   }
140
141   @Test
142   public void get_force_authentication() {
143     settings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false);
144     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
145     assertThatAttributeIs(protobuf, "Force authentication", false);
146   }
147
148   @Test
149   public void return_Lines_of_Codes_from_StatisticsSupport(){
150     when(statisticsSupport.getLinesOfCode()).thenReturn(17752L);
151     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
152     assertThatAttributeIs(protobuf,"Lines of Code", 17752L);
153   }
154
155   @Test
156   @UseDataProvider("trueOrFalse")
157   public void get_docker_flag(boolean flag) {
158     when(dockerSupport.isRunningInDocker()).thenReturn(flag);
159
160     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
161     assertThatAttributeIs(protobuf, "Docker", flag);
162   }
163
164   @Test
165   public void get_edition() {
166     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
167     assertThatAttributeIs(protobuf, "Edition", COMMUNITY.getLabel());
168   }
169
170   @DataProvider
171   public static Object[][] trueOrFalse() {
172     return new Object[][] {
173       {true},
174       {false},
175     };
176   }
177 }