|
@@ -5,7 +5,6 @@ import {
|
|
|
setupIntegrationDatabaseHooks,
|
|
setupIntegrationDatabaseHooks,
|
|
|
TestDataFactory
|
|
TestDataFactory
|
|
|
} from '../utils/integration-test-db';
|
|
} from '../utils/integration-test-db';
|
|
|
-import { IntegrationTestAssertions } from '../utils/integration-test-utils';
|
|
|
|
|
import { fileApiRoutes } from '../../src/api';
|
|
import { fileApiRoutes } from '../../src/api';
|
|
|
import { AuthService } from '@d8d/auth-module';
|
|
import { AuthService } from '@d8d/auth-module';
|
|
|
import { UserService } from '@d8d/user-module';
|
|
import { UserService } from '@d8d/user-module';
|
|
@@ -19,14 +18,7 @@ vi.mock('@d8d/file-module', async (importOriginal) => {
|
|
|
MinioService: vi.fn(() => ({
|
|
MinioService: vi.fn(() => ({
|
|
|
bucketName: 'd8dai',
|
|
bucketName: 'd8dai',
|
|
|
ensureBucketExists: vi.fn().mockResolvedValue(true),
|
|
ensureBucketExists: vi.fn().mockResolvedValue(true),
|
|
|
- objectExists: vi.fn().mockImplementation((bucket, key) => {
|
|
|
|
|
- // 对于删除操作,假设文件存在
|
|
|
|
|
- if (key.includes('testfile_delete') || key.includes('testfile_url') || key.includes('testfile_download')) {
|
|
|
|
|
- return Promise.resolve(true);
|
|
|
|
|
- }
|
|
|
|
|
- // 其他情况假设文件不存在
|
|
|
|
|
- return Promise.resolve(false);
|
|
|
|
|
- }),
|
|
|
|
|
|
|
+ objectExists: vi.fn().mockResolvedValue(false),
|
|
|
deleteObject: vi.fn().mockResolvedValue(true),
|
|
deleteObject: vi.fn().mockResolvedValue(true),
|
|
|
generateUploadPolicy: vi.fn().mockResolvedValue({
|
|
generateUploadPolicy: vi.fn().mockResolvedValue({
|
|
|
'x-amz-algorithm': 'AWS4-HMAC-SHA256',
|
|
'x-amz-algorithm': 'AWS4-HMAC-SHA256',
|
|
@@ -54,7 +46,7 @@ vi.mock('@d8d/file-module', async (importOriginal) => {
|
|
|
// 设置集成测试钩子
|
|
// 设置集成测试钩子
|
|
|
setupIntegrationDatabaseHooks()
|
|
setupIntegrationDatabaseHooks()
|
|
|
|
|
|
|
|
-describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
|
|
|
|
+describe('文件API连通性测试', () => {
|
|
|
let client: ReturnType<typeof testClient<typeof fileApiRoutes>>['api']['v1'];
|
|
let client: ReturnType<typeof testClient<typeof fileApiRoutes>>['api']['v1'];
|
|
|
let testToken: string;
|
|
let testToken: string;
|
|
|
|
|
|
|
@@ -75,8 +67,8 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
testToken = authService.generateToken(user);
|
|
testToken = authService.generateToken(user);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- describe('文件上传策略测试', () => {
|
|
|
|
|
- it('应该成功生成文件上传策略', async () => {
|
|
|
|
|
|
|
+ describe('文件上传策略API连通性', () => {
|
|
|
|
|
+ it('应该成功响应文件上传策略请求', async () => {
|
|
|
const fileData = {
|
|
const fileData = {
|
|
|
name: 'test.txt',
|
|
name: 'test.txt',
|
|
|
type: 'text/plain',
|
|
type: 'text/plain',
|
|
@@ -94,41 +86,10 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- // 断言响应
|
|
|
|
|
- if (response.status !== 200) {
|
|
|
|
|
- const errorData = await response.json();
|
|
|
|
|
- console.debug('File upload policy error:', JSON.stringify(errorData, null, 2));
|
|
|
|
|
- }
|
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.status).toBe(200);
|
|
|
- if (response.status === 200) {
|
|
|
|
|
- const responseData = await response.json();
|
|
|
|
|
- expect(responseData).toHaveProperty('file');
|
|
|
|
|
- expect(responseData).toHaveProperty('uploadPolicy');
|
|
|
|
|
- expect(responseData.file.name).toBe(fileData.name);
|
|
|
|
|
- expect(responseData.file.type).toBe(fileData.type);
|
|
|
|
|
- expect(responseData.file.size).toBe(fileData.size);
|
|
|
|
|
- }
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('应该拒绝无效请求数据的文件上传策略', async () => {
|
|
|
|
|
- const invalidData = {
|
|
|
|
|
- name: '', // 空文件名
|
|
|
|
|
- type: 'text/plain'
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const response = await client.files['upload-policy'].$post({
|
|
|
|
|
- json: invalidData as any
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- headers: {
|
|
|
|
|
- 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- expect(response.status).toBe(400);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('应该拒绝无认证令牌的文件上传策略请求', async () => {
|
|
|
|
|
|
|
+ it('应该拒绝无认证令牌的请求', async () => {
|
|
|
const fileData = {
|
|
const fileData = {
|
|
|
name: 'test.txt',
|
|
name: 'test.txt',
|
|
|
type: 'text/plain',
|
|
type: 'text/plain',
|
|
@@ -144,12 +105,11 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- describe('文件URL生成测试', () => {
|
|
|
|
|
- it('应该成功生成文件访问URL', async () => {
|
|
|
|
|
|
|
+ describe('文件URL生成API连通性', () => {
|
|
|
|
|
+ it('应该成功响应文件URL生成请求', async () => {
|
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
|
|
- // 创建测试文件
|
|
|
|
|
const testFile = await TestDataFactory.createTestFile(dataSource, {
|
|
const testFile = await TestDataFactory.createTestFile(dataSource, {
|
|
|
name: 'testfile_url.txt',
|
|
name: 'testfile_url.txt',
|
|
|
type: 'text/plain',
|
|
type: 'text/plain',
|
|
@@ -167,11 +127,6 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.status).toBe(200);
|
|
|
- if (response.status === 200) {
|
|
|
|
|
- const responseData = await response.json();
|
|
|
|
|
- expect(responseData).toHaveProperty('url');
|
|
|
|
|
- expect(typeof responseData.url).toBe('string');
|
|
|
|
|
- }
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('应该返回404当文件不存在时', async () => {
|
|
it('应该返回404当文件不存在时', async () => {
|
|
@@ -188,12 +143,11 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- describe('文件下载URL生成测试', () => {
|
|
|
|
|
- it('应该成功生成文件下载URL', async () => {
|
|
|
|
|
|
|
+ describe('文件下载URL生成API连通性', () => {
|
|
|
|
|
+ it('应该成功响应文件下载URL生成请求', async () => {
|
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
|
|
- // 创建测试文件
|
|
|
|
|
const testFile = await TestDataFactory.createTestFile(dataSource, {
|
|
const testFile = await TestDataFactory.createTestFile(dataSource, {
|
|
|
name: 'testfile_download.txt',
|
|
name: 'testfile_download.txt',
|
|
|
type: 'text/plain',
|
|
type: 'text/plain',
|
|
@@ -211,12 +165,6 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.status).toBe(200);
|
|
|
- if (response.status === 200) {
|
|
|
|
|
- const responseData = await response.json();
|
|
|
|
|
- expect(responseData).toHaveProperty('url');
|
|
|
|
|
- expect(responseData).toHaveProperty('filename');
|
|
|
|
|
- expect(responseData.filename).toBe('testfile_download.txt');
|
|
|
|
|
- }
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('应该返回404当文件不存在时', async () => {
|
|
it('应该返回404当文件不存在时', async () => {
|
|
@@ -233,80 +181,17 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- describe('文件删除测试', () => {
|
|
|
|
|
- it.skip('应该成功删除文件', async () => {
|
|
|
|
|
|
|
+ describe('文件CRUD API连通性', () => {
|
|
|
|
|
+ it('应该成功响应文件列表请求', async () => {
|
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
|
|
- // 创建测试文件
|
|
|
|
|
- const testFile = await TestDataFactory.createTestFile(dataSource, {
|
|
|
|
|
- name: 'testfile_delete.txt',
|
|
|
|
|
- type: 'text/plain',
|
|
|
|
|
- size: 1024,
|
|
|
|
|
- path: 'testfile_delete.txt'
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- console.debug('Created test file for deletion:', {
|
|
|
|
|
- id: testFile.id,
|
|
|
|
|
- name: testFile.name,
|
|
|
|
|
- path: testFile.path
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- const response = await client.files[':id'].$delete({
|
|
|
|
|
- param: { id: testFile.id }
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- headers: {
|
|
|
|
|
- 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- if (response.status !== 200) {
|
|
|
|
|
- const errorData = await response.json();
|
|
|
|
|
- console.debug('File deletion error:', JSON.stringify(errorData, null, 2));
|
|
|
|
|
- }
|
|
|
|
|
- IntegrationTestAssertions.expectStatus(response, 200);
|
|
|
|
|
-
|
|
|
|
|
- // 验证文件已从数据库中删除
|
|
|
|
|
- const fileRepository = dataSource.getRepository('File');
|
|
|
|
|
- const deletedFile = await fileRepository.findOne({
|
|
|
|
|
- where: { id: testFile.id }
|
|
|
|
|
- });
|
|
|
|
|
- expect(deletedFile).toBeNull();
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('应该返回404当删除不存在的文件时', async () => {
|
|
|
|
|
- const response = await client.files[':id'].$delete({
|
|
|
|
|
- param: { id: 999999 }
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- headers: {
|
|
|
|
|
- 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- IntegrationTestAssertions.expectStatus(response, 404);
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- describe('文件CRUD操作测试', () => {
|
|
|
|
|
- it('应该成功获取文件列表', async () => {
|
|
|
|
|
- const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
|
|
- if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
-
|
|
|
|
|
- // 创建几个测试文件
|
|
|
|
|
await TestDataFactory.createTestFile(dataSource, {
|
|
await TestDataFactory.createTestFile(dataSource, {
|
|
|
name: 'file1.txt',
|
|
name: 'file1.txt',
|
|
|
type: 'text/plain',
|
|
type: 'text/plain',
|
|
|
size: 1024,
|
|
size: 1024,
|
|
|
path: 'file1.txt'
|
|
path: 'file1.txt'
|
|
|
});
|
|
});
|
|
|
- await TestDataFactory.createTestFile(dataSource, {
|
|
|
|
|
- name: 'file2.txt',
|
|
|
|
|
- type: 'text/plain',
|
|
|
|
|
- size: 2048,
|
|
|
|
|
- path: 'file2.txt'
|
|
|
|
|
- });
|
|
|
|
|
|
|
|
|
|
const response = await client.files.$get({
|
|
const response = await client.files.$get({
|
|
|
query: {}
|
|
query: {}
|
|
@@ -318,14 +203,9 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.status).toBe(200);
|
|
|
- if (response.status === 200) {
|
|
|
|
|
- const responseData = await response.json();
|
|
|
|
|
- expect(Array.isArray(responseData.data)).toBe(true);
|
|
|
|
|
- expect(responseData.data.length).toBeGreaterThanOrEqual(2);
|
|
|
|
|
- }
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('应该成功获取单个文件详情', async () => {
|
|
|
|
|
|
|
+ it('应该成功响应单个文件详情请求', async () => {
|
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
|
@@ -346,63 +226,11 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.status).toBe(200);
|
|
|
- if (response.status === 200) {
|
|
|
|
|
- const responseData = await response.json();
|
|
|
|
|
- expect(responseData.id).toBe(testFile.id);
|
|
|
|
|
- expect(responseData.name).toBe(testFile.name);
|
|
|
|
|
- expect(responseData.type).toBe(testFile.type);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('应该能够按文件名搜索文件', async () => {
|
|
|
|
|
- const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
|
|
- if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
-
|
|
|
|
|
- await TestDataFactory.createTestFile(dataSource, {
|
|
|
|
|
- name: 'search_file_1.txt',
|
|
|
|
|
- type: 'text/plain',
|
|
|
|
|
- size: 1024,
|
|
|
|
|
- path: 'search_file_1.txt'
|
|
|
|
|
- });
|
|
|
|
|
- await TestDataFactory.createTestFile(dataSource, {
|
|
|
|
|
- name: 'search_file_2.txt',
|
|
|
|
|
- type: 'text/plain',
|
|
|
|
|
- size: 2048,
|
|
|
|
|
- path: 'search_file_2.txt'
|
|
|
|
|
- });
|
|
|
|
|
- await TestDataFactory.createTestFile(dataSource, {
|
|
|
|
|
- name: 'other_file.txt',
|
|
|
|
|
- type: 'text/plain',
|
|
|
|
|
- size: 1024,
|
|
|
|
|
- path: 'other_file.txt'
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- const response = await client.files.$get({
|
|
|
|
|
- query: { keyword: 'search_file' }
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- headers: {
|
|
|
|
|
- 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- IntegrationTestAssertions.expectStatus(response, 200);
|
|
|
|
|
- if (response.status === 200) {
|
|
|
|
|
- const responseData = await response.json();
|
|
|
|
|
- expect(Array.isArray(responseData.data)).toBe(true);
|
|
|
|
|
- expect(responseData.data.length).toBe(2);
|
|
|
|
|
-
|
|
|
|
|
- // 验证搜索结果包含正确的文件
|
|
|
|
|
- const filenames = responseData.data.map((file: any) => file.name);
|
|
|
|
|
- expect(filenames).toContain('search_file_1.txt');
|
|
|
|
|
- expect(filenames).toContain('search_file_2.txt');
|
|
|
|
|
- expect(filenames).not.toContain('other_file.txt');
|
|
|
|
|
- }
|
|
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- describe('多部分上传测试', () => {
|
|
|
|
|
- it('应该成功生成多部分上传策略', async () => {
|
|
|
|
|
|
|
+ describe('多部分上传API连通性', () => {
|
|
|
|
|
+ it('应该成功响应多部分上传策略请求', async () => {
|
|
|
const multipartData = {
|
|
const multipartData = {
|
|
|
fileKey: 'large-file.zip',
|
|
fileKey: 'large-file.zip',
|
|
|
totalSize: 1024 * 1024 * 100, // 100MB
|
|
totalSize: 1024 * 1024 * 100, // 100MB
|
|
@@ -421,99 +249,6 @@ describe('文件API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.status).toBe(200);
|
|
|
- if (response.status === 200) {
|
|
|
|
|
- const responseData = await response.json();
|
|
|
|
|
- expect(responseData).toHaveProperty('uploadId');
|
|
|
|
|
- expect(responseData).toHaveProperty('bucket');
|
|
|
|
|
- expect(responseData).toHaveProperty('key');
|
|
|
|
|
- expect(responseData).toHaveProperty('partUrls');
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('应该拒绝无效的多部分上传请求数据', async () => {
|
|
|
|
|
- const invalidData = {
|
|
|
|
|
- name: 'test.zip'
|
|
|
|
|
- // 缺少必需字段: fileKey, totalSize, partSize
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const response = await client.files['multipart-policy'].$post({
|
|
|
|
|
- json: invalidData as any
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- headers: {
|
|
|
|
|
- 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- expect(response.status).toBe(400);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it.skip('应该成功完成多部分上传', async () => {
|
|
|
|
|
- const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
|
|
- if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
-
|
|
|
|
|
- // 先创建一个文件记录 - 确保path与key完全匹配
|
|
|
|
|
- const testFile = await TestDataFactory.createTestFile(dataSource, {
|
|
|
|
|
- name: 'test-multipart-file.zip',
|
|
|
|
|
- type: 'application/zip',
|
|
|
|
|
- size: 104857600,
|
|
|
|
|
- path: '1/test-file.zip'
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- console.debug('Created test file for multipart completion:', {
|
|
|
|
|
- id: testFile.id,
|
|
|
|
|
- name: testFile.name,
|
|
|
|
|
- path: testFile.path
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- const completeData = {
|
|
|
|
|
- uploadId: 'upload-123',
|
|
|
|
|
- bucket: 'd8dai',
|
|
|
|
|
- key: '1/test-file.zip',
|
|
|
|
|
- parts: [
|
|
|
|
|
- { partNumber: 1, etag: 'etag1' },
|
|
|
|
|
- { partNumber: 2, etag: 'etag2' }
|
|
|
|
|
- ]
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const response = await client.files['multipart-complete'].$post({
|
|
|
|
|
- json: completeData
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- headers: {
|
|
|
|
|
- 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- if (response.status !== 200) {
|
|
|
|
|
- const errorData = await response.json();
|
|
|
|
|
- console.debug('Multipart completion error:', JSON.stringify(errorData, null, 2));
|
|
|
|
|
- }
|
|
|
|
|
- expect(response.status).toBe(200);
|
|
|
|
|
- if (response.status === 200) {
|
|
|
|
|
- const responseData = await response.json();
|
|
|
|
|
- expect(responseData).toHaveProperty('fileId');
|
|
|
|
|
- expect(responseData).toHaveProperty('url');
|
|
|
|
|
- expect(responseData).toHaveProperty('key');
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('应该拒绝无效的完成多部分上传请求数据', async () => {
|
|
|
|
|
- const invalidData = {
|
|
|
|
|
- uploadId: 'upload-123'
|
|
|
|
|
- // 缺少必需字段: bucket, key, parts
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const response = await client.files['multipart-complete'].$post({
|
|
|
|
|
- json: invalidData as any
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- headers: {
|
|
|
|
|
- 'Authorization': `Bearer ${testToken}`
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- expect(response.status).toBe(400);
|
|
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|