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
|
/*
* 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.db.user;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import static org.assertj.core.api.Assertions.assertThat;
class ExternalGroupDaoIT {
private static final String GROUP_UUID = "uuid";
private static final String EXTERNAL_ID = "external_id";
private static final String EXTERNAL_IDENTITY_PROVIDER = "external_identity_provider";
private static final String PROVIDER = "provider1";
@RegisterExtension
private final DbTester db = DbTester.create();
private final DbSession dbSession = db.getSession();
private final GroupDao groupDao = db.getDbClient().groupDao();
private final ExternalGroupDao underTest = db.getDbClient().externalGroupDao();
@Test
void insert_savesExternalGroup() {
GroupDto localGroup = insertGroup(GROUP_UUID);
insertGroup("67689");
ExternalGroupDto externalGroupDto = externalGroup(GROUP_UUID, EXTERNAL_IDENTITY_PROVIDER);
underTest.insert(dbSession, externalGroupDto);
List<ExternalGroupDto> savedGroups = underTest.selectByIdentityProvider(dbSession, EXTERNAL_IDENTITY_PROVIDER);
assertThat(savedGroups)
.hasSize(1)
.contains(createExternalGroupDto(localGroup.getName(), externalGroupDto));
}
@Test
void selectByGroupUuid_shouldReturnExternalGroup() {
ExternalGroupDto expectedExternalGroupDto = new ExternalGroupDto(GROUP_UUID, EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER);
underTest.insert(dbSession, expectedExternalGroupDto);
Optional<ExternalGroupDto> actualExternalGroupDto = underTest.selectByGroupUuid(dbSession, GROUP_UUID);
assertThat(actualExternalGroupDto).isPresent();
compareExpectedAndActualExternalGroupDto(expectedExternalGroupDto, actualExternalGroupDto.get());
}
@Test
void selectByIdentityProvider_returnOnlyGroupForTheIdentityProvider() {
List<ExternalGroupDto> expectedGroups = createAndInsertExternalGroupDtos(PROVIDER, 3);
createAndInsertExternalGroupDtos("provider2", 1);
List<ExternalGroupDto> savedGroup = underTest.selectByIdentityProvider(dbSession, PROVIDER);
assertThat(savedGroup).containsExactlyInAnyOrderElementsOf(expectedGroups);
}
@Test
void selectByExternalIdAndIdentityProvider_shouldReturnOnlyMatchingExternalGroup() {
ExternalGroupDto expectedExternalGroupDto = new ExternalGroupDto(GROUP_UUID, EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER);
underTest.insert(dbSession, expectedExternalGroupDto);
underTest.insert(dbSession, new ExternalGroupDto(GROUP_UUID + "1", EXTERNAL_ID, "another_external_identity_provider"));
underTest.insert(dbSession, new ExternalGroupDto(GROUP_UUID + "2", "another_external_id", EXTERNAL_IDENTITY_PROVIDER));
underTest.insert(dbSession, new ExternalGroupDto(GROUP_UUID + "3", "whatever", "whatever"));
Optional<ExternalGroupDto> actualExternalGroupDto = underTest.selectByExternalIdAndIdentityProvider(dbSession, EXTERNAL_ID,
EXTERNAL_IDENTITY_PROVIDER);
compareExpectedAndActualExternalGroupDto(expectedExternalGroupDto, actualExternalGroupDto.get());
}
private void compareExpectedAndActualExternalGroupDto(ExternalGroupDto expectedExternalGroupDto,
ExternalGroupDto actualExternalGroupDto) {
assertThat(actualExternalGroupDto)
.usingRecursiveComparison()
.isEqualTo(expectedExternalGroupDto);
}
@Test
void deleteByGroupUuid_deletesTheGroup() {
List<ExternalGroupDto> insertedGroups = createAndInsertExternalGroupDtos(PROVIDER, 3);
ExternalGroupDto toRemove = insertedGroups.remove(0);
underTest.deleteByGroupUuid(dbSession, toRemove.groupUuid());
List<ExternalGroupDto> remainingGroups = underTest.selectByIdentityProvider(dbSession, PROVIDER);
assertThat(remainingGroups).containsExactlyInAnyOrderElementsOf(insertedGroups);
}
@Test
void deleteByExternalIdentityProvider_onlyDeletesGroupOfTheRequestedProvider() {
createAndInsertExternalGroupDtos(PROVIDER, 3);
List<ExternalGroupDto> groupsProvider2 = createAndInsertExternalGroupDtos("provider2", 3);
underTest.deleteByExternalIdentityProvider(dbSession, PROVIDER);
assertThat(underTest.selectByIdentityProvider(dbSession, PROVIDER)).isEmpty();
assertThat(underTest.selectByIdentityProvider(dbSession, "provider2")).containsExactlyInAnyOrderElementsOf(groupsProvider2);
}
@Test
void getManagedGroupsSqlFilter_whenFilterByManagedIsTrue_returnsCorrectQuery() {
String filterManagedUser = underTest.getManagedGroupSqlFilter(true);
assertThat(filterManagedUser).isEqualTo(
"(exists (select group_uuid from external_groups eg where eg.group_uuid = uuid) "
+ "or exists (select group_uuid from github_orgs_groups gog where gog.group_uuid = uuid))");
}
@Test
void getManagedGroupsSqlFilter_whenFilterByManagedIsFalse_returnsCorrectQuery() {
String filterNonManagedUser = underTest.getManagedGroupSqlFilter(false);
assertThat(filterNonManagedUser).isEqualTo(
"(not exists (select group_uuid from external_groups eg where eg.group_uuid = uuid) "
+ "and not exists (select group_uuid from github_orgs_groups gog where gog.group_uuid = uuid))");
}
private List<ExternalGroupDto> createAndInsertExternalGroupDtos(String provider, int numberOfGroups) {
List<ExternalGroupDto> expectedExternalGroupDtos = new ArrayList<>();
for (int i = 1; i <= numberOfGroups; i++) {
ExternalGroupDto externalGroupDto = externalGroup(provider + "_" + i, provider);
GroupDto localGroup = insertGroup(externalGroupDto.groupUuid());
underTest.insert(dbSession, externalGroupDto);
expectedExternalGroupDtos.add(createExternalGroupDto(localGroup.getName(), externalGroupDto));
}
return expectedExternalGroupDtos;
}
private static ExternalGroupDto externalGroup(String groupUuid, String identityProvider) {
return new ExternalGroupDto(groupUuid, "external_" + groupUuid, identityProvider);
}
private GroupDto insertGroup(String groupUuid) {
GroupDto group = new GroupDto();
group.setUuid(groupUuid);
group.setName("name" + groupUuid);
return groupDao.insert(dbSession, group);
}
private ExternalGroupDto createExternalGroupDto(String name, ExternalGroupDto externalGroupDto) {
return new ExternalGroupDto(externalGroupDto.groupUuid(), externalGroupDto.externalId(), externalGroupDto.externalIdentityProvider(),
name);
}
}
|