jwt整合模块添加单元测试

This commit is contained in:
click33 2021-10-19 05:01:13 +08:00
parent 7740ab0a3f
commit 634a3be1aa
8 changed files with 319 additions and 8 deletions

View File

@ -299,10 +299,16 @@ public class StpLogic {
if(SaFoxUtil.isEmpty(tokenValue)) { if(SaFoxUtil.isEmpty(tokenValue)) {
return; return;
} }
// 如果打开了Cookie模式第一步先把cookie清除掉
// 从当前 [storage存储器] 里删除
SaHolder.getStorage().delete(splicingKeyJustCreatedSave());
// 如果打开了Cookie模式则把cookie清除掉
if(getConfig().getIsReadCookie()){ if(getConfig().getIsReadCookie()){
SaHolder.getResponse().deleteCookie(getTokenName()); SaHolder.getResponse().deleteCookie(getTokenName());
} }
// 清除这个token的相关信息
logoutByTokenValue(tokenValue); logoutByTokenValue(tokenValue);
} }

View File

@ -42,7 +42,7 @@
</dependency> </dependency>
<!-- Sa-Token 整合 Redis (使用jackson序列化方式) --> <!-- Sa-Token 整合 Redis (使用jackson序列化方式) -->
<dependency> <!-- <dependency>
<groupId>cn.dev33</groupId> <groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId> <artifactId>sa-token-dao-redis-jackson</artifactId>
<version>${sa-token-version}</version> <version>${sa-token-version}</version>
@ -50,7 +50,7 @@
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId> <artifactId>commons-pool2</artifactId>
</dependency> </dependency> -->
<!-- @ConfigurationProperties --> <!-- @ConfigurationProperties -->
<dependency> <dependency>
@ -59,6 +59,13 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,166 @@
package com.pj.test;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.jwt.SaJwtUtil;
import cn.dev33.satoken.jwt.StpLogicJwtForStateless;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
/**
* Sa-Token 整合 jwtstateless 模式 测试
*
*
* @author kong
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartUpApplication.class)
public class JwtForStatelessTest {
// 持久化Bean
static SaTokenDao dao;
// 开始
@BeforeClass
public static void beforeClass() {
System.out.println("\n\n------------------------ 基础测试 star ...");
dao = SaManager.getSaTokenDao();
StpUtil.setStpLogic(new StpLogicJwtForStateless());
}
// 结束
@AfterClass
public static void afterClass() {
System.out.println("\n\n------------------------ 基础测试 end ... \n");
}
// 测试登录
@Test
public void doLogin() {
// 登录
StpUtil.login(10001);
String token = StpUtil.getTokenValue();
// API 验证
Assert.assertTrue(StpUtil.isLogin());
Assert.assertNotNull(token); // token不为null
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
Assert.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备
// token 验证
JWT jwt = JWT.of(token);
JSONObject payloads = jwt.getPayloads();
Assert.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_ID), "10001"); // 账号
Assert.assertEquals(payloads.getStr(SaJwtUtil.DEVICE), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备
Assert.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_TYPE), StpUtil.TYPE); // 账号类型
// 时间
Assert.assertTrue(StpUtil.getTokenTimeout() <= SaManager.getConfig().getTimeout());
Assert.assertTrue(StpUtil.getTokenTimeout() > SaManager.getConfig().getTimeout() - 10000);
try {
// 尝试获取Session会抛出异常
StpUtil.getSession();
Assert.assertTrue(false);
} catch (Exception e) {
}
}
// 测试注销
@Test
public void logout() {
// 登录
StpUtil.login(10001);
String token = StpUtil.getTokenValue();
Assert.assertEquals(JWT.of(token).getPayloads().getStr("loginId"), "10001");
// 注销
StpUtil.logout();
// token 应该被清除
Assert.assertNull(StpUtil.getTokenValue());
Assert.assertFalse(StpUtil.isLogin());
}
// 测试Session会话
@Test(expected = SaTokenException.class)
public void testSession() {
StpUtil.login(10001);
// 会抛异常
StpUtil.getSession();
}
// 测试权限认证
@Test
public void testCheckPermission() {
StpUtil.login(10001);
// 权限认证
Assert.assertTrue(StpUtil.hasPermission("user-add"));
Assert.assertTrue(StpUtil.hasPermission("user-list"));
Assert.assertTrue(StpUtil.hasPermission("user"));
Assert.assertTrue(StpUtil.hasPermission("art-add"));
Assert.assertFalse(StpUtil.hasPermission("get-user"));
// and
Assert.assertTrue(StpUtil.hasPermissionAnd("art-add", "art-get"));
Assert.assertFalse(StpUtil.hasPermissionAnd("art-add", "comment-add"));
// or
Assert.assertTrue(StpUtil.hasPermissionOr("art-add", "comment-add"));
Assert.assertFalse(StpUtil.hasPermissionOr("comment-add", "comment-delete"));
}
// 测试角色认证
@Test
public void testCheckRole() {
StpUtil.login(10001);
// 角色认证
Assert.assertTrue(StpUtil.hasRole("admin"));
Assert.assertFalse(StpUtil.hasRole("teacher"));
// and
Assert.assertTrue(StpUtil.hasRoleAnd("admin", "super-admin"));
Assert.assertFalse(StpUtil.hasRoleAnd("admin", "ceo"));
// or
Assert.assertTrue(StpUtil.hasRoleOr("admin", "ceo"));
Assert.assertFalse(StpUtil.hasRoleOr("ceo", "cto"));
}
// 测试根据token强制注销
@Test(expected = SaTokenException.class)
public void testLogoutByToken() {
// 先登录上
StpUtil.login(10001);
Assert.assertTrue(StpUtil.isLogin());
String token = StpUtil.getTokenValue();
// 根据token注销
StpUtil.logoutByTokenValue(token);
}
// 测试根据账号id强制注销
@Test(expected = SaTokenException.class)
public void testLogoutByLoginId() {
// 先登录上
StpUtil.login(10001);
Assert.assertTrue(StpUtil.isLogin());
// 根据账号id注销
StpUtil.logout(10001);
}
}

View File

@ -0,0 +1,75 @@
package com.pj.test;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.jwt.StpLogicJwtForTokenStyle;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
/**
* Sa-Token 整合 jwttoken-style 模式 测试
*
* @author kong
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartUpApplication.class)
public class JwtForTokenStyleTest {
// 持久化Bean
static SaTokenDao dao;
// 开始
@BeforeClass
public static void beforeClass() {
System.out.println("\n\n------------------------ TokenStyleTest star ...");
dao = SaManager.getSaTokenDao();
StpUtil.setStpLogic(new StpLogicJwtForTokenStyle());
}
// 结束
@AfterClass
public static void afterClass() {
System.out.println("\n\n------------------------ TokenStyleTest end ... \n");
}
// 测试登录
@Test
public void doLogin() {
// 登录
StpUtil.login(10001);
String token = StpUtil.getTokenValue();
// API 验证
Assert.assertTrue(StpUtil.isLogin());
Assert.assertNotNull(token); // token不为null
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
Assert.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备
// token 验证
JWT jwt = JWT.of(token);
JSONObject payloads = jwt.getPayloads();
Assert.assertEquals(payloads.getStr("loginId"), "10001");
// db数据 验证
// token存在
Assert.assertEquals(dao.get("satoken:login:token:" + token), "10001");
// Session 存在
SaSession session = dao.getSession("satoken:login:session:" + 10001);
Assert.assertNotNull(session);
Assert.assertEquals(session.getId(), "satoken:login:session:" + 10001);
Assert.assertTrue(session.getTokenSignList().size() >= 1);
}
}

View File

@ -0,0 +1,16 @@
package com.pj.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类
* @author Auster
*
*/
@SpringBootApplication
public class StartUpApplication {
public static void main(String[] args) {
SpringApplication.run(StartUpApplication.class, args);
}
}

View File

@ -0,0 +1,35 @@
package com.pj.test.satoken;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import cn.dev33.satoken.stp.StpInterface;
/**
* 自定义权限验证接口扩展
*
* @author Auster
*
*/
@Component
public class StpInterfaceImpl implements StpInterface {
/**
* 返回一个账号所拥有的权限码集合
*/
@Override
public List<String> getPermissionList(Object loginId, String loginType) {
return Arrays.asList("user*", "art-add", "art-delete", "art-update", "art-get");
}
/**
* 返回一个账号所拥有的角色标识集合
*/
@Override
public List<String> getRoleList(Object loginId, String loginType) {
return Arrays.asList("admin", "super-admin");
}
}

View File

@ -1,6 +1,7 @@
package cn.dev33.satoken.jwt; package cn.dev33.satoken.jwt;
import cn.dev33.satoken.SaManager; import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.dao.SaTokenDao; import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.exception.NotLoginException; import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException; import cn.dev33.satoken.exception.SaTokenException;
@ -132,11 +133,14 @@ public class StpLogicJwtForStateless extends StpLogic {
*/ */
@Override @Override
public void logout() { public void logout() {
// stateless模式下清除Cookie即可 // ...
// 如果打开了cookie模式把cookie清除掉 // 从当前 [storage存储器] 里删除
if(getConfig().getIsReadCookie() == true){ SaHolder.getStorage().delete(splicingKeyJustCreatedSave());
SaManager.getSaTokenContext().getResponse().deleteCookie(getTokenName());
// 如果打开了Cookie模式则把cookie清除掉
if(getConfig().getIsReadCookie()){
SaHolder.getResponse().deleteCookie(getTokenName());
} }
} }

View File

@ -81,6 +81,8 @@ public class BasicsTest {
// 注销 // 注销
StpUtil.logout(); StpUtil.logout();
// token 应该被清除 // token 应该被清除
Assert.assertNull(StpUtil.getTokenValue());
Assert.assertFalse(StpUtil.isLogin());
Assert.assertNull(dao.get("satoken:login:token:" + token)); Assert.assertNull(dao.get("satoken:login:token:" + token));
// Session 应该被清除 // Session 应该被清除
SaSession session = dao.getSession("satoken:login:session:" + 10001); SaSession session = dao.getSession("satoken:login:session:" + 10001);