user.routes.integration.test.ts 22 KB

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