3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.monitoring.cluster;
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;
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;
47 @RunWith(DataProviderRunner.class)
48 public class GlobalSystemSectionTest {
51 public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
53 private MapSettings settings = new MapSettings();
54 private Server server = mock(Server.class);
55 private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
57 private DockerSupport dockerSupport = mock(DockerSupport.class);
59 private SonarRuntime sonarRuntime = mock(SonarRuntime.class);
60 private GlobalSystemSection underTest = new GlobalSystemSection(settings.asConfig(),
61 server, securityRealmFactory, identityProviderRepository, dockerSupport, sonarRuntime);
65 when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
69 public void name_is_not_empty() {
70 assertThat(underTest.toProtobuf().getName()).isEqualTo("System");
74 public void get_realm() {
75 SecurityRealm realm = mock(SecurityRealm.class);
76 when(realm.getName()).thenReturn("LDAP");
77 when(securityRealmFactory.getRealm()).thenReturn(realm);
79 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
80 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
84 public void no_realm() {
85 when(securityRealmFactory.getRealm()).thenReturn(null);
87 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
88 assertThat(attribute(protobuf, "External User Authentication")).isNull();
92 public void get_enabled_identity_providers() {
93 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
97 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
101 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
106 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
107 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
111 public void get_enabled_identity_providers_allowing_users_to_signup() {
112 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
116 .setAllowsUsersToSignUp(true));
117 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
119 .setName("Bitbucket")
121 .setAllowsUsersToSignUp(false));
122 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
126 .setAllowsUsersToSignUp(true));
128 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
129 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
133 public void get_force_authentication_defaults_to_true() {
134 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
135 assertThatAttributeIs(protobuf, "Force authentication", true);
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);
146 @UseDataProvider("trueOrFalse")
147 public void get_docker_flag(boolean flag) {
148 when(dockerSupport.isRunningInDocker()).thenReturn(flag);
150 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
151 assertThatAttributeIs(protobuf, "Docker", flag);
155 public void get_edition() {
156 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
157 assertThatAttributeIs(protobuf, "Edition", COMMUNITY.getLabel());
161 public static Object[][] trueOrFalse() {
162 return new Object[][] {