| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- import { describe, it, expect, beforeEach } from 'vitest';
- import { testClient } from 'hono/testing';
- import {
- IntegrationTestDatabase,
- setupIntegrationDatabaseHooks,
- TestDataFactory
- } from '~/utils/server/integration-test-db';
- import { IntegrationTestAssertions } from '~/utils/server/integration-test-utils';
- import { ordersRoutesExport } from '@d8d/server/api';
- import { AuthService } from '@d8d/server/modules/auth/auth.service';
- import { UserService } from '@d8d/server/modules/users/user.service';
- import { OrderStatus, PaymentStatus } from '@d8d/server/share/order.types';
- // 设置集成测试钩子
- setupIntegrationDatabaseHooks()
- describe('用户端订单API集成测试', () => {
- let client: ReturnType<typeof testClient<typeof ordersRoutesExport>>['api']['v1'];
- let testToken: string;
- let testUser: any;
- let testRoute: any;
- let testPassenger: any;
- beforeEach(async () => {
- // 创建测试客户端
- client = testClient(ordersRoutesExport).api.v1;
- // 创建测试用户并生成token
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const userService = new UserService(dataSource);
- const authService = new AuthService(userService);
- // 创建测试用户
- testUser = await TestDataFactory.createTestUser(dataSource);
- // 生成测试用户的token
- testToken = authService.generateToken(testUser);
- // 创建测试路线
- testRoute = await TestDataFactory.createTestRoute(dataSource);
- // 创建测试乘客
- testPassenger = await TestDataFactory.createTestPassenger(dataSource, {
- userId: testUser.id,
- name: '测试乘客'
- });
- });
- describe('订单创建测试', () => {
- it('应该成功创建订单', async () => {
- const orderData = {
- routeId: testRoute.id,
- passengerCount: 1,
- totalAmount: testRoute.price,
- passengerSnapshots: [
- {
- id: testPassenger.id,
- name: testPassenger.name,
- idType: testPassenger.idType,
- idNumber: testPassenger.idNumber,
- phone: testPassenger.phone
- }
- ],
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- // 断言响应
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const responseData = await response.json();
- expect(responseData).toHaveProperty('id');
- expect(responseData.userId).toBe(testUser.id);
- expect(responseData.routeId).toBe(orderData.routeId);
- expect(responseData.passengerCount).toBe(orderData.passengerCount);
- expect(responseData.totalAmount).toBe(orderData.totalAmount);
- expect(responseData.status).toBe(OrderStatus.PENDING_PAYMENT);
- expect(responseData.paymentStatus).toBe(PaymentStatus.PENDING);
- expect(responseData.passengerSnapshots).toEqual(orderData.passengerSnapshots);
- // 由于parseWithAwait会将Date转换为字符串,我们需要比较字符串格式
- const expectedRouteSnapshot = {
- ...orderData.routeSnapshot,
- departureTime: orderData.routeSnapshot.departureTime.toISOString()
- };
- expect(responseData.routeSnapshot).toEqual(expectedRouteSnapshot);
- // 断言数据库中存在订单
- await IntegrationTestAssertions.expectOrderToExist(responseData.id);
- } else {
- // 调试信息
- const errorData = await response.json();
- console.debug('订单创建失败:', errorData);
- }
- });
- it('应该拒绝创建不存在的路线订单', async () => {
- const orderData = {
- routeId: 999999, // 不存在的路线ID
- passengerCount: 1,
- totalAmount: 100,
- passengerSnapshots: [],
- routeSnapshot: {
- id: 999999,
- name: '不存在的路线',
- pickupPoint: '起点',
- dropoffPoint: '终点',
- departureTime: new Date(),
- price: 100,
- vehicleType: 'CAR',
- travelMode: 'DRIVING'
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- // 应该返回404错误
- expect(response.status).toBe(404);
- if (response.status === 404) {
- const responseData = await response.json();
- expect(responseData.message).toContain('路线不存在');
- }
- });
- it('应该拒绝创建超过座位数的订单', async () => {
- const orderData = {
- routeId: testRoute.id,
- passengerCount: testRoute.availableSeats + 1, // 超过可用座位数
- totalAmount: testRoute.price * (testRoute.availableSeats + 1),
- passengerSnapshots: [],
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- // 应该返回422业务逻辑错误
- expect(response.status).toBe(422);
- if (response.status === 422) {
- const responseData = await response.json();
- expect(responseData.message).toContain('乘客数量超过路线座位数');
- }
- });
- it('应该验证乘客快照信息', async () => {
- const orderData = {
- routeId: testRoute.id,
- passengerCount: 1,
- totalAmount: testRoute.price,
- passengerSnapshots: [
- {
- id: 999999, // 不存在的乘客ID
- name: '不存在的乘客',
- idType: 'ID_CARD',
- idNumber: '110101199001011234',
- phone: '13812345678'
- }
- ],
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- // 应该返回404错误
- expect(response.status).toBe(404);
- if (response.status === 404) {
- const responseData = await response.json();
- expect(responseData.message).toContain('乘客ID 999999 不存在');
- }
- });
- it('应该拒绝创建缺少必填字段的订单', async () => {
- const orderData = {
- // 缺少routeId
- routeId: undefined as any, // 故意不提供routeId来测试验证
- passengerCount: 1,
- totalAmount: 100,
- passengerSnapshots: [],
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- // 应该返回验证错误
- expect([400, 500]).toContain(response.status);
- });
- it('应该正确计算订单金额', async () => {
- const passengerCount = 2;
- const expectedTotalAmount = testRoute.price * passengerCount;
- const orderData = {
- routeId: testRoute.id,
- passengerCount: passengerCount,
- totalAmount: expectedTotalAmount,
- passengerSnapshots: [
- {
- id: testPassenger.id,
- name: testPassenger.name,
- idType: testPassenger.idType,
- idNumber: testPassenger.idNumber,
- phone: testPassenger.phone
- },
- {
- name: '第二个乘客',
- idType: 'ID_CARD',
- idNumber: '110101199001012345',
- phone: '13987654321'
- }
- ],
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const responseData = await response.json();
- expect(responseData.totalAmount).toBe(expectedTotalAmount);
- expect(responseData.passengerCount).toBe(passengerCount);
- }
- });
- });
- describe('权限控制测试', () => {
- it('应该拒绝未认证用户的订单创建', async () => {
- const orderData = {
- routeId: testRoute.id,
- passengerCount: 1,
- totalAmount: testRoute.price,
- passengerSnapshots: [],
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- });
- expect(response.status).toBe(401);
- });
- it('应该拒绝无效token的订单创建', async () => {
- const orderData = {
- routeId: testRoute.id,
- passengerCount: 1,
- totalAmount: testRoute.price,
- passengerSnapshots: [],
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': 'Bearer invalid_token'
- }
- });
- expect(response.status).toBe(401);
- });
- });
- describe('快照机制测试', () => {
- it('应该正确保存路线快照信息', async () => {
- const routeSnapshot = {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- };
- const orderData = {
- routeId: testRoute.id,
- passengerCount: 1,
- totalAmount: testRoute.price,
- passengerSnapshots: [],
- routeSnapshot: routeSnapshot
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const responseData = await response.json();
- // 由于parseWithAwait会将Date转换为字符串,我们需要比较字符串格式
- const expectedRouteSnapshot = {
- ...routeSnapshot,
- departureTime: routeSnapshot.departureTime.toISOString()
- };
- expect(responseData.routeSnapshot).toEqual(expectedRouteSnapshot);
- // 验证数据库中的快照数据
- const order = await IntegrationTestAssertions.getOrderById(responseData.id);
- expect(order).not.toBeNull();
- // 数据库中的routeSnapshot应该包含字符串格式的时间
- const expectedDbRouteSnapshot = {
- ...routeSnapshot,
- departureTime: routeSnapshot.departureTime.toISOString()
- };
- expect(order!.routeSnapshot).toEqual(expectedDbRouteSnapshot);
- }
- });
- it('应该正确保存乘客快照信息', async () => {
- const passengerSnapshots = [
- {
- id: testPassenger.id,
- name: testPassenger.name,
- idType: testPassenger.idType,
- idNumber: testPassenger.idNumber,
- phone: testPassenger.phone,
- isDefault: testPassenger.isDefault
- },
- {
- name: '新乘客',
- idType: 'PASSPORT',
- idNumber: 'E12345678',
- phone: '13987654321',
- isDefault: false
- }
- ];
- const orderData = {
- routeId: testRoute.id,
- passengerCount: passengerSnapshots.length,
- totalAmount: testRoute.price * passengerSnapshots.length,
- passengerSnapshots: passengerSnapshots,
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const responseData = await response.json();
- expect(responseData.passengerSnapshots).toEqual(passengerSnapshots);
- // 验证数据库中的快照数据
- const order = await IntegrationTestAssertions.getOrderById(responseData.id);
- expect(order).not.toBeNull();
- expect(order!.passengerSnapshots).toEqual(passengerSnapshots);
- }
- });
- });
- describe('订单状态测试', () => {
- it('新创建的订单应该处于待支付状态', async () => {
- const orderData = {
- routeId: testRoute.id,
- passengerCount: 1,
- totalAmount: testRoute.price,
- passengerSnapshots: [],
- routeSnapshot: {
- id: testRoute.id,
- name: testRoute.name,
- pickupPoint: testRoute.pickupPoint,
- dropoffPoint: testRoute.dropoffPoint,
- departureTime: testRoute.departureTime,
- price: testRoute.price,
- vehicleType: testRoute.vehicleType,
- travelMode: testRoute.travelMode
- }
- };
- const response = await client.orders.$post({
- json: orderData,
- },
- {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const responseData = await response.json();
- expect(responseData.status).toBe(OrderStatus.PENDING_PAYMENT);
- expect(responseData.paymentStatus).toBe(PaymentStatus.PENDING);
- }
- });
- });
- });
|