mirror of
https://gitee.com/dromara/sa-token.git
synced 2024-11-30 10:58:05 +08:00
完善注解鉴权单元测试
This commit is contained in:
parent
a673bfb550
commit
c7ce21fde7
@ -3,7 +3,6 @@ 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.SaCheckBasic;
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
@ -21,8 +20,6 @@ import cn.dev33.satoken.util.SaResult;
|
||||
@RequestMapping("/at/")
|
||||
public class SaAnnotationController {
|
||||
|
||||
// ------------------------ 简单测试
|
||||
|
||||
// 登录
|
||||
@RequestMapping("login")
|
||||
public SaResult login(long id) {
|
||||
@ -58,16 +55,9 @@ public class SaAnnotationController {
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// Http Basic 认证
|
||||
@SaCheckBasic
|
||||
@RequestMapping("checkBasic")
|
||||
public SaResult checkBasic() {
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// 开启二级认证
|
||||
@RequestMapping("openSafe")
|
||||
public SaResult openSafe(long id) {
|
||||
public SaResult openSafe() {
|
||||
StpUtil.openSafe(120);
|
||||
return SaResult.ok();
|
||||
}
|
||||
@ -79,5 +69,4 @@ public class SaAnnotationController {
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -62,6 +62,14 @@ public class SaAnnotationControllerTest {
|
||||
// 权限校验or角色校验,通过
|
||||
SaResult res5 = request("/at/checkPermission2?satoken=" + satoken);
|
||||
Assertions.assertEquals(res5.getCode(), 200);
|
||||
|
||||
// 开启二级认证
|
||||
SaResult res6 = request("/at/openSafe?satoken=" + satoken);
|
||||
Assertions.assertEquals(res6.getCode(), 200);
|
||||
|
||||
// 校验二级认证,通过
|
||||
SaResult res7 = request("/at/checkSafe?satoken=" + satoken);
|
||||
Assertions.assertEquals(res7.getCode(), 200);
|
||||
}
|
||||
|
||||
// 校验不通过的情况
|
||||
@ -87,8 +95,23 @@ public class SaAnnotationControllerTest {
|
||||
// 权限校验or角色校验,不通过
|
||||
SaResult res5 = request("/at/checkPermission2?satoken=" + satoken);
|
||||
Assertions.assertEquals(res5.getCode(), 403);
|
||||
|
||||
// 校验二级认证,不通过
|
||||
SaResult res7 = request("/at/checkSafe?satoken=" + satoken);
|
||||
Assertions.assertEquals(res7.getCode(), 901);
|
||||
}
|
||||
|
||||
// 测试忽略认证
|
||||
@Test
|
||||
public void testIgnore() {
|
||||
// 必须登录才能访问的
|
||||
SaResult res1 = request("/ig/show1");
|
||||
Assertions.assertEquals(res1.getCode(), 401);
|
||||
|
||||
// 不登录也可以访问的
|
||||
SaResult res2 = request("/ig/show2");
|
||||
Assertions.assertEquals(res2.getCode(), 200);
|
||||
}
|
||||
|
||||
// 封装请求
|
||||
private SaResult request(String path) {
|
||||
@ -114,5 +137,4 @@ public class SaAnnotationControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
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.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
|
||||
/**
|
||||
* 测试注解用的Controller
|
||||
*
|
||||
* @author kong
|
||||
* @since: 2022-9-2
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@RestController
|
||||
@RequestMapping("/ig/")
|
||||
public class SaAnnotationIgnoreController {
|
||||
|
||||
// 需要登录后访问
|
||||
@RequestMapping("show1")
|
||||
public SaResult show1() {
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// 不登录也可访问
|
||||
@SaIgnore
|
||||
@RequestMapping("show2")
|
||||
public SaResult show2() {
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.exception.NotPermissionException;
|
||||
import cn.dev33.satoken.exception.NotRoleException;
|
||||
import cn.dev33.satoken.exception.NotSafeException;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
|
||||
/**
|
||||
@ -34,4 +35,10 @@ public class HandlerException {
|
||||
return SaResult.error().setCode(403);
|
||||
}
|
||||
|
||||
// 二级认证失败,code=901
|
||||
@ExceptionHandler(NotSafeException.class)
|
||||
public SaResult handlerNotSafeException(NotSafeException e) {
|
||||
return SaResult.error().setCode(901);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user