BadRequestException.checkRequest(!managedInstanceService.isUserManaged(dbSession, userUuid), INSTANCE_EXCEPTION_MESSAGE);
}
+ public void throwIfGroupIsManaged(DbSession dbSession, String groupUuid) {
+ BadRequestException.checkRequest(!managedInstanceService.isGroupManaged(dbSession, groupUuid), INSTANCE_EXCEPTION_MESSAGE);
+ }
+
public void throwIfUserAndProjectAreManaged(DbSession dbSession, String userUuid, String projectUuid) {
boolean isUserManaged = managedInstanceService.isUserManaged(dbSession, userUuid);
boolean isProjectManaged = managedProjectService.isProjectManaged(dbSession, projectUuid);
assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserIsManaged(dbSession, userDto.getUuid()));
}
+ @Test
+ public void throwIfGroupIsManaged_whenGroupIsManaged_shouldThrow() {
+ GroupDto groupDto = mockManagedGroup();
+
+ String groupUuid = groupDto.getUuid();
+ assertThatThrownBy(() -> managedInstanceChecker.throwIfGroupIsManaged(dbSession, groupUuid))
+ .isInstanceOf(BadRequestException.class)
+ .hasMessage(INSTANCE_EXCEPTION_MESSAGE);
+ }
+
+ @Test
+ public void throwIfGroupIsManaged_whenGroupIsNotManaged_shouldNotThrow() {
+ GroupDto groupDto = mockNotManagedGroup();
+
+ assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfGroupIsManaged(dbSession, groupDto.getUuid()));
+ }
+
@Test
public void throwIfUserAndProjectAreManaged_whenUserAndProjectAreManaged_shouldThrow() {
ProjectDto projectDto = mockManagedProject();
@Override
public void deleteGroup(String id) {
- throwIfNotAllowedToModifyGroups();
try (DbSession session = dbClient.openSession(false)) {
+ throwIfNotAllowedToDeleteGroup(id, session);
GroupInformation group = findGroupInformationOrThrow(id, session);
groupService.delete(session, group.groupDto());
session.commit();
.orElseThrow(() -> new NotFoundException(String.format(GROUP_NOT_FOUND_MESSAGE, id)));
}
+ private void throwIfNotAllowedToDeleteGroup(String id, DbSession session) {
+ userSession.checkIsSystemAdministrator();
+ managedInstanceChecker.throwIfGroupIsManaged(session, id);
+ }
+
@Override
public GroupRestResponse create(GroupCreateRestRequest request) {
throwIfNotAllowedToModifyGroups();
}
@Test
- public void deleteGroup_whenInstanceIsManaged_shouldReturnException() throws Exception {
+ public void deleteGroup_whenInstanceAndGroupAreManaged_shouldReturnException() throws Exception {
userSession.logIn().setSystemAdministrator();
- doThrow(BadRequestException.create("the instance is managed")).when(managedInstanceChecker).throwIfInstanceIsManaged();
+ doThrow(BadRequestException.create("the instance is managed")).when(managedInstanceChecker).throwIfGroupIsManaged(any(), eq(GROUP_UUID));
mockMvc.perform(
delete(GROUPS_ENDPOINT + "/" + GROUP_UUID))
.andExpectAll(