From 3d89e48cd8d54e8cb3d553f68c02e2b17a7ab9a0 Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Fri, 24 Feb 2023 13:59:51 +0100 Subject: [PATCH] SONAR-18525 Support of DELETE verb for /api/scim/v2/Groups --- .../org/sonar/db/scim/ScimGroupDaoTest.java | 21 ++++++++----------- .../java/org/sonar/db/user/UserDbTester.java | 7 +++++++ .../server/usergroups/ws/DeleteActionIT.java | 16 ++++++++++++++ .../server/usergroups/ws/DeleteAction.java | 1 + 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java index 95fc808f037..75d52cb5203 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/scim/ScimGroupDaoTest.java @@ -45,8 +45,8 @@ public class ScimGroupDaoTest { @Test public void findAll_returnsAllEntries() { - ScimGroupDto scimGroup1 = insertScimGroup(); - ScimGroupDto scimGroup2 = insertScimGroup(); + ScimGroupDto scimGroup1 = db.users().insertScimGroup(db.users().insertGroup()); + ScimGroupDto scimGroup2 = db.users().insertScimGroup(db.users().insertGroup()); List underTest = scimGroupDao.findAll(db.getSession()); @@ -65,8 +65,8 @@ public class ScimGroupDaoTest { @Test public void findByScimUuid_whenScimUuidFound_shouldReturnDto() { - ScimGroupDto scimGroupDto = insertScimGroup(); - insertScimGroup(); + ScimGroupDto scimGroupDto = db.users().insertScimGroup(db.users().insertGroup()); + db.users().insertScimGroup(db.users().insertGroup()); ScimGroupDto underTest = scimGroupDao.findByScimUuid(db.getSession(), scimGroupDto.getScimGroupUuid()) .orElseGet(() -> fail("Group not found")); @@ -82,8 +82,8 @@ public class ScimGroupDaoTest { @Test public void findByGroupUuid_whenScimUuidFound_shouldReturnDto() { - ScimGroupDto scimGroupDto = insertScimGroup(); - insertScimGroup(); + ScimGroupDto scimGroupDto = db.users().insertScimGroup(db.users().insertGroup()); + db.users().insertScimGroup(db.users().insertGroup()); ScimGroupDto underTest = scimGroupDao.findByGroupUuid(db.getSession(), scimGroupDto.getGroupUuid()) .orElseGet(() -> fail("Group not found")); @@ -104,7 +104,7 @@ public class ScimGroupDaoTest { @Test public void deleteByGroupUuid_shouldDeleteScimGroup() { - ScimGroupDto scimGroupDto = insertScimGroup(); + ScimGroupDto scimGroupDto = db.users().insertScimGroup(db.users().insertGroup()); scimGroupDao.deleteByGroupUuid(db.getSession(), scimGroupDto.getGroupUuid()); @@ -113,8 +113,8 @@ public class ScimGroupDaoTest { @Test public void deleteByScimUuid_shouldDeleteScimGroup() { - ScimGroupDto scimGroupDto1 = insertScimGroup(); - ScimGroupDto scimGroupDto2 = insertScimGroup(); + ScimGroupDto scimGroupDto1 = db.users().insertScimGroup(db.users().insertGroup()); + ScimGroupDto scimGroupDto2 = db.users().insertScimGroup(db.users().insertGroup()); scimGroupDao.deleteByScimUuid(db.getSession(), scimGroupDto1.getScimGroupUuid()); @@ -131,7 +131,4 @@ public class ScimGroupDaoTest { assertThatCode(() -> scimGroupDao.deleteByGroupUuid(db.getSession(), randomAlphanumeric(6))).doesNotThrowAnyException(); } - private ScimGroupDto insertScimGroup() { - return scimGroupDao.enableScimForGroup(db.getSession(), randomAlphanumeric(40)); - } } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java index 46cacd0142d..190d0299478 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java @@ -38,6 +38,7 @@ import org.sonar.db.permission.GlobalPermission; import org.sonar.db.permission.GroupPermissionDto; import org.sonar.db.permission.UserPermissionDto; import org.sonar.db.project.ProjectDto; +import org.sonar.db.scim.ScimGroupDto; import static com.google.common.base.Preconditions.checkArgument; import static java.util.Arrays.stream; @@ -136,6 +137,12 @@ public class UserDbTester { return dto; } + public ScimGroupDto insertScimGroup(GroupDto dto) { + ScimGroupDto result = db.getDbClient().scimGroupDao().enableScimForGroup(db.getSession(), dto.getUuid()); + db.commit(); + return result; + } + public GroupDto insertDefaultGroup() { GroupDto dto = db.getDbClient().groupDao().insert(db.getSession(), newGroupDto().setName(DefaultGroups.USERS).setDescription("Users")); db.commit(); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java index 713ee70379e..858ea93d32e 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java @@ -180,6 +180,22 @@ public class DeleteActionIT { assertThat(db.countRowsOfTable("qgate_group_permissions")).isZero(); } + @Test + public void delete_scim_group() { + addAdmin(); + insertDefaultGroup(); + GroupDto group = db.users().insertGroup(); + db.users().insertScimGroup(group); + + loginAsAdmin(); + + newRequest() + .setParam(PARAM_GROUP_NAME, group.getName()) + .execute(); + + assertThat(db.countRowsOfTable("scim_groups")).isZero(); + } + @Test public void fail_to_delete_default_group() { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java index c726a7dd37e..d8cb25807c9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java @@ -76,6 +76,7 @@ public class DeleteAction implements UserGroupsWsAction { removeGroupMembers(dbSession, group); dbClient.qProfileEditGroupsDao().deleteByGroup(dbSession, group); dbClient.qualityGateGroupPermissionsDao().deleteByGroup(dbSession, group); + dbClient.scimGroupDao().deleteByGroupUuid(dbSession, group.getUuid()); dbClient.groupDao().deleteByUuid(dbSession, group.getUuid(), group.getName()); dbSession.commit(); -- 2.39.5