mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-13 21:32:07 +08:00
1、认证auth service/controller单测完善;2、新增mes系统jwt token创建接口和相关配置;
This commit is contained in:
+436
@@ -0,0 +1,436 @@
|
||||
package com.cf.imes.module.system.controller.admin.auth;
|
||||
|
||||
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import com.cf.imes.framework.ip.core.service.IPQueryService;
|
||||
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
|
||||
import com.cf.imes.framework.security.config.SecurityProperties;
|
||||
import com.cf.imes.framework.security.core.LoginUser;
|
||||
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import com.cf.imes.framework.test.core.ut.BaseWebUnitTest;
|
||||
import com.cf.imes.framework.web.config.ChenfengWebAutoConfiguration;
|
||||
import com.cf.imes.module.infra.api.logger.ApiErrorLogApi;
|
||||
import com.cf.imes.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginSmsCheckReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsSendReqVO;
|
||||
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.permission.MenuDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.permission.RoleDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.cf.imes.module.system.enums.logger.LoginLogTypeEnum;
|
||||
import com.cf.imes.module.system.enums.sms.SmsSceneEnum;
|
||||
import com.cf.imes.module.system.service.auth.AdminAuthService;
|
||||
import com.cf.imes.module.system.service.organ.OrganService;
|
||||
import com.cf.imes.module.system.service.permission.MenuService;
|
||||
import com.cf.imes.module.system.service.permission.PermissionService;
|
||||
import com.cf.imes.module.system.service.permission.RoleService;
|
||||
import com.cf.imes.module.system.service.sms.SmsCodeService;
|
||||
import com.cf.imes.module.system.service.user.AdminUserService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anySet;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
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.put;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2026/1/12 16:50
|
||||
*/
|
||||
@WebMvcTest(controllers = AuthController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
|
||||
@Import(ChenfengWebAutoConfiguration.class)
|
||||
@TestPropertySource(properties = {
|
||||
"spring.application.name=test-app"
|
||||
})
|
||||
class AuthControllerTest extends BaseWebUnitTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockitoBean
|
||||
private AdminAuthService authService;
|
||||
|
||||
@MockitoBean
|
||||
private AdminUserService userService;
|
||||
|
||||
@MockitoBean
|
||||
private RoleService roleService;
|
||||
|
||||
@MockitoBean
|
||||
private MenuService menuService;
|
||||
|
||||
@MockitoBean
|
||||
private PermissionService permissionService;
|
||||
|
||||
@MockitoBean
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
@MockitoBean
|
||||
private SmsCodeService smsCodeService;
|
||||
|
||||
@MockitoBean
|
||||
private OrganService organService;
|
||||
|
||||
@MockitoBean
|
||||
private IPQueryService ipQueryService;
|
||||
|
||||
@MockitoBean
|
||||
private ApiErrorLogApi apiErrorLogApi;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private AuthLoginReqVO reqVO;
|
||||
private AuthLoginRespVO respVO;
|
||||
private AuthLoginSmsCheckReqVO smsCheckReq;
|
||||
private AuthSmsSendReqVO smsSendReqVO;
|
||||
private SmsCodeSendReqDTO smsCodeSendReqDTO;
|
||||
private LoginUser loginUser;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
reqVO = new AuthLoginReqVO();
|
||||
reqVO.setUsername("testuser");
|
||||
reqVO.setPassword("12345678");
|
||||
|
||||
respVO = new AuthLoginRespVO();
|
||||
respVO.setAccessToken("fake-token");
|
||||
|
||||
smsCheckReq = new AuthLoginSmsCheckReqVO();
|
||||
smsCheckReq.setMobile("13800000000");
|
||||
smsCheckReq.setSmsCaptchaVerification("123456");
|
||||
|
||||
smsSendReqVO = new AuthSmsSendReqVO();
|
||||
smsSendReqVO.setMobile("13800000000");
|
||||
smsSendReqVO.setScene(SmsSceneEnum.USER_LOGIN_CAPTCHAVERIFICATION.getScene());
|
||||
|
||||
smsCodeSendReqDTO = new SmsCodeSendReqDTO();
|
||||
smsCodeSendReqDTO.setMobile("13800000000");
|
||||
smsCodeSendReqDTO.setScene(SmsSceneEnum.USER_LOGIN_CAPTCHAVERIFICATION.getScene());
|
||||
|
||||
loginUser = new LoginUser();
|
||||
loginUser.setId(1L);
|
||||
loginUser.setOrganId(100L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testManageEndPointLogin() throws Exception {
|
||||
when(authService.login(reqVO, true)).thenReturn(respVO);
|
||||
|
||||
mockMvc.perform(post("/admin-api/system/auth/manage/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(reqVO)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.accessToken").value("fake-token"));
|
||||
|
||||
verify(authService, times(1)).login(reqVO, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogin() throws Exception {
|
||||
when(authService.login(reqVO, false)).thenReturn(respVO);
|
||||
|
||||
mockMvc.perform(post("/admin-api/system/auth/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(reqVO)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.accessToken").value("fake-token"));
|
||||
|
||||
verify(authService, times(1)).login(reqVO, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginCheck() throws Exception {
|
||||
when(authService.loginCheck("13800000000")).thenReturn(true);
|
||||
|
||||
mockMvc.perform(get("/admin-api/system/auth/login/check")
|
||||
.param("mobile", "13800000000"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
|
||||
verify(authService, times(1)).loginCheck("13800000000");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginSmsCheck() throws Exception {
|
||||
when(authService.loginSmsCheck(smsCheckReq)).thenReturn(true);
|
||||
|
||||
mockMvc.perform(post("/admin-api/system/auth/login/sms/check")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(smsCheckReq)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
|
||||
verify(authService, times(1)).loginSmsCheck(smsCheckReq);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogout_withToken() throws Exception {
|
||||
// mock token header/parameter
|
||||
when(securityProperties.getTokenHeader()).thenReturn("Authorization");
|
||||
when(securityProperties.getTokenParameter()).thenReturn("token");
|
||||
|
||||
// 模拟 SecurityFrameworkUtils.obtainAuthorization 静态方法
|
||||
try (MockedStatic<SecurityFrameworkUtils> utilities = mockStatic(SecurityFrameworkUtils.class)) {
|
||||
utilities.when(() -> SecurityFrameworkUtils.obtainAuthorization(any(HttpServletRequest.class),
|
||||
eq("Authorization"), eq("token")))
|
||||
.thenReturn("fake-token");
|
||||
|
||||
mockMvc.perform(post("/admin-api/system/auth/logout"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
|
||||
verify(authService, times(1)).logout("fake-token", LoginLogTypeEnum.LOGOUT_SELF.getType());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogout_withoutToken() throws Exception {
|
||||
when(securityProperties.getTokenHeader()).thenReturn("Authorization");
|
||||
when(securityProperties.getTokenParameter()).thenReturn("token");
|
||||
|
||||
try (MockedStatic<SecurityFrameworkUtils> utilities = mockStatic(SecurityFrameworkUtils.class)) {
|
||||
utilities.when(() -> SecurityFrameworkUtils.obtainAuthorization(any(HttpServletRequest.class),
|
||||
eq("Authorization"), eq("token")))
|
||||
.thenReturn(null); // 没有 token
|
||||
|
||||
mockMvc.perform(post("/admin-api/system/auth/logout"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
|
||||
// 没有 token 时,不调用 logout
|
||||
verify(authService, times(0)).logout(anyString(), anyInt());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendLoginSmsCode() throws Exception {
|
||||
// authService.sendSmsCode 是 void
|
||||
doNothing().when(authService).sendSmsCode(smsSendReqVO);
|
||||
|
||||
mockMvc.perform(post("/admin-api/system/auth/send-sms-code")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(smsSendReqVO)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
|
||||
verify(authService, times(1)).sendSmsCode(smsSendReqVO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendSmsCode() throws Exception {
|
||||
// smsCodeService.sendSmsCode 是 void
|
||||
doNothing().when(smsCodeService).sendSmsCode(any());
|
||||
|
||||
mockMvc.perform(put("/admin-api/system/auth/sms-code")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(smsCodeSendReqDTO)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
|
||||
// 验证 sendSmsCode 被调用
|
||||
ArgumentCaptor<SmsCodeSendReqDTO> captor = ArgumentCaptor.forClass(SmsCodeSendReqDTO.class);
|
||||
verify(smsCodeService, times(1)).sendSmsCode(captor.capture());
|
||||
|
||||
SmsCodeSendReqDTO sent = captor.getValue();
|
||||
assertEquals("13800000000", sent.getMobile());
|
||||
assertNotNull(sent.getCreateIp()); // createIp 已被设置
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMesJwtToken() throws Exception {
|
||||
when(authService.getMesJwtToken()).thenReturn("fake-jwt-token");
|
||||
|
||||
mockMvc.perform(get("/admin-api/system/auth/mes/token"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value("fake-jwt-token"));
|
||||
|
||||
verify(authService, times(1)).getMesJwtToken();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPermissionInfo_success() throws Exception {
|
||||
try (MockedStatic<SecurityFrameworkUtils> utilities = mockStatic(SecurityFrameworkUtils.class)) {
|
||||
utilities.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser);
|
||||
utilities.when(SecurityFrameworkUtils::isManageEndPoint).thenReturn(false);
|
||||
|
||||
AdminUserDO user = new AdminUserDO();
|
||||
user.setId(1L);
|
||||
user.setOrganId(100L);
|
||||
when(userService.getUser(1L)).thenReturn(user);
|
||||
|
||||
OrganizationDO organization = new OrganizationDO();
|
||||
when(organService.validOrgan(100L)).thenReturn(organization);
|
||||
|
||||
when(ipQueryService.serviceEnable()).thenReturn(false);
|
||||
|
||||
Set<Long> roleIds = new HashSet<>(Arrays.asList(10L, 20L));
|
||||
when(permissionService.getUserRoleIdListByUserId(1L)).thenReturn(roleIds);
|
||||
|
||||
RoleDO role1 = new RoleDO(); role1.setId(10L); role1.setStatus(1);
|
||||
RoleDO role2 = new RoleDO(); role2.setId(20L); role2.setStatus(1);
|
||||
when(roleService.getRoleList1(roleIds))
|
||||
.thenReturn(new ArrayList<>(Arrays.asList(role1, role2)));
|
||||
|
||||
Set<Long> menuIds = new HashSet<>(Arrays.asList(1000L, 2000L));
|
||||
when(permissionService.getRoleMenuListByRoleId2(anySet(), eq(100L))).thenReturn(menuIds);
|
||||
|
||||
MenuDO menu1 = new MenuDO(); menu1.setId(1000L); menu1.setStatus(1);
|
||||
MenuDO menu2 = new MenuDO(); menu2.setId(2000L); menu2.setStatus(1);
|
||||
when(menuService.getCustomEndPointMenuList(menuIds))
|
||||
.thenReturn(new ArrayList<>(Arrays.asList(menu1, menu2)));
|
||||
|
||||
mockMvc.perform(get("/admin-api/system/auth/get-permission-info"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").exists());
|
||||
|
||||
// 验证调用顺序/次数(可选)
|
||||
verify(userService, times(1)).getUser(1L);
|
||||
verify(organService, times(1)).validOrgan(100L);
|
||||
verify(roleService, times(1)).getRoleList1(roleIds);
|
||||
verify(menuService, times(1)).getCustomEndPointMenuList(menuIds);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPermissionInfo_success_branch() throws Exception {
|
||||
try (MockedStatic<SecurityFrameworkUtils> utilities = mockStatic(SecurityFrameworkUtils.class)) {
|
||||
utilities.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser);
|
||||
utilities.when(SecurityFrameworkUtils::isManageEndPoint).thenReturn(true);
|
||||
|
||||
AdminUserDO user = new AdminUserDO();
|
||||
user.setId(1L);
|
||||
user.setOrganId(100L);
|
||||
when(userService.getUser(1L)).thenReturn(user);
|
||||
|
||||
OrganizationDO organization = new OrganizationDO();
|
||||
when(organService.validOrgan(100L)).thenReturn(organization);
|
||||
|
||||
IPQueryDataRespDTO ipResp = new IPQueryDataRespDTO();
|
||||
ipResp.setProv("浙江");
|
||||
ipResp.setCity("杭州");
|
||||
ipResp.setArea("西湖");
|
||||
when(ipQueryService.serviceEnable()).thenReturn(true);
|
||||
when(ipQueryService.querySource(anyString())).thenReturn(ipResp);
|
||||
|
||||
Set<Long> roleIds = new HashSet<>(Arrays.asList(10L, 20L));
|
||||
when(permissionService.getUserRoleIdListByUserId(1L)).thenReturn(roleIds);
|
||||
|
||||
RoleDO role1 = new RoleDO(); role1.setId(10L); role1.setStatus(1);
|
||||
RoleDO role2 = new RoleDO(); role2.setId(20L); role2.setStatus(1);
|
||||
when(roleService.getRoleList1(roleIds))
|
||||
.thenReturn(new ArrayList<>(Arrays.asList(role1, role2)));
|
||||
|
||||
Set<Long> menuIds = new HashSet<>(Arrays.asList(1000L, 2000L));
|
||||
when(permissionService.getRoleMenuListByRoleId2(anySet(), eq(100L))).thenReturn(menuIds);
|
||||
|
||||
MenuDO menu1 = new MenuDO(); menu1.setId(1000L); menu1.setStatus(1);
|
||||
MenuDO menu2 = new MenuDO(); menu2.setId(2000L); menu2.setStatus(1);
|
||||
when(menuService.getManageEndPointMenuList(menuIds))
|
||||
.thenReturn(new ArrayList<>(Arrays.asList(menu1, menu2)));
|
||||
|
||||
mockMvc.perform(get("/admin-api/system/auth/get-permission-info"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").exists());
|
||||
|
||||
// 验证调用顺序/次数(可选)
|
||||
verify(userService, times(1)).getUser(1L);
|
||||
verify(organService, times(1)).validOrgan(100L);
|
||||
verify(ipQueryService, times(1)).querySource(anyString());
|
||||
verify(roleService, times(1)).getRoleList1(roleIds);
|
||||
verify(menuService, times(1)).getManageEndPointMenuList(menuIds);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPermissionInfo_success_roleEmpty() throws Exception {
|
||||
try (MockedStatic<SecurityFrameworkUtils> utilities = mockStatic(SecurityFrameworkUtils.class)) {
|
||||
utilities.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser);
|
||||
utilities.when(SecurityFrameworkUtils::isManageEndPoint).thenReturn(true);
|
||||
|
||||
AdminUserDO user = new AdminUserDO();
|
||||
user.setId(1L);
|
||||
user.setOrganId(100L);
|
||||
when(userService.getUser(1L)).thenReturn(user);
|
||||
|
||||
OrganizationDO organization = new OrganizationDO();
|
||||
when(organService.validOrgan(100L)).thenReturn(organization);
|
||||
|
||||
when(ipQueryService.serviceEnable()).thenReturn(false);
|
||||
|
||||
Set<Long> roleIds = new HashSet<>();
|
||||
when(permissionService.getUserRoleIdListByUserId(1L)).thenReturn(roleIds);
|
||||
|
||||
mockMvc.perform(get("/admin-api/system/auth/get-permission-info"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.roles").isEmpty());
|
||||
|
||||
// 验证调用顺序/次数(可选)
|
||||
verify(userService, times(1)).getUser(1L);
|
||||
verify(organService, times(1)).validOrgan(100L);
|
||||
verify(ipQueryService, never()).querySource(anyString());
|
||||
verify(roleService, never()).getRoleList1(roleIds);
|
||||
verify(menuService, never()).getManageEndPointMenuList(anySet());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPermissionInfo_userNull() throws Exception {
|
||||
try (MockedStatic<SecurityFrameworkUtils> utilities = mockStatic(SecurityFrameworkUtils.class)) {
|
||||
utilities.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser);
|
||||
|
||||
when(userService.getUser(1L)).thenReturn(null);
|
||||
|
||||
mockMvc.perform(get("/admin-api/system/auth/get-permission-info"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").isEmpty()); // 返回 null
|
||||
|
||||
verify(userService, times(1)).getUser(1L);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPermissionInfo_noLoginUser() throws Exception {
|
||||
try (MockedStatic<SecurityFrameworkUtils> utilities = mockStatic(SecurityFrameworkUtils.class)) {
|
||||
utilities.when(SecurityFrameworkUtils::getLoginUser).thenReturn(null);
|
||||
|
||||
mockMvc.perform(get("/admin-api/system/auth/get-permission-info"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(GlobalErrorCodeConstants.UNAUTHORIZED.getCode()));
|
||||
}
|
||||
}
|
||||
}
|
||||
+513
-174
@@ -1,242 +1,531 @@
|
||||
package com.cf.imes.module.system.service.auth;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.anji.captcha.model.common.ResponseModel;
|
||||
import com.anji.captcha.service.CaptchaService;
|
||||
import com.cf.imes.framework.common.enums.CommonStatusEnum;
|
||||
import com.cf.imes.framework.common.enums.UserTypeEnum;
|
||||
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import com.cf.imes.framework.common.pojo.CommonResult;
|
||||
import com.cf.imes.framework.ip.core.service.IPQueryService;
|
||||
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
|
||||
import com.cf.imes.framework.organ.core.service.OrganFrameworkService;
|
||||
import com.cf.imes.framework.security.core.service.SecurityFrameworkService;
|
||||
import com.cf.imes.framework.security.test.WithMockLoginUser;
|
||||
import com.cf.imes.framework.test.core.ut.BaseDbUnitTest;
|
||||
import com.cf.imes.module.system.api.sms.SmsCodeApi;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginSmsCheckReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsSendReqVO;
|
||||
import com.cf.imes.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.cf.imes.module.system.enums.logger.LoginLogTypeEnum;
|
||||
import com.cf.imes.module.system.enums.logger.LoginResultEnum;
|
||||
import com.cf.imes.module.system.enums.sms.SmsSceneEnum;
|
||||
import com.cf.imes.module.system.framework.chenfeng.config.ChenfengProperties;
|
||||
import com.cf.imes.module.system.framework.jwt.config.JwtProperties;
|
||||
import com.cf.imes.module.system.framework.product.ProductProperties;
|
||||
import com.cf.imes.module.system.service.dept.DeptService;
|
||||
import com.cf.imes.module.system.service.logger.LoginLogService;
|
||||
import com.cf.imes.module.system.service.oauth2.OAuth2TokenService;
|
||||
import com.cf.imes.module.system.service.organ.OrganService;
|
||||
import com.cf.imes.module.system.service.sms.SmsCodeService;
|
||||
import com.cf.imes.module.system.service.user.AdminUserService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import jakarta.validation.Validator;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import jakarta.validation.Validation;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static com.cf.imes.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static com.cf.imes.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static com.cf.imes.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static com.cf.imes.framework.test.core.util.RandomUtils.randomString;
|
||||
import static com.cf.imes.module.system.enums.ErrorCodeConstants.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@Import(AdminAuthServiceImpl.class)
|
||||
@Import({AdminAuthServiceImpl.class, ChenfengProperties.class})
|
||||
public class AdminAuthServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private AdminAuthServiceImpl authService;
|
||||
|
||||
@MockBean
|
||||
@MockitoBean
|
||||
private AdminUserService userService;
|
||||
@MockBean
|
||||
|
||||
@MockitoBean
|
||||
private CaptchaService captchaService;
|
||||
@MockBean
|
||||
|
||||
@MockitoBean
|
||||
private LoginLogService loginLogService;
|
||||
|
||||
@MockBean
|
||||
@MockitoBean
|
||||
private SmsCodeApi smsCodeApi;
|
||||
@MockBean
|
||||
|
||||
@MockitoBean
|
||||
private OAuth2TokenService oauth2TokenService;
|
||||
|
||||
@MockitoBean
|
||||
private OrganService organService;
|
||||
|
||||
@MockitoBean
|
||||
private OrganFrameworkService organFrameworkService;
|
||||
|
||||
@MockitoBean
|
||||
private SmsCodeService smsCodeService;
|
||||
|
||||
@MockitoBean
|
||||
private IPQueryService ipQueryService;
|
||||
|
||||
@MockitoBean
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
@MockitoBean
|
||||
private SecurityFrameworkService securityFrameworkService;
|
||||
|
||||
@MockitoBean
|
||||
private DeptService deptService;
|
||||
|
||||
@MockitoBean
|
||||
private ProductProperties productProperties;
|
||||
|
||||
@MockitoBean
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
@MockitoBean
|
||||
private ChenfengProperties chenfengProperties;
|
||||
|
||||
@MockitoBean
|
||||
private Validator validator;
|
||||
|
||||
private AuthLoginReqVO reqVO;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
|
||||
// 注入一个 Validator 对象
|
||||
ReflectUtil.setFieldValue(authService, "validator",
|
||||
Validation.buildDefaultValidatorFactory().getValidator());
|
||||
void setUp() {
|
||||
reqVO = new AuthLoginReqVO();
|
||||
reqVO.setUsername("admin");
|
||||
reqVO.setPassword("123456");
|
||||
}
|
||||
|
||||
private AdminUserDO buildUser(){
|
||||
AdminUserDO user = new AdminUserDO();
|
||||
user.setId(1L);
|
||||
user.setUsername("admin");
|
||||
user.setPassword("encodedPwd");
|
||||
user.setOrganId(10L);
|
||||
user.setDeptId(100L);
|
||||
user.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
return user;
|
||||
}
|
||||
|
||||
private AuthLoginReqVO buildLoginReqVO() {
|
||||
AuthLoginReqVO reqVO = new AuthLoginReqVO();
|
||||
reqVO.setUsername("admin");
|
||||
reqVO.setPassword("123456");
|
||||
return reqVO;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticate_success() {
|
||||
// 准备参数
|
||||
String username = randomString();
|
||||
String password = randomString();
|
||||
// mock user 数据
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setUsername(username)
|
||||
.setPassword(password).setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||
when(userService.getUserByUsername(eq(username), null)).thenReturn(user);
|
||||
// mock password 匹配
|
||||
when(userService.isPasswordMatch(eq(password), eq(user.getPassword()))).thenReturn(true);
|
||||
void authenticate_userNotExists_throwBadCredentials() {
|
||||
// mock
|
||||
when(userService.getUserUniqueByUserName("admin"))
|
||||
.thenReturn(null);
|
||||
|
||||
// 调用
|
||||
AdminUserDO loginUser = authService.authenticate(AuthLoginReqVO.builder().username(username).password(password).build());
|
||||
// 校验
|
||||
assertPojoEquals(user, loginUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticate_userNotFound() {
|
||||
// 准备参数
|
||||
String username = randomString();
|
||||
String password = randomString();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> authService.authenticate(AuthLoginReqVO.builder().username(username).password(password).build()),
|
||||
assertServiceException(() -> authService.authenticate(reqVO),
|
||||
AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.BAD_CREDENTIALS.getResult())
|
||||
&& o.getUserId() == null)
|
||||
);
|
||||
|
||||
|
||||
verify(userService).getUserUniqueByUserName("admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticate_badCredentials() {
|
||||
// 准备参数
|
||||
String username = randomString();
|
||||
String password = randomString();
|
||||
// mock user 数据
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setUsername(username)
|
||||
.setPassword(password).setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||
when(userService.getUserByUsername(eq(username), null)).thenReturn(user);
|
||||
void authenticate_passwordNotMatch_throwBadCredentials() {
|
||||
AdminUserDO user = new AdminUserDO();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> authService.authenticate(AuthLoginReqVO.builder().username(username).password(password).build()),
|
||||
when(userService.getUserUniqueByUserName("admin"))
|
||||
.thenReturn(user);
|
||||
when(userService.isPasswordMatch("123456", "encodedPwd"))
|
||||
.thenReturn(false);
|
||||
|
||||
assertServiceException(() -> authService.authenticate(reqVO),
|
||||
AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.BAD_CREDENTIALS.getResult())
|
||||
&& o.getUserId().equals(user.getId()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticate_userDisabled() {
|
||||
// 准备参数
|
||||
String username = randomString();
|
||||
String password = randomString();
|
||||
// mock user 数据
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setUsername(username)
|
||||
.setPassword(password).setStatus(CommonStatusEnum.DISABLE.getStatus()));
|
||||
when(userService.getUserByUsername(eq(username), null)).thenReturn(user);
|
||||
// mock password 匹配
|
||||
when(userService.isPasswordMatch(eq(password), eq(user.getPassword()))).thenReturn(true);
|
||||
void authenticate_userDisabled_throwUserDisabled() {
|
||||
AdminUserDO user = buildUser();
|
||||
user.setStatus(CommonStatusEnum.DISABLE.getStatus());
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> authService.authenticate(AuthLoginReqVO.builder().username(username).password(password).build()),
|
||||
when(userService.getUserUniqueByUserName("admin"))
|
||||
.thenReturn(user);
|
||||
when(userService.isPasswordMatch("123456", "encodedPwd"))
|
||||
.thenReturn(true);
|
||||
|
||||
assertServiceException(() -> authService.authenticate(reqVO),
|
||||
AUTH_LOGIN_USER_DISABLED);
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.USER_DISABLED.getResult())
|
||||
&& o.getUserId().equals(user.getId()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendSmsCode() {
|
||||
// 准备参数
|
||||
String mobile = randomString();
|
||||
Integer scene = randomEle(SmsSceneEnum.values()).getScene();
|
||||
AuthSmsSendReqVO reqVO = new AuthSmsSendReqVO(mobile, scene);
|
||||
// mock 方法(用户信息)
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class);
|
||||
when(userService.getUserByMobile(eq(mobile))).thenReturn(user);
|
||||
void authenticate_success_returnUser() {
|
||||
AdminUserDO user = buildUser();
|
||||
|
||||
when(userService.getUserUniqueByUserName("admin"))
|
||||
.thenReturn(user);
|
||||
when(userService.isPasswordMatch("123456", "encodedPwd"))
|
||||
.thenReturn(true);
|
||||
|
||||
AdminUserDO result = authService.authenticate(reqVO);
|
||||
|
||||
assertSame(user, result);
|
||||
|
||||
verify(organFrameworkService).validOrgan(10L);
|
||||
verify(deptService).validUserLoginDept(100L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateCaptcha_disabled_returnDirectly() {
|
||||
ChenfengProperties.Captcha captcha = new ChenfengProperties.Captcha();
|
||||
captcha.setEnable(false);
|
||||
when(chenfengProperties.getCaptcha()).thenReturn(captcha);
|
||||
|
||||
authService.validateCaptcha(reqVO);
|
||||
|
||||
verifyNoInteractions(captchaService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateCaptcha_success() {
|
||||
when(chenfengProperties.getCaptcha()).thenReturn(new ChenfengProperties.Captcha());
|
||||
|
||||
ResponseModel response = mock(ResponseModel.class);
|
||||
when(response.isSuccess()).thenReturn(true);
|
||||
when(captchaService.verification(any())).thenReturn(response);
|
||||
|
||||
authService.validateCaptcha(reqVO);
|
||||
|
||||
verify(captchaService).verification(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateCaptcha_fail_throwException() {
|
||||
when(chenfengProperties.getCaptcha()).thenReturn(new ChenfengProperties.Captcha());
|
||||
when(validator.validate(any(), any()))
|
||||
.thenReturn(Collections.emptySet());
|
||||
|
||||
ResponseModel response = mock(ResponseModel.class);
|
||||
when(response.isSuccess()).thenReturn(false);
|
||||
when(response.getRepMsg()).thenReturn("captcha error");
|
||||
when(captchaService.verification(any())).thenReturn(response);
|
||||
|
||||
assertServiceException(() -> authService.validateCaptcha(reqVO),
|
||||
AUTH_LOGIN_CAPTCHA_CODE_ERROR);
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_auth_manageendpoint_permission_error() {
|
||||
ChenfengProperties.Captcha captcha = new ChenfengProperties.Captcha();
|
||||
captcha.setEnable(false);
|
||||
when(chenfengProperties.getCaptcha()).thenReturn(captcha);
|
||||
|
||||
// authenticate success
|
||||
AdminUserDO user = buildUser();
|
||||
|
||||
when(userService.getUserUniqueByUserName("admin"))
|
||||
.thenReturn(user);
|
||||
when(userService.isPasswordMatch("123456", "encodedPwd"))
|
||||
.thenReturn(true);
|
||||
|
||||
assertServiceException(() -> authService.login(buildLoginReqVO(), true),
|
||||
AUTH_MANAGEENDPOINT_LOGIN_PERMISSION_ERROR);
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_auth_org_data_code_not_exists() {
|
||||
ChenfengProperties.Captcha captcha = new ChenfengProperties.Captcha();
|
||||
captcha.setEnable(false);
|
||||
when(chenfengProperties.getCaptcha()).thenReturn(captcha);
|
||||
|
||||
// authenticate success
|
||||
AdminUserDO user = buildUser();
|
||||
|
||||
when(userService.getUserUniqueByUserName("admin"))
|
||||
.thenReturn(user);
|
||||
when(userService.isPasswordMatch("123456", "encodedPwd"))
|
||||
.thenReturn(true);
|
||||
when(securityFrameworkService.hasAnyRoles(anyLong(), anyString())).thenReturn(true);
|
||||
|
||||
OrganizationDO organ = new OrganizationDO();
|
||||
organ.setDataSourceCode(null);
|
||||
when(organService.getOrgan(anyLong())).thenReturn(organ);
|
||||
|
||||
assertServiceException(() -> authService.login(buildLoginReqVO(), false),
|
||||
ORGAN_DATA_CODE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_success(){
|
||||
ChenfengProperties.Captcha captcha = new ChenfengProperties.Captcha();
|
||||
captcha.setEnable(false);
|
||||
ChenfengProperties.Gray gray = new ChenfengProperties.Gray();
|
||||
gray.setVersion("2.0.0");
|
||||
|
||||
when(chenfengProperties.getCaptcha()).thenReturn(captcha);
|
||||
when(chenfengProperties.getGray()).thenReturn(gray);
|
||||
|
||||
// authenticate success
|
||||
AdminUserDO user = buildUser();
|
||||
user.setOrganId(1L);
|
||||
|
||||
when(userService.getUserUniqueByUserName("admin"))
|
||||
.thenReturn(user);
|
||||
when(userService.isPasswordMatch("123456", "encodedPwd"))
|
||||
.thenReturn(true);
|
||||
|
||||
OrganizationDO organ = new OrganizationDO();
|
||||
organ.setDataSourceCode("imes_prod");
|
||||
organ.setGrayStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
when(organService.getOrgan(anyLong())).thenReturn(organ);
|
||||
|
||||
authService.login(buildLoginReqVO(), true);
|
||||
|
||||
assertEquals(gray.getVersion(), user.getGrayVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_manageAndDefaultGrayVersion_success(){
|
||||
ChenfengProperties.Captcha captcha = new ChenfengProperties.Captcha();
|
||||
captcha.setEnable(false);
|
||||
ChenfengProperties.Gray gray = new ChenfengProperties.Gray();
|
||||
gray.setVersion("2.0.0");
|
||||
|
||||
when(chenfengProperties.getCaptcha()).thenReturn(captcha);
|
||||
when(chenfengProperties.getGray()).thenReturn(gray);
|
||||
|
||||
// authenticate success
|
||||
AdminUserDO user = buildUser();
|
||||
|
||||
when(userService.getUserUniqueByUserName("admin"))
|
||||
.thenReturn(user);
|
||||
when(userService.isPasswordMatch("123456", "encodedPwd"))
|
||||
.thenReturn(true);
|
||||
|
||||
OrganizationDO organ = new OrganizationDO();
|
||||
organ.setDataSourceCode("imes_prod");
|
||||
organ.setGrayStatus(CommonStatusEnum.DISABLE.getStatus());
|
||||
when(organService.getOrgan(anyLong())).thenReturn(organ);
|
||||
|
||||
authService.login(buildLoginReqVO(), false);
|
||||
|
||||
assertEquals(gray.getDefaultVersion(), user.getGrayDefaultVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordLoginLog_exception_shouldRollback() {
|
||||
// 1. mock TransactionStatus
|
||||
TransactionStatus status = mock(TransactionStatus.class);
|
||||
|
||||
// 2. 让 TransactionTemplate 直接执行回调
|
||||
doAnswer(invocation -> {
|
||||
Consumer<TransactionStatus> callback = invocation.getArgument(0);
|
||||
callback.accept(status);
|
||||
return null;
|
||||
}).when(transactionTemplate).executeWithoutResult(any());
|
||||
|
||||
// 3. 让 try 内部抛异常
|
||||
doThrow(new RuntimeException("db error"))
|
||||
.when(loginLogService).createLoginLog(any());
|
||||
|
||||
// 4. 调用方法
|
||||
authService.createLoginLog(1L, "测试用户", UserTypeEnum.ADMIN, 1L,
|
||||
LoginLogTypeEnum.LOGIN_USERNAME, LoginResultEnum.SUCCESS);
|
||||
|
||||
// 5. 验证回滚被标记
|
||||
verify(status).setRollbackOnly();
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordLoginLog_success() {
|
||||
// 1. mock TransactionStatus
|
||||
TransactionStatus status = mock(TransactionStatus.class);
|
||||
|
||||
// 2. 让 TransactionTemplate 直接执行回调
|
||||
doAnswer(invocation -> {
|
||||
Consumer<TransactionStatus> callback = invocation.getArgument(0);
|
||||
callback.accept(status);
|
||||
return null;
|
||||
}).when(transactionTemplate).executeWithoutResult(any());
|
||||
|
||||
// mock 行为
|
||||
when(ipQueryService.serviceEnable()).thenReturn(true);
|
||||
|
||||
IPQueryDataRespDTO ipResp = new IPQueryDataRespDTO();
|
||||
ipResp.setProv("浙江");
|
||||
ipResp.setCity("杭州");
|
||||
ipResp.setArea("西湖");
|
||||
when(ipQueryService.querySource(nullable(String.class))).thenReturn(ipResp);
|
||||
|
||||
// 4. 调用方法
|
||||
authService.createLoginLog(1L, "测试用户", UserTypeEnum.ADMIN, 1L,
|
||||
LoginLogTypeEnum.LOGIN_USERNAME, LoginResultEnum.SUCCESS);
|
||||
|
||||
// 验证更新用户登录信息
|
||||
verify(userService).updateUserLogin(eq(1L), nullable(String.class));
|
||||
|
||||
// 不应回滚
|
||||
verify(status, never()).setRollbackOnly();
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordLoginLog_errorlog_success() {
|
||||
// 1. mock TransactionStatus
|
||||
TransactionStatus status = mock(TransactionStatus.class);
|
||||
|
||||
// 2. 让 TransactionTemplate 直接执行回调
|
||||
doAnswer(invocation -> {
|
||||
Consumer<TransactionStatus> callback = invocation.getArgument(0);
|
||||
callback.accept(status);
|
||||
return null;
|
||||
}).when(transactionTemplate).executeWithoutResult(any());
|
||||
|
||||
// mock 行为
|
||||
when(ipQueryService.serviceEnable()).thenReturn(false);
|
||||
|
||||
// 4. 调用方法
|
||||
authService.createLoginLog(null, "测试用户", UserTypeEnum.ADMIN, 1L,
|
||||
LoginLogTypeEnum.LOGIN_USERNAME, LoginResultEnum.BAD_CREDENTIALS);
|
||||
|
||||
// 验证更新用户登录信息
|
||||
verify(userService, never()).updateUserLogin(anyLong(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loginCheck_userNotExist_shouldReturnFalse_andRecordLog() {
|
||||
// given
|
||||
String mobile = "13800000000";
|
||||
when(userService.getUserUniqueByUserName(mobile)).thenReturn(null);
|
||||
|
||||
// when
|
||||
boolean result = authService.loginCheck(mobile);
|
||||
|
||||
// then
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void loginCheck_userExist_passwordEmpty_shouldReturnTrue() {
|
||||
// given
|
||||
String mobile = "13800000000";
|
||||
AdminUserDO user = new AdminUserDO();
|
||||
user.setPassword(""); // 或 null
|
||||
|
||||
when(userService.getUserUniqueByUserName(mobile)).thenReturn(user);
|
||||
|
||||
// when
|
||||
boolean result = authService.loginCheck(mobile);
|
||||
|
||||
// then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void loginCheck_userExist_passwordNotEmpty_shouldReturnFalse() {
|
||||
// given
|
||||
String mobile = "13800000000";
|
||||
AdminUserDO user = new AdminUserDO();
|
||||
user.setPassword("encrypted-password");
|
||||
|
||||
when(userService.getUserUniqueByUserName(mobile)).thenReturn(user);
|
||||
|
||||
// when
|
||||
boolean result = authService.loginCheck(mobile);
|
||||
|
||||
// then
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginSmsCheck_userNotExist() {
|
||||
String mobile = "13800000000";
|
||||
AuthLoginSmsCheckReqVO reqVO = new AuthLoginSmsCheckReqVO();
|
||||
reqVO.setMobile(mobile);
|
||||
|
||||
// 模拟用户不存在
|
||||
when(userService.getUserUniqueByUserName(mobile)).thenReturn(null);
|
||||
|
||||
boolean result = authService.loginSmsCheck(reqVO);
|
||||
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginSmsCheck_userExistSmsValid() {
|
||||
String mobile = "13800000000";
|
||||
AuthLoginSmsCheckReqVO reqVO = new AuthLoginSmsCheckReqVO();
|
||||
reqVO.setMobile(mobile);
|
||||
|
||||
// 模拟用户存在
|
||||
AdminUserDO user = new AdminUserDO();
|
||||
when(userService.getUserUniqueByUserName(mobile)).thenReturn(user);
|
||||
|
||||
// 模拟验证码校验成功(不抛异常)
|
||||
doNothing().when(smsCodeService).validateSmsCode(reqVO);
|
||||
|
||||
boolean result = authService.loginSmsCheck(reqVO);
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
// 验证 smsCodeService.validateSmsCode 被调用
|
||||
verify(smsCodeService, times(1)).validateSmsCode(reqVO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendSmsCode_mobileNotExist() {
|
||||
String mobile = "13800000000";
|
||||
AuthSmsSendReqVO reqVO = new AuthSmsSendReqVO();
|
||||
reqVO.setMobile(mobile);
|
||||
|
||||
// 模拟用户不存在
|
||||
when(userService.getUserByMobile(mobile)).thenReturn(null);
|
||||
|
||||
assertServiceException(() -> authService.sendSmsCode(reqVO),
|
||||
AUTH_MOBILE_NOT_EXISTS);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendSmsCode_mobileExists() {
|
||||
String mobile = "13800000000";
|
||||
AuthSmsSendReqVO reqVO = new AuthSmsSendReqVO();
|
||||
reqVO.setMobile(mobile);
|
||||
|
||||
// 模拟用户存在
|
||||
when(userService.getUserByMobile(mobile)).thenReturn(new AdminUserDO());
|
||||
|
||||
// 模拟 smsCodeApi 调用成功
|
||||
CommonResult<Boolean> mockResult = CommonResult.success(true);
|
||||
when(smsCodeApi.sendSmsCode(any())).thenReturn(mockResult);
|
||||
|
||||
// 调用
|
||||
authService.sendSmsCode(reqVO);
|
||||
// 断言
|
||||
verify(smsCodeApi).sendSmsCode(argThat(sendReqDTO -> {
|
||||
assertEquals(mobile, sendReqDTO.getMobile());
|
||||
assertEquals(scene, sendReqDTO.getScene());
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
@Test
|
||||
public void testValidateCaptcha_successWithEnable() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
|
||||
// mock 验证码打开
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
|
||||
// mock 验证通过
|
||||
when(captchaService.verification(argThat(captchaVO -> {
|
||||
assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());
|
||||
return true;
|
||||
}))).thenReturn(ResponseModel.success());
|
||||
|
||||
// 调用,无需断言
|
||||
authService.validateCaptcha(reqVO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCaptcha_successWithDisable() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
|
||||
// mock 验证码关闭
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", false);
|
||||
|
||||
// 调用,无需断言
|
||||
authService.validateCaptcha(reqVO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateCaptcha_constraintViolationException() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class).setCaptchaVerification(null);
|
||||
|
||||
// mock 验证码打开
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
|
||||
|
||||
// 调用,并断言异常
|
||||
assertThrows(ConstraintViolationException.class, () -> authService.validateCaptcha(reqVO),
|
||||
"验证码不能为空");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCaptcha_fail() {
|
||||
// 准备参数
|
||||
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
|
||||
|
||||
// mock 验证码打开
|
||||
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
|
||||
// mock 验证通过
|
||||
when(captchaService.verification(argThat(captchaVO -> {
|
||||
assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());
|
||||
return true;
|
||||
}))).thenReturn(ResponseModel.errorMsg("就是不对"));
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> authService.validateCaptcha(reqVO), AUTH_LOGIN_CAPTCHA_CODE_ERROR, "就是不对");
|
||||
// 校验调用参数
|
||||
verify(loginLogService).createLoginLog(
|
||||
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
|
||||
&& o.getResult().equals(LoginResultEnum.CAPTCHA_CODE_ERROR.getResult()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefreshToken() {
|
||||
// 准备参数
|
||||
String refreshToken = randomString();
|
||||
// mock 方法
|
||||
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class);
|
||||
when(oauth2TokenService.refreshAccessToken(eq(refreshToken), eq("default")))
|
||||
.thenReturn(accessTokenDO);
|
||||
|
||||
// 调用
|
||||
AuthLoginRespVO loginRespVO = authService.refreshToken(refreshToken);
|
||||
// 断言
|
||||
assertPojoEquals(accessTokenDO, loginRespVO);
|
||||
// 验证 smsCodeApi 被调用一次
|
||||
verify(smsCodeApi, times(1)).sendSmsCode(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -270,4 +559,54 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
|
||||
verify(loginLogService, never()).createLoginLog(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordLogoutLog_success() {
|
||||
|
||||
// mock 行为
|
||||
when(ipQueryService.serviceEnable()).thenReturn(true);
|
||||
|
||||
IPQueryDataRespDTO ipResp = new IPQueryDataRespDTO();
|
||||
ipResp.setProv("浙江");
|
||||
ipResp.setCity("杭州");
|
||||
ipResp.setArea("西湖");
|
||||
when(ipQueryService.querySource(nullable(String.class))).thenReturn(ipResp);
|
||||
|
||||
// 4. 调用方法
|
||||
authService.createLogoutLog(1L, "测试用户", UserTypeEnum.ADMIN.getValue(), LoginLogTypeEnum.LOGOUT_SELF.getType());
|
||||
|
||||
// 验证更新用户登录信息
|
||||
verify(loginLogService).createLoginLog(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockLoginUser(userId = 123L, organId = 456L)
|
||||
void testGetMesJwtToken() {
|
||||
|
||||
// mock jwtProperties
|
||||
when(jwtProperties.getMesTokenTtl()).thenReturn(Duration.ofHours(1));
|
||||
when(jwtProperties.getMesSecret()).thenReturn("12345678901234567890123456789012"); // 至少 32 字节
|
||||
when(jwtProperties.getMesAud()).thenReturn("test-aud");
|
||||
|
||||
String token = authService.getMesJwtToken();
|
||||
assertNotNull(token);
|
||||
|
||||
// 验证 token 内容
|
||||
SecretKey secretKey = Keys.hmacShaKeyFor(jwtProperties.getMesSecret().getBytes(StandardCharsets.UTF_8));
|
||||
Claims claims = Jwts.parserBuilder()
|
||||
.setSigningKey(secretKey)
|
||||
.build()
|
||||
.parseClaimsJws(token)
|
||||
.getBody();
|
||||
|
||||
assertEquals(123L, claims.get("userId", Long.class));
|
||||
assertEquals(456L, claims.get("companyId", Long.class));
|
||||
assertEquals("test-aud", claims.getAudience());
|
||||
assertNotNull(claims.getExpiration());
|
||||
assertNotNull(claims.getNotBefore());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMesJwtToken_notLogin() {
|
||||
assertServiceException(() -> authService.getMesJwtToken(), GlobalErrorCodeConstants.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -5,7 +5,7 @@ import com.cf.imes.framework.common.enums.CommonStatusEnum;
|
||||
import com.cf.imes.framework.common.pojo.PageResult;
|
||||
import com.cf.imes.framework.security.test.WithMockLoginUser;
|
||||
import com.cf.imes.framework.test.core.ut.BaseDbAndRedisUnitTest;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.JwtConfig;
|
||||
import com.cf.imes.module.system.framework.jwt.config.JwtProperties;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigPageReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigSaveReqVO;
|
||||
@@ -58,7 +58,7 @@ class TokenConfigServiceImplTest extends BaseDbAndRedisUnitTest {
|
||||
private OrganService organService;
|
||||
|
||||
@MockitoBean
|
||||
private JwtConfig jwtConfig;
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
@@ -68,7 +68,7 @@ class TokenConfigServiceImplTest extends BaseDbAndRedisUnitTest {
|
||||
organ.setName("测试组织");
|
||||
|
||||
when(organService.validOrgan(1L)).thenReturn(organ);
|
||||
when(jwtConfig.getSecret()).thenReturn("BjEM0tnL3W5zYLQ6pllol49uYVXe+f66pHyOM/tkGWg=");
|
||||
when(jwtProperties.getSecret()).thenReturn("BjEM0tnL3W5zYLQ6pllol49uYVXe+f66pHyOM/tkGWg=");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user