feat(mobile-api): admin roles API (list, create, rename, module grants)
Member and ROLE_ADMIN roles are hidden; super admin grants are not editable; removing your own last BO_ROLES grant is refused. Renaming keeps child roles intact. 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
4d261d20d5
commit
a8cdf33a24
11 files changed
+964
-1
No files matched your search
+242
@@ -0,0 +1,242 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.dto.admin.AdminModuleGrant;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminAclServiceTest {
|
||||
|
||||
private static final String MODULES = "/module/findAllByArea/{area}";
|
||||
private static final String ADD = "/module/addModuleRole/{moduleId}/{roleId}";
|
||||
private static final String DEL = "/module/deleteModuleRole/{moduleId}/{roleId}";
|
||||
private static final long ROOT = 40000000L, USERS = 40700000L, ROLES = 40800000L;
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminAuthenticationService auth;
|
||||
private AdminRoleService roles;
|
||||
private AdminAclService service;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
auth = mock(AdminAuthenticationService.class);
|
||||
roles = mock(AdminRoleService.class);
|
||||
service = new AdminAclService(rest, auth, roles);
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
when(roles.find(1L)).thenReturn(Map.of("id", 1L, "name", "ROLE_SUPER_ADMIN"));
|
||||
when(roles.find(2L)).thenReturn(Map.of("id", 2L, "name", "ROLE_ADMIN"));
|
||||
when(roles.find(4L)).thenReturn(Map.of("id", 4L, "name", "ROLE_SALES"));
|
||||
when(roles.find(5L)).thenReturn(Map.of("id", 5L, "name", "ROLE_OPS"));
|
||||
when(roles.find(99L)).thenThrow(new CodedResponseStatusException(HttpStatus.NOT_FOUND, "Role not found", "ROLE_NOT_FOUND"));
|
||||
// BO_USERS held by 1 and 4; BO_ROLES held by 1 and 5. Root's role list comes first, so later modules see bare parent ids.
|
||||
stubModules(
|
||||
module(ROOT, "BO_ROOT", null, 1),
|
||||
module(USERS, "BO_USERS", ROOT, 1, 4),
|
||||
module(ROLES, "BO_ROLES", Map.of("id", ROOT, "name", "BO_ROOT"), 1, 5));
|
||||
caller(5);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private static Map<String, Object> module(long id, String name, Object parent, long... roleIds) {
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("id", id);
|
||||
m.put("name", name);
|
||||
m.put("parent", parent);
|
||||
m.put("type", parent == null ? "R" : "M");
|
||||
List<Object> rs = new ArrayList<>();
|
||||
for (long r : roleIds) rs.add(Map.of("id", r, "name", "R" + r));
|
||||
m.put("roles", rs);
|
||||
return m;
|
||||
}
|
||||
|
||||
private void stubModules(Map<String, Object>... modules) {
|
||||
when(rest.getForEntity(eq(MODULES), any(ParameterizedTypeReference.class), eq("BO"))).thenReturn(ResponseEntity.ok(List.of(modules)));
|
||||
}
|
||||
|
||||
private void caller(long... roleIds) {
|
||||
User u = new User();
|
||||
u.setId(50L);
|
||||
List<Role> rs = new ArrayList<>();
|
||||
for (long id : roleIds) {
|
||||
Role r = new Role();
|
||||
r.setId(id);
|
||||
rs.add(r);
|
||||
}
|
||||
u.setRoles(rs);
|
||||
when(auth.loadUserByToken("tok")).thenReturn(u);
|
||||
}
|
||||
|
||||
private void assertCoded(Runnable r, HttpStatus status, String code) {
|
||||
assertThatThrownBy(r::run).isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(status);
|
||||
assertThat(e.getCode()).isEqualTo(code);
|
||||
});
|
||||
}
|
||||
|
||||
private void verifyNothingChanged() {
|
||||
verify(rest, never()).getForEntity(eq(ADD), eq(Integer.class), any(), any());
|
||||
verify(rest, never()).getForEntity(eq(DEL), eq(Integer.class), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void grantTree_listsBoModulesWithGrantedFlagAndParentIds() {
|
||||
List<AdminModuleGrant> tree = service.getGrantTree(4L);
|
||||
// the type R root is not grantable, so it is not listed
|
||||
assertThat(tree).containsExactly(
|
||||
new AdminModuleGrant(USERS, "BO_USERS", ROOT, true), // parent came as a bare id
|
||||
new AdminModuleGrant(ROLES, "BO_ROLES", ROOT, false)); // parent came as an object
|
||||
}
|
||||
|
||||
@Test
|
||||
void grantTree_unknownRoleIs404() {
|
||||
assertCoded(() -> service.getGrantTree(99L), HttpStatus.NOT_FOUND, "ROLE_NOT_FOUND");
|
||||
}
|
||||
|
||||
@Test
|
||||
void grantTree_ofAHiddenRoleIs404() {
|
||||
when(roles.find(3L)).thenReturn(Map.of("id", 3L, "name", "ROLE_MEMBER"));
|
||||
assertCoded(() -> service.getGrantTree(2L), HttpStatus.NOT_FOUND, "ROLE_NOT_FOUND");
|
||||
assertCoded(() -> service.getGrantTree(3L), HttpStatus.NOT_FOUND, "ROLE_NOT_FOUND");
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_rootModuleIsRefused() {
|
||||
assertCoded(() -> service.updateGrants(4L, List.of(ROOT)), HttpStatus.BAD_REQUEST, "UNKNOWN_MODULE");
|
||||
verifyNothingChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_addsOnlyMissingGrants() {
|
||||
// 4 holds BO_USERS; wanting BO_USERS + BO_ROLES must add BO_ROLES only
|
||||
service.updateGrants(4L, List.of(USERS, ROLES));
|
||||
verify(rest).getForEntity(eq(ADD), eq(Integer.class), eq(ROLES), eq(4L));
|
||||
verify(rest, never()).getForEntity(eq(ADD), eq(Integer.class), eq(USERS), any());
|
||||
verify(rest, never()).getForEntity(eq(DEL), eq(Integer.class), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_removesOnlyDroppedGrants() {
|
||||
service.updateGrants(4L, List.of());
|
||||
verify(rest).getForEntity(eq(DEL), eq(Integer.class), eq(USERS), eq(4L));
|
||||
verify(rest, never()).getForEntity(eq(ADD), eq(Integer.class), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_sameSetChangesNothing() {
|
||||
service.updateGrants(4L, List.of(USERS));
|
||||
verifyNothingChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_returnsTheTreeAfterTheChange() {
|
||||
assertThat(service.updateGrants(4L, List.of(USERS))).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_nonBoModuleIsRefused() {
|
||||
assertCoded(() -> service.updateGrants(4L, List.of(USERS, 7000L)), HttpStatus.BAD_REQUEST, "UNKNOWN_MODULE");
|
||||
verifyNothingChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_missingModuleIdsIsRefused() {
|
||||
assertCoded(() -> service.updateGrants(4L, null), HttpStatus.BAD_REQUEST, "REQUIRED_FIELD");
|
||||
verifyNothingChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_superAdminGrantsAreNotEditable() {
|
||||
assertCoded(() -> service.updateGrants(1L, List.of(USERS)), HttpStatus.BAD_REQUEST, "ROLE_NOT_EDITABLE");
|
||||
verifyNothingChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_hiddenRoleIs404() {
|
||||
assertCoded(() -> service.updateGrants(2L, List.of(USERS)), HttpStatus.NOT_FOUND, "ROLE_NOT_FOUND");
|
||||
verifyNothingChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_unknownRoleIs404() {
|
||||
assertCoded(() -> service.updateGrants(99L, List.of()), HttpStatus.NOT_FOUND, "ROLE_NOT_FOUND");
|
||||
verifyNothingChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_removingBoRolesFromTheCallersOnlyGrantingRoleIsALockout() {
|
||||
caller(5, 2); // role 2 is attached to every admin and does not count
|
||||
assertCoded(() -> service.updateGrants(5L, List.of()), HttpStatus.BAD_REQUEST, "ACL_LOCKOUT");
|
||||
verifyNothingChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_removingBoRolesIsFineWhenAnotherCallerRoleStillGrantsIt() {
|
||||
stubModules(
|
||||
module(ROOT, "BO_ROOT", null, 1),
|
||||
module(USERS, "BO_USERS", ROOT, 1, 4),
|
||||
module(ROLES, "BO_ROLES", ROOT, 1, 5, 6));
|
||||
caller(5, 6, 2);
|
||||
service.updateGrants(5L, List.of());
|
||||
verify(rest).getForEntity(eq(DEL), eq(Integer.class), eq(ROLES), eq(5L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_keepingBoRolesOnTheCallersRoleIsFine() {
|
||||
service.updateGrants(5L, List.of(ROLES, USERS));
|
||||
verify(rest).getForEntity(eq(ADD), eq(Integer.class), eq(USERS), eq(5L));
|
||||
verify(rest, never()).getForEntity(eq(DEL), eq(Integer.class), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_editingAnotherRoleNeverLocksOut() {
|
||||
service.updateGrants(4L, List.of(ROLES));
|
||||
verify(rest).getForEntity(eq(ADD), eq(Integer.class), eq(ROLES), eq(4L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_superAdminCallerIsNotSubjectToLockout() {
|
||||
caller(1);
|
||||
service.updateGrants(5L, List.of());
|
||||
verify(rest).getForEntity(eq(DEL), eq(Integer.class), eq(ROLES), eq(5L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_unresolvedCallerIsUnauthorized() {
|
||||
when(auth.loadUserByToken("tok")).thenReturn(null);
|
||||
assertCoded(() -> service.updateGrants(4L, List.of()), HttpStatus.UNAUTHORIZED, "UNAUTHORIZED");
|
||||
verifyNothingChanged();
|
||||
}
|
||||
}
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.dto.admin.AdminRoleRequest;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminRoleServiceTest {
|
||||
|
||||
private static final String FIND_ALL = "/role/findAll";
|
||||
private static final String SAVE = "/role/save";
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminRoleService service;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
AdminAuthenticationService auth = mock(AdminAuthenticationService.class);
|
||||
service = new AdminRoleService(rest, auth);
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
User caller = new User();
|
||||
caller.setUsername("boss");
|
||||
when(auth.loadUserByToken("tok")).thenReturn(caller);
|
||||
stubRoles(stored());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private static Map<String, Object> role(long id, String name, Object parent, int level, String tracekey) {
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("id", id);
|
||||
m.put("name", name);
|
||||
m.put("level", level);
|
||||
m.put("tracekey", tracekey);
|
||||
m.put("parent", parent);
|
||||
m.put("children", List.of());
|
||||
m.put("createBy", "sys");
|
||||
m.put("createDate", 1700000000000L);
|
||||
return m;
|
||||
}
|
||||
|
||||
private static List<Map<String, Object>> stored() {
|
||||
Map<String, Object> sales = role(4, "ROLE_SALES", Map.of("id", 1, "name", "ROLE_SUPER_ADMIN", "level", 1, "tracekey", "ROLE_SUPER_ADMIN"),
|
||||
2, "ROLE_SUPER_ADMIN/ROLE_SALES");
|
||||
sales.put("children", List.of(Map.of("id", 9L, "name", "ROLE_SALES_JR", "createBy", "sys", "createDate", 1L, "parent", 4L)));
|
||||
return List.of(
|
||||
role(1, "ROLE_SUPER_ADMIN", null, 1, "ROLE_SUPER_ADMIN"),
|
||||
role(2, "ROLE_ADMIN", Map.of("id", 1), 2, "ROLE_SUPER_ADMIN/ROLE_ADMIN"),
|
||||
role(3, "ROLE_MEMBER", null, 1, "ROLE_MEMBER"),
|
||||
sales);
|
||||
}
|
||||
|
||||
private void stubRoles(List<Map<String, Object>> first, List<Map<String, Object>>... later) {
|
||||
var stub = when(rest.getForEntity(eq(FIND_ALL), any(ParameterizedTypeReference.class))).thenReturn(ResponseEntity.ok(first));
|
||||
for (var l : later) stub = stub.thenReturn(ResponseEntity.ok(l));
|
||||
}
|
||||
|
||||
private void stubSave(Map<String, Object> saved) {
|
||||
when(rest.postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(saved));
|
||||
}
|
||||
|
||||
private Map<String, Object> savedBody() {
|
||||
ArgumentCaptor<Object> c = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), c.capture());
|
||||
return (Map<String, Object>) c.getValue();
|
||||
}
|
||||
|
||||
private static void assertCoded(Runnable r, HttpStatus status, String code) {
|
||||
assertThatThrownBy(r::run).isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(status);
|
||||
assertThat(e.getCode()).isEqualTo(code);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void list_hidesMemberAndAdminRoles() {
|
||||
assertThat(service.list()).extracting(m -> m.get("name")).containsExactly("ROLE_SUPER_ADMIN", "ROLE_SALES");
|
||||
}
|
||||
|
||||
@Test
|
||||
void list_returnsOnlyPortalFields() {
|
||||
Map<String, Object> sales = service.list().get(1);
|
||||
assertThat(sales).containsOnlyKeys("id", "name", "level", "parent");
|
||||
assertThat(sales.get("parent")).isEqualTo(Map.of("id", 1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_returnsVisibleRole() {
|
||||
assertThat(service.get(4L).get("name")).isEqualTo("ROLE_SALES");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = {2, 3, 99})
|
||||
void get_hiddenOrUnknownRoleIs404(long id) {
|
||||
assertCoded(() -> service.get(id), HttpStatus.NOT_FOUND, "ROLE_NOT_FOUND");
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_savesUnderSuperAdminWithActor() {
|
||||
Map<String, Object> created = role(10, "ROLE_OPS", Map.of("id", 1), 2, "ROLE_SUPER_ADMIN/ROLE_OPS");
|
||||
stubSave(created);
|
||||
List<Map<String, Object>> after = new java.util.ArrayList<>(stored());
|
||||
after.add(created);
|
||||
stubRoles(stored(), after);
|
||||
|
||||
Map<String, Object> out = service.create(new AdminRoleRequest(" ROLE_OPS "));
|
||||
|
||||
Map<String, Object> body = savedBody();
|
||||
assertThat(body.get("name")).isEqualTo("ROLE_OPS");
|
||||
assertThat(body.get("parent")).isEqualTo(Map.of("id", 1L));
|
||||
assertThat(body.get("children")).isEqualTo(List.of());
|
||||
assertThat(body.get("createBy")).isEqualTo("boss");
|
||||
assertThat(body.get("doneBy")).isEqualTo("boss");
|
||||
assertThat(body.get("createDate")).isInstanceOf(Long.class);
|
||||
assertThat(body).doesNotContainKey("id");
|
||||
assertThat(out.get("id")).isEqualTo(10L);
|
||||
assertThat(out.get("name")).isEqualTo("ROLE_OPS");
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_nameIsRequired() {
|
||||
assertCoded(() -> service.create(new AdminRoleRequest(" ")), HttpStatus.BAD_REQUEST, "REQUIRED_FIELD");
|
||||
assertCoded(() -> service.create(new AdminRoleRequest(null)), HttpStatus.BAD_REQUEST, "REQUIRED_FIELD");
|
||||
verify(rest, never()).postForEntity(any(String.class), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"ROLE_SALES", "role_sales", "ROLE_ADMIN", "ROLE_MEMBER"})
|
||||
void create_duplicateNameIsRefusedCaseInsensitively(String name) {
|
||||
assertCoded(() -> service.create(new AdminRoleRequest(name)), HttpStatus.CONFLICT, "ROLE_NAME_TAKEN");
|
||||
verify(rest, never()).postForEntity(any(String.class), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_failedSaveIsReported() {
|
||||
// services-system swallows save errors and returns the unsaved entity (no id)
|
||||
stubSave(new HashMap<>(Map.of("name", "ROLE_OPS")));
|
||||
assertCoded(() -> service.create(new AdminRoleRequest("ROLE_OPS")), HttpStatus.INTERNAL_SERVER_ERROR, "SAVE_FAILED");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rename_keepsStoredLevelParentTracekeyChildrenAndCreateFields() {
|
||||
Map<String, Object> renamed = role(4, "ROLE_REVENUE", Map.of("id", 1), 2, "ROLE_SUPER_ADMIN/ROLE_REVENUE");
|
||||
stubSave(renamed);
|
||||
List<Map<String, Object>> after = List.of(stored().get(0), renamed);
|
||||
stubRoles(stored(), after);
|
||||
|
||||
Map<String, Object> out = service.update(4L, new AdminRoleRequest("ROLE_REVENUE"));
|
||||
|
||||
Map<String, Object> body = savedBody();
|
||||
assertThat(body.get("id")).isEqualTo(4L);
|
||||
assertThat(body.get("name")).isEqualTo("ROLE_REVENUE");
|
||||
assertThat(body.get("level")).isEqualTo(2);
|
||||
assertThat(body.get("tracekey")).isEqualTo("ROLE_SUPER_ADMIN/ROLE_SALES");
|
||||
assertThat(body.get("parent")).isEqualTo(Map.of("id", 1L));
|
||||
// full nested objects pass through: services-system parseChild re-saves each child from them
|
||||
assertThat(body.get("children")).isEqualTo(stored().get(3).get("children"));
|
||||
assertThat(body.get("createBy")).isEqualTo("sys");
|
||||
assertThat(body.get("createDate")).isEqualTo(1700000000000L);
|
||||
assertThat(body.get("doneBy")).isEqualTo("boss");
|
||||
assertThat(out.get("name")).isEqualTo("ROLE_REVENUE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rename_toItsOwnNameIsAllowed() {
|
||||
stubSave(stored().get(3));
|
||||
assertThat(service.update(4L, new AdminRoleRequest("role_sales")).get("id")).isEqualTo(4L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rename_toAnotherRolesNameIsRefused() {
|
||||
assertCoded(() -> service.update(4L, new AdminRoleRequest("role_admin")), HttpStatus.CONFLICT, "ROLE_NAME_TAKEN");
|
||||
verify(rest, never()).postForEntity(any(String.class), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = {1, 2, 3})
|
||||
void rename_builtInRolesAreNotEditable(long id) {
|
||||
assertCoded(() -> service.update(id, new AdminRoleRequest("ROLE_X")), HttpStatus.BAD_REQUEST, "ROLE_NOT_EDITABLE");
|
||||
verify(rest, never()).postForEntity(any(String.class), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rename_unknownRoleIs404() {
|
||||
assertCoded(() -> service.update(99L, new AdminRoleRequest("ROLE_X")), HttpStatus.NOT_FOUND, "ROLE_NOT_FOUND");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rename_silentlyFailedSaveIsReported() {
|
||||
// /role/save swallows errors; the reread still shows the old name
|
||||
stubSave(stored().get(3));
|
||||
assertCoded(() -> service.update(4L, new AdminRoleRequest("ROLE_REVENUE")), HttpStatus.INTERNAL_SERVER_ERROR, "SAVE_FAILED");
|
||||
}
|
||||
|
||||
@Test
|
||||
void isProtected_matchesBuiltInRoleNames() {
|
||||
assertThat(AdminRoleService.isProtected(stored().get(0))).isTrue();
|
||||
assertThat(AdminRoleService.isProtected(stored().get(1))).isTrue();
|
||||
assertThat(AdminRoleService.isProtected(stored().get(2))).isTrue();
|
||||
assertThat(AdminRoleService.isProtected(stored().get(3))).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user