feat: soft delete for interests and roles
A super admin can delete an interest or a role. Deleted rows leave every list, picker and filter, keep their names on existing leads, and keep their names reserved. A role held by any user, and the protected roles, cannot be deleted. Deletes are audited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
0f83e9cc08
commit
0b201a51ca
25 files changed
+765
-9
No files matched your search
+135
@@ -0,0 +1,135 @@
|
||||
package com.solvway.services.system.service;
|
||||
|
||||
import com.solvway.services.system.model.Role;
|
||||
import com.solvway.services.system.repo.RoleRepository;
|
||||
import com.solvway.services.system.repo.UserRepository;
|
||||
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.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Soft delete of roles: list exclusion, in-use and protected refusals, 404s, reserved names. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RoleServiceDeleteTest {
|
||||
|
||||
@Mock
|
||||
private RoleRepository roleRepository;
|
||||
@Mock
|
||||
private UserRepository userRepository;
|
||||
@InjectMocks
|
||||
private RoleService service;
|
||||
|
||||
@Test
|
||||
void findAllExcludesDeletedRoles() {
|
||||
Role live = role(5L, "ROLE_SALES");
|
||||
when(roleRepository.findByDeletedAtIsNull()).thenReturn(List.of(live));
|
||||
|
||||
assertThat(service.findAll()).containsExactly(live);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteUnusedRoleStampsDeletedAt() {
|
||||
Role r = role(5L, "ROLE_TEMP");
|
||||
when(roleRepository.findById(5L)).thenReturn(Optional.of(r));
|
||||
when(userRepository.countByRoleId(5L)).thenReturn(0L);
|
||||
|
||||
service.delete(5L);
|
||||
|
||||
assertThat(r.getDeletedAt()).isNotNull();
|
||||
verify(roleRepository).save(r);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRoleInUseIsRefusedWithUserCount() {
|
||||
Role r = role(5L, "ROLE_SALES");
|
||||
when(roleRepository.findById(5L)).thenReturn(Optional.of(r));
|
||||
when(userRepository.countByRoleId(5L)).thenReturn(3L);
|
||||
|
||||
assertThatThrownBy(() -> service.delete(5L))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
|
||||
assertThat(e.getBody().getDetail()).isEqualTo("ROLE_IN_USE");
|
||||
assertThat(e.getBody().getProperties()).containsEntry("users", 3L);
|
||||
});
|
||||
assertThat(r.getDeletedAt()).isNull();
|
||||
verify(roleRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdminAndRoleAdminAreProtected() {
|
||||
when(roleRepository.findById(1L)).thenReturn(Optional.of(role(1L, "ROLE_SUPER_ADMIN")));
|
||||
when(roleRepository.findById(2L)).thenReturn(Optional.of(role(2L, "ROLE_ADMIN")));
|
||||
|
||||
for (long id : new long[]{1L, 2L}) {
|
||||
assertThatThrownBy(() -> service.delete(id))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
|
||||
assertThat(e.getBody().getDetail()).isEqualTo("ROLE_PROTECTED");
|
||||
});
|
||||
}
|
||||
verify(roleRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleMemberIsProtected() {
|
||||
when(roleRepository.findById(3L)).thenReturn(Optional.of(role(3L, "ROLE_MEMBER")));
|
||||
assertThatThrownBy(() -> service.delete(3L))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
|
||||
assertThat(e.getBody().getDetail()).isEqualTo("ROLE_PROTECTED");
|
||||
});
|
||||
verify(roleRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownOrDeletedRoleIs404() {
|
||||
Role gone = role(6L, "ROLE_OLD");
|
||||
gone.setDeletedAt(new Timestamp(1L));
|
||||
when(roleRepository.findById(9L)).thenReturn(Optional.empty());
|
||||
when(roleRepository.findById(6L)).thenReturn(Optional.of(gone));
|
||||
|
||||
for (long id : new long[]{9L, 6L}) {
|
||||
assertThatThrownBy(() -> service.delete(id))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
verify(roleRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void savingADeletedRolesNameIsRefused() {
|
||||
when(roleRepository.existsByNameIgnoreCaseAndDeletedAtIsNotNull("ROLE_TEMP")).thenReturn(true);
|
||||
|
||||
assertThatThrownBy(() -> service.requireNameNotDeleted(role(null, "ROLE_TEMP")))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
|
||||
assertThat(e.getBody().getDetail()).isEqualTo("ROLE_NAME_DELETED");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void savingALiveNameIsAllowed() {
|
||||
service.requireNameNotDeleted(role(null, "ROLE_NEW"));
|
||||
}
|
||||
|
||||
private static Role role(Long id, String name) {
|
||||
Role r = new Role();
|
||||
r.setId(id);
|
||||
r.setName(name);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user