完善注解鉴权单元测试

This commit is contained in:
click33 2022-09-02 23:38:20 +08:00
parent a673bfb550
commit c7ce21fde7
4 changed files with 65 additions and 13 deletions

View File

@ -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();
}
}

View File

@ -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 {
}
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}