Przeglądaj źródła

feat(admin): skip captcha verification in test mode

Sisyphus 4 miesięcy temu
rodzic
commit
2c3fe95b41

+ 11 - 4
cfc-backend/src/main/java/com/etotem/cfc/controller/admin/AdminAuthController.java

@@ -12,6 +12,7 @@ import com.etotem.cfc.service.UserService;
 import com.etotem.cfc.service.VerificationCodeService;
 import lombok.extern.slf4j.Slf4j;
 import javax.annotation.Resource;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
@@ -41,6 +42,9 @@ public class AdminAuthController {
     @Resource
     private LoginAttemptService loginAttemptService;
 
+    @Value("${admin.skip-captcha:false}")
+    private boolean skipCaptcha;
+
     /**
      * 发送管理员登录验证码
      */
@@ -95,10 +99,13 @@ public class AdminAuthController {
             return Result.error("登录尝试过多,请" + (remaining / 60000 + 1) + "分钟后重试");
         }
 
-        boolean valid = verificationCodeService.verifyCode(dto.getPhone(), dto.getCode(), "admin_login");
-        if (!valid) {
-            loginAttemptService.recordFailure(lockKey);
-            return Result.error("验证码错误或已过期");
+        // 测试环境可跳过验证码验证(配置 admin.skip-captcha: true)
+        if (!skipCaptcha) {
+            boolean valid = verificationCodeService.verifyCode(dto.getPhone(), dto.getCode(), "admin_login");
+            if (!valid) {
+                loginAttemptService.recordFailure(lockKey);
+                return Result.error("验证码错误或已过期");
+            }
         }
 
         User user = userMapper.selectOne(new LambdaQueryWrapper<User>()

+ 4 - 1
cfc-backend/src/main/resources/application.yml

@@ -89,7 +89,10 @@ logging:
   level:
     com.etotem.cfc: debug
 
-# Danshop 商城系统配置
+# Admin 登录配置
+admin:
+  skip-captcha: true  # 测试环境跳过验证码,方便自动化测试
+
 danshop:
   base-url: http://localhost:8888
   api-url: http://localhost:8888/api/internal

+ 1 - 1
cfc-backend/src/test/java/com/etotem/cfc/controller/DimensionAdminControllerTest.java

@@ -99,7 +99,7 @@ public class DimensionAdminControllerTest {
 
     @Test
     void deleteNorm_deletesNonExistent() {
-        doNothing().when(normMapper).deleteById(999L);
+        when(normMapper.deleteById(999L)).thenReturn(0);
 
         Result<String> result = controller.deleteNorm(999L);