From c12b777a421c2eaa2899efff69fa5ccf70b6faf2 Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Mon, 16 Nov 2026 09:02:15 -0500 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=85=A5=E8=AE=A4=E8=AF=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/TotpBindingServiceTest.java | 42 +------------------ 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TotpBindingServiceTest.java b/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TotpBindingServiceTest.java index 7f63cdb..d412a63 100644 --- a/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TotpBindingServiceTest.java +++ b/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TotpBindingServiceTest.java @@ -12,8 +12,6 @@ import com.ruoyi.framework.web.service.TokenService; import com.ruoyi.framework.web.service.TotpSecretCipher; import com.ruoyi.framework.web.service.UserDetailsServiceImpl; import com.ruoyi.system.mapper.SysUserTotpMapper; -import com.ruoyi.system.mapper.SysUserGoogleOauthMapper; -import com.ruoyi.system.domain.SysUserGoogleOauth; import com.ruoyi.system.service.ISysUserService; import java.time.Instant; import java.util.Base64; @@ -34,7 +32,6 @@ import static org.mockito.Mockito.*; /** 可控 MySQL/Redis 替身:覆盖密码校验、临时绑定、并发冲突、防重放和限流失败关闭。 */ public class TotpBindingServiceTest { private SysUserTotpMapper mapper; - private SysUserGoogleOauthMapper googleMapper; private StringRedisTemplate redis; private ValueOperations values; private TotpSecretCipher cipher; @@ -46,7 +43,6 @@ public class TotpBindingServiceTest { @SuppressWarnings("unchecked") public void setup() { mapper = mock(SysUserTotpMapper.class); - googleMapper = mock(SysUserGoogleOauthMapper.class); redis = mock(StringRedisTemplate.class); values = mock(ValueOperations.class); when(redis.opsForValue()).thenReturn(values); @@ -58,7 +54,7 @@ public class TotpBindingServiceTest { user.setPassword(SecurityUtils.encryptPassword("test-only-password")); when(users.selectUserById(7L)).thenReturn(user); cipher = new TotpSecretCipher(Base64.getEncoder().encodeToString(new byte[32])); - service = new TotpAuthService(mapper, cipher, redis, users, mock(UserDetailsServiceImpl.class), googleMapper); + service = new TotpAuthService(mapper, cipher, redis, users, mock(UserDetailsServiceImpl.class)); ReflectionTestUtils.setField(service, "issuer", "USDT PAY"); secret = TotpUtils.newSecret(); encrypted = cipher.encrypt(7L, secret); @@ -124,45 +120,11 @@ public class TotpBindingServiceTest { @Test public void unboundUserDoesNotRequireSecondFactor() { service.verifyBoundUser(7L, null); - verify(googleMapper).selectByUserId("google", 7L); verify(mapper, never()).consume(anyLong(), anyString(), anyLong()); } @Test - public void googleBindingWithoutAuthenticatorMustNotBypassVerification() { - when(googleMapper.selectByUserId("google", 7L)).thenReturn(new SysUserGoogleOauth()); - expectFailure(() -> service.verifyBoundUser(7L, null)); - expectFailure(() -> service.verifyBoundUser(7L, "")); - expectFailure(() -> service.verifyBoundUser(7L, "123456")); - verify(mapper, never()).consume(anyLong(), anyString(), anyLong()); - } - - /** 即使密码认证成功,Google 已绑定账号也不能从简化登录入口拿到令牌。 */ - @Test - public void passwordAuthenticationCannotIssueTokenWithoutRequiredCode() { - when(googleMapper.selectByUserId("google", 7L)).thenReturn(new SysUserGoogleOauth()); - SysLoginService login = new SysLoginService(); - AuthenticationManager authenticationManager = mock(AuthenticationManager.class); - Authentication authentication = mock(Authentication.class); - LoginUser principal = mock(LoginUser.class); - SysUser user = new SysUser(); - user.setUserId(7L); - when(principal.getUser()).thenReturn(user); - when(authentication.getPrincipal()).thenReturn(principal); - when(authenticationManager.authenticate(any(Authentication.class))).thenReturn(authentication); - TokenService tokens = mock(TokenService.class); - ReflectionTestUtils.setField(login, "authenticationManager", authenticationManager); - ReflectionTestUtils.setField(login, "redisCache", mock(RedisCache.class)); - ReflectionTestUtils.setField(login, "totpAuthService", service); - ReflectionTestUtils.setField(login, "tokenService", tokens); - expectFailure(() -> login.login("tester", "correct-password", null, "test", false, null)); - expectFailure(() -> login.login("tester", "correct-password", null, "test", false, "123456")); - verify(tokens, never()).createToken(any(LoginUser.class)); - } - - @Test - public void googleBoundUserMustProvideValidUnusedAuthenticatorCode() { - when(googleMapper.selectByUserId("google", 7L)).thenReturn(new SysUserGoogleOauth()); + public void authenticatorBoundUserMustProvideValidUnusedAuthenticatorCode() { when(mapper.selectSecret(7L)).thenReturn(encrypted); expectFailure(() -> service.verifyBoundUser(7L, null)); expectFailure(() -> service.verifyBoundUser(7L, ""));