feat(mobile-api): admin users API (list, create, edit, deactivate, reset password)
New users get a temporary password by email and the creating admin's company. Self-deactivation and super-admin changes by non-super admins are refused. services-system: saving a user no longer cascades into role rows. Local seed: dev admin on the GLOBAL branch (id 0 does not exist). 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
9f22837a00
commit
f5d1b6d032
11 files changed
+1038
-3
No files matched your search
+108
@@ -0,0 +1,108 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.mobile.api.dto.admin.AdminUserRequest;
|
||||
import com.solvway.mobile.api.service.AdminUserService;
|
||||
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.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
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.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 AdminUserControllerTest {
|
||||
|
||||
@Mock private AdminUserService users;
|
||||
@InjectMocks private AdminUserController controller;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void guardedByBackOfficeUsersModule() {
|
||||
assertThat(AdminUserController.class.getAnnotation(AdminModule.class).value()).containsExactly("BO_USERS");
|
||||
}
|
||||
|
||||
@Test
|
||||
void list_passesParamsAndReturnsPageShape() throws Exception {
|
||||
when(users.list("ali", "true", 1, 5, "name", "desc"))
|
||||
.thenReturn(Map.of("content", List.of(Map.of("id", 1)), "totalElements", 6L));
|
||||
mvc.perform(get("/api/admin/users").param("q", "ali").param("enabled", "true").param("page", "1")
|
||||
.param("size", "5").param("sortBy", "name").param("sortDir", "desc"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.content[0].id").value(1))
|
||||
.andExpect(jsonPath("$.totalElements").value(6));
|
||||
}
|
||||
|
||||
@Test
|
||||
void list_legacySearchKeyActsAsQ() throws Exception {
|
||||
when(users.list("bob", null, 0, 20, null, null)).thenReturn(Map.of("content", List.of()));
|
||||
mvc.perform(get("/api/admin/users").param("searchKey", "bob")).andExpect(status().isOk());
|
||||
verify(users).list("bob", null, 0, 20, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getById_returnsUser() throws Exception {
|
||||
when(users.findById(5L)).thenReturn(Map.of("id", 5, "username", "a"));
|
||||
mvc.perform(get("/api/admin/users/5")).andExpect(status().isOk()).andExpect(jsonPath("$.username").value("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_ignoresPortalExtrasAndPassesRequest() throws Exception {
|
||||
when(users.create(any())).thenReturn(Map.of("id", 9));
|
||||
mvc.perform(post("/api/admin/users").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"name\":\"N\",\"username\":\"u\",\"email\":\"e@x.com\",\"password\":\"x\",\"companyId\":99,"
|
||||
+ "\"roles\":[{\"id\":4,\"name\":\"R\"}]}"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.id").value(9));
|
||||
ArgumentCaptor<AdminUserRequest> c = ArgumentCaptor.forClass(AdminUserRequest.class);
|
||||
verify(users).create(c.capture());
|
||||
assertThat(c.getValue().username()).isEqualTo("u");
|
||||
assertThat(c.getValue().roles().get(0).id()).isEqualTo(4L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_passesIdAndRequest() throws Exception {
|
||||
when(users.update(any(), any())).thenReturn(Map.of("id", 5));
|
||||
mvc.perform(put("/api/admin/users/5").contentType(MediaType.APPLICATION_JSON).content("{\"enabled\":false}"))
|
||||
.andExpect(status().isOk());
|
||||
ArgumentCaptor<AdminUserRequest> c = ArgumentCaptor.forClass(AdminUserRequest.class);
|
||||
verify(users).update(org.mockito.ArgumentMatchers.eq(5L), c.capture());
|
||||
assertThat(c.getValue().enabled()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resetPassword_returnsUpdated() throws Exception {
|
||||
when(users.resetPassword(5L)).thenReturn(1);
|
||||
mvc.perform(post("/api/admin/users/5/resetPassword")).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.updated").value(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changePassword_returnsUpdated() throws Exception {
|
||||
when(users.changePassword(5L, "Str0ng#Pass")).thenReturn(1);
|
||||
mvc.perform(post("/api/admin/users/5/changePassword").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"password\":\"Str0ng#Pass\"}"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.updated").value(1));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user