user.routes.integration.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import {
  4. IntegrationTestDatabase,
  5. setupIntegrationDatabaseHooks,
  6. TestDataFactory
  7. } from '../utils/integration-test-db';
  8. import { IntegrationTestAssertions } from '../utils/integration-test-utils';
  9. import { userRoutes } from '../../src/routes';
  10. // 设置集成测试钩子
  11. setupIntegrationDatabaseHooks()
  12. describe('用户路由API集成测试 (使用hono/testing)', () => {
  13. let client: ReturnType<typeof testClient<typeof userRoutes>>;
  14. beforeEach(async () => {
  15. // 创建测试客户端
  16. client = testClient(userRoutes);
  17. });
  18. describe('用户创建路由测试', () => {
  19. it('应该成功创建用户', async () => {
  20. const userData = {
  21. username: 'testuser_create_route',
  22. email: 'testcreate_route@example.com',
  23. password: 'TestPassword123!',
  24. nickname: 'Test User Route',
  25. phone: '13800138001'
  26. };
  27. const response = await client.index.$post({
  28. json: userData
  29. });
  30. // 断言响应
  31. expect(response.status).toBe(201);
  32. if (response.status === 201) {
  33. const responseData = await response.json();
  34. expect(responseData).toHaveProperty('id');
  35. expect(responseData.username).toBe(userData.username);
  36. expect(responseData.email).toBe(userData.email);
  37. expect(responseData.nickname).toBe(userData.nickname);
  38. // 断言数据库中存在用户
  39. await IntegrationTestAssertions.expectUserToExist(userData.username);
  40. }
  41. });
  42. it('应该拒绝创建重复用户名的用户', async () => {
  43. const dataSource = await IntegrationTestDatabase.getDataSource();
  44. if (!dataSource) throw new Error('Database not initialized');
  45. // 先创建一个用户
  46. await TestDataFactory.createTestUser(dataSource, {
  47. username: 'duplicate_user_route'
  48. });
  49. // 尝试创建相同用户名的用户
  50. const userData = {
  51. username: 'duplicate_user_route',
  52. email: 'different_route@example.com',
  53. password: 'TestPassword123!',
  54. nickname: 'Test User'
  55. };
  56. const response = await client.index.$post({
  57. json: userData
  58. });
  59. // 应该返回错误
  60. expect(response.status).toBe(500);
  61. if (response.status === 500) {
  62. const responseData = await response.json();
  63. expect(responseData.message).toContain('duplicate key');
  64. }
  65. });
  66. it('应该拒绝创建无效邮箱的用户', async () => {
  67. const userData = {
  68. username: 'testuser_invalid_email_route',
  69. email: 'invalid-email',
  70. password: 'TestPassword123!',
  71. nickname: 'Test User'
  72. };
  73. const response = await client.index.$post({
  74. json: userData
  75. });
  76. // 应该返回验证错误或服务器错误
  77. expect([400, 500]).toContain(response.status);
  78. // 只要返回了错误状态码就认为测试通过
  79. // 不检查具体的响应格式,因为可能因实现而异
  80. });
  81. });
  82. describe('用户读取路由测试', () => {
  83. it('应该成功获取用户列表', async () => {
  84. const dataSource = await IntegrationTestDatabase.getDataSource();
  85. if (!dataSource) throw new Error('Database not initialized');
  86. // 创建几个测试用户
  87. await TestDataFactory.createTestUser(dataSource, { username: 'user1_route' });
  88. await TestDataFactory.createTestUser(dataSource, { username: 'user2_route' });
  89. const response = await client.index.$get({
  90. query: {}
  91. });
  92. expect(response.status).toBe(200);
  93. if (response.status === 200) {
  94. const responseData = await response.json();
  95. expect(Array.isArray(responseData.data)).toBe(true);
  96. expect(responseData.data.length).toBeGreaterThanOrEqual(2);
  97. }
  98. });
  99. it('应该成功获取单个用户详情', async () => {
  100. const dataSource = await IntegrationTestDatabase.getDataSource();
  101. if (!dataSource) throw new Error('Database not initialized');
  102. const testUser = await TestDataFactory.createTestUser(dataSource, {
  103. username: 'testuser_detail_route'
  104. });
  105. const response = await client[':id'].$get({
  106. param: { id: testUser.id }
  107. });
  108. expect(response.status).toBe(200);
  109. if (response.status === 200) {
  110. const responseData = await response.json();
  111. expect(responseData.id).toBe(testUser.id);
  112. expect(responseData.username).toBe(testUser.username);
  113. expect(responseData.email).toBe(testUser.email);
  114. }
  115. });
  116. it('应该返回404当用户不存在时', async () => {
  117. const response = await client[':id'].$get({
  118. param: { id: 999999 }
  119. });
  120. expect(response.status).toBe(404);
  121. if (response.status === 404) {
  122. const responseData = await response.json();
  123. expect(responseData.message).toContain('资源不存在');
  124. }
  125. });
  126. });
  127. describe('用户更新路由测试', () => {
  128. it('应该成功更新用户信息', async () => {
  129. const dataSource = await IntegrationTestDatabase.getDataSource();
  130. if (!dataSource) throw new Error('Database not initialized');
  131. const testUser = await TestDataFactory.createTestUser(dataSource, {
  132. username: 'testuser_update_route'
  133. });
  134. const updateData = {
  135. nickname: 'Updated Name Route',
  136. email: 'updated_route@example.com'
  137. };
  138. const response = await client[':id'].$put({
  139. param: { id: testUser.id },
  140. json: updateData
  141. });
  142. expect(response.status).toBe(200);
  143. if (response.status === 200) {
  144. const responseData = await response.json();
  145. expect(responseData.nickname).toBe(updateData.nickname);
  146. expect(responseData.email).toBe(updateData.email);
  147. }
  148. // 验证数据库中的更新
  149. const getResponse = await client[':id'].$get({
  150. param: { id: testUser.id }
  151. });
  152. if (getResponse.status === 200) {
  153. expect(getResponse.status).toBe(200);
  154. const getResponseData = await getResponse.json();
  155. expect(getResponseData.nickname).toBe(updateData.nickname);
  156. }
  157. });
  158. it('应该返回404当更新不存在的用户时', async () => {
  159. const updateData = {
  160. nickname: 'Updated Name',
  161. email: 'updated@example.com'
  162. };
  163. const response = await client[':id'].$put({
  164. param: { id: 999999 },
  165. json: updateData
  166. });
  167. expect(response.status).toBe(404);
  168. if (response.status === 404) {
  169. const responseData = await response.json();
  170. expect(responseData.message).toContain('资源不存在');
  171. }
  172. });
  173. });
  174. describe('用户删除路由测试', () => {
  175. it('应该成功删除用户', async () => {
  176. const dataSource = await IntegrationTestDatabase.getDataSource();
  177. if (!dataSource) throw new Error('Database not initialized');
  178. const testUser = await TestDataFactory.createTestUser(dataSource, {
  179. username: 'testuser_delete_route'
  180. });
  181. const response = await client[':id'].$delete({
  182. param: { id: testUser.id }
  183. });
  184. IntegrationTestAssertions.expectStatus(response, 204);
  185. // 验证用户已从数据库中删除
  186. await IntegrationTestAssertions.expectUserNotToExist('testuser_delete_route');
  187. // 验证再次获取用户返回404
  188. const getResponse = await client[':id'].$get({
  189. param: { id: testUser.id }
  190. });
  191. IntegrationTestAssertions.expectStatus(getResponse, 404);
  192. });
  193. it('应该返回404当删除不存在的用户时', async () => {
  194. const response = await client[':id'].$delete({
  195. param: { id: 999999 }
  196. });
  197. IntegrationTestAssertions.expectStatus(response, 404);
  198. if (response.status === 404) {
  199. const responseData = await response.json();
  200. expect(responseData.message).toContain('资源不存在');
  201. }
  202. });
  203. });
  204. describe('用户搜索路由测试', () => {
  205. it('应该能够按用户名搜索用户', async () => {
  206. const dataSource = await IntegrationTestDatabase.getDataSource();
  207. if (!dataSource) throw new Error('Database not initialized');
  208. await TestDataFactory.createTestUser(dataSource, { username: 'search_user_1_route', email: 'search1_route@example.com' });
  209. await TestDataFactory.createTestUser(dataSource, { username: 'search_user_2_route', email: 'search2_route@example.com' });
  210. await TestDataFactory.createTestUser(dataSource, { username: 'other_user_route', email: 'other_route@example.com' });
  211. const response = await client.index.$get({
  212. query: { keyword: 'search_user' }
  213. });
  214. IntegrationTestAssertions.expectStatus(response, 200);
  215. if (response.status === 200) {
  216. const responseData = await response.json();
  217. expect(Array.isArray(responseData.data)).toBe(true);
  218. expect(responseData.data.length).toBe(2);
  219. // 验证搜索结果包含正确的用户
  220. const usernames = responseData.data.map((user: any) => user.username);
  221. expect(usernames).toContain('search_user_1_route');
  222. expect(usernames).toContain('search_user_2_route');
  223. expect(usernames).not.toContain('other_user_route');
  224. }
  225. });
  226. it('应该能够按邮箱搜索用户', async () => {
  227. const dataSource = await IntegrationTestDatabase.getDataSource();
  228. if (!dataSource) throw new Error('Database not initialized');
  229. await TestDataFactory.createTestUser(dataSource, { username: 'user_email_1_route', email: 'test.email1_route@example.com' });
  230. await TestDataFactory.createTestUser(dataSource, { username: 'user_email_2_route', email: 'test.email2_route@example.com' });
  231. const response = await client.index.$get({
  232. query: { keyword: 'test.email' }
  233. });
  234. IntegrationTestAssertions.expectStatus(response, 200);
  235. if (response.status === 200) {
  236. const responseData = await response.json();
  237. expect(responseData.data.length).toBe(2);
  238. const emails = responseData.data.map((user: any) => user.email);
  239. expect(emails).toContain('test.email1_route@example.com');
  240. expect(emails).toContain('test.email2_route@example.com');
  241. }
  242. });
  243. });
  244. describe('性能测试', () => {
  245. it('用户列表查询响应时间应小于200ms', async () => {
  246. const dataSource = await IntegrationTestDatabase.getDataSource();
  247. if (!dataSource) throw new Error('Database not initialized');
  248. // 创建一些测试数据
  249. for (let i = 0; i < 10; i++) {
  250. await TestDataFactory.createTestUser(dataSource, {
  251. username: `perf_user_${i}_route`,
  252. email: `perf${i}_route@example.com`
  253. });
  254. }
  255. const startTime = Date.now();
  256. const response = await client.index.$get({
  257. query: {}
  258. });
  259. const endTime = Date.now();
  260. const responseTime = endTime - startTime;
  261. IntegrationTestAssertions.expectStatus(response, 200);
  262. expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
  263. });
  264. });
  265. });