2
0

users.integration.test.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  2. import userRoutes from '../index';
  3. import {
  4. setupIntegrationTestHooks,
  5. TestDataFactory,
  6. IntegrationTestAssertions,
  7. setupIntegrationTestEnvironment
  8. } from '../../../__test_utils__/integration-test-utils';
  9. // 设置集成测试钩子
  10. setupIntegrationTestHooks();
  11. describe('用户API集成测试', () => {
  12. let testContext: any;
  13. beforeEach(async () => {
  14. // 设置测试环境
  15. testContext = await setupIntegrationTestEnvironment([userRoutes], {
  16. setupDatabase: true,
  17. setupAuth: true
  18. });
  19. });
  20. afterEach(async () => {
  21. // 清理测试数据
  22. await TestDataFactory.clearAllTestData();
  23. });
  24. describe('用户创建测试', () => {
  25. it('应该成功创建用户', async () => {
  26. const userData = {
  27. username: 'testuser_create',
  28. email: 'testcreate@example.com',
  29. password: 'TestPassword123!',
  30. firstName: 'Test',
  31. lastName: 'User',
  32. phone: '13800138000'
  33. };
  34. const response = await testContext.client.post('/users', userData);
  35. // 断言响应
  36. IntegrationTestAssertions.expectStatus(response, 201);
  37. expect(response.data).toHaveProperty('id');
  38. expect(response.data.username).toBe(userData.username);
  39. expect(response.data.email).toBe(userData.email);
  40. expect(response.data.firstName).toBe(userData.firstName);
  41. expect(response.data.lastName).toBe(userData.lastName);
  42. // 断言数据库中存在用户
  43. await IntegrationTestAssertions.expectUserToExist(userData.username);
  44. });
  45. it('应该拒绝创建重复用户名的用户', async () => {
  46. // 先创建一个用户
  47. const existingUser = await TestDataFactory.createTestUser({
  48. username: 'duplicate_user'
  49. });
  50. // 尝试创建相同用户名的用户
  51. const userData = {
  52. username: 'duplicate_user',
  53. email: 'different@example.com',
  54. password: 'TestPassword123!',
  55. firstName: 'Test',
  56. lastName: 'User'
  57. };
  58. const response = await testContext.client.post('/users', userData);
  59. // 应该返回错误
  60. IntegrationTestAssertions.expectStatus(response, 500);
  61. expect(response.data.message).toContain('用户已存在');
  62. });
  63. it('应该拒绝创建无效邮箱的用户', async () => {
  64. const userData = {
  65. username: 'testuser_invalid_email',
  66. email: 'invalid-email',
  67. password: 'TestPassword123!',
  68. firstName: 'Test',
  69. lastName: 'User'
  70. };
  71. const response = await testContext.client.post('/users', userData);
  72. // 应该返回验证错误
  73. IntegrationTestAssertions.expectStatus(response, 400);
  74. expect(response.data.code).toBe(400);
  75. });
  76. });
  77. describe('用户读取测试', () => {
  78. it('应该成功获取用户列表', async () => {
  79. // 创建几个测试用户
  80. await TestDataFactory.createTestUser({ username: 'user1' });
  81. await TestDataFactory.createTestUser({ username: 'user2' });
  82. const response = await testContext.client.get('/users');
  83. IntegrationTestAssertions.expectStatus(response, 200);
  84. expect(Array.isArray(response.data)).toBe(true);
  85. expect(response.data.length).toBeGreaterThanOrEqual(2);
  86. });
  87. it('应该成功获取单个用户详情', async () => {
  88. const testUser = await TestDataFactory.createTestUser({
  89. username: 'testuser_detail'
  90. });
  91. const response = await testContext.client.get(`/users/${testUser.id}`);
  92. IntegrationTestAssertions.expectStatus(response, 200);
  93. expect(response.data.id).toBe(testUser.id);
  94. expect(response.data.username).toBe(testUser.username);
  95. expect(response.data.email).toBe(testUser.email);
  96. });
  97. it('应该返回404当用户不存在时', async () => {
  98. const response = await testContext.client.get('/users/999999');
  99. IntegrationTestAssertions.expectStatus(response, 404);
  100. expect(response.data.message).toContain('用户不存在');
  101. });
  102. });
  103. describe('用户更新测试', () => {
  104. it('应该成功更新用户信息', async () => {
  105. const testUser = await TestDataFactory.createTestUser({
  106. username: 'testuser_update'
  107. });
  108. const updateData = {
  109. firstName: 'Updated',
  110. lastName: 'Name',
  111. email: 'updated@example.com'
  112. };
  113. const response = await testContext.client.put(`/users/${testUser.id}`, updateData);
  114. IntegrationTestAssertions.expectStatus(response, 200);
  115. expect(response.data.firstName).toBe(updateData.firstName);
  116. expect(response.data.lastName).toBe(updateData.lastName);
  117. expect(response.data.email).toBe(updateData.email);
  118. // 验证数据库中的更新
  119. const getResponse = await testContext.client.get(`/users/${testUser.id}`);
  120. expect(getResponse.data.firstName).toBe(updateData.firstName);
  121. expect(getResponse.data.lastName).toBe(updateData.lastName);
  122. });
  123. it('应该返回404当更新不存在的用户时', async () => {
  124. const updateData = {
  125. firstName: 'Updated',
  126. lastName: 'Name'
  127. };
  128. const response = await testContext.client.put('/users/999999', updateData);
  129. IntegrationTestAssertions.expectStatus(response, 404);
  130. expect(response.data.message).toContain('用户不存在');
  131. });
  132. });
  133. describe('用户删除测试', () => {
  134. it('应该成功删除用户', async () => {
  135. const testUser = await TestDataFactory.createTestUser({
  136. username: 'testuser_delete'
  137. });
  138. const response = await testContext.client.delete(`/users/${testUser.id}`);
  139. IntegrationTestAssertions.expectStatus(response, 204);
  140. // 验证用户已从数据库中删除
  141. await IntegrationTestAssertions.expectUserNotToExist('testuser_delete');
  142. // 验证再次获取用户返回404
  143. const getResponse = await testContext.client.get(`/users/${testUser.id}`);
  144. IntegrationTestAssertions.expectStatus(getResponse, 404);
  145. });
  146. it('应该返回404当删除不存在的用户时', async () => {
  147. const response = await testContext.client.delete('/users/999999');
  148. IntegrationTestAssertions.expectStatus(response, 404);
  149. expect(response.data.message).toContain('用户不存在');
  150. });
  151. });
  152. describe('用户搜索测试', () => {
  153. it('应该能够按用户名搜索用户', async () => {
  154. await TestDataFactory.createTestUser({ username: 'search_user_1', email: 'search1@example.com' });
  155. await TestDataFactory.createTestUser({ username: 'search_user_2', email: 'search2@example.com' });
  156. await TestDataFactory.createTestUser({ username: 'other_user', email: 'other@example.com' });
  157. const response = await testContext.client.get('/users?search=search_user');
  158. IntegrationTestAssertions.expectStatus(response, 200);
  159. expect(Array.isArray(response.data)).toBe(true);
  160. expect(response.data.length).toBe(2);
  161. // 验证搜索结果包含正确的用户
  162. const usernames = response.data.map((user: any) => user.username);
  163. expect(usernames).toContain('search_user_1');
  164. expect(usernames).toContain('search_user_2');
  165. expect(usernames).not.toContain('other_user');
  166. });
  167. it('应该能够按邮箱搜索用户', async () => {
  168. await TestDataFactory.createTestUser({ username: 'user_email_1', email: 'test.email1@example.com' });
  169. await TestDataFactory.createTestUser({ username: 'user_email_2', email: 'test.email2@example.com' });
  170. const response = await testContext.client.get('/users?search=test.email');
  171. IntegrationTestAssertions.expectStatus(response, 200);
  172. expect(response.data.length).toBe(2);
  173. const emails = response.data.map((user: any) => user.email);
  174. expect(emails).toContain('test.email1@example.com');
  175. expect(emails).toContain('test.email2@example.com');
  176. });
  177. });
  178. describe('性能测试', () => {
  179. it('用户列表查询响应时间应小于200ms', async () => {
  180. // 创建一些测试数据
  181. for (let i = 0; i < 10; i++) {
  182. await TestDataFactory.createTestUser({
  183. username: `perf_user_${i}`,
  184. email: `perf${i}@example.com`
  185. });
  186. }
  187. const startTime = Date.now();
  188. const response = await testContext.client.get('/users');
  189. const endTime = Date.now();
  190. const responseTime = endTime - startTime;
  191. IntegrationTestAssertions.expectStatus(response, 200);
  192. expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
  193. });
  194. });
  195. });