user.routes.integration.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import {
  4. IntegrationTestDatabase,
  5. setupIntegrationDatabaseHooksWithEntities
  6. } from '@d8d/shared-test-util';
  7. import {
  8. IntegrationTestAssertions
  9. } from '../utils/integration-test-utils';
  10. import { userRoutes } from '../../src/routes';
  11. import { UserEntity } from '../../src/entities/user.entity';
  12. import { Role } from '../../src/entities/role.entity';
  13. import { TestDataFactory } from '../utils/integration-test-db';
  14. import { AuthService } from '@d8d/auth-module';
  15. import { UserService } from '../../src/services/user.service';
  16. // 设置集成测试钩子
  17. setupIntegrationDatabaseHooksWithEntities([UserEntity, Role])
  18. describe('用户路由API集成测试 (使用hono/testing)', () => {
  19. let client: ReturnType<typeof testClient<typeof userRoutes>>;
  20. let authService: AuthService;
  21. let userService: UserService;
  22. let testToken: string;
  23. let testUser: any;
  24. beforeEach(async () => {
  25. // 创建测试客户端
  26. client = testClient(userRoutes);
  27. // 获取数据源
  28. const dataSource = await IntegrationTestDatabase.getDataSource();
  29. if (!dataSource) throw new Error('Database not initialized');
  30. // 初始化服务
  31. userService = new UserService(dataSource);
  32. authService = new AuthService(userService);
  33. // 创建测试用户并生成token
  34. testUser = await TestDataFactory.createTestUser(dataSource, {
  35. username: 'testuser_auth',
  36. password: 'TestPassword123!',
  37. email: 'testuser_auth@example.com'
  38. });
  39. // 生成测试用户的token
  40. testToken = authService.generateToken(testUser);
  41. });
  42. describe('用户创建路由测试', () => {
  43. it('应该拒绝无认证令牌的用户创建请求', async () => {
  44. const userData = {
  45. username: 'testuser_create_route_no_auth',
  46. email: 'testcreate_route_no_auth@example.com',
  47. password: 'TestPassword123!',
  48. nickname: 'Test User Route No Auth',
  49. phone: '13800138001'
  50. };
  51. const response = await client.index.$post({
  52. json: userData
  53. });
  54. // 应该返回401状态码,因为缺少认证
  55. expect(response.status).toBe(401);
  56. if (response.status === 401) {
  57. const responseData = await response.json();
  58. expect(responseData.message).toContain('Authorization header missing');
  59. }
  60. });
  61. it('应该拒绝无效认证令牌的用户创建请求', async () => {
  62. const userData = {
  63. username: 'testuser_create_route_invalid_token',
  64. email: 'testcreate_route_invalid_token@example.com',
  65. password: 'TestPassword123!',
  66. nickname: 'Test User Route Invalid Token',
  67. phone: '13800138001'
  68. };
  69. const response = await client.index.$post({
  70. json: userData
  71. }, {
  72. headers: {
  73. 'Authorization': 'Bearer invalid.token.here'
  74. }
  75. });
  76. // 应该返回401状态码,因为令牌无效
  77. expect(response.status).toBe(401);
  78. if (response.status === 401) {
  79. const responseData = await response.json();
  80. expect(responseData.message).toContain('Invalid token');
  81. }
  82. });
  83. it('应该成功创建用户(使用有效认证令牌)', async () => {
  84. const userData = {
  85. username: 'testuser_create_route',
  86. email: 'testcreate_route@example.com',
  87. password: 'TestPassword123!',
  88. nickname: 'Test User Route',
  89. phone: '13800138001'
  90. };
  91. const response = await client.index.$post({
  92. json: userData
  93. }, {
  94. headers: {
  95. 'Authorization': `Bearer ${testToken}`
  96. }
  97. });
  98. // 断言响应
  99. expect(response.status).toBe(201);
  100. if (response.status === 201) {
  101. const responseData = await response.json();
  102. expect(responseData).toHaveProperty('id');
  103. expect(responseData.username).toBe(userData.username);
  104. expect(responseData.email).toBe(userData.email);
  105. expect(responseData.nickname).toBe(userData.nickname);
  106. // 断言数据库中存在用户
  107. await IntegrationTestAssertions.expectUserToExist(userData.username);
  108. }
  109. });
  110. it('应该拒绝创建重复用户名的用户', async () => {
  111. const dataSource = await IntegrationTestDatabase.getDataSource();
  112. if (!dataSource) throw new Error('Database not initialized');
  113. // 先创建一个用户
  114. await TestDataFactory.createTestUser(dataSource, {
  115. username: 'duplicate_user_route'
  116. });
  117. // 尝试创建相同用户名的用户
  118. const userData = {
  119. username: 'duplicate_user_route',
  120. email: 'different_route@example.com',
  121. password: 'TestPassword123!',
  122. nickname: 'Test User'
  123. };
  124. const response = await client.index.$post({
  125. json: userData
  126. }, {
  127. headers: {
  128. 'Authorization': `Bearer ${testToken}`
  129. }
  130. });
  131. // 应该返回错误
  132. expect(response.status).toBe(500);
  133. if (response.status === 500) {
  134. const responseData = await response.json();
  135. expect(responseData.message).toContain('duplicate key');
  136. }
  137. });
  138. it('应该拒绝创建无效邮箱的用户', async () => {
  139. const userData = {
  140. username: 'testuser_invalid_email_route',
  141. email: 'invalid-email',
  142. password: 'TestPassword123!',
  143. nickname: 'Test User'
  144. };
  145. const response = await client.index.$post({
  146. json: userData
  147. }, {
  148. headers: {
  149. 'Authorization': `Bearer ${testToken}`
  150. }
  151. });
  152. // 应该返回验证错误或服务器错误
  153. expect([400, 500]).toContain(response.status);
  154. // 只要返回了错误状态码就认为测试通过
  155. // 不检查具体的响应格式,因为可能因实现而异
  156. });
  157. });
  158. describe('用户读取路由测试', () => {
  159. it('应该成功获取用户列表', async () => {
  160. const dataSource = await IntegrationTestDatabase.getDataSource();
  161. if (!dataSource) throw new Error('Database not initialized');
  162. // 创建几个测试用户
  163. await TestDataFactory.createTestUser(dataSource, { username: 'user1_route' });
  164. await TestDataFactory.createTestUser(dataSource, { username: 'user2_route' });
  165. const response = await client.index.$get({
  166. query: {}
  167. }, {
  168. headers: {
  169. 'Authorization': `Bearer ${testToken}`
  170. }
  171. });
  172. expect(response.status).toBe(200);
  173. if (response.status === 200) {
  174. const responseData = await response.json();
  175. expect(Array.isArray(responseData.data)).toBe(true);
  176. expect(responseData.data.length).toBeGreaterThanOrEqual(2);
  177. }
  178. });
  179. it('应该成功获取单个用户详情', async () => {
  180. const dataSource = await IntegrationTestDatabase.getDataSource();
  181. if (!dataSource) throw new Error('Database not initialized');
  182. const testUser = await TestDataFactory.createTestUser(dataSource, {
  183. username: 'testuser_detail_route'
  184. });
  185. const response = await client[':id'].$get({
  186. param: { id: testUser.id }
  187. }, {
  188. headers: {
  189. 'Authorization': `Bearer ${testToken}`
  190. }
  191. });
  192. expect(response.status).toBe(200);
  193. if (response.status === 200) {
  194. const responseData = await response.json();
  195. expect(responseData.id).toBe(testUser.id);
  196. expect(responseData.username).toBe(testUser.username);
  197. expect(responseData.email).toBe(testUser.email);
  198. }
  199. });
  200. it('应该返回404当用户不存在时', async () => {
  201. const response = await client[':id'].$get({
  202. param: { id: 999999 }
  203. }, {
  204. headers: {
  205. 'Authorization': `Bearer ${testToken}`
  206. }
  207. });
  208. expect(response.status).toBe(404);
  209. if (response.status === 404) {
  210. const responseData = await response.json();
  211. expect(responseData.message).toContain('资源不存在');
  212. }
  213. });
  214. });
  215. describe('用户更新路由测试', () => {
  216. it('应该拒绝无认证令牌的用户更新请求', async () => {
  217. const dataSource = await IntegrationTestDatabase.getDataSource();
  218. if (!dataSource) throw new Error('Database not initialized');
  219. const testUser = await TestDataFactory.createTestUser(dataSource, {
  220. username: 'testuser_update_no_auth'
  221. });
  222. const updateData = {
  223. nickname: 'Updated Name Route',
  224. email: 'updated_route@example.com'
  225. };
  226. const response = await client[':id'].$put({
  227. param: { id: testUser.id },
  228. json: updateData
  229. });
  230. // 应该返回401状态码,因为缺少认证
  231. expect(response.status).toBe(401);
  232. if (response.status === 401) {
  233. const responseData = await response.json();
  234. expect(responseData.message).toContain('Authorization header missing');
  235. }
  236. });
  237. it('应该成功更新用户信息(使用有效认证令牌)', async () => {
  238. const dataSource = await IntegrationTestDatabase.getDataSource();
  239. if (!dataSource) throw new Error('Database not initialized');
  240. const testUser = await TestDataFactory.createTestUser(dataSource, {
  241. username: 'testuser_update_route'
  242. });
  243. const updateData = {
  244. nickname: 'Updated Name Route',
  245. email: 'updated_route@example.com'
  246. };
  247. const response = await client[':id'].$put({
  248. param: { id: testUser.id },
  249. json: updateData
  250. }, {
  251. headers: {
  252. 'Authorization': `Bearer ${testToken}`
  253. }
  254. });
  255. expect(response.status).toBe(200);
  256. if (response.status === 200) {
  257. const responseData = await response.json();
  258. expect(responseData.nickname).toBe(updateData.nickname);
  259. expect(responseData.email).toBe(updateData.email);
  260. }
  261. // 验证数据库中的更新
  262. const getResponse = await client[':id'].$get({
  263. param: { id: testUser.id }
  264. });
  265. if (getResponse.status === 200) {
  266. expect(getResponse.status).toBe(200);
  267. const getResponseData = await getResponse.json();
  268. expect(getResponseData.nickname).toBe(updateData.nickname);
  269. }
  270. });
  271. it('应该返回404当更新不存在的用户时', async () => {
  272. const updateData = {
  273. nickname: 'Updated Name',
  274. email: 'updated@example.com'
  275. };
  276. const response = await client[':id'].$put({
  277. param: { id: 999999 },
  278. json: updateData
  279. }, {
  280. headers: {
  281. 'Authorization': `Bearer ${testToken}`
  282. }
  283. });
  284. expect(response.status).toBe(404);
  285. if (response.status === 404) {
  286. const responseData = await response.json();
  287. expect(responseData.message).toContain('资源不存在');
  288. }
  289. });
  290. });
  291. describe('用户删除路由测试', () => {
  292. it('应该拒绝无认证令牌的用户删除请求', async () => {
  293. const dataSource = await IntegrationTestDatabase.getDataSource();
  294. if (!dataSource) throw new Error('Database not initialized');
  295. const testUser = await TestDataFactory.createTestUser(dataSource, {
  296. username: 'testuser_delete_no_auth'
  297. });
  298. const response = await client[':id'].$delete({
  299. param: { id: testUser.id }
  300. });
  301. // 应该返回401状态码,因为缺少认证
  302. expect(response.status).toBe(401);
  303. if (response.status === 401) {
  304. const responseData = await response.json();
  305. expect(responseData.message).toContain('Authorization header missing');
  306. }
  307. });
  308. it('应该成功删除用户(使用有效认证令牌)', async () => {
  309. const dataSource = await IntegrationTestDatabase.getDataSource();
  310. if (!dataSource) throw new Error('Database not initialized');
  311. const testUser = await TestDataFactory.createTestUser(dataSource, {
  312. username: 'testuser_delete_route'
  313. });
  314. const response = await client[':id'].$delete({
  315. param: { id: testUser.id }
  316. }, {
  317. headers: {
  318. 'Authorization': `Bearer ${testToken}`
  319. }
  320. });
  321. IntegrationTestAssertions.expectStatus(response, 204);
  322. // 验证用户已从数据库中删除
  323. await IntegrationTestAssertions.expectUserNotToExist('testuser_delete_route');
  324. // 验证再次获取用户返回404
  325. const getResponse = await client[':id'].$get({
  326. param: { id: testUser.id }
  327. }, {
  328. headers: {
  329. 'Authorization': `Bearer ${testToken}`
  330. }
  331. });
  332. IntegrationTestAssertions.expectStatus(getResponse, 404);
  333. });
  334. it('应该返回404当删除不存在的用户时', async () => {
  335. const response = await client[':id'].$delete({
  336. param: { id: 999999 }
  337. }, {
  338. headers: {
  339. 'Authorization': `Bearer ${testToken}`
  340. }
  341. });
  342. IntegrationTestAssertions.expectStatus(response, 404);
  343. if (response.status === 404) {
  344. const responseData = await response.json();
  345. expect(responseData.message).toContain('资源不存在');
  346. }
  347. });
  348. });
  349. describe('用户搜索路由测试', () => {
  350. it('应该能够按用户名搜索用户', async () => {
  351. const dataSource = await IntegrationTestDatabase.getDataSource();
  352. if (!dataSource) throw new Error('Database not initialized');
  353. await TestDataFactory.createTestUser(dataSource, { username: 'search_user_1_route', email: 'search1_route@example.com' });
  354. await TestDataFactory.createTestUser(dataSource, { username: 'search_user_2_route', email: 'search2_route@example.com' });
  355. await TestDataFactory.createTestUser(dataSource, { username: 'other_user_route', email: 'other_route@example.com' });
  356. const response = await client.index.$get({
  357. query: { keyword: 'search_user' }
  358. }, {
  359. headers: {
  360. 'Authorization': `Bearer ${testToken}`
  361. }
  362. });
  363. IntegrationTestAssertions.expectStatus(response, 200);
  364. if (response.status === 200) {
  365. const responseData = await response.json();
  366. expect(Array.isArray(responseData.data)).toBe(true);
  367. expect(responseData.data.length).toBe(2);
  368. // 验证搜索结果包含正确的用户
  369. const usernames = responseData.data.map((user: any) => user.username);
  370. expect(usernames).toContain('search_user_1_route');
  371. expect(usernames).toContain('search_user_2_route');
  372. expect(usernames).not.toContain('other_user_route');
  373. }
  374. });
  375. it('应该能够按邮箱搜索用户', async () => {
  376. const dataSource = await IntegrationTestDatabase.getDataSource();
  377. if (!dataSource) throw new Error('Database not initialized');
  378. await TestDataFactory.createTestUser(dataSource, { username: 'user_email_1_route', email: 'test.email1_route@example.com' });
  379. await TestDataFactory.createTestUser(dataSource, { username: 'user_email_2_route', email: 'test.email2_route@example.com' });
  380. const response = await client.index.$get({
  381. query: { keyword: 'test.email' }
  382. }, {
  383. headers: {
  384. 'Authorization': `Bearer ${testToken}`
  385. }
  386. });
  387. IntegrationTestAssertions.expectStatus(response, 200);
  388. if (response.status === 200) {
  389. const responseData = await response.json();
  390. expect(responseData.data.length).toBe(2);
  391. const emails = responseData.data.map((user: any) => user.email);
  392. expect(emails).toContain('test.email1_route@example.com');
  393. expect(emails).toContain('test.email2_route@example.com');
  394. }
  395. });
  396. });
  397. describe('性能测试', () => {
  398. it('用户列表查询响应时间应小于200ms', async () => {
  399. const dataSource = await IntegrationTestDatabase.getDataSource();
  400. if (!dataSource) throw new Error('Database not initialized');
  401. // 创建一些测试数据
  402. for (let i = 0; i < 10; i++) {
  403. await TestDataFactory.createTestUser(dataSource, {
  404. username: `perf_user_${i}_route`,
  405. email: `perf${i}_route@example.com`
  406. });
  407. }
  408. const startTime = Date.now();
  409. const response = await client.index.$get({
  410. query: {}
  411. }, {
  412. headers: {
  413. 'Authorization': `Bearer ${testToken}`
  414. }
  415. });
  416. const endTime = Date.now();
  417. const responseTime = endTime - startTime;
  418. IntegrationTestAssertions.expectStatus(response, 200);
  419. expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
  420. });
  421. });
  422. describe('认证令牌测试', () => {
  423. it('应该能够生成有效的JWT令牌', async () => {
  424. // 验证生成的令牌是有效的字符串
  425. expect(typeof testToken).toBe('string');
  426. expect(testToken.length).toBeGreaterThan(0);
  427. // 验证令牌可以被正确解码
  428. const decoded = authService.verifyToken(testToken);
  429. expect(decoded).toHaveProperty('id');
  430. expect(decoded).toHaveProperty('username');
  431. expect(decoded.id).toBe(testUser.id);
  432. expect(decoded.username).toBe(testUser.username);
  433. });
  434. it('应该拒绝过期令牌的请求', async () => {
  435. // 创建立即过期的令牌
  436. const expiredToken = authService.generateToken(testUser, '1ms');
  437. // 等待令牌过期
  438. await new Promise(resolve => setTimeout(resolve, 10));
  439. const response = await client.index.$post({
  440. json: {
  441. username: 'test_expired_token',
  442. email: 'test_expired@example.com',
  443. password: 'TestPassword123!',
  444. nickname: 'Test Expired Token'
  445. }
  446. }, {
  447. headers: {
  448. 'Authorization': `Bearer ${expiredToken}`
  449. }
  450. });
  451. // 应该返回401状态码,因为令牌过期
  452. expect(response.status).toBe(401);
  453. if (response.status === 401) {
  454. const responseData = await response.json();
  455. expect(responseData.message).toContain('Invalid token');
  456. }
  457. });
  458. it('应该拒绝格式错误的认证头', async () => {
  459. const response = await client.index.$post({
  460. json: {
  461. username: 'test_bad_auth_header',
  462. email: 'test_bad_auth@example.com',
  463. password: 'TestPassword123!',
  464. nickname: 'Test Bad Auth Header'
  465. }
  466. }, {
  467. headers: {
  468. 'Authorization': 'Basic invalid_format'
  469. }
  470. });
  471. // 应该返回401状态码,因为认证头格式错误
  472. expect(response.status).toBe(401);
  473. if (response.status === 401) {
  474. const responseData = await response.json();
  475. expect(responseData.message).toContain('Authorization header missing');
  476. }
  477. });
  478. });
  479. });