1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.auth.saml;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.utils.System2;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@RunWith(DataProviderRunner.class)
public class SamlSettingsTest {
private MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, SamlSettings.definitions()));
private SamlSettings underTest = new SamlSettings(settings.asConfig());
@Test
public void return_application_id() {
settings.setProperty("sonar.auth.saml.applicationId", "MyApp");
assertThat(underTest.getApplicationId()).isEqualTo("MyApp");
}
@Test
public void return_default_value_of_application_id() {
assertThat(underTest.getApplicationId()).isEqualTo("sonarqube");
}
@Test
public void return_provider_name() {
settings.setProperty("sonar.auth.saml.providerName", "MyProviderName");
assertThat(underTest.getProviderName()).isEqualTo("MyProviderName");
}
@Test
public void return_default_value_of_application_name() {
assertThat(underTest.getProviderName()).isEqualTo("SAML");
}
@Test
public void return_provider_id() {
settings.setProperty("sonar.auth.saml.applicationId", "http://localhost:8080/auth/realms/sonarqube");
assertThat(underTest.getApplicationId()).isEqualTo("http://localhost:8080/auth/realms/sonarqube");
}
@Test
public void return_login_url() {
settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080/");
assertThat(underTest.getLoginUrl()).isEqualTo("http://localhost:8080/");
settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080");
assertThat(underTest.getLoginUrl()).isEqualTo("http://localhost:8080");
}
@Test
public void return_certificate() {
settings.setProperty("sonar.auth.saml.certificate.secured", "ABCDEFG");
assertThat(underTest.getCertificate()).isEqualTo("ABCDEFG");
}
@Test
public void is_sign_requests_enabled() {
settings.setProperty("sonar.auth.saml.signature.enabled", true);
assertThat(underTest.isSignRequestsEnabled()).isTrue();
settings.setProperty("sonar.auth.saml.signature.enabled", false);
assertThat(underTest.isSignRequestsEnabled()).isFalse();
}
@Test
public void return_service_provider_certificate() {
settings.setProperty("sonar.auth.saml.sp.certificate.secured", "my_certificate");
assertThat(underTest.getServiceProviderCertificate()).isEqualTo("my_certificate");
}
@Test
public void return_service_provider_private_key() {
settings.setProperty("sonar.auth.saml.sp.privateKey.secured", "my_private_secret_private_key");
assertThat(underTest.getServiceProviderPrivateKey()).hasValue("my_private_secret_private_key");
}
@Test
public void return_user_login_attribute() {
settings.setProperty("sonar.auth.saml.user.login", "userLogin");
assertThat(underTest.getUserLogin()).isEqualTo("userLogin");
}
@Test
public void return_user_name_attribute() {
settings.setProperty("sonar.auth.saml.user.name", "userName");
assertThat(underTest.getUserName()).isEqualTo("userName");
}
@Test
public void return_user_email_attribute() {
settings.setProperty("sonar.auth.saml.user.email", "userEmail");
assertThat(underTest.getUserEmail()).contains("userEmail");
}
@Test
public void return_empty_user_email_when_no_setting() {
assertThat(underTest.getUserEmail()).isNotPresent();
}
@Test
public void return_group_name_attribute() {
settings.setProperty("sonar.auth.saml.group.name", "groupName");
assertThat(underTest.getGroupName()).contains("groupName");
}
@Test
public void return_empty_group_name_when_no_setting() {
assertThat(underTest.getGroupName()).isNotPresent();
}
@Test
public void is_enabled() {
settings.setProperty("sonar.auth.saml.applicationId", "MyApp");
settings.setProperty("sonar.auth.saml.providerId", "http://localhost:8080/auth/realms/sonarqube");
settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080/auth/realms/sonarqube/protocol/saml");
settings.setProperty("sonar.auth.saml.certificate.secured", "ABCDEFG");
settings.setProperty("sonar.auth.saml.user.login", "login");
settings.setProperty("sonar.auth.saml.user.name", "name");
settings.setProperty("sonar.auth.saml.enabled", true);
assertThat(underTest.isEnabled()).isTrue();
settings.setProperty("sonar.auth.saml.enabled", false);
assertThat(underTest.isEnabled()).isFalse();
}
@Test
public void is_enabled_using_default_values() {
settings.setProperty("sonar.auth.saml.providerId", "http://localhost:8080/auth/realms/sonarqube");
settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080/auth/realms/sonarqube/protocol/saml");
settings.setProperty("sonar.auth.saml.certificate.secured", "ABCDEFG");
settings.setProperty("sonar.auth.saml.user.login", "login");
settings.setProperty("sonar.auth.saml.user.name", "name");
settings.setProperty("sonar.auth.saml.enabled", true);
assertThat(underTest.isEnabled()).isTrue();
}
@DataProvider
public static Object[][] settingsRequiredToEnablePlugin() {
return new Object[][] {
{"sonar.auth.saml.providerId"},
{"sonar.auth.saml.loginUrl"},
{"sonar.auth.saml.certificate.secured"},
{"sonar.auth.saml.user.login"},
{"sonar.auth.saml.user.name"},
{"sonar.auth.saml.enabled"},
};
}
@Test
@UseDataProvider("settingsRequiredToEnablePlugin")
public void is_enabled_return_false_when_one_required_setting_is_missing(String setting) {
initAllSettings();
settings.setProperty(setting, (String) null);
assertThat(underTest.isEnabled()).isFalse();
}
@Test
public void fail_to_get_provider_id_when_null() {
assertThatThrownBy(() -> underTest.getProviderId())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Provider ID is missing");
}
@Test
public void fail_to_get_login_url_when_null() {
assertThatThrownBy(() -> underTest.getLoginUrl())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Login URL is missing");
}
@Test
public void fail_to_get_certificate_when_null() {
assertThatThrownBy(() -> underTest.getCertificate())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Identity provider certificate is missing");
}
@Test
public void fail_to_get_user_login_attribute_when_null() {
assertThatThrownBy(() -> underTest.getUserLogin())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("User login attribute is missing");
}
@Test
public void fail_to_get_user_name_attribute_when_null() {
assertThatThrownBy(() -> underTest.getUserName())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("User name attribute is missing");
}
private void initAllSettings() {
settings.setProperty("sonar.auth.saml.applicationId", "MyApp");
settings.setProperty("sonar.auth.saml.providerId", "http://localhost:8080/auth/realms/sonarqube");
settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080/auth/realms/sonarqube/protocol/saml");
settings.setProperty("sonar.auth.saml.certificate.secured", "ABCDEFG");
settings.setProperty("sonar.auth.saml.user.login", "login");
settings.setProperty("sonar.auth.saml.user.name", "name");
settings.setProperty("sonar.auth.saml.enabled", true);
}
}
|