-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
MakeComponentsPrivateBasedOnPermissionsTest.java
492 lines (402 loc) · 19.4 KB
/
MakeComponentsPrivateBasedOnPermissionsTest.java
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* SonarQube
* Copyright (C) 2009-2017 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.server.platform.db.migration.version.v64;
import java.sql.SQLException;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.CoreDbTester;
import static java.lang.String.valueOf;
import static org.assertj.core.api.Assertions.assertThat;
public class MakeComponentsPrivateBasedOnPermissionsTest {
private static final String ROLE_USER = "user";
private static final String ROLE_CODEVIEWER = "codeviewer";
private static final String PROJECT_QUALIFIER = "TRK";
private static final String VIEW_QUALIFIER = "VW";
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(MakeComponentsPrivateBasedOnPermissionsTest.class, "projects_and_group_roles_and_user_roles.sql");
private final Random random = new Random();
private final String randomPublicConditionRole = random.nextBoolean() ? ROLE_CODEVIEWER : ROLE_USER;
private final String randomQualifier = random.nextBoolean() ? PROJECT_QUALIFIER : VIEW_QUALIFIER;
private final String randomRole = "role_" + random.nextInt(12);
private MakeComponentsPrivateBasedOnPermissions underTest = new MakeComponentsPrivateBasedOnPermissions(db.database());
@Test
public void execute_does_nothing_on_empty_tables() throws SQLException {
underTest.execute();
}
@Test
public void execute_makes_project_private_if_Anyone_has_only_user_permission_and_project_has_at_least_one_other_group_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_USER, pId1, null);
insertGroupPermission("foo", pId1, random.nextInt(10));
underTest.execute();
assertThat(isPrivate("p1")).isTrue();
}
@Test
public void execute_makes_project_private_if_Anyone_has_only_user_permission_and_project_has_one_user_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_USER, pId1, null);
insertUserPermission("foo", pId1, random.nextInt(10));
underTest.execute();
assertThat(isPrivate("p1")).isTrue();
}
@Test
public void execute_keeps_project_public_if_Anyone_has_only_user_permission_and_project_has_no_user_nor_other_group_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_USER, pId1, null);
underTest.execute();
assertThat(isPrivate("p1")).isFalse();
}
@Test
public void execute_makes_project_private_if_Anyone_has_only_codeviewer_permission_and_project_has_one_other_group_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_CODEVIEWER, pId1, null);
insertGroupPermission("foo", pId1, random.nextInt(10));
underTest.execute();
assertThat(isPrivate("p1")).isTrue();
}
@Test
public void execute_makes_project_private_if_Anyone_has_only_codeviewer_permission_and_project_has_one_user_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_CODEVIEWER, pId1, null);
insertUserPermission("foo", pId1, random.nextInt(10));
underTest.execute();
assertThat(isPrivate("p1")).isTrue();
}
@Test
public void execute_keeps_project_public_if_Anyone_has_only_codeviewer_permission_and_project_has_no_user_nor_other_group_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_CODEVIEWER, pId1, null);
underTest.execute();
assertThat(isPrivate("p1")).isFalse();
}
@Test
public void execute_makes_project_private_if_Anyone_has_neither_user_nor_codeviewer_permission_and_project_has_one_other_group_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(randomRole, pId1, null);
insertGroupPermission("foo", pId1, random.nextInt(10));
underTest.execute();
assertThat(isPrivate("p1")).isTrue();
}
@Test
public void execute_makes_project_private_if_Anyone_has_neither_user_nor_codeviewer_permission_and_project_has_one_user_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(randomRole, pId1, null);
insertUserPermission("foo", pId1, random.nextInt(10));
underTest.execute();
assertThat(isPrivate("p1")).isTrue();
}
@Test
public void execute_keeps_project_public_if_Anyone_has_neither_user_nor_codeviewer_permission_and_project_has_no_user_nor_other_group_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission("foo", pId1, null);
underTest.execute();
assertThat(isPrivate("p1")).isFalse();
}
@Test
public void execute_keeps_project_public_if_Anyone_has_both_user_and_codeviewer_permission_and_project_has_one_other_group_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_USER, pId1, null);
insertGroupPermission(ROLE_CODEVIEWER, pId1, null);
insertGroupPermission("foo", pId1, random.nextInt(10));
underTest.execute();
assertThat(isPrivate("p1")).isFalse();
}
@Test
public void execute_keeps_project_public_if_Anyone_has_both_user_and_codeviewer_permission_and_project_has_user_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_USER, pId1, null);
insertGroupPermission(ROLE_CODEVIEWER, pId1, null);
insertUserPermission("foo", pId1, random.nextInt(10));
underTest.execute();
assertThat(isPrivate("p1")).isFalse();
}
@Test
public void execute_keeps_project_public_if_Anyone_has_both_user_and_codeviewer_permission_and_project_has_no_user_nor_other_group_permission() throws SQLException {
long pId1 = insertRootComponent("p1", false);
insertGroupPermission(ROLE_USER, pId1, null);
insertGroupPermission(ROLE_CODEVIEWER, pId1, null);
underTest.execute();
assertThat(isPrivate("p1")).isFalse();
}
@Test
public void execute_keeps_project_public_if_it_has_no_user_nor_group_permission_at_all() throws SQLException {
insertRootComponent("p1", false);
underTest.execute();
assertThat(isPrivate("p1")).isFalse();
}
@Test
public void execute_does_not_change_private_projects_to_public_when_they_actually_should_be() throws SQLException {
long p1Id = insertRootComponent("p1", true); // both user and codeviewer
long p2Id = insertRootComponent("p2", true); // only user but no other permission
long p3Id = insertRootComponent("p3", true); // only codeviewer but no other permission
long p4Id = insertRootComponent("p4", true); // neither codeviewer nor user but no other permission
insertGroupPermission(ROLE_CODEVIEWER, p1Id, null);
insertGroupPermission(ROLE_USER, p1Id, null);
insertGroupPermission(ROLE_CODEVIEWER, p2Id, null);
insertGroupPermission(ROLE_USER, p3Id, null);
insertGroupPermission(randomRole, p4Id, null);
underTest.execute();
assertThat(isPrivate("p1")).isTrue();
assertThat(isPrivate("p2")).isTrue();
assertThat(isPrivate("p3")).isTrue();
assertThat(isPrivate("p4")).isTrue();
}
@Test
public void execute_changes_non_root_rows_to_private_based_on_permissions_of_their_root_row() throws SQLException {
// root stays public, children are unchanged
long pId1 = insertRootComponent("root1", false);
insertGroupPermission(randomPublicConditionRole, pId1, null);
insertComponent("u1", "root1", false);
// root becomes privates, children are changed accordingly
long pId2 = insertRootComponent("root2", false);
int someUserId = random.nextInt(50);
insertGroupPermission(randomRole, pId2, someUserId);
insertComponent("u2", "root2", false);
insertComponent("u3", "root2", true);
underTest.execute();
assertThat(isPrivate("root1")).isFalse();
assertThat(isPrivate("u1")).isFalse();
assertThat(isPrivate("root2")).isTrue();
assertThat(isPrivate("u2")).isTrue();
assertThat(isPrivate("u3")).isTrue();
}
@Test
public void execute_does_not_fix_inconsistencies_of_non_root_rows_if_root_stays_public_or_is_already_private() throws SQLException {
// root stays public, children are unchanged
long pId1 = insertRootComponent("root1", false);
insertGroupPermission(randomPublicConditionRole, pId1, null);
insertComponent("u1", "root1", false);
insertComponent("u2", "root1", true); // inconsistent information is not fixed
// root is already private but children are inconsistent => not fixed
insertRootComponent("root2", true);
insertGroupPermission(randomPublicConditionRole, pId1, null);
insertComponent("u3", "root2", false);
insertComponent("u4", "root2", true);
underTest.execute();
assertThat(isPrivate("root1")).isFalse();
assertThat(isPrivate("u1")).isFalse();
assertThat(isPrivate("u2")).isTrue();
assertThat(isPrivate("root2")).isTrue();
assertThat(isPrivate("u3")).isFalse();
assertThat(isPrivate("u4")).isTrue();
}
@Test
public void execute_does_change_non_root_rows_which_root_does_not_exist() throws SQLException {
// non existent root, won't be changed
long pId1 = insertComponent("u1", "non existent root", false);
insertGroupPermission(randomPublicConditionRole, pId1, null);
insertComponent("u2", "non existent root", true);
underTest.execute();
assertThat(isPrivate("u1")).isFalse();
assertThat(isPrivate("u2")).isTrue();
}
@Test
public void execute_deletes_any_permission_to_group_Anyone_for_root_components_which_are_made_private() throws SQLException {
long idRoot1 = insertRootComponent("root1", false);
int someGroupId = random.nextInt(50);
int someUserId = random.nextInt(50);
insertGroupPermission(randomRole, idRoot1, null);
insertGroupPermission(randomRole, idRoot1, someGroupId);
insertUserPermission(randomRole, idRoot1, someUserId);
underTest.execute();
assertThat(isPrivate("root1")).isTrue();
assertThat(permissionsOfGroupAnyone(idRoot1)).isEmpty();
assertThat(permissionsOfGroup(idRoot1, someGroupId)).containsOnly(randomRole, ROLE_USER, ROLE_CODEVIEWER);
assertThat(permissionsOfUser(idRoot1, someUserId)).containsOnly(randomRole, ROLE_USER, ROLE_CODEVIEWER);
}
@Test
public void execute_ensures_any_user_of_with_at_least_one_permission_on_root_component_which_is_made_private_also_has_permissions_USER_and_CODEVIEWER() throws SQLException {
long idRoot = insertRootComponent("root1", false);
String someRole = "role_" + random.nextInt(12);
int user1 = insertUser();
int user2 = insertUser();
insertUserPermission(someRole, idRoot, user1);
underTest.execute();
assertThat(isPrivate("root1")).isTrue();
assertThat(permissionsOfGroupAnyone(idRoot)).isEmpty();
assertThat(permissionsOfUser(idRoot, user1)).containsOnly(someRole, ROLE_USER, ROLE_CODEVIEWER);
assertThat(permissionsOfUser(idRoot, user2)).isEmpty();
}
@Test
public void execute_ensures_any_group_of_with_at_least_one_permission_on_root_component_which_is_made_private_also_has_permissions_USER_and_CODEVIEWER() throws SQLException {
long idRoot = insertRootComponent("root1", false);
String someRole = "role_" + random.nextInt(12);
int group1 = insertGroup();
int group2 = insertGroup();
insertGroupPermission(someRole, idRoot, group1);
underTest.execute();
assertThat(isPrivate("root1")).isTrue();
assertThat(permissionsOfGroup(idRoot, group1)).containsOnly(someRole, ROLE_USER, ROLE_CODEVIEWER);
assertThat(permissionsOfGroup(idRoot, group2)).isEmpty();
}
@Test
public void execute_does_not_delete_permissions_to_group_Anyone_for_root_components_which_are_already_private() throws SQLException {
long idRoot = insertRootComponent("root1", true);
String someRole = "role_" + random.nextInt(12);
int someGroupId = random.nextInt(50);
int someUserId = random.nextInt(50);
insertGroupPermission(someRole, idRoot, null);
insertGroupPermission(someRole, idRoot, someGroupId);
insertGroupPermission(randomPublicConditionRole, idRoot, someGroupId);
insertUserPermission(someRole, idRoot, someUserId);
insertUserPermission(randomPublicConditionRole, idRoot, someUserId);
underTest.execute();
assertThat(isPrivate("root1")).isTrue();
assertThat(permissionsOfGroupAnyone(idRoot)).containsOnly(someRole);
assertThat(permissionsOfGroup(idRoot, someGroupId)).containsOnly(someRole, ROLE_USER, ROLE_CODEVIEWER);
assertThat(permissionsOfUser(idRoot, someUserId)).containsOnly(someRole, ROLE_USER, ROLE_CODEVIEWER);
}
@Test
public void execute_ensures_any_user_of_with_at_least_one_permission_on_root_component_which_is_already_private_also_has_permissions_USER_and_CODEVIEWER() throws SQLException {
long idRoot = insertRootComponent("root1", true);
String someRole = "role_" + random.nextInt(12);
int user1 = insertUser();
int user2 = insertUser();
insertUserPermission(someRole, idRoot, user1);
underTest.execute();
assertThat(isPrivate("root1")).isTrue();
assertThat(permissionsOfGroupAnyone(idRoot)).isEmpty();
assertThat(permissionsOfUser(idRoot, user1)).containsOnly(someRole, ROLE_USER, ROLE_CODEVIEWER);
assertThat(permissionsOfUser(idRoot, user2)).isEmpty();
}
@Test
public void execute_ensures_any_group_of_with_at_least_one_permission_on_root_component_which_is_already_private_also_has_permissions_USER_and_CODEVIEWER() throws SQLException {
long idRoot = insertRootComponent("root1", true);
String someRole = "role_" + random.nextInt(12);
int group1 = insertGroup();
int group2 = insertGroup();
insertGroupPermission(someRole, idRoot, group1);
underTest.execute();
assertThat(isPrivate("root1")).isTrue();
assertThat(permissionsOfGroup(idRoot, group1)).containsOnly(someRole, ROLE_USER, ROLE_CODEVIEWER);
assertThat(permissionsOfGroup(idRoot, group2)).isEmpty();
}
@Test
public void execute_deletes_any_USER_or_BROWSE_permission_of_public_project() throws SQLException {
long idRoot = insertRootComponent("root1", false);
int someGroupId = random.nextInt(55);
int someUserId = random.nextInt(55);
String someRole = "role_" + random.nextInt(12);
Stream.of(ROLE_USER, ROLE_CODEVIEWER, someRole)
.forEach(role -> {
insertGroupPermission(role, idRoot, null);
insertGroupPermission(role, idRoot, someGroupId);
insertUserPermission(role, idRoot, someUserId);
});
assertThat(isPrivate("root1")).isFalse();
assertThat(permissionsOfGroupAnyone(idRoot)).containsOnly(ROLE_USER, ROLE_CODEVIEWER, someRole);
assertThat(permissionsOfGroup(idRoot, someGroupId)).containsOnly(ROLE_USER, ROLE_CODEVIEWER, someRole);
assertThat(permissionsOfUser(idRoot, someUserId)).containsOnly(ROLE_USER, ROLE_CODEVIEWER, someRole);
underTest.execute();
assertThat(isPrivate("root1")).isFalse();
assertThat(permissionsOfGroupAnyone(idRoot)).containsOnly(someRole);
assertThat(permissionsOfGroup(idRoot, someGroupId)).containsOnly(someRole);
assertThat(permissionsOfUser(idRoot, someUserId)).containsOnly(someRole);
}
private long insertRootComponent(String uuid, boolean isPrivate) {
db.executeInsert(
"PROJECTS",
"ORGANIZATION_UUID", "org_" + uuid,
"SCOPE", "PRJ",
"QUALIFIER", randomQualifier,
"UUID", uuid,
"UUID_PATH", "path_" + uuid,
"ROOT_UUID", "root_" + uuid,
"PROJECT_UUID", uuid,
"PRIVATE", valueOf(isPrivate));
return (long) db.selectFirst("select id as \"ID\" from projects where uuid='" + uuid + "'").get("ID");
}
private long insertComponent(String uuid, String projectUuid, boolean isPrivate) {
db.executeInsert(
"PROJECTS",
"ORGANIZATION_UUID", "org_" + uuid,
"UUID", uuid,
"UUID_PATH", "path_" + uuid,
"ROOT_UUID", "root_" + uuid,
"PROJECT_UUID", projectUuid,
"PRIVATE", valueOf(isPrivate));
return (long) db.selectFirst("select id as \"ID\" from projects where uuid='" + uuid + "'").get("ID");
}
private void insertGroupPermission(String role, @Nullable Long resourceId, @Nullable Integer groupId) {
db.executeInsert(
"GROUP_ROLES",
"ORGANIZATION_UUID", "org" + random.nextInt(50),
"GROUP_ID", groupId == null ? null : valueOf(groupId),
"RESOURCE_ID", resourceId == null ? null : valueOf(resourceId),
"ROLE", role);
}
private int groupCount = Math.abs(random.nextInt(22));
private int insertGroup() {
String name = "group" + groupCount++;
db.executeInsert(
"GROUPS",
"ORGANIZATION_UUID", "org" + random.nextInt(12),
"NAME", name);
return ((Long) db.selectFirst("select id as \"ID\" from groups where name='" + name + "'").get("ID")).intValue();
}
private void insertUserPermission(String role, @Nullable Long resourceId, int userId) {
db.executeInsert(
"USER_ROLES",
"ORGANIZATION_UUID", "org_" + random.nextInt(66),
"USER_ID", valueOf(userId),
"RESOURCE_ID", resourceId == null ? null : valueOf(resourceId),
"ROLE", role);
}
private int userCount = Math.abs(random.nextInt(22));
private int insertUser() {
String login = "user" + userCount++;
db.executeInsert(
"USERS",
"LOGIN", login,
"IS_ROOT", String.valueOf(false));
return ((Long) db.selectFirst("select id as \"ID\" from users where login='" + login + "'").get("ID")).intValue();
}
private boolean isPrivate(String uuid) {
Map<String, Object> row = db.selectFirst("select private as \"PRIVATE\" from projects where uuid = '" + uuid + "'");
return (boolean) row.get("PRIVATE");
}
private Set<String> permissionsOfGroupAnyone(long resourceId) {
return db.select("select role from group_roles where group_id is null and resource_id = " + resourceId)
.stream()
.flatMap(map -> map.entrySet().stream())
.map(entry -> (String) entry.getValue())
.collect(MoreCollectors.toSet());
}
private Set<String> permissionsOfGroup(long resourceId, int groupId) {
return db.select("select role from group_roles where group_id = " + groupId + " and resource_id = " + resourceId)
.stream()
.flatMap(map -> map.entrySet().stream())
.map(entry -> (String) entry.getValue())
.collect(MoreCollectors.toSet());
}
private Set<String> permissionsOfUser(long resourceId, int userId) {
return db.select("select role from user_roles where resource_id = " + resourceId + " and user_id = " + userId)
.stream()
.flatMap(map -> map.entrySet().stream())
.map(entry -> (String) entry.getValue())
.collect(MoreCollectors.toSet());
}
}