This commit is contained in:
2025-09-06 11:58:18 +08:00
commit 8d34972119
1483 changed files with 141190 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
package com.gxwebsoft.common.core.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxwebsoft.common.core.dto.qr.CreateEncryptedQrCodeRequest;
import com.gxwebsoft.common.core.utils.EncryptedQrCodeUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* QR码控制器测试
*
* @author WebSoft
* @since 2025-08-18
*/
@WebMvcTest(QrCodeController.class)
public class QrCodeControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private EncryptedQrCodeUtil encryptedQrCodeUtil;
@Autowired
private ObjectMapper objectMapper;
@Test
public void testCreateEncryptedQrCodeWithValidRequest() throws Exception {
// 准备测试数据
CreateEncryptedQrCodeRequest request = new CreateEncryptedQrCodeRequest();
request.setData("test data");
request.setWidth(200);
request.setHeight(200);
request.setExpireMinutes(30L);
request.setBusinessType("TEST");
Map<String, Object> mockResult = new HashMap<>();
mockResult.put("qrCodeBase64", "base64_encoded_image");
mockResult.put("token", "test_token");
when(encryptedQrCodeUtil.generateEncryptedQrCode(anyString(), anyInt(), anyInt(), anyLong(), anyString()))
.thenReturn(mockResult);
// 执行测试
mockMvc.perform(post("/api/qr-code/create-encrypted-qr-code")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("生成加密二维码成功"))
.andExpect(jsonPath("$.data.qrCodeBase64").value("base64_encoded_image"))
.andExpect(jsonPath("$.data.token").value("test_token"));
}
@Test
public void testCreateEncryptedQrCodeWithInvalidRequest() throws Exception {
// 准备无效的测试数据data为空
CreateEncryptedQrCodeRequest request = new CreateEncryptedQrCodeRequest();
request.setData(""); // 空字符串,应该触发验证失败
request.setWidth(200);
request.setHeight(200);
request.setExpireMinutes(30L);
// 执行测试
mockMvc.perform(post("/api/qr-code/create-encrypted-qr-code")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(500))
.andExpect(jsonPath("$.message").value("数据不能为空"));
}
@Test
public void testCreateEncryptedQrCodeWithInvalidSize() throws Exception {
// 准备无效的测试数据(尺寸超出范围)
CreateEncryptedQrCodeRequest request = new CreateEncryptedQrCodeRequest();
request.setData("test data");
request.setWidth(2000); // 超出最大值1000
request.setHeight(200);
request.setExpireMinutes(30L);
// 执行测试
mockMvc.perform(post("/api/qr-code/create-encrypted-qr-code")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(500))
.andExpect(jsonPath("$.message").value("宽度不能大于1000像素"));
}
}

View File

@@ -0,0 +1,136 @@
package com.gxwebsoft.common.core.utils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.Resource;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
/**
* 加密二维码工具类测试
*
* @author WebSoft
* @since 2025-08-18
*/
@SpringBootTest
@ActiveProfiles("dev")
public class EncryptedQrCodeUtilTest {
@Resource
private EncryptedQrCodeUtil encryptedQrCodeUtil;
@Test
public void testGenerateAndDecryptData() {
// 测试数据
String originalData = "https://www.example.com/user/123";
Long expireMinutes = 30L;
// 生成加密数据
Map<String, String> encryptedInfo = encryptedQrCodeUtil.generateEncryptedData(originalData, expireMinutes);
assertNotNull(encryptedInfo);
assertNotNull(encryptedInfo.get("token"));
assertNotNull(encryptedInfo.get("encryptedData"));
assertEquals(expireMinutes.toString(), encryptedInfo.get("expireMinutes"));
// 解密数据
String decryptedData = encryptedQrCodeUtil.decryptData(
encryptedInfo.get("token"),
encryptedInfo.get("encryptedData")
);
assertEquals(originalData, decryptedData);
}
@Test
public void testGenerateEncryptedQrCode() {
// 测试数据
String originalData = "测试二维码数据";
int width = 300;
int height = 300;
Long expireMinutes = 60L;
// 生成加密二维码
Map<String, Object> result = encryptedQrCodeUtil.generateEncryptedQrCode(
originalData, width, height, expireMinutes
);
assertNotNull(result);
assertNotNull(result.get("qrCodeBase64"));
assertNotNull(result.get("token"));
assertEquals(originalData, result.get("originalData"));
assertEquals(expireMinutes.toString(), result.get("expireMinutes"));
}
@Test
public void testTokenValidation() {
// 生成测试数据
String originalData = "token验证测试";
Map<String, String> encryptedInfo = encryptedQrCodeUtil.generateEncryptedData(originalData, 30L);
String token = encryptedInfo.get("token");
// 验证token有效性
assertTrue(encryptedQrCodeUtil.isTokenValid(token));
// 使token失效
encryptedQrCodeUtil.invalidateToken(token);
// 再次验证token应该无效
assertFalse(encryptedQrCodeUtil.isTokenValid(token));
}
@Test
public void testInvalidToken() {
// 测试无效token
assertFalse(encryptedQrCodeUtil.isTokenValid("invalid_token"));
assertFalse(encryptedQrCodeUtil.isTokenValid(""));
assertFalse(encryptedQrCodeUtil.isTokenValid(null));
}
@Test
public void testDecryptWithInvalidToken() {
// 测试用无效token解密
assertThrows(RuntimeException.class, () -> {
encryptedQrCodeUtil.decryptData("invalid_token", "encrypted_data");
});
}
@Test
public void testGenerateEncryptedQrCodeWithBusinessType() {
// 测试数据
String originalData = "用户登录数据";
int width = 200;
int height = 200;
Long expireMinutes = 30L;
String businessType = "LOGIN";
// 生成带业务类型的加密二维码
Map<String, Object> result = encryptedQrCodeUtil.generateEncryptedQrCode(
originalData, width, height, expireMinutes, businessType
);
assertNotNull(result);
assertNotNull(result.get("qrCodeBase64"));
assertNotNull(result.get("token"));
assertEquals(originalData, result.get("originalData"));
assertEquals(expireMinutes.toString(), result.get("expireMinutes"));
assertEquals(businessType, result.get("businessType"));
System.out.println("=== 带BusinessType的二维码生成测试 ===");
System.out.println("原始数据: " + originalData);
System.out.println("业务类型: " + businessType);
System.out.println("Token: " + result.get("token"));
System.out.println("二维码Base64长度: " + ((String)result.get("qrCodeBase64")).length());
// 验证不传businessType的情况
Map<String, Object> resultWithoutType = encryptedQrCodeUtil.generateEncryptedQrCode(
originalData, width, height, expireMinutes
);
assertNull(resultWithoutType.get("businessType"));
System.out.println("不传BusinessType时的结果: " + resultWithoutType.get("businessType"));
}
}

View File

@@ -0,0 +1,136 @@
package com.gxwebsoft.common.system.controller;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.Resource;
/**
* 微信登录控制器测试
*
* @author WebSoft
* @since 2025-08-23
*/
@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class WxLoginControllerTest {
@Resource
private UserService userService;
/**
* 测试从scene参数解析租户ID的逻辑
*/
@Test
public void testExtractTenantIdFromScene() {
log.info("=== 开始测试scene参数解析 ===");
// 测试用户ID 33103
Integer testUserId = 33103;
// 查询用户信息
User user = userService.getByIdIgnoreTenant(testUserId);
if (user != null) {
log.info("用户ID {} 对应的租户ID: {}", testUserId, user.getTenantId());
log.info("用户信息 - 用户名: {}, 手机: {}", user.getUsername(), user.getPhone());
} else {
log.warn("未找到用户ID: {}", testUserId);
}
// 测试不同的scene格式
String[] testScenes = {
"uid_33103",
"uid_1",
"uid_999999",
"invalid_scene",
null
};
for (String scene : testScenes) {
log.info("测试scene: {} -> 预期解析结果", scene);
// 这里模拟解析逻辑
if (scene != null && scene.startsWith("uid_")) {
try {
String userIdStr = scene.substring(4);
Integer userId = Integer.parseInt(userIdStr);
User testUser = userService.getByIdIgnoreTenant(userId);
if (testUser != null) {
log.info(" 解析成功: 用户ID {} -> 租户ID {}", userId, testUser.getTenantId());
} else {
log.info(" 用户不存在: 用户ID {} -> 默认租户ID 10550", userId);
}
} catch (Exception e) {
log.info(" 解析异常: {} -> 默认租户ID 10550", e.getMessage());
}
} else {
log.info(" 无效格式 -> 默认租户ID 10550");
}
}
log.info("=== scene参数解析测试完成 ===");
}
/**
* 测试查找特定用户
*/
@Test
public void testFindSpecificUsers() {
log.info("=== 开始查找特定用户 ===");
// 查找租户10550的用户
Integer[] testUserIds = {1, 2, 3, 33103, 10001, 10002};
for (Integer userId : testUserIds) {
User user = userService.getByIdIgnoreTenant(userId);
if (user != null) {
log.info("用户ID: {}, 租户ID: {}, 用户名: {}, 手机: {}",
userId, user.getTenantId(), user.getUsername(), user.getPhone());
} else {
log.info("用户ID: {} - 不存在", userId);
}
}
log.info("=== 特定用户查找完成 ===");
}
/**
* 测试URL解析
*/
@Test
public void testUrlParsing() {
log.info("=== 开始测试URL解析 ===");
String testUrl = "127.0.0.1:9200/api/wx-login/getOrderQRCodeUnlimited/uid_33103";
log.info("测试URL: {}", testUrl);
// 提取scene部分
String[] parts = testUrl.split("/");
String scene = parts[parts.length - 1]; // 最后一部分
log.info("提取的scene: {}", scene);
// 解析用户ID
if (scene.startsWith("uid_")) {
String userIdStr = scene.substring(4);
try {
Integer userId = Integer.parseInt(userIdStr);
log.info("解析的用户ID: {}", userId);
User user = userService.getByIdIgnoreTenant(userId);
if (user != null) {
log.info("对应的租户ID: {}", user.getTenantId());
} else {
log.warn("用户不存在");
}
} catch (NumberFormatException e) {
log.error("用户ID格式错误: {}", userIdStr);
}
}
log.info("=== URL解析测试完成 ===");
}
}

View File

@@ -0,0 +1,100 @@
package com.gxwebsoft.common.system.service;
import com.gxwebsoft.common.system.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.Resource;
/**
* 用户忽略租户隔离功能测试
*
* @author WebSoft
* @since 2025-08-23
*/
@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class UserIgnoreTenantTest {
@Resource
private UserService userService;
/**
* 测试忽略租户隔离查询用户
*/
@Test
public void testGetByIdIgnoreTenant() {
// 测试用户ID请根据实际数据库中的用户ID进行调整
Integer testUserId = 1;
log.info("=== 开始测试忽略租户隔离查询用户功能 ===");
// 1. 使用普通方法查询用户(受租户隔离影响)
User userNormal = userService.getById(testUserId);
log.info("普通查询结果 - 用户ID: {}, 用户信息: {}", testUserId,
userNormal != null ? userNormal.getUsername() : "null");
// 2. 使用忽略租户隔离方法查询用户
User userIgnoreTenant = userService.getByIdIgnoreTenant(testUserId);
log.info("忽略租户隔离查询结果 - 用户ID: {}, 用户信息: {}", testUserId,
userIgnoreTenant != null ? userIgnoreTenant.getUsername() : "null");
// 3. 验证结果
if (userIgnoreTenant != null) {
log.info("✅ 忽略租户隔离查询成功!");
log.info("用户详情 - ID: {}, 用户名: {}, 昵称: {}, 租户ID: {}",
userIgnoreTenant.getUserId(),
userIgnoreTenant.getUsername(),
userIgnoreTenant.getNickname(),
userIgnoreTenant.getTenantId());
} else {
log.error("❌ 忽略租户隔离查询失败!");
}
log.info("=== 忽略租户隔离查询用户功能测试完成 ===");
}
/**
* 测试参数验证
*/
@Test
public void testGetByIdIgnoreTenantValidation() {
log.info("=== 开始测试参数验证 ===");
// 测试null用户ID
User result1 = userService.getByIdIgnoreTenant(null);
log.info("null用户ID测试结果: {}", result1 == null ? "成功(返回null)" : "失败");
// 测试不存在的用户ID
User result2 = userService.getByIdIgnoreTenant(999999);
log.info("不存在用户ID测试结果: {}", result2 == null ? "成功(返回null)" : "失败");
log.info("=== 参数验证测试完成 ===");
}
/**
* 测试跨租户查询
*/
@Test
public void testCrossTenantQuery() {
log.info("=== 开始测试跨租户查询 ===");
// 查询不同租户的用户(请根据实际数据调整)
Integer[] testUserIds = {1, 2, 3, 4, 5};
for (Integer userId : testUserIds) {
User user = userService.getByIdIgnoreTenant(userId);
if (user != null) {
log.info("用户ID: {}, 用户名: {}, 租户ID: {}",
user.getUserId(), user.getUsername(), user.getTenantId());
} else {
log.info("用户ID: {} - 不存在", userId);
}
}
log.info("=== 跨租户查询测试完成 ===");
}
}

View File

@@ -0,0 +1,174 @@
package com.gxwebsoft.common.system.service;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.CmsWebsiteField;
import com.gxwebsoft.cms.service.CmsWebsiteFieldService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.Resource;
import java.util.List;
/**
* 微信小程序配置测试
*
* @author WebSoft
* @since 2025-08-23
*/
@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class WeixinConfigTest {
@Resource
private SettingService settingService;
@Resource
private CmsWebsiteFieldService cmsWebsiteFieldService;
/**
* 测试从cms_website_field表获取微信小程序配置
*/
@Test
public void testGetWeixinConfigFromWebsiteField() {
Integer tenantId = 10550;
log.info("=== 开始测试从cms_website_field表获取微信小程序配置 ===");
// 1. 查看cms_website_field表中的所有配置
List<CmsWebsiteField> allFields = cmsWebsiteFieldService.list(
new LambdaQueryWrapper<CmsWebsiteField>()
.eq(CmsWebsiteField::getTenantId, tenantId)
.eq(CmsWebsiteField::getDeleted, 0)
);
log.info("租户{}的所有cms_website_field配置:", tenantId);
for (CmsWebsiteField field : allFields) {
log.info(" - ID: {}, Name: {}, Value: {}", field.getId(), field.getName(), field.getValue());
}
// 2. 查找AppID配置
CmsWebsiteField appIdField = cmsWebsiteFieldService.getOne(
new LambdaQueryWrapper<CmsWebsiteField>()
.eq(CmsWebsiteField::getName, "AppID")
.eq(CmsWebsiteField::getTenantId, tenantId)
.eq(CmsWebsiteField::getDeleted, 0)
);
log.info("AppID配置: {}", appIdField);
// 3. 查找AppSecret配置
CmsWebsiteField appSecretField = cmsWebsiteFieldService.getOne(
new LambdaQueryWrapper<CmsWebsiteField>()
.eq(CmsWebsiteField::getName, "AppSecret")
.eq(CmsWebsiteField::getTenantId, tenantId)
.eq(CmsWebsiteField::getDeleted, 0)
);
log.info("AppSecret配置: {}", appSecretField);
// 4. 测试获取微信小程序配置
try {
JSONObject config = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId);
log.info("✅ 成功获取微信小程序配置: {}", config);
} catch (Exception e) {
log.error("❌ 获取微信小程序配置失败: {}", e.getMessage());
}
log.info("=== 微信小程序配置测试完成 ===");
}
/**
* 测试不同name的查询
*/
@Test
public void testDifferentNameQueries() {
Integer tenantId = 10550;
log.info("=== 开始测试不同name的查询 ===");
String[] nameVariations = {"AppID", "appId", "APPID", "app_id", "AppSecret", "appSecret", "APPSECRET", "app_secret"};
for (String name : nameVariations) {
CmsWebsiteField field = cmsWebsiteFieldService.getOne(
new LambdaQueryWrapper<CmsWebsiteField>()
.eq(CmsWebsiteField::getName, name)
.eq(CmsWebsiteField::getTenantId, tenantId)
.eq(CmsWebsiteField::getDeleted, 0)
);
if (field != null) {
log.info("找到配置 - Name: {}, Value: {}", name, field.getValue());
} else {
log.info("未找到配置 - Name: {}", name);
}
}
log.info("=== 不同name查询测试完成 ===");
}
/**
* 测试创建测试配置
*/
@Test
public void testCreateTestConfig() {
Integer tenantId = 10550;
log.info("=== 开始创建测试配置 ===");
// 创建AppID配置
CmsWebsiteField appIdField = new CmsWebsiteField();
appIdField.setName("AppID");
appIdField.setValue("wx1234567890abcdef"); // 测试AppID
appIdField.setTenantId(tenantId);
appIdField.setType(0); // 文本类型
appIdField.setComments("微信小程序AppID");
appIdField.setDeleted(0);
// 创建AppSecret配置
CmsWebsiteField appSecretField = new CmsWebsiteField();
appSecretField.setName("AppSecret");
appSecretField.setValue("abcdef1234567890abcdef1234567890"); // 测试AppSecret
appSecretField.setTenantId(tenantId);
appSecretField.setType(0); // 文本类型
appSecretField.setComments("微信小程序AppSecret");
appSecretField.setDeleted(0);
try {
// 检查是否已存在
CmsWebsiteField existingAppId = cmsWebsiteFieldService.getOne(
new LambdaQueryWrapper<CmsWebsiteField>()
.eq(CmsWebsiteField::getName, "AppID")
.eq(CmsWebsiteField::getTenantId, tenantId)
);
if (existingAppId == null) {
cmsWebsiteFieldService.save(appIdField);
log.info("✅ 创建AppID配置成功");
} else {
log.info("AppID配置已存在跳过创建");
}
CmsWebsiteField existingAppSecret = cmsWebsiteFieldService.getOne(
new LambdaQueryWrapper<CmsWebsiteField>()
.eq(CmsWebsiteField::getName, "AppSecret")
.eq(CmsWebsiteField::getTenantId, tenantId)
);
if (existingAppSecret == null) {
cmsWebsiteFieldService.save(appSecretField);
log.info("✅ 创建AppSecret配置成功");
} else {
log.info("AppSecret配置已存在跳过创建");
}
} catch (Exception e) {
log.error("❌ 创建测试配置失败: {}", e.getMessage());
}
log.info("=== 创建测试配置完成 ===");
}
}