mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 17:37:53 +08:00
完善单元测试
This commit is contained in:
parent
28701115dd
commit
a8a6bce1a0
@ -7,6 +7,9 @@ package cn.dev33.satoken.basic;
|
||||
*/
|
||||
public class SaBasicUtil {
|
||||
|
||||
private SaBasicUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 底层 SaBasicTemplate 对象
|
||||
*/
|
||||
|
@ -21,7 +21,7 @@ public class SaTokenContextDefaultImpl implements SaTokenContext {
|
||||
/**
|
||||
* 默认的上下文处理器对象
|
||||
*/
|
||||
public static SaTokenContext defaultContext = new SaTokenContextDefaultImpl();
|
||||
public static SaTokenContextDefaultImpl defaultContext = new SaTokenContextDefaultImpl();
|
||||
|
||||
/**
|
||||
* 默认的错误提示语
|
||||
|
@ -10,6 +10,9 @@ import java.util.Map;
|
||||
*
|
||||
*/
|
||||
public class SaLoginConfig {
|
||||
|
||||
private SaLoginConfig() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param device 此次登录的客户端设备类型
|
||||
@ -31,7 +34,7 @@ public class SaLoginConfig {
|
||||
* @param timeout 指定此次登录token的有效期, 单位:秒 (如未指定,自动取全局配置的timeout值)
|
||||
* @return 对象自身
|
||||
*/
|
||||
public static SaLoginModel setTimeout(Long timeout) {
|
||||
public static SaLoginModel setTimeout(long timeout) {
|
||||
return create().setTimeout(timeout);
|
||||
}
|
||||
|
||||
|
@ -227,13 +227,4 @@ public class SaLoginModel {
|
||||
return new SaLoginModel();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更换为 getDeviceOrDefault()
|
||||
* @return /
|
||||
*/
|
||||
@Deprecated
|
||||
public String getDeviceOrDefalut() {
|
||||
return getDeviceOrDefault();
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package cn.dev33.satoken.servlet.model;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
@ -71,7 +69,7 @@ public class SaResponseForServlet implements SaResponse {
|
||||
public Object redirect(String url) {
|
||||
try {
|
||||
response.sendRedirect(url);
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
throw new SaTokenException(e);
|
||||
}
|
||||
return null;
|
||||
|
@ -0,0 +1,25 @@
|
||||
package cn.dev33.satoken.core.context.model;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
|
||||
/**
|
||||
* 默认上下文测试
|
||||
*
|
||||
* @author kong
|
||||
* @since: 2022-9-5
|
||||
*/
|
||||
public class SaTokenContextDefaultImplTest {
|
||||
|
||||
@Test
|
||||
public void testSaTokenContextDefaultImpl() {
|
||||
SaTokenContextDefaultImpl context = new SaTokenContextDefaultImpl();
|
||||
Assertions.assertThrows(SaTokenException.class, () -> context.getStorage());
|
||||
Assertions.assertThrows(SaTokenException.class, () -> context.getRequest());
|
||||
Assertions.assertThrows(SaTokenException.class, () -> context.getResponse());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package cn.dev33.satoken.core.stp;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import cn.dev33.satoken.stp.SaLoginConfig;
|
||||
import cn.dev33.satoken.stp.SaLoginModel;
|
||||
import cn.dev33.satoken.stp.SaTokenInfo;
|
||||
import cn.dev33.satoken.util.SaTokenConsts;
|
||||
|
||||
/**
|
||||
* Token 参数扩展
|
||||
*
|
||||
* @author kong
|
||||
* @since: 2022-9-5
|
||||
*/
|
||||
public class TokenInfoTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
SaTokenInfo info = new SaTokenInfo();
|
||||
info.setTokenName("satoken");
|
||||
info.setTokenValue("xxxxx-xxxxx-xxxxx-xxxxx");
|
||||
info.setIsLogin(true);
|
||||
info.setLoginId(10001);
|
||||
info.setLoginType("login");
|
||||
info.setTokenTimeout(1800);
|
||||
info.setSessionTimeout(120);
|
||||
info.setTokenSessionTimeout(1800);
|
||||
info.setTokenActivityTimeout(120);
|
||||
info.setLoginDevice("PC");
|
||||
info.setTag("xxx");
|
||||
|
||||
Assertions.assertEquals(info.getTokenName(), "satoken");
|
||||
Assertions.assertEquals(info.getTokenValue(), "xxxxx-xxxxx-xxxxx-xxxxx");
|
||||
Assertions.assertEquals(info.getIsLogin(), true);
|
||||
Assertions.assertEquals(info.getLoginId(), 10001);
|
||||
Assertions.assertEquals(info.getLoginType(), "login");
|
||||
Assertions.assertEquals(info.getTokenTimeout(), 1800);
|
||||
Assertions.assertEquals(info.getSessionTimeout(), 120);
|
||||
Assertions.assertEquals(info.getTokenSessionTimeout(), 1800);
|
||||
Assertions.assertEquals(info.getTokenActivityTimeout(), 120);
|
||||
Assertions.assertEquals(info.getLoginDevice(), "PC");
|
||||
Assertions.assertEquals(info.getTag(), "xxx");
|
||||
|
||||
Assertions.assertNotNull(info.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoginModel() {
|
||||
Assertions.assertEquals(SaLoginConfig.setDevice("PC").getDevice(), "PC");
|
||||
Assertions.assertEquals(SaLoginConfig.setIsLastingCookie(false).getIsLastingCookie(), false);
|
||||
Assertions.assertEquals(SaLoginConfig.setTimeout(1600).getTimeout(), 1600);
|
||||
Assertions.assertEquals(SaLoginConfig.setToken("token-xxx").getToken(), "token-xxx");
|
||||
Assertions.assertEquals(SaLoginConfig.setExtra("age", 18).getExtra("age"), 18);
|
||||
|
||||
Map<String, Object> extraData = new HashMap<>();
|
||||
extraData.put("age", 20);
|
||||
SaLoginModel lm = SaLoginConfig.setExtraData(extraData);
|
||||
Assertions.assertEquals(lm.getExtraData(), extraData);
|
||||
Assertions.assertEquals(lm.getExtra("age"), 20);
|
||||
Assertions.assertTrue(lm.isSetExtraData());
|
||||
Assertions.assertNotNull(lm.toString());
|
||||
|
||||
// 计算 CookieTimeout
|
||||
SaLoginModel loginModel = SaLoginModel
|
||||
.create()
|
||||
.setTimeout(-1);
|
||||
loginModel.build();
|
||||
Assertions.assertEquals(loginModel.getCookieTimeout(), Integer.MAX_VALUE);
|
||||
Assertions.assertEquals(loginModel.getDeviceOrDefault(), SaTokenConsts.DEFAULT_LOGIN_DEVICE);
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import cn.dev33.satoken.exception.IdTokenInvalidException;
|
||||
import cn.dev33.satoken.exception.NotBasicAuthException;
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.exception.NotPermissionException;
|
||||
import cn.dev33.satoken.exception.NotRoleException;
|
||||
@ -47,5 +48,11 @@ public class HandlerException {
|
||||
public SaResult handlerIdTokenInvalidException(IdTokenInvalidException e) {
|
||||
return SaResult.error().setCode(902);
|
||||
}
|
||||
|
||||
// Http Basic 校验失败,code=903
|
||||
@ExceptionHandler(NotBasicAuthException.class)
|
||||
public SaResult handlerNotBasicAuthException(NotBasicAuthException e) {
|
||||
return SaResult.error().setCode(903);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,11 +14,13 @@ import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
*/
|
||||
@Configuration
|
||||
public class SaTokenConfigure implements WebMvcConfigurer {
|
||||
// 注册 Sa-Token 拦截器,打开注解式鉴权功能
|
||||
|
||||
// 注册 Sa-Token 拦截器,打开注解式鉴权功能
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 注册 Sa-Token 拦截器,打开注解式鉴权功能
|
||||
registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package cn.dev33.satoken.integrate.more;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.basic.SaBasicUtil;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
@ -33,8 +34,17 @@ public class MoreController {
|
||||
&& SaFoxUtil.equals(req.getHeader("div", "zhang"), "val")
|
||||
&& SaFoxUtil.equals(req.getHeader("div2", "zhang"), "zhang")
|
||||
;
|
||||
|
||||
|
||||
System.out.println("::: "+SaHolder.getRequest().getUrl());
|
||||
SaHolder.getResponse().setServer("sa-server");
|
||||
return SaResult.data(flag);
|
||||
}
|
||||
|
||||
// Http Basic 认证
|
||||
@RequestMapping("basicAuth")
|
||||
public SaResult basicAuth() {
|
||||
SaBasicUtil.check("sa:123456");
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -64,8 +64,48 @@ public class MoreControllerTest {
|
||||
public void testApi() {
|
||||
SaResult res = request("/more/getInfo?name=zhang");
|
||||
Assertions.assertEquals(res.getData(), true);
|
||||
}
|
||||
|
||||
// Http Basic 认证
|
||||
@Test
|
||||
public void testBasic() throws Exception {
|
||||
|
||||
// ---------------- 认证不通过
|
||||
MvcResult mvcResult = mvc.perform(
|
||||
MockMvcRequestBuilders.post("/more/basicAuth")
|
||||
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
.accept(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
)
|
||||
.andExpect(MockMvcResultMatchers.status().is(401))
|
||||
.andReturn();
|
||||
|
||||
// 转 Map
|
||||
String content = mvcResult.getResponse().getContentAsString();
|
||||
Map<String, Object> map = SaManager.getSaJsonTemplate().parseJsonToMap(content);
|
||||
// 转 SaResult 对象
|
||||
SaResult res = new SaResult().setMap(map);
|
||||
Assertions.assertEquals(res.getCode(), 903);
|
||||
// 会有一个特殊响应头
|
||||
String header = mvcResult.getResponse().getHeader("WWW-Authenticate");
|
||||
Assertions.assertEquals(header, "Basic Realm=Sa-Token");
|
||||
|
||||
|
||||
// ---------------- 认证通过
|
||||
MvcResult mvcResult2 = mvc.perform(
|
||||
MockMvcRequestBuilders.post("/more/basicAuth")
|
||||
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
.accept(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
.header("Authorization", "Basic c2E6MTIzNDU2")
|
||||
)
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
|
||||
// 转 Map
|
||||
String content2 = mvcResult2.getResponse().getContentAsString();
|
||||
Map<String, Object> map2 = SaManager.getSaJsonTemplate().parseJsonToMap(content2);
|
||||
// 转 SaResult 对象
|
||||
SaResult res2 = new SaResult().setMap(map2);
|
||||
Assertions.assertEquals(res2.getCode(), 200);
|
||||
}
|
||||
|
||||
|
||||
|
@ -3,6 +3,7 @@ package cn.dev33.satoken.integrate.router;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
|
||||
/**
|
||||
@ -25,4 +26,23 @@ public class RouterController {
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// 读url
|
||||
@RequestMapping("getInfo_101")
|
||||
public SaResult getInfo_101() {
|
||||
return SaResult.data(SaHolder.getRequest().getUrl());
|
||||
}
|
||||
|
||||
// 读Cookie
|
||||
@RequestMapping("getInfo_102")
|
||||
public SaResult getInfo_102() {
|
||||
return SaResult.data(SaHolder.getRequest().getCookieValue("x-token"));
|
||||
}
|
||||
|
||||
// 测试转发
|
||||
@RequestMapping("getInfo_103")
|
||||
public SaResult getInfo_103() {
|
||||
SaHolder.getRequest().forward("/rt/getInfo_102");
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package cn.dev33.satoken.integrate.router;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -88,6 +90,7 @@ public class RouterControllerTest {
|
||||
Assertions.assertTrue(SaRouter.notMatch(r -> false).isHit());
|
||||
}
|
||||
|
||||
// 各种路由测试
|
||||
@Test
|
||||
public void testRouter() {
|
||||
// getInfo
|
||||
@ -149,9 +152,69 @@ public class RouterControllerTest {
|
||||
// getInfo15
|
||||
SaResult res15 = request("/rt/getInfo15");
|
||||
Assertions.assertEquals(res15.getCode(), 215);
|
||||
|
||||
}
|
||||
|
||||
// 测试 getUrl()
|
||||
@Test
|
||||
public void testGetUrl() {
|
||||
// getInfo_101
|
||||
SaResult res = request("/rt/getInfo_101");
|
||||
Assertions.assertTrue(res.getData().toString().endsWith("/rt/getInfo_101"));
|
||||
|
||||
// getInfo_101,不包括后面的参数
|
||||
SaResult res2 = request("/rt/getInfo_101?id=1");
|
||||
Assertions.assertTrue(res2.getData().toString().endsWith("/rt/getInfo_101"));
|
||||
|
||||
// 自定义当前域名
|
||||
SaManager.getConfig().setCurrDomain("http://xxx.com");
|
||||
SaResult res3 = request("/rt/getInfo_101?id=1");
|
||||
Assertions.assertEquals(res3.getData().toString(), "http://xxx.com/rt/getInfo_101");
|
||||
SaManager.getConfig().setCurrDomain(null);
|
||||
}
|
||||
|
||||
// 测试读取Cookie
|
||||
@Test
|
||||
public void testGetCookie() throws Exception {
|
||||
MvcResult mvcResult = mvc.perform(
|
||||
MockMvcRequestBuilders.post("/rt/getInfo_102")
|
||||
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
.accept(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
.cookie(new Cookie("x-token", "token-111"))
|
||||
)
|
||||
.andExpect(MockMvcResultMatchers.status().is(200))
|
||||
.andReturn();
|
||||
|
||||
// 转 Map
|
||||
String content = mvcResult.getResponse().getContentAsString();
|
||||
Map<String, Object> map = SaManager.getSaJsonTemplate().parseJsonToMap(content);
|
||||
|
||||
// 转 SaResult 对象
|
||||
SaResult res = new SaResult().setMap(map);
|
||||
Assertions.assertEquals(res.getData(), "token-111");
|
||||
}
|
||||
|
||||
// 测试重定向
|
||||
@Test
|
||||
public void testRedirect() throws Exception {
|
||||
MvcResult mvcResult = mvc.perform(
|
||||
MockMvcRequestBuilders.post("/rt/getInfo16")
|
||||
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
.accept(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
)
|
||||
.andExpect(MockMvcResultMatchers.status().is(302))
|
||||
.andReturn();
|
||||
|
||||
Assertions.assertEquals(mvcResult.getResponse().getHeader("Location"), "/rt/getInfo3");
|
||||
}
|
||||
|
||||
// 测试转发
|
||||
@Test
|
||||
public void testForward() {
|
||||
SaResult res = request("/rt/getInfo_103");
|
||||
Assertions.assertEquals(res.getCode(), 200);
|
||||
}
|
||||
|
||||
// 封装请求
|
||||
private SaResult request(String path) {
|
||||
try {
|
||||
|
@ -2,6 +2,7 @@ package cn.dev33.satoken.integrate.router;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
@ -10,6 +11,7 @@ import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import cn.dev33.satoken.router.SaHttpMethod;
|
||||
import cn.dev33.satoken.router.SaRouter;
|
||||
import cn.dev33.satoken.spring.SpringMVCUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
|
||||
/**
|
||||
@ -70,10 +72,22 @@ public class SaTokenConfigure2 implements WebMvcConfigurer {
|
||||
|
||||
// SaRouter.match(Arrays.asList("/rt/getInfo15", "/rt/getInfo16"))
|
||||
if(SaRouter.isMatchCurrURI("/rt/getInfo15")) {
|
||||
SaRouter.newMatch().free(r -> SaRouter.back(SaResult.code(215)));
|
||||
if(SaHolder.getRequest().getCookieValue("ddd") == null
|
||||
&& SaHolder.getStorage().getSource() == SpringMVCUtil.getRequest()
|
||||
&& SaHolder.getRequest().getSource() == SpringMVCUtil.getRequest()
|
||||
&& SaHolder.getResponse().getSource() == SpringMVCUtil.getResponse()
|
||||
) {
|
||||
SaRouter.newMatch().free(r -> SaRouter.back(SaResult.code(215)));
|
||||
}
|
||||
}
|
||||
|
||||
SaRouter.match("/rt/getInfo16", () -> {
|
||||
Assertions.assertThrows(Exception.class, () -> SaHolder.getResponse().redirect(null));
|
||||
SaHolder.getResponse().redirect("/rt/getInfo3");
|
||||
});
|
||||
|
||||
})).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.dev33.satoken.springboot;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
@ -10,18 +11,22 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.exception.DisableLoginException;
|
||||
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.json.SaJsonTemplate;
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.stp.SaLoginConfig;
|
||||
import cn.dev33.satoken.stp.SaLoginModel;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaTokenConsts;
|
||||
import cn.dev33.satoken.util.SoMap;
|
||||
|
||||
/**
|
||||
* Sa-Token 基础API测试
|
||||
@ -590,6 +595,28 @@ public class BasicsTest {
|
||||
// 不会抛出异常
|
||||
Assertions.assertDoesNotThrow(() -> StpUtil.checkActivityTimeout());
|
||||
}
|
||||
|
||||
|
||||
// 测试,上下文 API
|
||||
@Test
|
||||
public void testSaTokenContext() {
|
||||
SaTokenContext context = SaHolder.getContext();
|
||||
Assertions.assertTrue(context.matchPath("/user/**", "/user/add"));
|
||||
}
|
||||
|
||||
// 测试json转换
|
||||
@Test
|
||||
public void testSaJsonTemplate() {
|
||||
SaJsonTemplate saJsonTemplate = SaManager.getSaJsonTemplate();
|
||||
|
||||
// map 转 json
|
||||
SoMap map = SoMap.getSoMap("name", "zhangsan");
|
||||
String jsonString = saJsonTemplate.toJsonString(map);
|
||||
Assertions.assertEquals(jsonString, "{\"name\":\"zhangsan\"}");
|
||||
|
||||
// json 转 map
|
||||
Map<String, Object> map2 = saJsonTemplate.parseJsonToMap("{\"name\":\"zhangsan\"}");
|
||||
Assertions.assertEquals(map2.get("name"), "zhangsan");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user