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.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.sonar.api.CoreProperties;
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.process.systeminfo.protobuf.ProtobufSystemInfo;
33 import org.sonar.server.authentication.IdentityProviderRepositoryRule;
34 import org.sonar.server.authentication.TestIdentityProvider;
35 import org.sonar.server.platform.DockerSupport;
36 import org.sonar.server.user.SecurityRealmFactory;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
42 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
44 @RunWith(DataProviderRunner.class)
45 public class GlobalSystemSectionTest {
48 public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
50 private MapSettings settings = new MapSettings();
51 private Server server = mock(Server.class);
52 private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
54 private DockerSupport dockerSupport = mock(DockerSupport.class);
55 private GlobalSystemSection underTest = new GlobalSystemSection(settings.asConfig(),
56 server, securityRealmFactory, identityProviderRepository, dockerSupport);
59 public void name_is_not_empty() {
60 assertThat(underTest.toProtobuf().getName()).isEqualTo("System");
64 public void get_realm() {
65 SecurityRealm realm = mock(SecurityRealm.class);
66 when(realm.getName()).thenReturn("LDAP");
67 when(securityRealmFactory.getRealm()).thenReturn(realm);
69 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
70 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
74 public void no_realm() {
75 when(securityRealmFactory.getRealm()).thenReturn(null);
77 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
78 assertThat(attribute(protobuf, "External User Authentication")).isNull();
82 public void get_enabled_identity_providers() {
83 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
87 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
91 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
96 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
97 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
101 public void get_enabled_identity_providers_allowing_users_to_signup() {
102 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
106 .setAllowsUsersToSignUp(true));
107 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
109 .setName("Bitbucket")
111 .setAllowsUsersToSignUp(false));
112 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
116 .setAllowsUsersToSignUp(true));
118 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
119 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
123 public void get_force_authentication_defaults_to_true() {
124 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
125 assertThatAttributeIs(protobuf, "Force authentication", true);
129 public void get_force_authentication() {
130 settings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false);
131 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
132 assertThatAttributeIs(protobuf, "Force authentication", false);
136 @UseDataProvider("trueOrFalse")
137 public void get_docker_flag(boolean flag) {
138 when(dockerSupport.isRunningInDocker()).thenReturn(flag);
140 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
141 assertThatAttributeIs(protobuf, "Docker", flag);
145 public static Object[][] trueOrFalse() {
146 return new Object[][] {