添加单元测试

This commit is contained in:
zhizhi
2021-06-08 00:16:42 +08:00
parent 8b8977eedf
commit 8a0fa6724a
4 changed files with 227 additions and 0 deletions

View File

@@ -26,6 +26,14 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.0.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- config -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

View File

@@ -0,0 +1,168 @@
package com.pj.test;
import java.util.List;
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.exception.DisableLoginException;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaTokenConsts;
/**
* Sa-Token 单元测试
*
* <p> 注解详解参考: https://www.cnblogs.com/flypig666/p/11505277.html
* @author Auster
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartUpApplication.class)
public class SaTokenSpringBootStarterTest {
// 开始
@BeforeClass
public static void beforeClass() {
System.out.println("\n\n------------------------ Test star ...");
}
// 结束
@AfterClass
public static void afterClass() {
System.out.println("\n\n------------------------ Test end ... \n");
}
// 测试:登录 & 注销
@Test
public void testDoLogin() {
// 登录
StpUtil.setLoginId(10001);
Assert.assertTrue(StpUtil.isLogin());
Assert.assertNotNull(StpUtil.getTokenValue()); // token不为null
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
Assert.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备
// 注销
StpUtil.logout();
Assert.assertFalse(StpUtil.isLogin());
// Assert.assertNull(StpUtil.getTokenValue());
Assert.assertNull(StpUtil.getLoginIdDefaultNull());
}
// 测试:权限认证
@Test
public void testCheckPermission() {
StpUtil.setLoginId(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"));
}
// 测试:角色认证
@Test
public void testCheckRole() {
StpUtil.setLoginId(10001);
// 角色认证
Assert.assertTrue(StpUtil.hasRole("admin"));
Assert.assertFalse(StpUtil.hasRole("teacher"));
}
// 测试:踢人下线
@Test
public void testKickOut() {
// 根据token踢人
StpUtil.setLoginId(10001);
Assert.assertTrue(StpUtil.isLogin());
StpUtil.logoutByTokenValue(StpUtil.getTokenValue());
Assert.assertFalse(StpUtil.isLogin());
// 根据账号id踢人
StpUtil.setLoginId(10001);
Assert.assertTrue(StpUtil.isLogin());
StpUtil.logoutByLoginId(10001);
Assert.assertFalse(StpUtil.isLogin());
}
// 测试:账号封禁
@Test(expected = DisableLoginException.class)
public void testDisable() {
// 封号
StpUtil.disable(10007, 200);
Assert.assertTrue(StpUtil.isDisable(10007));
// 解封
StpUtil.untieDisable(10007);
Assert.assertFalse(StpUtil.isDisable(10007));
// 封号后登陆 (会抛出 DisableLoginException 异常)
StpUtil.disable(10007, 200);
StpUtil.setLoginId(10007);
}
// 测试Session会话
@Test
public void testSession() {
StpUtil.setLoginId(10001);
// Session 应该存在
Assert.assertNotNull(StpUtil.getSession(false));
// 存取值
SaSession session = StpUtil.getSession();
session.set("name", "zhang");
session.set("age", "18");
Assert.assertEquals(session.get("name"), "zhang");
Assert.assertEquals(session.getInt("age"), 18);
Assert.assertEquals((int)session.get("name2", 20), 20);
Assert.assertEquals((int)session.get("name2", () -> 30), 30);
}
// 测试:身份切换
@Test
public void testSwitch() {
// 登录
StpUtil.setLoginId(10001);
Assert.assertFalse(StpUtil.isSwitch());
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
// 开始身份切换
StpUtil.switchTo(10044);
Assert.assertTrue(StpUtil.isSwitch());
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10044);
// 结束切换
StpUtil.endSwitch();
Assert.assertFalse(StpUtil.isSwitch());
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
}
// 测试:会话管理
@Test
public void testSearchTokenValue() {
// 登录
StpUtil.setLoginId(10001);
StpUtil.setLoginId(10002);
StpUtil.setLoginId(10003);
StpUtil.setLoginId(10004);
StpUtil.setLoginId(10005);
// 查询
List<String> list = StpUtil.searchTokenValue("", 0, 10);
Assert.assertTrue(list.size() >= 5);
}
}

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 loginKey) {
return Arrays.asList("user*", "art-add", "art-delete", "art-update", "art-get");
}
/**
* 返回一个账号所拥有的角色标识集合
*/
@Override
public List<String> getRoleList(Object loginId, String loginKey) {
return Arrays.asList("admin", "super-admin");
}
}