user.routes.integration.test.ts 19 KB

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