From 3fe0787a5c7b69dbf1479eca6e356a28c457a010 Mon Sep 17 00:00:00 2001 From: aelassas Date: Mon, 18 Nov 2024 13:08:09 +0100 Subject: [PATCH] Update bookingController.ts and booking.test.ts --- api/src/controllers/bookingController.ts | 4 +- api/tests/booking.test.ts | 114 ++++++++++++++++++++++- api/tests/testHelper.ts | 11 +++ packages/bookcars-types/index.ts | 2 +- 4 files changed, 126 insertions(+), 5 deletions(-) diff --git a/api/src/controllers/bookingController.ts b/api/src/controllers/bookingController.ts index 437383de2..46c167bd4 100644 --- a/api/src/controllers/bookingController.ts +++ b/api/src/controllers/bookingController.ts @@ -107,7 +107,7 @@ export const notify = async (driver: env.User, bookingId: string, user: env.User * @param {boolean} payLater * @returns {unknown} */ -export const confirm = async (user: env.User, supplier: env.User, booking: env.Booking, payLater: boolean = false) => { +export const confirm = async (user: env.User, supplier: env.User, booking: env.Booking, payLater: boolean) => { const { language } = user const locale = language === 'fr' ? 'fr-FR' : 'en-US' const options: Intl.DateTimeFormatOptions = { @@ -140,7 +140,7 @@ export const confirm = async (user: env.User, supplier: env.User, booking: env.B const dropOffLocationName = dropOffLocation.values.filter((value) => value.language === language)[0].value let contractFile: string | null = null - if (supplier.contracts) { + if (supplier.contracts && supplier.contracts.length > 0) { contractFile = supplier.contracts.find((c) => c.language === user.language)?.file || null if (!contractFile) { contractFile = supplier.contracts.find((c) => c.language === 'en')?.file || null diff --git a/api/tests/booking.test.ts b/api/tests/booking.test.ts index e941e38fe..622232039 100644 --- a/api/tests/booking.test.ts +++ b/api/tests/booking.test.ts @@ -240,6 +240,78 @@ describe('POST /api/checkout', () => { bookings = await Booking.find({ driver: DRIVER1_ID }) expect(bookings.length).toBeGreaterThan(1) + // test success (driver.enableEmailNotifications disabled) + let driver = await User.findById(DRIVER1_ID) + driver!.enableEmailNotifications = false + await driver!.save() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(200) + driver!.enableEmailNotifications = true + await driver!.save() + + // test success (supplier.enableEmailNotifications disabled) + let supplier = await User.findById(SUPPLIER_ID) + supplier!.enableEmailNotifications = false + await supplier!.save() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(200) + supplier!.enableEmailNotifications = true + await supplier!.save() + + // test success (without contract) + supplier = await User.findById(SUPPLIER_ID) + let { contracts } = (supplier!) + supplier!.contracts = undefined + await supplier!.save() + await driver!.save() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(200) + supplier!.contracts = contracts + await supplier!.save() + + // test success (with contract file not found) + supplier = await User.findById(SUPPLIER_ID) + contracts = supplier!.contracts + supplier!.contracts = [{ language: 'en', file: `${nanoid()}.pdf` }] + await supplier!.save() + await driver!.save() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(200) + supplier!.contracts = contracts + await supplier!.save() + + // test success (with contract file null) + supplier = await User.findById(SUPPLIER_ID) + contracts = supplier!.contracts + supplier!.contracts = [{ language: 'en', file: null }] + await supplier!.save() + await driver!.save() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(200) + supplier!.contracts = contracts + await supplier!.save() + + // test success (with contract fr language) + driver = await User.findById(DRIVER1_ID) + driver!.language = 'fr' + await driver!.save() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(200) + driver!.language = 'en' + await driver!.save() + // test failure (stripe payment failed) payload.payLater = false const receiptEmail = testHelper.GetRandomEmail() @@ -270,7 +342,7 @@ describe('POST /api/checkout', () => { await stripeAPI.paymentIntents.confirm(paymentIntentId, { payment_method: 'pm_card_visa', }) - const driver = await User.findOne({ _id: DRIVER1_ID }) + driver = await User.findOne({ _id: DRIVER1_ID }) driver!.language = 'fr' await driver?.save() res = await request(app) @@ -280,6 +352,15 @@ describe('POST /api/checkout', () => { expect(res.statusCode).toBe(200) bookings = await Booking.find({ driver: DRIVER1_ID }) expect(bookings.length).toBeGreaterThan(2) + + // test failure (car not found) + const carId = payload.booking!.car + payload.booking!.car = testHelper.GetRandromObjectIdAsString() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(400) + payload.booking!.car = carId } catch (err) { console.error(err) } finally { @@ -301,7 +382,17 @@ describe('POST /api/checkout', () => { const booking = await Booking.findById(bookingId) expect(booking?.status).toBe(bookcarsTypes.BookingStatus.Void) expect(booking?.sessionId).toBe(payload.sessionId) - payload.payLater = true + + // test success (checkout session driver not verified) + driver = await User.findById(DRIVER1_ID) + driver!.verified = false + await driver!.save() + res = await request(app) + .post('/api/checkout') + .send(payload) + expect(res.statusCode).toBe(200) + driver!.verified = true + await driver!.save() // test success (checkout session with no additional driver) payload.booking!.additionalDriver = false @@ -313,6 +404,7 @@ describe('POST /api/checkout', () => { expect(bookings.length).toBeGreaterThan(3) payload.booking!.additionalDriver = true + payload.payLater = true payload.driver = { fullName: 'driver', email: testHelper.GetRandomEmail(), @@ -430,6 +522,19 @@ describe('POST /api/update-booking', () => { expect(additionalDriver).not.toBeNull() expect(additionalDriver?.fullName).toBe(ADDITIONAL_DRIVER.fullName) + // test success (enableEmailNotifications disabled) + payload.booking.status = bookcarsTypes.BookingStatus.Reserved + const driver = await User.findById(DRIVER1_ID) + driver!.enableEmailNotifications = false + await driver!.save() + res = await request(app) + .put('/api/update-booking') + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + expect(res.statusCode).toBe(200) + driver!.enableEmailNotifications = true + await driver!.save() + const booking = await Booking.findById(BOOKING_ID) expect(booking).not.toBeNull() booking!._additionalDriver = testHelper.GetRandromObjectId() @@ -774,6 +879,11 @@ describe('DELETE /api/delete-temp-booking', () => { const _driver = await User.findById(driver._id) expect(_driver).toBeNull() + // test booking not found + res = await request(app) + .delete(`/api/delete-temp-booking/${testHelper.GetRandromObjectIdAsString()}/${sessionId}`) + expect(res.statusCode).toBe(200) + // // Test failure // diff --git a/api/tests/testHelper.ts b/api/tests/testHelper.ts index 5fd4b0e8e..40ed50b11 100644 --- a/api/tests/testHelper.ts +++ b/api/tests/testHelper.ts @@ -53,6 +53,17 @@ export const initialize = async () => { expect(admin.id).toBeDefined() ADMIN_USER_ID = admin.id + const adminFromEnv = env.ADMIN_EMAIL && await User.findOne({ email: env.ADMIN_EMAIL }) + if (!adminFromEnv) { + await (new User({ + fullName: 'admin', + email: env.ADMIN_EMAIL, + language: LANGUAGE, + password: passwordHash, + type: bookcarsTypes.UserType.Admin, + })).save() + } + // user const user = new User({ fullName: USER_FULL_NAME, diff --git a/packages/bookcars-types/index.ts b/packages/bookcars-types/index.ts index 2eb6fb6a0..26779d230 100644 --- a/packages/bookcars-types/index.ts +++ b/packages/bookcars-types/index.ts @@ -99,7 +99,7 @@ export interface CheckoutPayload { driver?: User booking?: Booking additionalDriver?: AdditionalDriver - payLater?: boolean + payLater: boolean sessionId?: string paymentIntentId?: string customerId?: string