]> source.dussan.org Git - sonarqube.git/blob
adffc0b91a234ef0155d6abf013b2c16c658d07e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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 org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.impl.config.MapSettings;
25 import org.sonar.api.platform.Server;
26 import org.sonar.api.security.SecurityRealm;
27 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
28 import org.sonar.server.authentication.IdentityProviderRepositoryRule;
29 import org.sonar.server.authentication.TestIdentityProvider;
30 import org.sonar.server.user.SecurityRealmFactory;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
36 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
37
38 public class GlobalSystemSectionTest {
39
40   @Rule
41   public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
42
43   private MapSettings settings = new MapSettings();
44   private Server server = mock(Server.class);
45   private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
46
47   private GlobalSystemSection underTest = new GlobalSystemSection(settings.asConfig(),
48     server, securityRealmFactory, identityProviderRepository);
49
50   @Test
51   public void name_is_not_empty() {
52     assertThat(underTest.toProtobuf().getName()).isEqualTo("System");
53   }
54
55   @Test
56   public void get_realm() {
57     SecurityRealm realm = mock(SecurityRealm.class);
58     when(realm.getName()).thenReturn("LDAP");
59     when(securityRealmFactory.getRealm()).thenReturn(realm);
60
61     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
62     assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
63   }
64
65   @Test
66   public void no_realm() {
67     when(securityRealmFactory.getRealm()).thenReturn(null);
68
69     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
70     assertThat(attribute(protobuf, "External User Authentication")).isNull();
71   }
72
73   @Test
74   public void get_enabled_identity_providers() {
75     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
76       .setKey("github")
77       .setName("GitHub")
78       .setEnabled(true));
79     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
80       .setKey("bitbucket")
81       .setName("Bitbucket")
82       .setEnabled(true));
83     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
84       .setKey("disabled")
85       .setName("Disabled")
86       .setEnabled(false));
87
88     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
89     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
90   }
91
92   @Test
93   public void get_enabled_identity_providers_allowing_users_to_signup() {
94     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
95       .setKey("github")
96       .setName("GitHub")
97       .setEnabled(true)
98       .setAllowsUsersToSignUp(true));
99     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
100       .setKey("bitbucket")
101       .setName("Bitbucket")
102       .setEnabled(true)
103       .setAllowsUsersToSignUp(false));
104     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
105       .setKey("disabled")
106       .setName("Disabled")
107       .setEnabled(false)
108       .setAllowsUsersToSignUp(true));
109
110     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
111     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
112   }
113 }