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