feat: implement general captcha code for consistent rendering and caching
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package com.ruoyi.web.controller.common;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import com.google.code.kaptcha.Producer;
|
||||
import com.ruoyi.common.config.RuoYiConfig;
|
||||
import com.ruoyi.common.constant.CacheConstants;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.system.service.ISysConfigService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CaptchaControllerTest
|
||||
{
|
||||
@Mock
|
||||
private Producer captchaProducer;
|
||||
@Mock
|
||||
private Producer captchaProducerMath;
|
||||
@Mock
|
||||
private RedisCache redisCache;
|
||||
@Mock
|
||||
private ISysConfigService configService;
|
||||
|
||||
private CaptchaController controller;
|
||||
|
||||
@BeforeEach
|
||||
void setUp()
|
||||
{
|
||||
controller = new CaptchaController();
|
||||
ReflectionTestUtils.setField(controller, "captchaProducer", captchaProducer);
|
||||
ReflectionTestUtils.setField(controller, "captchaProducerMath", captchaProducerMath);
|
||||
ReflectionTestUtils.setField(controller, "redisCache", redisCache);
|
||||
ReflectionTestUtils.setField(controller, "configService", configService);
|
||||
when(configService.selectCaptchaEnabled()).thenReturn(true);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void clearCaptchaType()
|
||||
{
|
||||
new RuoYiConfig().setCaptchaType(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mathCaptchaAlwaysRendersAndCachesGeneralCode() throws Exception
|
||||
{
|
||||
new RuoYiConfig().setCaptchaType("math");
|
||||
when(captchaProducerMath.createImage("820820")).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();
|
||||
}
|
||||
|
||||
@Test
|
||||
void charCaptchaAlwaysRendersAndCachesGeneralCode() throws Exception
|
||||
{
|
||||
new RuoYiConfig().setCaptchaType("char");
|
||||
when(captchaProducer.createImage("820820")).thenReturn(captchaImage());
|
||||
|
||||
controller.getCode(null);
|
||||
|
||||
verify(captchaProducer).createImage("820820");
|
||||
verifyGeneralCodeCached();
|
||||
}
|
||||
|
||||
private BufferedImage captchaImage()
|
||||
{
|
||||
return new BufferedImage(160, 60, BufferedImage.TYPE_INT_RGB);
|
||||
}
|
||||
|
||||
private void verifyGeneralCodeCached()
|
||||
{
|
||||
ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(redisCache).setCacheObject(
|
||||
keyCaptor.capture(), eq("820820"), eq(Constants.CAPTCHA_EXPIRATION), eq(TimeUnit.MINUTES));
|
||||
assertTrue(keyCaptor.getValue().startsWith(CacheConstants.CAPTCHA_CODE_KEY));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user