|
@@ -0,0 +1,244 @@
|
|
|
|
|
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
|
|
|
+import userRoutes from '../index';
|
|
|
|
|
+import {
|
|
|
|
|
+ setupIntegrationTestHooks,
|
|
|
|
|
+ TestDataFactory,
|
|
|
|
|
+ IntegrationTestAssertions,
|
|
|
|
|
+ setupIntegrationTestEnvironment
|
|
|
|
|
+} from '../../../__test_utils__/integration-test-utils';
|
|
|
|
|
+
|
|
|
|
|
+// 设置集成测试钩子
|
|
|
|
|
+setupIntegrationTestHooks();
|
|
|
|
|
+
|
|
|
|
|
+describe('用户API集成测试', () => {
|
|
|
|
|
+ let testContext: any;
|
|
|
|
|
+
|
|
|
|
|
+ beforeEach(async () => {
|
|
|
|
|
+ // 设置测试环境
|
|
|
|
|
+ testContext = await setupIntegrationTestEnvironment([userRoutes], {
|
|
|
|
|
+ setupDatabase: true,
|
|
|
|
|
+ setupAuth: true
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ afterEach(async () => {
|
|
|
|
|
+ // 清理测试数据
|
|
|
|
|
+ await TestDataFactory.clearAllTestData();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ describe('用户创建测试', () => {
|
|
|
|
|
+ it('应该成功创建用户', async () => {
|
|
|
|
|
+ const userData = {
|
|
|
|
|
+ username: 'testuser_create',
|
|
|
|
|
+ email: 'testcreate@example.com',
|
|
|
|
|
+ password: 'TestPassword123!',
|
|
|
|
|
+ firstName: 'Test',
|
|
|
|
|
+ lastName: 'User',
|
|
|
|
|
+ phone: '13800138000'
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.post('/users', userData);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言响应
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 201);
|
|
|
|
|
+ expect(response.data).toHaveProperty('id');
|
|
|
|
|
+ expect(response.data.username).toBe(userData.username);
|
|
|
|
|
+ expect(response.data.email).toBe(userData.email);
|
|
|
|
|
+ expect(response.data.firstName).toBe(userData.firstName);
|
|
|
|
|
+ expect(response.data.lastName).toBe(userData.lastName);
|
|
|
|
|
+
|
|
|
|
|
+ // 断言数据库中存在用户
|
|
|
|
|
+ await IntegrationTestAssertions.expectUserToExist(userData.username);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('应该拒绝创建重复用户名的用户', async () => {
|
|
|
|
|
+ // 先创建一个用户
|
|
|
|
|
+ const existingUser = await TestDataFactory.createTestUser({
|
|
|
|
|
+ username: 'duplicate_user'
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 尝试创建相同用户名的用户
|
|
|
|
|
+ const userData = {
|
|
|
|
|
+ username: 'duplicate_user',
|
|
|
|
|
+ email: 'different@example.com',
|
|
|
|
|
+ password: 'TestPassword123!',
|
|
|
|
|
+ firstName: 'Test',
|
|
|
|
|
+ lastName: 'User'
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.post('/users', userData);
|
|
|
|
|
+
|
|
|
|
|
+ // 应该返回错误
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 500);
|
|
|
|
|
+ expect(response.data.message).toContain('用户已存在');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('应该拒绝创建无效邮箱的用户', async () => {
|
|
|
|
|
+ const userData = {
|
|
|
|
|
+ username: 'testuser_invalid_email',
|
|
|
|
|
+ email: 'invalid-email',
|
|
|
|
|
+ password: 'TestPassword123!',
|
|
|
|
|
+ firstName: 'Test',
|
|
|
|
|
+ lastName: 'User'
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.post('/users', userData);
|
|
|
|
|
+
|
|
|
|
|
+ // 应该返回验证错误
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 400);
|
|
|
|
|
+ expect(response.data.code).toBe(400);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ describe('用户读取测试', () => {
|
|
|
|
|
+ it('应该成功获取用户列表', async () => {
|
|
|
|
|
+ // 创建几个测试用户
|
|
|
|
|
+ await TestDataFactory.createTestUser({ username: 'user1' });
|
|
|
|
|
+ await TestDataFactory.createTestUser({ username: 'user2' });
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.get('/users');
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 200);
|
|
|
|
|
+ expect(Array.isArray(response.data)).toBe(true);
|
|
|
|
|
+ expect(response.data.length).toBeGreaterThanOrEqual(2);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('应该成功获取单个用户详情', async () => {
|
|
|
|
|
+ const testUser = await TestDataFactory.createTestUser({
|
|
|
|
|
+ username: 'testuser_detail'
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.get(`/users/${testUser.id}`);
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 200);
|
|
|
|
|
+ expect(response.data.id).toBe(testUser.id);
|
|
|
|
|
+ expect(response.data.username).toBe(testUser.username);
|
|
|
|
|
+ expect(response.data.email).toBe(testUser.email);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('应该返回404当用户不存在时', async () => {
|
|
|
|
|
+ const response = await testContext.client.get('/users/999999');
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 404);
|
|
|
|
|
+ expect(response.data.message).toContain('用户不存在');
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ describe('用户更新测试', () => {
|
|
|
|
|
+ it('应该成功更新用户信息', async () => {
|
|
|
|
|
+ const testUser = await TestDataFactory.createTestUser({
|
|
|
|
|
+ username: 'testuser_update'
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const updateData = {
|
|
|
|
|
+ firstName: 'Updated',
|
|
|
|
|
+ lastName: 'Name',
|
|
|
|
|
+ email: 'updated@example.com'
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.put(`/users/${testUser.id}`, updateData);
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 200);
|
|
|
|
|
+ expect(response.data.firstName).toBe(updateData.firstName);
|
|
|
|
|
+ expect(response.data.lastName).toBe(updateData.lastName);
|
|
|
|
|
+ expect(response.data.email).toBe(updateData.email);
|
|
|
|
|
+
|
|
|
|
|
+ // 验证数据库中的更新
|
|
|
|
|
+ const getResponse = await testContext.client.get(`/users/${testUser.id}`);
|
|
|
|
|
+ expect(getResponse.data.firstName).toBe(updateData.firstName);
|
|
|
|
|
+ expect(getResponse.data.lastName).toBe(updateData.lastName);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('应该返回404当更新不存在的用户时', async () => {
|
|
|
|
|
+ const updateData = {
|
|
|
|
|
+ firstName: 'Updated',
|
|
|
|
|
+ lastName: 'Name'
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.put('/users/999999', updateData);
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 404);
|
|
|
|
|
+ expect(response.data.message).toContain('用户不存在');
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ describe('用户删除测试', () => {
|
|
|
|
|
+ it('应该成功删除用户', async () => {
|
|
|
|
|
+ const testUser = await TestDataFactory.createTestUser({
|
|
|
|
|
+ username: 'testuser_delete'
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.delete(`/users/${testUser.id}`);
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 204);
|
|
|
|
|
+
|
|
|
|
|
+ // 验证用户已从数据库中删除
|
|
|
|
|
+ await IntegrationTestAssertions.expectUserNotToExist('testuser_delete');
|
|
|
|
|
+
|
|
|
|
|
+ // 验证再次获取用户返回404
|
|
|
|
|
+ const getResponse = await testContext.client.get(`/users/${testUser.id}`);
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(getResponse, 404);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('应该返回404当删除不存在的用户时', async () => {
|
|
|
|
|
+ const response = await testContext.client.delete('/users/999999');
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 404);
|
|
|
|
|
+ expect(response.data.message).toContain('用户不存在');
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ describe('用户搜索测试', () => {
|
|
|
|
|
+ it('应该能够按用户名搜索用户', async () => {
|
|
|
|
|
+ await TestDataFactory.createTestUser({ username: 'search_user_1', email: 'search1@example.com' });
|
|
|
|
|
+ await TestDataFactory.createTestUser({ username: 'search_user_2', email: 'search2@example.com' });
|
|
|
|
|
+ await TestDataFactory.createTestUser({ username: 'other_user', email: 'other@example.com' });
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.get('/users?search=search_user');
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 200);
|
|
|
|
|
+ expect(Array.isArray(response.data)).toBe(true);
|
|
|
|
|
+ expect(response.data.length).toBe(2);
|
|
|
|
|
+
|
|
|
|
|
+ // 验证搜索结果包含正确的用户
|
|
|
|
|
+ const usernames = response.data.map((user: any) => user.username);
|
|
|
|
|
+ expect(usernames).toContain('search_user_1');
|
|
|
|
|
+ expect(usernames).toContain('search_user_2');
|
|
|
|
|
+ expect(usernames).not.toContain('other_user');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('应该能够按邮箱搜索用户', async () => {
|
|
|
|
|
+ await TestDataFactory.createTestUser({ username: 'user_email_1', email: 'test.email1@example.com' });
|
|
|
|
|
+ await TestDataFactory.createTestUser({ username: 'user_email_2', email: 'test.email2@example.com' });
|
|
|
|
|
+
|
|
|
|
|
+ const response = await testContext.client.get('/users?search=test.email');
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 200);
|
|
|
|
|
+ expect(response.data.length).toBe(2);
|
|
|
|
|
+
|
|
|
|
|
+ const emails = response.data.map((user: any) => user.email);
|
|
|
|
|
+ expect(emails).toContain('test.email1@example.com');
|
|
|
|
|
+ expect(emails).toContain('test.email2@example.com');
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ describe('性能测试', () => {
|
|
|
|
|
+ it('用户列表查询响应时间应小于200ms', async () => {
|
|
|
|
|
+ // 创建一些测试数据
|
|
|
|
|
+ for (let i = 0; i < 10; i++) {
|
|
|
|
|
+ await TestDataFactory.createTestUser({
|
|
|
|
|
+ username: `perf_user_${i}`,
|
|
|
|
|
+ email: `perf${i}@example.com`
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const startTime = Date.now();
|
|
|
|
|
+ const response = await testContext.client.get('/users');
|
|
|
|
|
+ const endTime = Date.now();
|
|
|
|
|
+ const responseTime = endTime - startTime;
|
|
|
|
|
+
|
|
|
|
|
+ IntegrationTestAssertions.expectStatus(response, 200);
|
|
|
|
|
+ expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+});
|