]> source.dussan.org Git - sonarqube.git/blob
fcc4bdfda2678fc4b09555c84bb328436d69d0ce
[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.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;
37
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;
43
44 @RunWith(DataProviderRunner.class)
45 public class GlobalSystemSectionTest {
46
47   @Rule
48   public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
49
50   private MapSettings settings = new MapSettings();
51   private Server server = mock(Server.class);
52   private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
53
54   private DockerSupport dockerSupport = mock(DockerSupport.class);
55   private GlobalSystemSection underTest = new GlobalSystemSection(settings.asConfig(),
56     server, securityRealmFactory, identityProviderRepository, dockerSupport);
57
58   @Test
59   public void name_is_not_empty() {
60     assertThat(underTest.toProtobuf().getName()).isEqualTo("System");
61   }
62
63   @Test
64   public void get_realm() {
65     SecurityRealm realm = mock(SecurityRealm.class);
66     when(realm.getName()).thenReturn("LDAP");
67     when(securityRealmFactory.getRealm()).thenReturn(realm);
68
69     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
70     assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
71   }
72
73   @Test
74   public void no_realm() {
75     when(securityRealmFactory.getRealm()).thenReturn(null);
76
77     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
78     assertThat(attribute(protobuf, "External User Authentication")).isNull();
79   }
80
81   @Test
82   public void get_enabled_identity_providers() {
83     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
84       .setKey("github")
85       .setName("GitHub")
86       .setEnabled(true));
87     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
88       .setKey("bitbucket")
89       .setName("Bitbucket")
90       .setEnabled(true));
91     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
92       .setKey("disabled")
93       .setName("Disabled")
94       .setEnabled(false));
95
96     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
97     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
98   }
99
100   @Test
101   public void get_enabled_identity_providers_allowing_users_to_signup() {
102     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
103       .setKey("github")
104       .setName("GitHub")
105       .setEnabled(true)
106       .setAllowsUsersToSignUp(true));
107     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
108       .setKey("bitbucket")
109       .setName("Bitbucket")
110       .setEnabled(true)
111       .setAllowsUsersToSignUp(false));
112     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
113       .setKey("disabled")
114       .setName("Disabled")
115       .setEnabled(false)
116       .setAllowsUsersToSignUp(true));
117
118     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
119     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
120   }
121
122   @Test
123   public void get_force_authentication_defaults_to_true() {
124     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
125     assertThatAttributeIs(protobuf, "Force authentication", true);
126   }
127
128   @Test
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);
133   }
134
135   @Test
136   @UseDataProvider("trueOrFalse")
137   public void get_docker_flag(boolean flag) {
138     when(dockerSupport.isRunningInDocker()).thenReturn(flag);
139
140     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
141     assertThatAttributeIs(protobuf, "Docker", flag);
142   }
143
144   @DataProvider
145   public static Object[][] trueOrFalse() {
146     return new Object[][] {
147       {true},
148       {false},
149     };
150   }
151 }