| | |
| | | } |
| | | |
| | | /** |
| | | * 验证码校验成功后不应删除缓存,保证 5 分钟有效期内可重复提交。 |
| | | */ |
| | | @Test |
| | | void validateLoginCodeShouldKeepCachedCodeAfterSuccess() { |
| | | when(stringRedisTemplate.opsForValue()).thenReturn(valueOperations); |
| | | when(valueOperations.get("app:sms:login:13800138000")).thenReturn("123456"); |
| | | |
| | | assertDoesNotThrow(() -> appSmsCodeService.validateLoginCode("13800138000", "123456")); |
| | | |
| | | verify(stringRedisTemplate, never()).delete(anyString()); |
| | | } |
| | | |
| | | /** |
| | | * 同一验证码在缓存 TTL 内多次提交都应校验通过。 |
| | | */ |
| | | @Test |
| | | void validateLoginCodeShouldAllowRepeatedSubmissionsWithinTtl() { |
| | | when(stringRedisTemplate.opsForValue()).thenReturn(valueOperations); |
| | | when(valueOperations.get("app:sms:login:13800138000")).thenReturn("123456"); |
| | | |
| | | assertDoesNotThrow(() -> appSmsCodeService.validateLoginCode("13800138000", "123456")); |
| | | assertDoesNotThrow(() -> appSmsCodeService.validateLoginCode("13800138000", "123456")); |
| | | |
| | | verify(stringRedisTemplate, never()).delete(anyString()); |
| | | } |
| | | |
| | | /** |
| | | * 缓存中存在验证码但输入不一致时仍应提示验证码错误。 |
| | | */ |
| | | @Test |
| | | void validateLoginCodeShouldRejectMismatchedCode() { |
| | | when(stringRedisTemplate.opsForValue()).thenReturn(valueOperations); |
| | | when(valueOperations.get("app:sms:login:13800138000")).thenReturn("123456"); |
| | | |
| | | IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, |
| | | () -> appSmsCodeService.validateLoginCode("13800138000", "654321")); |
| | | |
| | | assertEquals("验证码错误", exception.getMessage()); |
| | | verify(stringRedisTemplate, never()).delete(anyString()); |
| | | } |
| | | |
| | | /** |
| | | * 短信发送成功后才应写入验证码缓存。 |
| | | */ |
| | | @Test |