activities.integration.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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/server/integration-test-db';
  8. import { IntegrationTestAssertions } from '~/utils/server/integration-test-utils';
  9. import { adminActivitiesRoutesExport } from '@/server/api';
  10. import { AuthService } from '@/server/modules/auth/auth.service';
  11. import { ActivityType } from '@/server/modules/activities/activity.entity';
  12. import { UserService } from '@/server/modules/users/user.service';
  13. // 设置集成测试钩子
  14. setupIntegrationDatabaseHooks()
  15. describe('活动管理API集成测试', () => {
  16. let client: ReturnType<typeof testClient<typeof adminActivitiesRoutesExport>>['api']['v1']['admin'];
  17. let testToken: string;
  18. beforeEach(async () => {
  19. // 创建测试客户端
  20. client = testClient(adminActivitiesRoutesExport).api.v1.admin;
  21. // 创建测试用户并生成token
  22. const dataSource = await IntegrationTestDatabase.getDataSource();
  23. const userService = new UserService(dataSource);
  24. const authService = new AuthService(userService);
  25. // 确保admin用户存在
  26. const user = await authService.ensureAdminExists();
  27. // 生成admin用户的token
  28. testToken = authService.generateToken(user);
  29. });
  30. describe('活动创建测试', () => {
  31. it('应该成功创建去程活动', async () => {
  32. const activityData = {
  33. name: '测试去程活动',
  34. description: '这是一个测试去程活动',
  35. type: ActivityType.DEPARTURE,
  36. startDate: '2025-10-17T08:00:00.000Z',
  37. endDate: '2025-10-17T18:00:00.000Z'
  38. };
  39. const response = await client.activities.$post({
  40. json: activityData,
  41. },
  42. {
  43. headers: {
  44. 'Authorization': `Bearer ${testToken}`
  45. }
  46. });
  47. // 断言响应
  48. expect(response.status).toBe(201);
  49. if (response.status === 201) {
  50. const responseData = await response.json();
  51. expect(responseData).toHaveProperty('id');
  52. expect(responseData.name).toBe(activityData.name);
  53. expect(responseData.type).toBe(activityData.type);
  54. expect(responseData.isDisabled).toBe(0); // 默认启用
  55. // 断言数据库中存在活动
  56. await IntegrationTestAssertions.expectActivityToExist(responseData.id);
  57. }
  58. });
  59. it('应该成功创建返程活动', async () => {
  60. const activityData = {
  61. name: '测试返程活动',
  62. description: '这是一个测试返程活动',
  63. type: ActivityType.RETURN,
  64. startDate: '2025-10-17T16:00:00.000Z',
  65. endDate: '2025-10-17T20:00:00.000Z'
  66. };
  67. const response = await client.activities.$post({
  68. json: activityData,
  69. },
  70. {
  71. headers: {
  72. 'Authorization': `Bearer ${testToken}`
  73. }
  74. });
  75. expect(response.status).toBe(201);
  76. if (response.status === 201) {
  77. const responseData = await response.json();
  78. expect(responseData.type).toBe('return');
  79. }
  80. });
  81. it('应该拒绝创建无效活动类型的活动', async () => {
  82. const activityData = {
  83. name: '测试无效活动',
  84. description: '这是一个测试活动',
  85. type: 'invalid_type' as any, // 无效类型
  86. startDate: '2025-10-17T08:00:00.000Z',
  87. endDate: '2025-10-17T18:00:00.000Z'
  88. };
  89. const response = await client.activities.$post({
  90. json: activityData,
  91. },
  92. {
  93. headers: {
  94. 'Authorization': `Bearer ${testToken}`
  95. }
  96. });
  97. // 应该返回验证错误
  98. expect([400, 500]).toContain(response.status);
  99. });
  100. });
  101. describe('活动读取测试', () => {
  102. it('应该成功获取活动列表', async () => {
  103. const dataSource = await IntegrationTestDatabase.getDataSource();
  104. if (!dataSource) throw new Error('Database not initialized');
  105. // 创建几个测试活动
  106. await TestDataFactory.createTestActivity(dataSource, { name: '活动1', type: ActivityType.DEPARTURE });
  107. await TestDataFactory.createTestActivity(dataSource, { name: '活动2', type: ActivityType.RETURN });
  108. const response = await client.activities.$get({
  109. query: {}
  110. },
  111. {
  112. headers: {
  113. 'Authorization': `Bearer ${testToken}`
  114. }
  115. });
  116. expect(response.status).toBe(200);
  117. if (response.status === 200) {
  118. const responseData = await response.json();
  119. expect(Array.isArray(responseData.data)).toBe(true);
  120. expect(responseData.data.length).toBeGreaterThanOrEqual(2);
  121. }
  122. });
  123. it('应该成功获取单个活动详情', async () => {
  124. const dataSource = await IntegrationTestDatabase.getDataSource();
  125. if (!dataSource) throw new Error('Database not initialized');
  126. const testActivity = await TestDataFactory.createTestActivity(dataSource, {
  127. name: '测试活动详情'
  128. });
  129. const response = await client.activities[':id'].$get({
  130. param: { id: testActivity.id }
  131. },
  132. {
  133. headers: {
  134. 'Authorization': `Bearer ${testToken}`
  135. }
  136. });
  137. expect(response.status).toBe(200);
  138. if (response.status === 200) {
  139. const responseData = await response.json();
  140. expect(responseData.id).toBe(testActivity.id);
  141. expect(responseData.name).toBe(testActivity.name);
  142. expect(responseData.type).toBe(testActivity.type);
  143. }
  144. });
  145. it('应该返回404当活动不存在时', async () => {
  146. const response = await client.activities[':id'].$get({
  147. param: { id: 999999 }
  148. },
  149. {
  150. headers: {
  151. 'Authorization': `Bearer ${testToken}`
  152. }
  153. });
  154. expect(response.status).toBe(404);
  155. if (response.status === 404) {
  156. const responseData = await response.json();
  157. expect(responseData.message).toContain('资源不存在');
  158. }
  159. });
  160. });
  161. describe('活动更新测试', () => {
  162. it('应该成功更新活动信息', async () => {
  163. const dataSource = await IntegrationTestDatabase.getDataSource();
  164. if (!dataSource) throw new Error('Database not initialized');
  165. const testActivity = await TestDataFactory.createTestActivity(dataSource, {
  166. name: '测试活动更新'
  167. });
  168. const updateData = {
  169. name: '更新后的活动名称',
  170. description: '更新后的活动描述'
  171. };
  172. const response = await client.activities[':id'].$put({
  173. param: { id: testActivity.id },
  174. json: updateData
  175. },
  176. {
  177. headers: {
  178. 'Authorization': `Bearer ${testToken}`
  179. }
  180. });
  181. expect(response.status).toBe(200);
  182. if (response.status === 200) {
  183. const responseData = await response.json();
  184. expect(responseData.name).toBe(updateData.name);
  185. expect(responseData.description).toBe(updateData.description);
  186. }
  187. // 验证数据库中的更新
  188. const getResponse = await client.activities[':id'].$get({
  189. param: { id: testActivity.id }
  190. },
  191. {
  192. headers: {
  193. 'Authorization': `Bearer ${testToken}`
  194. }
  195. });
  196. if (getResponse.status === 200) {
  197. expect(getResponse.status).toBe(200);
  198. const getResponseData = await getResponse.json();
  199. expect(getResponseData.name).toBe(updateData.name);
  200. }
  201. });
  202. it('应该成功启用/禁用活动', async () => {
  203. const dataSource = await IntegrationTestDatabase.getDataSource();
  204. if (!dataSource) throw new Error('Database not initialized');
  205. const testActivity = await TestDataFactory.createTestActivity(dataSource, {
  206. name: '测试状态切换',
  207. isDisabled: 0 // 启用状态
  208. });
  209. // 禁用活动
  210. const disableResponse = await client.activities[':id'].$put({
  211. param: { id: testActivity.id },
  212. json: { isDisabled: 1 } // 禁用
  213. },
  214. {
  215. headers: {
  216. 'Authorization': `Bearer ${testToken}`
  217. }
  218. });
  219. expect(disableResponse.status).toBe(200);
  220. if (disableResponse.status === 200) {
  221. const disableData = await disableResponse.json();
  222. expect(disableData.isDisabled).toBe(1);
  223. }
  224. // 重新启用活动
  225. const enableResponse = await client.activities[':id'].$put({
  226. param: { id: testActivity.id },
  227. json: { isDisabled: 0 } // 启用
  228. },
  229. {
  230. headers: {
  231. 'Authorization': `Bearer ${testToken}`
  232. }
  233. });
  234. expect(enableResponse.status).toBe(200);
  235. if (enableResponse.status === 200) {
  236. const enableData = await enableResponse.json();
  237. expect(enableData.isDisabled).toBe(0);
  238. }
  239. });
  240. it('应该返回404当更新不存在的活动时', async () => {
  241. const updateData = {
  242. name: '更新后的名称'
  243. };
  244. const response = await client.activities[':id'].$put({
  245. param: { id: 999999 },
  246. json: updateData
  247. },
  248. {
  249. headers: {
  250. 'Authorization': `Bearer ${testToken}`
  251. }
  252. });
  253. expect(response.status).toBe(404);
  254. if (response.status === 404) {
  255. const responseData = await response.json();
  256. expect(responseData.message).toContain('资源不存在');
  257. }
  258. });
  259. });
  260. describe('活动删除测试', () => {
  261. it('应该成功删除活动', async () => {
  262. const dataSource = await IntegrationTestDatabase.getDataSource();
  263. if (!dataSource) throw new Error('Database not initialized');
  264. const testActivity = await TestDataFactory.createTestActivity(dataSource, {
  265. name: '测试活动删除'
  266. });
  267. const response = await client.activities[':id'].$delete({
  268. param: { id: testActivity.id }
  269. },
  270. {
  271. headers: {
  272. 'Authorization': `Bearer ${testToken}`
  273. }
  274. });
  275. IntegrationTestAssertions.expectStatus(response, 204);
  276. // 验证活动已从数据库中删除
  277. await IntegrationTestAssertions.expectActivityNotToExist(testActivity.id);
  278. // 验证再次获取活动返回404
  279. const getResponse = await client.activities[':id'].$get({
  280. param: { id: testActivity.id }
  281. },
  282. {
  283. headers: {
  284. 'Authorization': `Bearer ${testToken}`
  285. }
  286. });
  287. IntegrationTestAssertions.expectStatus(getResponse, 404);
  288. });
  289. it('应该返回404当删除不存在的活动时', async () => {
  290. const response = await client.activities[':id'].$delete({
  291. param: { id: 999999 }
  292. },
  293. {
  294. headers: {
  295. 'Authorization': `Bearer ${testToken}`
  296. }
  297. });
  298. IntegrationTestAssertions.expectStatus(response, 404);
  299. if (response.status === 404) {
  300. const responseData = await response.json();
  301. expect(responseData.message).toContain('资源不存在');
  302. }
  303. });
  304. });
  305. describe('活动搜索测试', () => {
  306. it('应该能够按活动名称搜索活动', async () => {
  307. const dataSource = await IntegrationTestDatabase.getDataSource();
  308. if (!dataSource) throw new Error('Database not initialized');
  309. await TestDataFactory.createTestActivity(dataSource, { name: '搜索活动1', description: '测试描述1' });
  310. await TestDataFactory.createTestActivity(dataSource, { name: '搜索活动2', description: '测试描述2' });
  311. await TestDataFactory.createTestActivity(dataSource, { name: '其他活动', description: '其他描述' });
  312. const response = await client.activities.$get({
  313. query: { keyword: '搜索活动' }
  314. },
  315. {
  316. headers: {
  317. 'Authorization': `Bearer ${testToken}`
  318. }
  319. });
  320. IntegrationTestAssertions.expectStatus(response, 200);
  321. if (response.status === 200) {
  322. const responseData = await response.json();
  323. expect(Array.isArray(responseData.data)).toBe(true);
  324. expect(responseData.data.length).toBe(2);
  325. // 验证搜索结果包含正确的活动
  326. const names = responseData.data.map((activity: any) => activity.name);
  327. expect(names).toContain('搜索活动1');
  328. expect(names).toContain('搜索活动2');
  329. expect(names).not.toContain('其他活动');
  330. }
  331. });
  332. it('应该能够按活动描述搜索活动', async () => {
  333. const dataSource = await IntegrationTestDatabase.getDataSource();
  334. if (!dataSource) throw new Error('Database not initialized');
  335. await TestDataFactory.createTestActivity(dataSource, { name: '活动1', description: '测试描述搜索1' });
  336. await TestDataFactory.createTestActivity(dataSource, { name: '活动2', description: '测试描述搜索2' });
  337. const response = await client.activities.$get({
  338. query: { keyword: '测试描述' }
  339. },
  340. {
  341. headers: {
  342. 'Authorization': `Bearer ${testToken}`
  343. }
  344. });
  345. IntegrationTestAssertions.expectStatus(response, 200);
  346. if (response.status === 200) {
  347. const responseData = await response.json();
  348. expect(responseData.data.length).toBe(2);
  349. const descriptions = responseData.data.map((activity: any) => activity.description);
  350. expect(descriptions).toContain('测试描述搜索1');
  351. expect(descriptions).toContain('测试描述搜索2');
  352. }
  353. });
  354. it('应该能够按活动类型筛选活动', async () => {
  355. const dataSource = await IntegrationTestDatabase.getDataSource();
  356. if (!dataSource) throw new Error('Database not initialized');
  357. await TestDataFactory.createTestActivity(dataSource, { name: '去程活动1', type: ActivityType.DEPARTURE });
  358. await TestDataFactory.createTestActivity(dataSource, { name: '去程活动2', type: ActivityType.DEPARTURE });
  359. await TestDataFactory.createTestActivity(dataSource, { name: '返程活动1', type: ActivityType.RETURN });
  360. const response = await client.activities.$get({
  361. query: { filters: JSON.stringify({ type: ActivityType.DEPARTURE }) }
  362. },
  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(responseData.data.length).toBe(2);
  372. const types = responseData.data.map((activity: any) => activity.type);
  373. expect(types.every((type: string) => type === 'departure')).toBe(true);
  374. }
  375. });
  376. });
  377. describe('性能测试', () => {
  378. it('活动列表查询响应时间应小于200ms', async () => {
  379. const dataSource = await IntegrationTestDatabase.getDataSource();
  380. if (!dataSource) throw new Error('Database not initialized');
  381. // 创建一些测试数据
  382. for (let i = 0; i < 10; i++) {
  383. await TestDataFactory.createTestActivity(dataSource, {
  384. name: `性能测试活动_${i}`,
  385. description: `性能测试描述_${i}`
  386. });
  387. }
  388. const startTime = Date.now();
  389. const response = await client.activities.$get({
  390. query: {}
  391. },
  392. {
  393. headers: {
  394. 'Authorization': `Bearer ${testToken}`
  395. }
  396. });
  397. const endTime = Date.now();
  398. const responseTime = endTime - startTime;
  399. IntegrationTestAssertions.expectStatus(response, 200);
  400. expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
  401. });
  402. });
  403. });