forked from wangziqi/ruoyi-vue-pro
feat(education): resolve student tenant context
This commit is contained in:
@@ -4,6 +4,10 @@ import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 教育模块配置属性
|
||||
*
|
||||
@@ -24,4 +28,19 @@ public class EducationProperties {
|
||||
*/
|
||||
private String version = "1.0.0";
|
||||
|
||||
/**
|
||||
* 精确主机名到租户名的映射,用于 DNS 与 system_tenant.websites 不一致的场景。
|
||||
* key = 标准化后的主机名(小写、无端口),value = 租户名。
|
||||
* 示例:{ "staging.school.com": "demo-school" }
|
||||
*
|
||||
* 映射优先级高于 system_tenant.websites 字段匹配。
|
||||
*/
|
||||
private Map<String, String> hostnameTenantMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 学生端全局开放的登录方式。它描述当前部署启用的 Member 登录入口,
|
||||
* 不是租户级 OAuth 提供方探测结果。
|
||||
*/
|
||||
private List<String> loginMethods = List.of("PASSWORD", "SMS");
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.TenantCommonApi;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.dto.TenantRespDTO;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||
import cn.iocoder.yudao.module.education.controller.app.vo.EducationContextRespVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.UNAUTHORIZED;
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.module.education.enums.ErrorCodeConstants.EDUCATION_TENANT_NOT_ACTIVE;
|
||||
|
||||
/**
|
||||
* 教育当前上下文 Controller — 学生端
|
||||
*
|
||||
* <p>根据当前认证用户和安全上下文返回教育业务所需的租户与用户信息。
|
||||
* 该端点需要认证,不信任请求体中的 userId。</p>
|
||||
*
|
||||
* @author 恭学教育
|
||||
*/
|
||||
@Tag(name = "用户 APP - 教育上下文")
|
||||
@RestController
|
||||
@RequestMapping("/education")
|
||||
@Validated
|
||||
@ConditionalOnProperty(prefix = "yudao.education", name = "enabled", havingValue = "true")
|
||||
public class EducationContextController {
|
||||
|
||||
@Resource
|
||||
private TenantCommonApi tenantCommonApi;
|
||||
|
||||
@GetMapping("/context")
|
||||
@Operation(summary = "获取当前教育上下文",
|
||||
description = "根据当前认证用户和租户上下文返回教育业务信息。需要登录态。")
|
||||
public CommonResult<EducationContextRespVO> getContext() {
|
||||
// 1. 从安全上下文获取用户编号(不信任请求参数)
|
||||
Long userId = SecurityFrameworkUtils.getLoginUserId();
|
||||
if (userId == null) {
|
||||
throw exception(UNAUTHORIZED);
|
||||
}
|
||||
|
||||
// 2. 获取当前租户并验证状态(由 TenantSecurityWebFilter 前置完成,此处二次验证)
|
||||
Long tenantId = TenantContextHolder.getRequiredTenantId();
|
||||
tenantCommonApi.validateTenant(tenantId);
|
||||
|
||||
// 3. 获取租户信息用于展示
|
||||
TenantRespDTO tenant = tenantCommonApi.getTenant(tenantId);
|
||||
if (tenant == null) {
|
||||
throw exception(EDUCATION_TENANT_NOT_ACTIVE);
|
||||
}
|
||||
|
||||
// 构建响应
|
||||
EducationContextRespVO resp = EducationContextRespVO.builder()
|
||||
.userId(userId)
|
||||
.tenantId(tenantId)
|
||||
.tenantName(tenant.getName())
|
||||
.displayName(tenant.getName())
|
||||
.build();
|
||||
return success(resp);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app.tenant;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.TenantCommonApi;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.dto.TenantRespDTO;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||
import cn.iocoder.yudao.module.education.config.EducationProperties;
|
||||
import cn.iocoder.yudao.module.education.controller.app.tenant.vo.EducationTenantRespVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.annotation.security.PermitAll;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.module.education.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 教育租户识别 Controller — 学生端入口
|
||||
*
|
||||
* <p>用于在登录前通过主机名或租户名解析当前租户,返回引导信息。
|
||||
* 该端点无需认证(@PermitAll),忽略租户上下文(@TenantIgnore)。</p>
|
||||
*
|
||||
* @author 恭学教育
|
||||
*/
|
||||
@Tag(name = "用户 APP - 教育租户识别")
|
||||
@RestController
|
||||
@RequestMapping("/education/tenant")
|
||||
@Validated
|
||||
@ConditionalOnProperty(prefix = "yudao.education", name = "enabled", havingValue = "true")
|
||||
public class EducationTenantController {
|
||||
|
||||
@Resource
|
||||
private TenantCommonApi tenantCommonApi;
|
||||
|
||||
@Resource
|
||||
private EducationProperties educationProperties;
|
||||
|
||||
@GetMapping("/resolve")
|
||||
@PermitAll
|
||||
@TenantIgnore
|
||||
@Operation(summary = "解析租户",
|
||||
description = "通过主机名或租户名解析租户信息,返回登录引导所需的基础字段。" +
|
||||
"hostname 和 tenantName 至少提供一个。")
|
||||
@Parameters({
|
||||
@Parameter(name = "hostname", description = "标准化主机名(小写、无端口、无协议)", example = "school.example.com"),
|
||||
@Parameter(name = "tenantName", description = "租户名", example = "demo-school")
|
||||
})
|
||||
public CommonResult<EducationTenantRespVO> resolve(
|
||||
@RequestParam(value = "hostname", required = false) String hostname,
|
||||
@RequestParam(value = "tenantName", required = false) String tenantName) {
|
||||
|
||||
// 1. 校验输入:至少提供一个
|
||||
if (StrUtil.isBlank(hostname) && StrUtil.isBlank(tenantName)) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 和 tenantName 不能同时为空");
|
||||
}
|
||||
|
||||
// 2. 主机名标准化
|
||||
String normalizedHostname = normalizeHostname(hostname);
|
||||
|
||||
// 3. 解析租户并验证一致性
|
||||
TenantRespDTO tenant = resolveTenantAndEnsureConsistency(normalizedHostname, tenantName);
|
||||
|
||||
// 4. 验证租户状态
|
||||
if (tenant == null) {
|
||||
throw exception(EDUCATION_TENANT_NOT_FOUND);
|
||||
}
|
||||
if (CommonStatusEnum.isDisable(tenant.getStatus())) {
|
||||
throw exception(EDUCATION_TENANT_DISABLED);
|
||||
}
|
||||
if (DateUtils.isExpired(tenant.getExpireTime())) {
|
||||
throw exception(EDUCATION_TENANT_NOT_ACTIVE);
|
||||
}
|
||||
|
||||
// 5. 构建响应
|
||||
EducationTenantRespVO resp = EducationTenantRespVO.builder()
|
||||
.tenantId(tenant.getId())
|
||||
.tenantName(tenant.getName())
|
||||
.displayName(tenant.getName())
|
||||
.status("ACTIVE")
|
||||
.loginMethods(new ArrayList<>(educationProperties.getLoginMethods()))
|
||||
.build();
|
||||
return success(resp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按优先级解析租户:
|
||||
* 1. 如果提供了 tenantName,直接通过租户名查询
|
||||
* 2. 如果提供了 hostname:
|
||||
* a. 先查 EducationProperties.hostnameTenantMap 配置映射
|
||||
* b. 再通过 system_tenant.websites 字段匹配
|
||||
*
|
||||
* @param hostname 标准化后的主机名,可能为 null
|
||||
* @param tenantName 租户名,可能为 null
|
||||
* @return 租户 DTO,未找到返回 null
|
||||
*/
|
||||
private TenantRespDTO resolveTenantAndEnsureConsistency(String hostname, String tenantName) {
|
||||
TenantRespDTO byName = StrUtil.isNotBlank(tenantName)
|
||||
? tenantCommonApi.getTenantByName(tenantName.trim()) : null;
|
||||
TenantRespDTO byHostname = StrUtil.isNotBlank(hostname) ? resolveTenantByHostname(hostname) : null;
|
||||
if (byName != null && byHostname != null && !byName.getId().equals(byHostname.getId())) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 与 tenantName 指向不同租户");
|
||||
}
|
||||
if (StrUtil.isNotBlank(tenantName) && byName == null) {
|
||||
return null;
|
||||
}
|
||||
return byName != null ? byName : byHostname;
|
||||
}
|
||||
|
||||
private TenantRespDTO resolveTenantByHostname(String hostname) {
|
||||
String mappedTenantName = educationProperties.getHostnameTenantMap().entrySet().stream()
|
||||
.filter(entry -> hostname.equals(normalizeHostname(entry.getKey())))
|
||||
.map(entry -> entry.getValue())
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (StrUtil.isNotBlank(mappedTenantName)) {
|
||||
return tenantCommonApi.getTenantByName(mappedTenantName);
|
||||
}
|
||||
return tenantCommonApi.getTenantByWebsite(hostname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主机名标准化:转为小写并拒绝协议、路径和非法端口。
|
||||
* 端口会被保留,因为 system_tenant.websites 使用精确 authority 匹配。
|
||||
*
|
||||
* @param hostname 原始主机名
|
||||
* @return 标准化后的主机名,输入为空时返回 null
|
||||
* @throws cn.iocoder.yudao.framework.common.exception.ServiceException 格式不合法时
|
||||
*/
|
||||
static String normalizeHostname(String hostname) {
|
||||
if (StrUtil.isBlank(hostname)) {
|
||||
return null;
|
||||
}
|
||||
String normalized = hostname.trim();
|
||||
|
||||
// 拒绝含协议的输入(如 http://example.com)
|
||||
if (normalized.contains("://")) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED,
|
||||
"hostname 不应包含协议,收到: " + hostname);
|
||||
}
|
||||
// 拒绝含路径的输入
|
||||
if (normalized.contains("/")) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED,
|
||||
"hostname 不应包含路径,收到: " + hostname);
|
||||
}
|
||||
|
||||
// 校验端口。保留合法端口,确保与 system_tenant.websites 的精确值一致。
|
||||
int colonIdx = normalized.lastIndexOf(':');
|
||||
if (colonIdx > 0 && !normalized.startsWith("[")) {
|
||||
String afterColon = normalized.substring(colonIdx + 1);
|
||||
if (afterColon.isEmpty() || !afterColon.chars().allMatch(Character::isDigit)) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 端口不合法");
|
||||
}
|
||||
int port;
|
||||
try {
|
||||
port = Integer.parseInt(afterColon);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 端口不合法");
|
||||
}
|
||||
if (port < 1 || port > 65535) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 端口不合法");
|
||||
}
|
||||
}
|
||||
|
||||
return normalized.toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app.tenant.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Schema(description = "用户 APP - 教育租户识别 Response VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class EducationTenantRespVO {
|
||||
|
||||
@Schema(description = "租户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private Long tenantId;
|
||||
|
||||
@Schema(description = "租户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "demo-school")
|
||||
private String tenantName;
|
||||
|
||||
@Schema(description = "租户显示名称", example = "Demo School")
|
||||
private String displayName;
|
||||
|
||||
@Schema(description = "租户状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "ACTIVE",
|
||||
allowableValues = {"ACTIVE", "DISABLED", "EXPIRED"})
|
||||
private String status;
|
||||
|
||||
@Schema(description = "支持的登录方式", requiredMode = Schema.RequiredMode.REQUIRED,
|
||||
example = "[\"PASSWORD\", \"SMS\"]")
|
||||
private List<String> loginMethods;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Schema(description = "用户 APP - 教育当前上下文 Response VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class EducationContextRespVO {
|
||||
|
||||
@Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "租户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
|
||||
private Long tenantId;
|
||||
|
||||
@Schema(description = "租户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "demo-school")
|
||||
private String tenantName;
|
||||
|
||||
@Schema(description = "租户显示名称", example = "Demo School")
|
||||
private String displayName;
|
||||
|
||||
}
|
||||
@@ -12,4 +12,10 @@ public interface ErrorCodeConstants {
|
||||
// ========== 通用模块 1-005-001-000 ==========
|
||||
ErrorCode EDUCATION_DISABLED = new ErrorCode(1_005_001_000, "教育模块未启用,请联系管理员");
|
||||
|
||||
// ========== 租户识别 1-005-001-001 ~ 1-005-001-010 ==========
|
||||
ErrorCode EDUCATION_TENANT_NOT_FOUND = new ErrorCode(1_005_001_001, "租户不存在");
|
||||
ErrorCode EDUCATION_TENANT_DISABLED = new ErrorCode(1_005_001_002, "租户已被禁用");
|
||||
ErrorCode EDUCATION_TENANT_RESOLVE_FAILED = new ErrorCode(1_005_001_003, "租户识别失败:{}");
|
||||
ErrorCode EDUCATION_TENANT_NOT_ACTIVE = new ErrorCode(1_005_001_004, "当前租户不可用,请联系管理员");
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
@@ -47,4 +49,12 @@ public class EducationPropertiesTest {
|
||||
assertEquals("1.0.0", defaults.getVersion(), "默认版本应为 1.0.0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHostnameTenantMapDefaults() {
|
||||
EducationProperties defaults = new EducationProperties();
|
||||
assertNotNull(defaults.getHostnameTenantMap(), "hostnameTenantMap 默认不应为 null");
|
||||
assertTrue(defaults.getHostnameTenantMap().isEmpty(), "hostnameTenantMap 默认应为空");
|
||||
assertEquals(List.of("PASSWORD", "SMS"), defaults.getLoginMethods(), "默认登录方式应与 Member 入口一致");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app.tenant;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* {@link EducationTenantController} 的单元测试
|
||||
*
|
||||
* @author 恭学教育
|
||||
*/
|
||||
class EducationTenantControllerTest {
|
||||
|
||||
// ========== normalizeHostname ==========
|
||||
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
void normalizeHostname_shouldReturnNullForBlankInput(String hostname) {
|
||||
assertNull(EducationTenantController.normalizeHostname(hostname));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldLowercase() {
|
||||
assertEquals("school.example.com",
|
||||
EducationTenantController.normalizeHostname("School.Example.COM"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldTrimWhitespace() {
|
||||
assertEquals("school.example.com",
|
||||
EducationTenantController.normalizeHostname(" school.example.com "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldKeepPort() {
|
||||
assertEquals("school.example.com:8080",
|
||||
EducationTenantController.normalizeHostname("school.example.com:8080"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldLowercaseAndKeepPort() {
|
||||
assertEquals("school.example.com:8080",
|
||||
EducationTenantController.normalizeHostname("School.Example.COM:8080"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldRejectProtocol() {
|
||||
assertThrows(RuntimeException.class, () ->
|
||||
EducationTenantController.normalizeHostname("http://school.example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldRejectPath() {
|
||||
assertThrows(RuntimeException.class, () ->
|
||||
EducationTenantController.normalizeHostname("school.example.com/path"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldRejectProtocolWithPort() {
|
||||
assertThrows(RuntimeException.class, () ->
|
||||
EducationTenantController.normalizeHostname("https://school.example.com:8443"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldRejectOversizedPort() {
|
||||
assertThrows(RuntimeException.class,
|
||||
() -> EducationTenantController.normalizeHostname("school.example.com:999999999999"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldKeepIPv6Colons() {
|
||||
// A bare IPv6 address like ::1: the port-stripping should be safe
|
||||
// because last colon is followed by digits -> treated as port
|
||||
// We'll just check no exception thrown
|
||||
assertDoesNotThrow(() -> EducationTenantController.normalizeHostname("[::1]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHostname_shouldRejectNonNumericPort() {
|
||||
assertThrows(RuntimeException.class,
|
||||
() -> EducationTenantController.normalizeHostname("host:name"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app.tenant;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.TenantCommonApi;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.dto.TenantRespDTO;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.module.education.config.EducationProperties;
|
||||
import cn.iocoder.yudao.module.education.controller.app.tenant.vo.EducationTenantRespVO;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@link EducationTenantController} 的集成测试 — 测试租户解析流程
|
||||
*
|
||||
* @author 恭学教育
|
||||
*/
|
||||
@SpringBootTest(classes = EducationTenantResolveIntegrationTest.Config.class,
|
||||
properties = {
|
||||
"yudao.education.enabled=true",
|
||||
"yudao.education.version=1.0.0-test"
|
||||
},
|
||||
webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
@ActiveProfiles("unit-test")
|
||||
class EducationTenantResolveIntegrationTest {
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(EducationProperties.class)
|
||||
@Import(EducationTenantController.class)
|
||||
static class Config {
|
||||
}
|
||||
|
||||
@Resource
|
||||
private EducationTenantController controller;
|
||||
|
||||
@MockitoBean
|
||||
private TenantCommonApi tenantCommonApi;
|
||||
|
||||
// ========== happy path ==========
|
||||
|
||||
@Test
|
||||
void resolve_byTenantName_shouldReturnActiveTenant() {
|
||||
TenantRespDTO dto = buildTenant(100L, "demo-school", CommonStatusEnum.ENABLE.getStatus());
|
||||
when(tenantCommonApi.getTenantByName("demo-school")).thenReturn(dto);
|
||||
|
||||
CommonResult<EducationTenantRespVO> result = controller.resolve(null, "demo-school");
|
||||
|
||||
assertTrue(result.isSuccess());
|
||||
EducationTenantRespVO data = result.getData();
|
||||
assertEquals(100L, data.getTenantId());
|
||||
assertEquals("demo-school", data.getTenantName());
|
||||
assertEquals("ACTIVE", data.getStatus());
|
||||
assertTrue(data.getLoginMethods().contains("PASSWORD"));
|
||||
assertTrue(data.getLoginMethods().contains("SMS"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_byHostname_shouldFindByWebsite() {
|
||||
TenantRespDTO dto = buildTenant(200L, "school-alpha", CommonStatusEnum.ENABLE.getStatus());
|
||||
when(tenantCommonApi.getTenantByWebsite("school.example.com")).thenReturn(dto);
|
||||
|
||||
CommonResult<EducationTenantRespVO> result = controller.resolve("school.example.com", null);
|
||||
|
||||
assertTrue(result.isSuccess());
|
||||
assertEquals(200L, result.getData().getTenantId());
|
||||
assertEquals("school-alpha", result.getData().getTenantName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_byHostname_shouldNormalizeCaseAndKeepPort() {
|
||||
TenantRespDTO dto = buildTenant(300L, "school-beta", CommonStatusEnum.ENABLE.getStatus());
|
||||
when(tenantCommonApi.getTenantByWebsite("school.example.com:8443")).thenReturn(dto);
|
||||
|
||||
CommonResult<EducationTenantRespVO> result = controller.resolve("School.Example.COM:8443", null);
|
||||
|
||||
assertTrue(result.isSuccess());
|
||||
assertEquals(300L, result.getData().getTenantId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_conflictingHostnameAndTenantName_shouldFail() {
|
||||
TenantRespDTO byName = buildTenant(600L, "school-a", CommonStatusEnum.ENABLE.getStatus());
|
||||
TenantRespDTO byHost = buildTenant(601L, "school-b", CommonStatusEnum.ENABLE.getStatus());
|
||||
when(tenantCommonApi.getTenantByName("school-a")).thenReturn(byName);
|
||||
when(tenantCommonApi.getTenantByWebsite("school-b.example.com")).thenReturn(byHost);
|
||||
|
||||
RuntimeException exception = assertThrows(RuntimeException.class,
|
||||
() -> controller.resolve("school-b.example.com", "school-a"));
|
||||
assertTrue(exception.getMessage().contains("指向不同租户"));
|
||||
}
|
||||
|
||||
// ========== error cases ==========
|
||||
|
||||
@Test
|
||||
void resolve_noInput_shouldFail() {
|
||||
try {
|
||||
controller.resolve(null, null);
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("不能同时为空"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_blankInput_shouldFail() {
|
||||
try {
|
||||
controller.resolve(" ", "");
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("不能同时为空"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_notFound_shouldFail() {
|
||||
when(tenantCommonApi.getTenantByName("nonexistent")).thenReturn(null);
|
||||
when(tenantCommonApi.getTenantByWebsite(anyString())).thenReturn(null);
|
||||
|
||||
try {
|
||||
controller.resolve(null, "nonexistent");
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("租户不存在"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_disabled_shouldFail() {
|
||||
TenantRespDTO dto = buildTenant(400L, "disabled-school", CommonStatusEnum.DISABLE.getStatus());
|
||||
when(tenantCommonApi.getTenantByName("disabled-school")).thenReturn(dto);
|
||||
|
||||
try {
|
||||
controller.resolve(null, "disabled-school");
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("已被禁用"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_expired_shouldFail() {
|
||||
TenantRespDTO dto = buildTenant(500L, "expired-school", CommonStatusEnum.ENABLE.getStatus());
|
||||
dto.setExpireTime(LocalDateTime.now().minusDays(1));
|
||||
when(tenantCommonApi.getTenantByName("expired-school")).thenReturn(dto);
|
||||
|
||||
try {
|
||||
controller.resolve(null, "expired-school");
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("当前租户不可用"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_protocolInHostname_shouldFail() {
|
||||
try {
|
||||
controller.resolve("http://evil.com", null);
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("协议"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_pathInHostname_shouldFail() {
|
||||
try {
|
||||
controller.resolve("school.com/admin", null);
|
||||
fail("Expected exception");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("路径"));
|
||||
}
|
||||
}
|
||||
|
||||
// ========== helpers ==========
|
||||
|
||||
private static TenantRespDTO buildTenant(Long id, String name, Integer status) {
|
||||
TenantRespDTO dto = new TenantRespDTO();
|
||||
dto.setId(id);
|
||||
dto.setName(name);
|
||||
dto.setStatus(status);
|
||||
dto.setWebsites(Collections.singletonList(name + ".example.com"));
|
||||
dto.setExpireTime(LocalDateTime.now().plusYears(1));
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
package cn.iocoder.yudao.module.education.framework.web.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.TenantCommonApi;
|
||||
import cn.iocoder.yudao.module.education.config.EducationProperties;
|
||||
import cn.iocoder.yudao.module.education.controller.admin.EducationCapabilityController;
|
||||
import cn.iocoder.yudao.module.education.controller.app.EducationContextController;
|
||||
import cn.iocoder.yudao.module.education.controller.app.tenant.EducationTenantController;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* {@link EducationWebConfiguration} 的单元测试
|
||||
@@ -14,12 +19,19 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
class EducationWebConfigurationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(EducationWebConfiguration.class, EducationCapabilityController.class);
|
||||
.withUserConfiguration(EducationWebConfiguration.class,
|
||||
EducationCapabilityController.class,
|
||||
EducationTenantController.class,
|
||||
EducationContextController.class)
|
||||
.withBean(TenantCommonApi.class, () -> mock(TenantCommonApi.class))
|
||||
.withBean(EducationProperties.class, EducationProperties::new);
|
||||
|
||||
@Test
|
||||
void shouldNotRegisterEducationWebBeansByDefault() {
|
||||
contextRunner.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(EducationCapabilityController.class);
|
||||
assertThat(context).doesNotHaveBean(EducationTenantController.class);
|
||||
assertThat(context).doesNotHaveBean(EducationContextController.class);
|
||||
assertThat(context).doesNotHaveBean("educationGroupedOpenApi");
|
||||
});
|
||||
}
|
||||
@@ -29,6 +41,8 @@ class EducationWebConfigurationTest {
|
||||
contextRunner.withPropertyValues("yudao.education.enabled=true")
|
||||
.run(context -> {
|
||||
assertThat(context).hasSingleBean(EducationCapabilityController.class);
|
||||
assertThat(context).hasSingleBean(EducationTenantController.class);
|
||||
assertThat(context).hasSingleBean(EducationContextController.class);
|
||||
assertThat(context).hasBean("educationGroupedOpenApi");
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user