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
|
/*
* 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 java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.sonar.api.server.authentication.UserIdentity;
import org.springframework.security.saml2.provider.service.authentication.DefaultSaml2AuthenticatedPrincipal;
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class PrincipalToUserIdentityConverterTest {
@Mock
private SamlSettings samlSettings;
@InjectMocks
private PrincipalToUserIdentityConverter converter;
@BeforeEach
public void setUp() {
lenient().when(samlSettings.getUserLogin()).thenReturn("login");
lenient().when(samlSettings.getUserName()).thenReturn("name");
lenient().when(samlSettings.getUserEmail()).thenReturn(Optional.of("email"));
lenient().when(samlSettings.getGroupName()).thenReturn(Optional.of("group"));
}
@Test
void convertToUserIdentity_whenLoginIsNull_throws() {
Saml2AuthenticatedPrincipal principal = mock();
assertThatIllegalStateException()
.isThrownBy(() -> converter.convertToUserIdentity(principal))
.withMessage("%s is missing".formatted(samlSettings.getUserLogin()));
}
@Test
void convertToUserIdentity_whenNameIsNull_throws() {
Saml2AuthenticatedPrincipal principal = mock();
when(principal.getFirstAttribute("login")).thenReturn("mylogin");
assertThatIllegalStateException()
.isThrownBy(() -> converter.convertToUserIdentity(principal))
.withMessage("%s is missing".formatted(samlSettings.getUserName()));
}
@Test
void convertToUserIdentity_whenOnlyLoginAndNameSettingConfigured_succeeds() {
when(samlSettings.getUserEmail()).thenReturn(Optional.empty());
when(samlSettings.getGroupName()).thenReturn(Optional.empty());
Saml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal("unused", Map.of("name", List.of("myname"), "login", List.of("mylogin")));
UserIdentity userIdentity = converter.convertToUserIdentity(principal);
assertThat(userIdentity.getProviderLogin()).isEqualTo("mylogin");
assertThat(userIdentity.getName()).isEqualTo("myname");
assertThat(userIdentity.getEmail()).isNull();
assertThat(userIdentity.getGroups()).isEmpty();
}
@Test
void convertToUserIdentity_whenAllSettingConfiguredButNoValue_succeeds() {
Saml2AuthenticatedPrincipal principal = new DefaultSaml2AuthenticatedPrincipal("unused", Map.of("name", List.of("myname"), "login", List.of("mylogin")));
UserIdentity userIdentity = converter.convertToUserIdentity(principal);
assertThat(userIdentity.getProviderLogin()).isEqualTo("mylogin");
assertThat(userIdentity.getName()).isEqualTo("myname");
assertThat(userIdentity.getEmail()).isNull();
assertThat(userIdentity.getGroups()).isEmpty();
}
@Test
void convertToUserIdentity_whenAllSettingConfiguredAndNullValuesForEmailAndGroup_succeeds() {
List<Object> listWithNull = new ArrayList<>();
listWithNull.add(null);
Saml2AuthenticatedPrincipal principal = buildPrincipal("myname", "mylogin", listWithNull, listWithNull);
UserIdentity userIdentity = converter.convertToUserIdentity(principal);
assertThat(userIdentity.getProviderLogin()).isEqualTo("mylogin");
assertThat(userIdentity.getName()).isEqualTo("myname");
assertThat(userIdentity.getEmail()).isNull();
assertThat(userIdentity.getGroups()).isEmpty();
}
@Test
void convertToUserIdentity_whenAllSettingConfiguredAndEmptyValueForEmailAndGroup_succeeds() {
Saml2AuthenticatedPrincipal principal = buildPrincipal("myname", "mylogin", List.of(), List.of());
UserIdentity userIdentity = converter.convertToUserIdentity(principal);
assertThat(userIdentity.getProviderLogin()).isEqualTo("mylogin");
assertThat(userIdentity.getName()).isEqualTo("myname");
assertThat(userIdentity.getEmail()).isNull();
assertThat(userIdentity.getGroups()).isEmpty();
}
@Test
void convertToUserIdentity_whenAllSettingConfiguredAndValuePresent_succeeds() {
Saml2AuthenticatedPrincipal principal = buildPrincipal("myname", "mylogin", List.of("myemail"), List.of("mygroup"));
UserIdentity userIdentity = converter.convertToUserIdentity(principal);
assertThat(userIdentity.getProviderLogin()).isEqualTo("mylogin");
assertThat(userIdentity.getName()).isEqualTo("myname");
assertThat(userIdentity.getEmail()).isEqualTo("myemail");
assertThat(userIdentity.getGroups()).containsExactly("mygroup");
}
@Test
void convertToUserIdentity_whenAllSettingConfiguredAndMultipleEmails_takesFirstOne() {
Saml2AuthenticatedPrincipal principal = buildPrincipal("myname", "mylogin", List.of("myemail", "myemail2"), List.of());
UserIdentity userIdentity = converter.convertToUserIdentity(principal);
assertThat(userIdentity.getProviderLogin()).isEqualTo("mylogin");
assertThat(userIdentity.getName()).isEqualTo("myname");
assertThat(userIdentity.getEmail()).isEqualTo("myemail");
}
@Test
void convertToUserIdentity_whenAllSettingConfiguredAndMultipleGroups_mapsThemAll() {
Saml2AuthenticatedPrincipal principal = buildPrincipal("myname", "mylogin", List.of(), List.of("group1", "group2"));
UserIdentity userIdentity = converter.convertToUserIdentity(principal);
assertThat(userIdentity.getProviderLogin()).isEqualTo("mylogin");
assertThat(userIdentity.getName()).isEqualTo("myname");
assertThat(userIdentity.getGroups()).containsExactlyInAnyOrder("group1", "group2");
}
private Saml2AuthenticatedPrincipal buildPrincipal(String name, String login, List<Object> emails, List<Object> groups) {
return new DefaultSaml2AuthenticatedPrincipal("unused",
Map.of("name", List.of(name),
"login", List.of(login),
"email", emails,
"group", groups)
);
}
}
|