mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-02-27 16:50:24 +08:00
完善账号封禁模块的单元测试
This commit is contained in:
@@ -207,15 +207,16 @@ public class JwtForMixinTest {
|
||||
// 封号
|
||||
StpUtil.disable(10007, 200);
|
||||
Assertions.assertTrue(StpUtil.isDisable(10007));
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:" + 10007), DisableServiceException.BE_VALUE);
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:login:" + 10007), DisableServiceException.BE_VALUE);
|
||||
|
||||
// 解封
|
||||
StpUtil.untieDisable(10007);
|
||||
Assertions.assertFalse(StpUtil.isDisable(10007));
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:" + 10007), null);
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:login:" + 10007), null);
|
||||
|
||||
// 封号后登陆 (会抛出 DisableLoginException 异常)
|
||||
// 封号后校验 (会抛出 DisableLoginException 异常)
|
||||
StpUtil.disable(10007, 200);
|
||||
StpUtil.checkDisable(10007);
|
||||
StpUtil.login(10007);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package cn.dev33.satoken.integrate.annotation;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckDisable;
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
@@ -68,5 +69,26 @@ public class SaAnnotationController {
|
||||
public SaResult checkSafe() {
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
|
||||
// 封禁账号
|
||||
@RequestMapping("disable")
|
||||
public SaResult disable(long id) {
|
||||
StpUtil.disable(id, "comment", 200);
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// 服务封禁校验
|
||||
@SaCheckDisable("comment")
|
||||
@RequestMapping("checkDisable")
|
||||
public SaResult checkDisable() {
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// 解封账号
|
||||
@RequestMapping("untieDisable")
|
||||
public SaResult untieDisable(long id) {
|
||||
StpUtil.untieDisable(id, "comment");
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -70,6 +70,10 @@ public class SaAnnotationControllerTest {
|
||||
// 校验二级认证,通过
|
||||
SaResult res7 = request("/at/checkSafe?satoken=" + satoken);
|
||||
Assertions.assertEquals(res7.getCode(), 200);
|
||||
|
||||
// 访问校验封禁的接口 ,通过
|
||||
SaResult res9 = request("/at/checkDisable?satoken=" + satoken);
|
||||
Assertions.assertEquals(res9.getCode(), 200);
|
||||
}
|
||||
|
||||
// 校验不通过的情况
|
||||
@@ -99,6 +103,23 @@ public class SaAnnotationControllerTest {
|
||||
// 校验二级认证,不通过
|
||||
SaResult res7 = request("/at/checkSafe?satoken=" + satoken);
|
||||
Assertions.assertEquals(res7.getCode(), 901);
|
||||
|
||||
// -------- 登录拿到Token
|
||||
String satoken10042 = request("/at/login?id=10042").get("token", String.class);
|
||||
Assertions.assertNotNull(satoken10042);
|
||||
|
||||
// 校验账号封禁 ,通过
|
||||
SaResult res8 = request("/at/disable?id=10042");
|
||||
Assertions.assertEquals(res8.getCode(), 200);
|
||||
|
||||
// 访问校验封禁的接口 ,不通过
|
||||
SaResult res9 = request("/at/checkDisable?satoken=" + satoken10042);
|
||||
Assertions.assertEquals(res9.getCode(), 904);
|
||||
|
||||
// 解封后就能访问了
|
||||
request("/at/untieDisable?id=10042");
|
||||
SaResult res10 = request("/at/checkDisable?satoken=" + satoken10042);
|
||||
Assertions.assertEquals(res10.getCode(), 200);
|
||||
}
|
||||
|
||||
// 测试忽略认证
|
||||
|
||||
@@ -3,6 +3,7 @@ package cn.dev33.satoken.integrate.configure;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import cn.dev33.satoken.exception.DisableServiceException;
|
||||
import cn.dev33.satoken.exception.IdTokenInvalidException;
|
||||
import cn.dev33.satoken.exception.NotBasicAuthException;
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
@@ -54,5 +55,11 @@ public class HandlerException {
|
||||
public SaResult handlerNotBasicAuthException(NotBasicAuthException e) {
|
||||
return SaResult.error().setCode(903);
|
||||
}
|
||||
|
||||
// 服务被封禁 ,code=904
|
||||
@ExceptionHandler(DisableServiceException.class)
|
||||
public SaResult handlerDisableServiceException(DisableServiceException e) {
|
||||
return SaResult.error().setCode(904);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -359,7 +359,10 @@ public class BasicsTest {
|
||||
// 封号
|
||||
StpUtil.disable(10007, 200);
|
||||
Assertions.assertTrue(StpUtil.isDisable(10007));
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:" + 10007), DisableServiceException.BE_VALUE);
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:login:" + 10007), DisableServiceException.BE_VALUE);
|
||||
|
||||
// 封号后检测一下 (会抛出 DisableLoginException 异常)
|
||||
Assertions.assertThrows(DisableServiceException.class, () -> StpUtil.checkDisable(10007));
|
||||
|
||||
// 封号时间
|
||||
long disableTime = StpUtil.getDisableTime(10007);
|
||||
@@ -368,11 +371,38 @@ public class BasicsTest {
|
||||
// 解封
|
||||
StpUtil.untieDisable(10007);
|
||||
Assertions.assertFalse(StpUtil.isDisable(10007));
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:" + 10007), null);
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:login:" + 10007), null);
|
||||
Assertions.assertDoesNotThrow(() -> StpUtil.checkDisable(10007));
|
||||
}
|
||||
|
||||
// 封号后检测一下 (会抛出 DisableLoginException 异常)
|
||||
StpUtil.disable(10007, 200);
|
||||
Assertions.assertThrows(DisableServiceException.class, () -> StpUtil.checkDisable(10007));
|
||||
// 测试:账号封禁,根据服务
|
||||
@Test
|
||||
public void testDisableService() {
|
||||
// 封掉评论功能
|
||||
StpUtil.disable(10008, "comment", 200);
|
||||
Assertions.assertTrue(StpUtil.isDisable(10008, "comment"));
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:comment:" + 10008), DisableServiceException.BE_VALUE);
|
||||
Assertions.assertNull(dao.get("satoken:login:disable:login:" + 10008));
|
||||
|
||||
// 封号后检测一下
|
||||
Assertions.assertThrows(DisableServiceException.class, () -> StpUtil.checkDisable(10008, "comment"));
|
||||
// 检查多个,有一个不通过就报异常
|
||||
Assertions.assertThrows(DisableServiceException.class, () -> StpUtil.checkDisable(10008, "comment", "login"));
|
||||
|
||||
// 封号时间
|
||||
long disableTime = StpUtil.getDisableTime(10008, "comment");
|
||||
Assertions.assertTrue(disableTime <= 200 && disableTime >= 199);
|
||||
|
||||
// 解封 (不加服务名不会成功)
|
||||
StpUtil.untieDisable(10008);
|
||||
Assertions.assertTrue(StpUtil.isDisable(10008, "comment"));
|
||||
Assertions.assertNotNull(dao.get("satoken:login:disable:comment:" + 10008));
|
||||
|
||||
// 解封 (加服务名才会成功)
|
||||
StpUtil.untieDisable(10008, "comment");
|
||||
Assertions.assertFalse(StpUtil.isDisable(10008, "comment"));
|
||||
Assertions.assertEquals(dao.get("satoken:login:disable:comment:" + 10008), null);
|
||||
Assertions.assertDoesNotThrow(() -> StpUtil.checkDisable(10007, "comment"));
|
||||
}
|
||||
|
||||
// 测试:身份切换
|
||||
|
||||
Reference in New Issue
Block a user