2
0

activities.integration.test.ts 15 KB

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