# Conflicts:
#	yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
#	yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java
This commit is contained in:
YunaiV 2025-01-25 10:10:19 +08:00
commit 3682bcda3e
2 changed files with 10 additions and 30 deletions

View File

@ -28,13 +28,14 @@ import com.google.common.annotations.VisibleForTesting;
import com.xingyuv.captcha.model.common.ResponseModel; import com.xingyuv.captcha.model.common.ResponseModel;
import com.xingyuv.captcha.model.vo.CaptchaVO; import com.xingyuv.captcha.model.vo.CaptchaVO;
import com.xingyuv.captcha.service.CaptchaService; import com.xingyuv.captcha.service.CaptchaService;
import jakarta.annotation.Resource;
import jakarta.validation.Validator;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.Objects; import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -71,6 +72,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
* 验证码的开关默认为 true * 验证码的开关默认为 true
*/ */
@Value("${yudao.captcha.enable:true}") @Value("${yudao.captcha.enable:true}")
@Setter // 为了单测开启或者关闭验证码
private Boolean captchaEnable; private Boolean captchaEnable;
@Override @Override

View File

@ -21,16 +21,14 @@ import cn.iocoder.yudao.module.system.service.social.SocialUserService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService; import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.xingyuv.captcha.model.common.ResponseModel; import com.xingyuv.captcha.model.common.ResponseModel;
import com.xingyuv.captcha.service.CaptchaService; import com.xingyuv.captcha.service.CaptchaService;
import jakarta.annotation.Resource;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
@ -38,7 +36,6 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -67,7 +64,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
ReflectUtil.setFieldValue(authService, "captchaEnable", true); authService.setCaptchaEnable(true);
// 注入一个 Validator 对象 // 注入一个 Validator 对象
ReflectUtil.setFieldValue(authService, "validator", ReflectUtil.setFieldValue(authService, "validator",
Validation.buildDefaultValidatorFactory().getValidator()); Validation.buildDefaultValidatorFactory().getValidator());
@ -157,7 +154,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
.setSocialType(randomEle(SocialTypeEnum.values()).getType())); .setSocialType(randomEle(SocialTypeEnum.values()).getType()));
// mock 验证码正确 // mock 验证码正确
ReflectUtil.setFieldValue(authService, "captchaEnable", false); authService.setCaptchaEnable(false);
// mock user 数据 // mock user 数据
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username") AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username")
.setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus())); .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus()));
@ -270,8 +267,6 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
// 准备参数 // 准备参数
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
// mock 验证码打开
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
// mock 验证通过 // mock 验证通过
when(captchaService.verification(argThat(captchaVO -> { when(captchaService.verification(argThat(captchaVO -> {
assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification()); assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());
@ -288,34 +283,17 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
// mock 验证码关闭 // mock 验证码关闭
ReflectUtil.setFieldValue(authService, "captchaEnable", false); authService.setCaptchaEnable(false);
// 调用无需断言 // 调用无需断言
authService.validateCaptcha(reqVO); authService.validateCaptcha(reqVO);
} }
@Test
public void testValidateCaptcha_constraintViolationException() {
// 准备参数
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
reqVO.setCaptchaVerification(null);
// mock 验证码打开
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
// 调用并断言异常
assertThrows(ConstraintViolationException.class, () -> authService.validateCaptcha(reqVO),
"验证码不能为空");
}
@Test @Test
public void testCaptcha_fail() { public void testCaptcha_fail() {
// 准备参数 // 准备参数
AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
// mock 验证码打开
ReflectUtil.setFieldValue(authService, "captchaEnable", true);
// mock 验证通过 // mock 验证通过
when(captchaService.verification(argThat(captchaVO -> { when(captchaService.verification(argThat(captchaVO -> {
assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification()); assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());