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:
ngqileandClaude Opus 5.5 committed 2026-11-26 20:48:05 +08:00
1 parent 4d261d20d5
commit a8cdf33a24
11 files changed
+964 -1

No files matched your search

@@ -0,0 +1,137 @@
package com.solvway.mobile.api.controller.admin;
import com.solvway.core.annotation.AppAuth;
import com.solvway.mobile.api.dto.admin.AdminGrantsRequest;
import com.solvway.mobile.api.dto.admin.AdminModuleGrant;
import com.solvway.mobile.api.dto.admin.AdminRoleRequest;
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
import com.solvway.mobile.api.service.AdminAclService;
import com.solvway.mobile.api.service.AdminRoleService;
import com.solvway.mobile.api.useraccess.AdminModule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(MockitoExtension.class)
class AdminRoleControllerTest {
@Mock private AdminRoleService roles;
@Mock private AdminAclService acl;
@InjectMocks private AdminRoleController controller;
private MockMvc mvc;
@BeforeEach
void setUp() {
mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
void everyHandlerIsAuthenticatedAndGuardedByTheRightModule() {
for (Method m : AdminRoleController.class.getDeclaredMethods()) {
if (m.getDeclaringClass() != AdminRoleController.class || m.isSynthetic()) continue;
assertThat(m.isAnnotationPresent(AppAuth.class)).as(m.getName() + " @AppAuth").isTrue();
List<String> modules = Arrays.asList(m.getAnnotation(AdminModule.class).value());
if (m.getName().equals("list") || m.getName().equals("getById")) {
assertThat(modules).as(m.getName()).containsExactlyInAnyOrder("BO_ROLES", "BO_USERS");
} else {
assertThat(modules).as(m.getName()).containsExactly("BO_ROLES");
}
}
}
@Test
void list_returnsRoles() throws Exception {
when(roles.list()).thenReturn(List.of(Map.of("id", 4, "name", "ROLE_SALES")));
mvc.perform(get("/api/admin/roles")).andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("ROLE_SALES"));
}
@Test
void getById_returnsRole() throws Exception {
when(roles.get(4L)).thenReturn(Map.of("id", 4, "name", "ROLE_SALES"));
mvc.perform(get("/api/admin/roles/4")).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(4));
}
@Test
void getById_unknownRoleAnswers404() throws Exception {
when(roles.get(99L)).thenThrow(new CodedResponseStatusException(HttpStatus.NOT_FOUND, "Role not found", "ROLE_NOT_FOUND"));
mvc.perform(get("/api/admin/roles/99")).andExpect(status().isNotFound());
}
@Test
void create_ignoresPortalExtrasAndPassesName() throws Exception {
when(roles.create(any())).thenReturn(Map.of("id", 9, "name", "ROLE_SALES"));
mvc.perform(post("/api/admin/roles").contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"ROLE_SALES\",\"parent\":{\"id\":1},\"level\":2}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.id").value(9));
ArgumentCaptor<AdminRoleRequest> c = ArgumentCaptor.forClass(AdminRoleRequest.class);
verify(roles).create(c.capture());
assertThat(c.getValue().name()).isEqualTo("ROLE_SALES");
}
@Test
void create_duplicateAnswers409() throws Exception {
when(roles.create(any())).thenThrow(new CodedResponseStatusException(HttpStatus.CONFLICT, "Role name is already taken", "ROLE_NAME_TAKEN"));
mvc.perform(post("/api/admin/roles").contentType(MediaType.APPLICATION_JSON).content("{\"name\":\"ROLE_SALES\"}"))
.andExpect(status().isConflict());
}
@Test
void update_passesIdAndName() throws Exception {
when(roles.update(eq(4L), any())).thenReturn(Map.of("id", 4, "name", "ROLE_OPS"));
mvc.perform(put("/api/admin/roles/4").contentType(MediaType.APPLICATION_JSON).content("{\"name\":\"ROLE_OPS\"}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.name").value("ROLE_OPS"));
ArgumentCaptor<AdminRoleRequest> c = ArgumentCaptor.forClass(AdminRoleRequest.class);
verify(roles).update(eq(4L), c.capture());
assertThat(c.getValue().name()).isEqualTo("ROLE_OPS");
}
@Test
void getModules_returnsGrantTree() throws Exception {
when(acl.getGrantTree(4L)).thenReturn(List.of(new AdminModuleGrant(40700000L, "BO_USERS", 40000000L, true)));
mvc.perform(get("/api/admin/roles/4/modules")).andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(40700000))
.andExpect(jsonPath("$[0].parentId").value(40000000))
.andExpect(jsonPath("$[0].granted").value(true));
}
@Test
void putModules_passesModuleIdsAndReturnsTree() throws Exception {
when(acl.updateGrants(eq(4L), any())).thenReturn(List.of(new AdminModuleGrant(40700000L, "BO_USERS", null, true)));
mvc.perform(put("/api/admin/roles/4/modules").contentType(MediaType.APPLICATION_JSON)
.content("{\"moduleIds\":[40700000,40800000]}"))
.andExpect(status().isOk()).andExpect(jsonPath("$[0].granted").value(true));
verify(acl).updateGrants(4L, List.of(40700000L, 40800000L));
}
@Test
void putModules_lockoutAnswers400() throws Exception {
when(acl.updateGrants(eq(4L), any())).thenThrow(new CodedResponseStatusException(HttpStatus.BAD_REQUEST, "lockout", "ACL_LOCKOUT"));
mvc.perform(put("/api/admin/roles/4/modules").contentType(MediaType.APPLICATION_JSON).content("{\"moduleIds\":[]}"))
.andExpect(status().isBadRequest());
}
}