|
@@ -5,6 +5,8 @@ import com.auth0.jwt.algorithms.Algorithm;
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
|
|
+import com.ruoyi.system.domain.InfoUser;
|
|
|
|
|
+import com.ruoyi.system.mapper.InfoUserMapper;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
@@ -29,6 +31,7 @@ import static org.mockito.Mockito.when;
|
|
|
class AuthAspectTest {
|
|
class AuthAspectTest {
|
|
|
|
|
|
|
|
private RedisCache redisCache;
|
|
private RedisCache redisCache;
|
|
|
|
|
+ private InfoUserMapper infoUserMapper;
|
|
|
private HttpServletRequest request;
|
|
private HttpServletRequest request;
|
|
|
private ProceedingJoinPoint joinPoint;
|
|
private ProceedingJoinPoint joinPoint;
|
|
|
private MethodSignature signature;
|
|
private MethodSignature signature;
|
|
@@ -37,10 +40,11 @@ class AuthAspectTest {
|
|
|
@BeforeEach
|
|
@BeforeEach
|
|
|
void setUp() {
|
|
void setUp() {
|
|
|
redisCache = mock(RedisCache.class);
|
|
redisCache = mock(RedisCache.class);
|
|
|
|
|
+ infoUserMapper = mock(InfoUserMapper.class);
|
|
|
request = mock(HttpServletRequest.class);
|
|
request = mock(HttpServletRequest.class);
|
|
|
joinPoint = mock(ProceedingJoinPoint.class);
|
|
joinPoint = mock(ProceedingJoinPoint.class);
|
|
|
signature = mock(MethodSignature.class);
|
|
signature = mock(MethodSignature.class);
|
|
|
- aspect = new AuthAspect(redisCache);
|
|
|
|
|
|
|
+ aspect = new AuthAspect(redisCache, infoUserMapper);
|
|
|
aspect.setHttpServletRequest(request);
|
|
aspect.setHttpServletRequest(request);
|
|
|
when(joinPoint.getSignature()).thenReturn(signature);
|
|
when(joinPoint.getSignature()).thenReturn(signature);
|
|
|
}
|
|
}
|
|
@@ -75,11 +79,62 @@ class AuthAspectTest {
|
|
|
verify(request).setAttribute(AuthContext.JTI_ATTRIBUTE, jti);
|
|
verify(request).setAttribute(AuthContext.JTI_ATTRIBUTE, jti);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void merchantSessionAuthAllowsOrdinaryUserWithoutRedisSession() throws Throwable {
|
|
|
|
|
+ String token = JwtUtil.token("936", "user");
|
|
|
|
|
+ prepareInvocation("merchantSession", token);
|
|
|
|
|
+ loginUser("0");
|
|
|
|
|
+ when(redisCache.hasKey(anyString())).thenReturn(false);
|
|
|
|
|
+ Object expected = new Object();
|
|
|
|
|
+ when(joinPoint.proceed()).thenReturn(expected);
|
|
|
|
|
+
|
|
|
|
|
+ Object actual = aspect.around(joinPoint);
|
|
|
|
|
+
|
|
|
|
|
+ assertSame(expected, actual);
|
|
|
|
|
+ verify(request).setAttribute(AuthContext.USER_ID_ATTRIBUTE, 936L);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void merchantSessionAuthAllowsRiderWithoutRedisSession() throws Throwable {
|
|
|
|
|
+ String token = JwtUtil.token("936", "rider");
|
|
|
|
|
+ prepareInvocation("merchantSession", token);
|
|
|
|
|
+ loginUser("2");
|
|
|
|
|
+ when(redisCache.hasKey(anyString())).thenReturn(false);
|
|
|
|
|
+ Object expected = new Object();
|
|
|
|
|
+ when(joinPoint.proceed()).thenReturn(expected);
|
|
|
|
|
+
|
|
|
|
|
+ Object actual = aspect.around(joinPoint);
|
|
|
|
|
+
|
|
|
|
|
+ assertSame(expected, actual);
|
|
|
|
|
+ verify(request).setAttribute(AuthContext.USER_ID_ATTRIBUTE, 936L);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Test
|
|
@Test
|
|
|
void merchantSessionAuthRejectsMissingRedisSessionBeforeController() throws Throwable {
|
|
void merchantSessionAuthRejectsMissingRedisSessionBeforeController() throws Throwable {
|
|
|
String token = JwtUtil.token("936", "merchant");
|
|
String token = JwtUtil.token("936", "merchant");
|
|
|
String jti = tokenJti(token);
|
|
String jti = tokenJti(token);
|
|
|
prepareInvocation("merchantSession", token);
|
|
prepareInvocation("merchantSession", token);
|
|
|
|
|
+ loginUser("1");
|
|
|
|
|
+ when(redisCache.hasKey(jti)).thenReturn(false);
|
|
|
|
|
+
|
|
|
|
|
+ try (MockedStatic<MessageUtils> messages = mockStatic(MessageUtils.class)) {
|
|
|
|
|
+ messages.when(() -> MessageUtils.message("merchant.session.invalid"))
|
|
|
|
|
+ .thenReturn("session invalid");
|
|
|
|
|
+
|
|
|
|
|
+ ServiceException exception = assertThrows(ServiceException.class,
|
|
|
|
|
+ () -> aspect.around(joinPoint));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("session invalid", exception.getMessage());
|
|
|
|
|
+ verify(joinPoint, never()).proceed();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void merchantSessionAuthRejectsUnknownUserWithoutRedisSession() throws Throwable {
|
|
|
|
|
+ String token = JwtUtil.token("936", "stranger");
|
|
|
|
|
+ String jti = tokenJti(token);
|
|
|
|
|
+ prepareInvocation("merchantSession", token);
|
|
|
|
|
+ when(infoUserMapper.selectById(936L)).thenReturn(null);
|
|
|
when(redisCache.hasKey(jti)).thenReturn(false);
|
|
when(redisCache.hasKey(jti)).thenReturn(false);
|
|
|
|
|
|
|
|
try (MockedStatic<MessageUtils> messages = mockStatic(MessageUtils.class)) {
|
|
try (MockedStatic<MessageUtils> messages = mockStatic(MessageUtils.class)) {
|
|
@@ -101,6 +156,7 @@ class AuthAspectTest {
|
|
|
.withExpiresAt(new Date(System.currentTimeMillis() + 60_000))
|
|
.withExpiresAt(new Date(System.currentTimeMillis() + 60_000))
|
|
|
.sign(Algorithm.HMAC256("TEST-AUTH-TOKEN"));
|
|
.sign(Algorithm.HMAC256("TEST-AUTH-TOKEN"));
|
|
|
prepareInvocation("merchantSession", token);
|
|
prepareInvocation("merchantSession", token);
|
|
|
|
|
+ loginUser("1");
|
|
|
|
|
|
|
|
try (MockedStatic<MessageUtils> messages = mockStatic(MessageUtils.class)) {
|
|
try (MockedStatic<MessageUtils> messages = mockStatic(MessageUtils.class)) {
|
|
|
messages.when(() -> MessageUtils.message("merchant.session.invalid"))
|
|
messages.when(() -> MessageUtils.message("merchant.session.invalid"))
|
|
@@ -121,6 +177,12 @@ class AuthAspectTest {
|
|
|
when(request.getHeader("token")).thenReturn(token);
|
|
when(request.getHeader("token")).thenReturn(token);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void loginUser(String userType) {
|
|
|
|
|
+ InfoUser user = new InfoUser();
|
|
|
|
|
+ user.setUserType(userType);
|
|
|
|
|
+ when(infoUserMapper.selectById(936L)).thenReturn(user);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private String tokenJti(String token) {
|
|
private String tokenJti(String token) {
|
|
|
Map<String, Object> claims = JwtUtil.verifyToken(token);
|
|
Map<String, Object> claims = JwtUtil.verifyToken(token);
|
|
|
return (String) claims.get("jti");
|
|
return (String) claims.get("jti");
|