| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- import { describe, it, expect, beforeEach } from 'vitest';
- import { testClient } from 'hono/testing';
- import {
- IntegrationTestDatabase,
- setupIntegrationDatabaseHooksWithEntities
- } from '@d8d/shared-test-util';
- import {
- IntegrationTestAssertions
- } from '../utils/integration-test-utils';
- import fileRoutes from '../../src/routes';
- import { File } from '../../src/entities';
- import { UserEntity } from '@d8d/user-module';
- import { TestDataFactory } from '../utils/integration-test-db';
- import { AuthService } from '@d8d/auth-module';
- import { UserService } from '@d8d/user-module';
- // 设置集成测试钩子
- setupIntegrationDatabaseHooksWithEntities([File, UserEntity])
- describe('文件路由API集成测试 (使用hono/testing)', () => {
- let client: ReturnType<typeof testClient<typeof fileRoutes>>;
- let authService: AuthService;
- let userService: UserService;
- let testToken: string;
- let testUser: any;
- beforeEach(async () => {
- // 创建测试客户端
- client = testClient(fileRoutes);
- // 获取数据源
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- // 初始化服务
- userService = new UserService(dataSource);
- authService = new AuthService(userService);
- // 创建测试用户并生成token
- testUser = await TestDataFactory.createTestUser(dataSource, {
- username: 'testuser_file',
- password: 'TestPassword123!',
- email: 'testuser_file@example.com'
- });
- // 生成测试用户的token
- testToken = authService.generateToken(testUser);
- });
- describe('文件创建路由测试', () => {
- it('应该拒绝无认证令牌的文件创建请求', async () => {
- const fileData = {
- name: 'test.txt',
- type: 'text/plain',
- size: 1024,
- path: '/uploads/test.txt',
- description: 'Test file'
- };
- const response = await client['upload-policy'].$post({
- json: fileData
- });
- // 应该返回401状态码,因为缺少认证
- expect(response.status).toBe(401);
- if (response.status === 401) {
- const responseData = await response.json();
- expect(responseData.message).toContain('Authorization header missing');
- }
- });
- it('应该拒绝无效认证令牌的文件创建请求', async () => {
- const fileData = {
- name: 'test.txt',
- type: 'text/plain',
- size: 1024,
- path: '/uploads/test.txt',
- description: 'Test file'
- };
- const response = await client['upload-policy'].$post({
- json: fileData
- }, {
- headers: {
- 'Authorization': 'Bearer invalid.token.here'
- }
- });
- // 应该返回401状态码,因为令牌无效
- expect(response.status).toBe(401);
- if (response.status === 401) {
- const responseData = await response.json();
- expect(responseData.message).toContain('Invalid token');
- }
- });
- it('应该成功创建文件上传策略(使用有效认证令牌)', async () => {
- const fileData = {
- name: 'test.txt',
- type: 'text/plain',
- size: 1024,
- path: '/uploads/test.txt',
- description: 'Test file'
- };
- const response = await client['upload-policy'].$post({
- json: fileData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- // 断言响应
- 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);
- expect(responseData.file.uploadUserId).toBe(testUser.id);
- // 断言数据库中存在文件记录
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- const fileRepository = dataSource.getRepository(File);
- const savedFile = await fileRepository.findOne({
- where: { name: fileData.name }
- });
- expect(savedFile).toBeTruthy();
- expect(savedFile?.uploadUserId).toBe(testUser.id);
- }
- });
- it('应该拒绝创建无效文件数据的请求', async () => {
- const invalidFileData = {
- name: '', // 空文件名
- type: 'text/plain',
- path: 'test/path.txt'
- };
- const response = await client['upload-policy'].$post({
- json: invalidFileData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- // 应该返回验证错误
- expect([400, 500]).toContain(response.status);
- });
- });
- describe('文件读取路由测试', () => {
- it('应该成功获取文件列表', async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- // 创建几个测试文件
- await TestDataFactory.createTestFile(dataSource, {
- name: 'file1.txt',
- uploadUserId: testUser.id
- });
- await TestDataFactory.createTestFile(dataSource, {
- name: 'file2.txt',
- uploadUserId: testUser.id
- });
- const response = await client.index.$get({
- query: {}
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- 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 () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- const testFile = await TestDataFactory.createTestFile(dataSource, {
- name: 'testfile_detail',
- uploadUserId: testUser.id
- });
- const response = await client[':id'].$get({
- param: { id: testFile.id }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- 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('应该返回404当文件不存在时', async () => {
- const response = await client[':id'].$get({
- param: { id: 999999 }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(404);
- if (response.status === 404) {
- const responseData = await response.json();
- expect(responseData.message).toContain('资源不存在');
- }
- });
- });
- describe('文件URL生成路由测试', () => {
- it('应该成功生成文件访问URL', async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- const testFile = await TestDataFactory.createTestFile(dataSource, {
- name: 'testfile_url',
- uploadUserId: testUser.id
- });
- const response = await client[':id']['url'].$get({
- param: { id: testFile.id }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const responseData = await response.json();
- expect(responseData).toHaveProperty('url');
- expect(typeof responseData.url).toBe('string');
- expect(responseData.url.length).toBeGreaterThan(0);
- }
- });
- it('应该返回404当为不存在的文件生成URL时', async () => {
- const response = await client[':id']['url'].$get({
- param: { id: 999999 }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(404);
- if (response.status === 404) {
- const responseData = await response.json();
- expect(responseData.message).toContain('文件不存在');
- }
- });
- });
- describe('文件下载路由测试', () => {
- it('应该成功生成文件下载URL', async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- const testFile = await TestDataFactory.createTestFile(dataSource, {
- name: 'testfile_download.txt',
- uploadUserId: testUser.id
- });
- const response = await client[':id']['download'].$get({
- param: { id: testFile.id }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const responseData = await response.json();
- expect(responseData).toHaveProperty('url');
- expect(responseData).toHaveProperty('filename');
- expect(typeof responseData.url).toBe('string');
- expect(responseData.filename).toBe(testFile.name);
- }
- });
- it('应该返回404当为不存在的文件生成下载URL时', async () => {
- const response = await client[':id']['download'].$get({
- param: { id: 999999 }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(404);
- if (response.status === 404) {
- const responseData = await response.json();
- expect(responseData.message).toContain('文件不存在');
- }
- });
- });
- describe('文件删除路由测试', () => {
- it('应该拒绝无认证令牌的文件删除请求', async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- const testFile = await TestDataFactory.createTestFile(dataSource, {
- name: 'testfile_delete_no_auth',
- uploadUserId: testUser.id
- });
- const response = await client[':id'].$delete({
- param: { id: testFile.id }
- });
- // 应该返回401状态码,因为缺少认证
- expect(response.status).toBe(401);
- if (response.status === 401) {
- const responseData = await response.json();
- expect(responseData.message).toContain('Authorization header missing');
- }
- });
- it('应该成功删除文件(使用有效认证令牌)', async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- const testFile = await TestDataFactory.createTestFile(dataSource, {
- name: 'testfile_delete',
- uploadUserId: testUser.id
- });
- const response = await client[':id'].$delete({
- param: { id: testFile.id }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- IntegrationTestAssertions.expectStatus(response, 200);
- // 验证文件已从数据库中删除
- const fileRepository = dataSource.getRepository(File);
- const deletedFile = await fileRepository.findOne({
- where: { id: testFile.id }
- });
- expect(deletedFile).toBeNull();
- // 验证再次获取文件返回404
- const getResponse = await client[':id'].$get({
- param: { id: testFile.id }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- IntegrationTestAssertions.expectStatus(getResponse, 404);
- });
- it('应该返回404当删除不存在的文件时', async () => {
- const response = await client[':id'].$delete({
- param: { id: 999999 }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- IntegrationTestAssertions.expectStatus(response, 404);
- if (response.status === 404) {
- const responseData = await response.json();
- expect(responseData.message).toContain('文件不存在');
- }
- });
- });
- describe('文件搜索路由测试', () => {
- it('应该能够按文件名搜索文件', async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- await TestDataFactory.createTestFile(dataSource, {
- name: 'search_file_1.txt',
- uploadUserId: testUser.id
- });
- await TestDataFactory.createTestFile(dataSource, {
- name: 'search_file_2.txt',
- uploadUserId: testUser.id
- });
- await TestDataFactory.createTestFile(dataSource, {
- name: 'other_file.txt',
- uploadUserId: testUser.id
- });
- const response = await client.index.$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');
- }
- });
- it('应该能够按文件类型搜索文件', async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- await TestDataFactory.createTestFile(dataSource, {
- name: 'image1.jpg',
- type: 'image/jpeg',
- uploadUserId: testUser.id
- });
- await TestDataFactory.createTestFile(dataSource, {
- name: 'image2.png',
- type: 'image/png',
- uploadUserId: testUser.id
- });
- const response = await client.index.$get({
- query: { keyword: 'image' }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- IntegrationTestAssertions.expectStatus(response, 200);
- if (response.status === 200) {
- const responseData = await response.json();
- expect(responseData.data.length).toBe(2);
- const types = responseData.data.map((file: any) => file.type);
- expect(types).toContain('image/jpeg');
- expect(types).toContain('image/png');
- }
- });
- });
- describe('性能测试', () => {
- it('文件列表查询响应时间应小于200ms', async () => {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) throw new Error('Database not initialized');
- // 创建一些测试数据
- for (let i = 0; i < 10; i++) {
- await TestDataFactory.createTestFile(dataSource, {
- name: `perf_file_${i}.txt`,
- uploadUserId: testUser.id
- });
- }
- const startTime = Date.now();
- const response = await client.index.$get({
- query: {}
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- const endTime = Date.now();
- const responseTime = endTime - startTime;
- IntegrationTestAssertions.expectStatus(response, 200);
- expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
- });
- });
- describe('认证令牌测试', () => {
- it('应该拒绝过期令牌的文件请求', async () => {
- // 创建立即过期的令牌
- const expiredToken = authService.generateToken(testUser, '1ms');
- // 等待令牌过期
- await new Promise(resolve => setTimeout(resolve, 10));
- const response = await client['upload-policy'].$post({
- json: {
- name: 'test_expired_token.txt',
- type: 'text/plain',
- size: 1024,
- path: 'test/expired_token.txt'
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${expiredToken}`
- }
- });
- // 应该返回401状态码,因为令牌过期
- expect(response.status).toBe(401);
- if (response.status === 401) {
- const responseData = await response.json();
- expect(responseData.message).toContain('Invalid token');
- }
- });
- it('应该拒绝格式错误的认证头', async () => {
- const response = await client['upload-policy'].$post({
- json: {
- name: 'test_bad_auth_header.txt',
- type: 'text/plain',
- size: 1024,
- path: 'test/bad_auth_header.txt'
- }
- }, {
- headers: {
- 'Authorization': 'Basic invalid_format'
- }
- });
- // 应该返回401状态码,因为认证头格式错误
- expect(response.status).toBe(401);
- if (response.status === 401) {
- const responseData = await response.json();
- expect(responseData.message).toContain('Authorization header missing');
- }
- });
- });
- });
|