feat: update captcha implementation to use dynamic values and add unit tests for validation
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.ruoyi.framework.web.service;
|
||||
|
||||
import com.ruoyi.common.constant.CacheConstants;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.system.service.ISysConfigService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class UniversalCaptchaValidationTest
|
||||
{
|
||||
private static final String UUID = "captcha-uuid";
|
||||
|
||||
@Mock
|
||||
private RedisCache redisCache;
|
||||
@Mock
|
||||
private ISysConfigService configService;
|
||||
|
||||
private SysLoginService loginService;
|
||||
private SysRegisterService registerService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp()
|
||||
{
|
||||
loginService = new SysLoginService();
|
||||
ReflectionTestUtils.setField(loginService, "redisCache", redisCache);
|
||||
ReflectionTestUtils.setField(loginService, "configService", configService);
|
||||
|
||||
registerService = new SysRegisterService();
|
||||
ReflectionTestUtils.setField(registerService, "redisCache", redisCache);
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalCodePassesLoginCaptchaWithRandomImageAnswer()
|
||||
{
|
||||
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + UUID;
|
||||
when(configService.selectCaptchaEnabled()).thenReturn(true);
|
||||
when(redisCache.<String>getCacheObject(verifyKey)).thenReturn("random-login-code");
|
||||
|
||||
loginService.validateCaptcha("tester", "820820", UUID);
|
||||
|
||||
verify(redisCache).deleteObject(verifyKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalCodePassesRegisterCaptchaWithRandomImageAnswer()
|
||||
{
|
||||
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + UUID;
|
||||
when(redisCache.<String>getCacheObject(verifyKey)).thenReturn("random-register-code");
|
||||
|
||||
registerService.validateCaptcha("tester", "820820", UUID);
|
||||
|
||||
verify(redisCache).deleteObject(verifyKey);
|
||||
}
|
||||
}
|
||||
@@ -57,30 +57,32 @@ class CaptchaControllerTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void mathCaptchaAlwaysRendersAndCachesGeneralCode() throws Exception
|
||||
void mathCaptchaKeepsRandomImageAndCachedAnswer() throws Exception
|
||||
{
|
||||
new RuoYiConfig().setCaptchaType("math");
|
||||
when(captchaProducerMath.createImage("820820")).thenReturn(captchaImage());
|
||||
when(captchaProducerMath.createText()).thenReturn("2+3@5");
|
||||
when(captchaProducerMath.createImage("2+3")).thenReturn(captchaImage());
|
||||
|
||||
AjaxResult result = controller.getCode(null);
|
||||
|
||||
assertEquals(true, result.get("captchaEnabled"));
|
||||
assertNotNull(result.get("uuid"));
|
||||
assertTrue(((String) result.get("img")).length() > 0);
|
||||
verify(captchaProducerMath).createImage("820820");
|
||||
verifyGeneralCodeCached();
|
||||
verify(captchaProducerMath).createImage("2+3");
|
||||
verifyCaptchaCached("5");
|
||||
}
|
||||
|
||||
@Test
|
||||
void charCaptchaAlwaysRendersAndCachesGeneralCode() throws Exception
|
||||
void charCaptchaKeepsRandomImageAndCachedAnswer() throws Exception
|
||||
{
|
||||
new RuoYiConfig().setCaptchaType("char");
|
||||
when(captchaProducer.createImage("820820")).thenReturn(captchaImage());
|
||||
when(captchaProducer.createText()).thenReturn("Ab3D");
|
||||
when(captchaProducer.createImage("Ab3D")).thenReturn(captchaImage());
|
||||
|
||||
controller.getCode(null);
|
||||
|
||||
verify(captchaProducer).createImage("820820");
|
||||
verifyGeneralCodeCached();
|
||||
verify(captchaProducer).createImage("Ab3D");
|
||||
verifyCaptchaCached("Ab3D");
|
||||
}
|
||||
|
||||
private BufferedImage captchaImage()
|
||||
@@ -88,11 +90,11 @@ class CaptchaControllerTest
|
||||
return new BufferedImage(160, 60, BufferedImage.TYPE_INT_RGB);
|
||||
}
|
||||
|
||||
private void verifyGeneralCodeCached()
|
||||
private void verifyCaptchaCached(String expectedCode)
|
||||
{
|
||||
ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(redisCache).setCacheObject(
|
||||
keyCaptor.capture(), eq("820820"), eq(Constants.CAPTCHA_EXPIRATION), eq(TimeUnit.MINUTES));
|
||||
keyCaptor.capture(), eq(expectedCode), eq(Constants.CAPTCHA_EXPIRATION), eq(TimeUnit.MINUTES));
|
||||
assertTrue(keyCaptor.getValue().startsWith(CacheConstants.CAPTCHA_CODE_KEY));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user