|
@@ -10,6 +10,9 @@ import { AreaEntityMt, AreaLevel } from '../../src/modules/areas/area.entity.mt'
|
|
|
import { DisabledStatus } from '@d8d/shared-types';
|
|
import { DisabledStatus } from '@d8d/shared-types';
|
|
|
import { TestDataFactory } from '../utils/test-data-factory';
|
|
import { TestDataFactory } from '../utils/test-data-factory';
|
|
|
import { TestQueryFactory } from '../utils/test-query-factory';
|
|
import { TestQueryFactory } from '../utils/test-query-factory';
|
|
|
|
|
+import { AuthService } from '@d8d/auth-module-mt';
|
|
|
|
|
+import { UserServiceMt } from '@d8d/user-module-mt';
|
|
|
|
|
+import { UserEntityMt } from '@d8d/user-module-mt';
|
|
|
|
|
|
|
|
// 定义响应类型
|
|
// 定义响应类型
|
|
|
interface SuccessResponse {
|
|
interface SuccessResponse {
|
|
@@ -42,18 +45,39 @@ interface ErrorResponse {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 设置集成测试钩子
|
|
// 设置集成测试钩子
|
|
|
-setupIntegrationDatabaseHooksWithEntities([AreaEntityMt])
|
|
|
|
|
|
|
+setupIntegrationDatabaseHooksWithEntities([AreaEntityMt, UserEntityMt])
|
|
|
|
|
|
|
|
describe('区域API集成测试', () => {
|
|
describe('区域API集成测试', () => {
|
|
|
let client: ReturnType<typeof testClient<typeof areasRoutesMt>>;
|
|
let client: ReturnType<typeof testClient<typeof areasRoutesMt>>;
|
|
|
|
|
+ let authService: AuthService;
|
|
|
|
|
+ let userService: UserServiceMt;
|
|
|
|
|
+ let testToken: string;
|
|
|
|
|
+ let testUser: any;
|
|
|
let testAreas: AreaEntityMt[];
|
|
let testAreas: AreaEntityMt[];
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
beforeEach(async () => {
|
|
|
// 创建测试客户端
|
|
// 创建测试客户端
|
|
|
client = testClient(areasRoutesMt);
|
|
client = testClient(areasRoutesMt);
|
|
|
|
|
|
|
|
- // 创建测试数据
|
|
|
|
|
|
|
+ // 获取数据源
|
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
|
|
+ if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化服务
|
|
|
|
|
+ userService = new UserServiceMt(dataSource);
|
|
|
|
|
+ authService = new AuthService(userService);
|
|
|
|
|
+
|
|
|
|
|
+ // 创建测试用户并生成token
|
|
|
|
|
+ testUser = await TestDataFactory.createTestUser(dataSource, {
|
|
|
|
|
+ username: 'testuser_areas',
|
|
|
|
|
+ password: 'TestPassword123!',
|
|
|
|
|
+ email: 'testuser_areas@example.com'
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 生成测试用户的token
|
|
|
|
|
+ testToken = authService.generateToken(testUser);
|
|
|
|
|
+
|
|
|
|
|
+ // 创建测试数据
|
|
|
|
|
|
|
|
// 创建启用状态的省份(租户1)
|
|
// 创建启用状态的省份(租户1)
|
|
|
const province1 = await TestDataFactory.createTestArea(dataSource, {
|
|
const province1 = await TestDataFactory.createTestArea(dataSource, {
|
|
@@ -164,6 +188,10 @@ describe('区域API集成测试', () => {
|
|
|
it('应该成功获取启用状态的省份列表', async () => {
|
|
it('应该成功获取启用状态的省份列表', async () => {
|
|
|
const response = await client.provinces.$get({
|
|
const response = await client.provinces.$get({
|
|
|
query: TestQueryFactory.createProvincesQuery()
|
|
query: TestQueryFactory.createProvincesQuery()
|
|
|
|
|
+ }, {
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
IntegrationTestAssertions.expectStatus(response, 200);
|
|
IntegrationTestAssertions.expectStatus(response, 200);
|
|
@@ -198,6 +226,10 @@ describe('区域API集成测试', () => {
|
|
|
it('应该正确处理分页参数', async () => {
|
|
it('应该正确处理分页参数', async () => {
|
|
|
const response = await client.provinces.$get({
|
|
const response = await client.provinces.$get({
|
|
|
query: TestQueryFactory.createPaginationQuery(1, 2)
|
|
query: TestQueryFactory.createPaginationQuery(1, 2)
|
|
|
|
|
+ }, {
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
IntegrationTestAssertions.expectStatus(response, 200);
|
|
IntegrationTestAssertions.expectStatus(response, 200);
|
|
@@ -382,10 +414,24 @@ describe('区域API集成测试', () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
describe('租户数据隔离测试', () => {
|
|
describe('租户数据隔离测试', () => {
|
|
|
|
|
+ let tenant2Token: string;
|
|
|
|
|
+ let tenant2User: any;
|
|
|
|
|
+
|
|
|
beforeEach(async () => {
|
|
beforeEach(async () => {
|
|
|
// 为租户2创建测试数据
|
|
// 为租户2创建测试数据
|
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
|
|
|
|
|
|
|
+ // 创建租户2的用户
|
|
|
|
|
+ tenant2User = await TestDataFactory.createTestUser(dataSource, {
|
|
|
|
|
+ username: 'testuser_tenant2',
|
|
|
|
|
+ password: 'TestPassword123!',
|
|
|
|
|
+ email: 'testuser_tenant2@example.com',
|
|
|
|
|
+ tenantId: 2
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 生成租户2用户的token
|
|
|
|
|
+ tenant2Token = authService.generateToken(tenant2User);
|
|
|
|
|
+
|
|
|
// 租户2的省份
|
|
// 租户2的省份
|
|
|
await TestDataFactory.createTestArea(dataSource, {
|
|
await TestDataFactory.createTestArea(dataSource, {
|
|
|
name: '租户2-北京市',
|
|
name: '租户2-北京市',
|
|
@@ -407,6 +453,10 @@ describe('区域API集成测试', () => {
|
|
|
// 测试租户1的数据
|
|
// 测试租户1的数据
|
|
|
const response1 = await client.provinces.$get({
|
|
const response1 = await client.provinces.$get({
|
|
|
query: TestQueryFactory.createProvincesQuery()
|
|
query: TestQueryFactory.createProvincesQuery()
|
|
|
|
|
+ }, {
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
IntegrationTestAssertions.expectStatus(response1, 200);
|
|
IntegrationTestAssertions.expectStatus(response1, 200);
|
|
@@ -425,7 +475,11 @@ describe('区域API集成测试', () => {
|
|
|
|
|
|
|
|
// 测试租户2的数据
|
|
// 测试租户2的数据
|
|
|
const response2 = await client.provinces.$get({
|
|
const response2 = await client.provinces.$get({
|
|
|
- query: { tenantId: 2, page: 1, pageSize: 50 }
|
|
|
|
|
|
|
+ query: { page: 1, pageSize: 50 }
|
|
|
|
|
+ }, {
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Authorization': `Bearer ${tenant2Token}`
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
IntegrationTestAssertions.expectStatus(response2, 200);
|
|
IntegrationTestAssertions.expectStatus(response2, 200);
|
|
@@ -447,6 +501,10 @@ describe('区域API集成测试', () => {
|
|
|
// 租户1查询省份
|
|
// 租户1查询省份
|
|
|
const response1 = await client.provinces.$get({
|
|
const response1 = await client.provinces.$get({
|
|
|
query: TestQueryFactory.createProvincesQuery()
|
|
query: TestQueryFactory.createProvincesQuery()
|
|
|
|
|
+ }, {
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
IntegrationTestAssertions.expectStatus(response1, 200);
|
|
IntegrationTestAssertions.expectStatus(response1, 200);
|
|
@@ -454,7 +512,11 @@ describe('区域API集成测试', () => {
|
|
|
|
|
|
|
|
// 租户2查询省份
|
|
// 租户2查询省份
|
|
|
const response2 = await client.provinces.$get({
|
|
const response2 = await client.provinces.$get({
|
|
|
- query: { tenantId: 2, page: 1, pageSize: 50 }
|
|
|
|
|
|
|
+ query: { page: 1, pageSize: 50 }
|
|
|
|
|
+ }, {
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Authorization': `Bearer ${tenant2Token}`
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
IntegrationTestAssertions.expectStatus(response2, 200);
|
|
IntegrationTestAssertions.expectStatus(response2, 200);
|
|
@@ -478,19 +540,34 @@ describe('区域API集成测试', () => {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('应该验证tenantId参数', async () => {
|
|
|
|
|
- // 测试缺少tenantId参数 - 明确排除tenantId
|
|
|
|
|
|
|
+ it('应该验证认证令牌', async () => {
|
|
|
|
|
+ // 测试缺少认证令牌
|
|
|
const response = await client.provinces.$get({
|
|
const response = await client.provinces.$get({
|
|
|
- query: { page: 1, pageSize: 50 } as any // 不包含tenantId,使用any绕过类型检查
|
|
|
|
|
|
|
+ query: { page: 1, pageSize: 50 }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- // 应该返回400错误,因为缺少必需的tenantId参数
|
|
|
|
|
- IntegrationTestAssertions.expectStatus(response, 400);
|
|
|
|
|
|
|
+ // 应该返回401错误,因为缺少认证
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 401);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('应该处理不存在的租户ID', async () => {
|
|
it('应该处理不存在的租户ID', async () => {
|
|
|
|
|
+ // 创建不存在的租户用户
|
|
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
|
|
+ const nonExistentTenantUser = await TestDataFactory.createTestUser(dataSource, {
|
|
|
|
|
+ username: 'testuser_nonexistent',
|
|
|
|
|
+ password: 'TestPassword123!',
|
|
|
|
|
+ email: 'testuser_nonexistent@example.com',
|
|
|
|
|
+ tenantId: 999 // 不存在的租户ID
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const nonExistentTenantToken = authService.generateToken(nonExistentTenantUser);
|
|
|
|
|
+
|
|
|
const response = await client.provinces.$get({
|
|
const response = await client.provinces.$get({
|
|
|
- query: { tenantId: 999, page: 1, pageSize: 50 }
|
|
|
|
|
|
|
+ query: { page: 1, pageSize: 50 }
|
|
|
|
|
+ }, {
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Authorization': `Bearer ${nonExistentTenantToken}`
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
IntegrationTestAssertions.expectStatus(response, 200);
|
|
IntegrationTestAssertions.expectStatus(response, 200);
|