Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Nov 12, 2024
1 parent f445ee5 commit 5807997
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 38 deletions.
12 changes: 6 additions & 6 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ app.use('/', countryRoutes)

i18n.locale = env.DEFAULT_LANGUAGE

helper.mkdir(env.CDN_USERS)
helper.mkdir(env.CDN_TEMP_USERS)
helper.mkdir(env.CDN_CARS)
helper.mkdir(env.CDN_TEMP_CARS)
helper.mkdir(env.CDN_LOCATIONS)
helper.mkdir(env.CDN_TEMP_LOCATIONS)
await helper.mkdir(env.CDN_USERS)
await helper.mkdir(env.CDN_TEMP_USERS)
await helper.mkdir(env.CDN_CARS)
await helper.mkdir(env.CDN_TEMP_CARS)
await helper.mkdir(env.CDN_LOCATIONS)
await helper.mkdir(env.CDN_TEMP_LOCATIONS)

export default app
2 changes: 1 addition & 1 deletion api/src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export const BACKEND_HOST = __env__('BC_BACKEND_HOST', true)
export const FRONTEND_HOST = __env__('BC_FRONTEND_HOST', true)

/**
* Default language. Default is en. Available options: en, fr.
* Default language. Default is en. Available options: en, fr, es.
*
* @type {string}
*/
Expand Down
9 changes: 2 additions & 7 deletions api/src/controllers/bookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,25 +969,20 @@ export const cancelBooking = async (req: Request, res: Response) => {
booking.cancelRequest = true
await booking.save()

// Notify supplier
await notify(booking.driver, booking._id.toString(), booking.supplier, i18n.t('CANCEL_BOOKING_NOTIFICATION'))

// Notify supplier
const supplier = await User.findById(booking.supplier)
if (!supplier) {
logger.info(`Supplier ${booking.supplier} not found`)
return res.sendStatus(204)
}
i18n.locale = supplier.language
let message = i18n.t('CANCEL_BOOKING_NOTIFICATION')
await notify(booking.driver, booking._id.toString(), supplier, message)
await notify(booking.driver, booking.id, supplier, i18n.t('CANCEL_BOOKING_NOTIFICATION'))

// Notify admin
const admin = !!env.ADMIN_EMAIL && await User.findOne({ email: env.ADMIN_EMAIL, type: bookcarsTypes.UserType.Admin })
if (admin) {
i18n.locale = admin.language
message = i18n.t('CANCEL_BOOKING_NOTIFICATION')
await notify(booking.driver, booking._id.toString(), admin, message)
await notify(booking.driver, booking.id, admin, i18n.t('CANCEL_BOOKING_NOTIFICATION'))
}

return res.sendStatus(200)
Expand Down
3 changes: 3 additions & 0 deletions api/src/controllers/countryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ export const getCountryId = async (req: Request, res: Response) => {
const { name, language } = req.params

try {
if (language.length !== 2) {
throw new Error('Language not valid')
}
const lv = await LocationValue.findOne({ language, value: { $regex: new RegExp(`^${escapeStringRegexp(helper.trim(name, ' '))}$`, 'i') } })
if (lv) {
const country = await Country.findOne({ values: lv.id })
Expand Down
9 changes: 9 additions & 0 deletions api/src/controllers/locationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ export const getLocations = async (req: Request, res: Response) => {
export const getLocationsWithPosition = async (req: Request, res: Response) => {
try {
const { language } = req.params
if (language.length !== 2) {
throw new Error('Language not valid')
}
const keyword = escapeStringRegexp(String(req.query.s || ''))
const options = 'i'

Expand Down Expand Up @@ -563,6 +566,9 @@ export const getLocationId = async (req: Request, res: Response) => {
const { name, language } = req.params

try {
if (language.length !== 2) {
throw new Error('Language not valid')
}
const lv = await LocationValue.findOne({ language, value: { $regex: new RegExp(`^${escapeStringRegexp(helper.trim(name, ' '))}$`, 'i') } })
if (lv) {
const location = await Location.findOne({ values: lv.id })
Expand Down Expand Up @@ -662,6 +668,9 @@ export const deleteImage = async (req: Request, res: Response) => {
const { id } = req.params

try {
if (!helper.isValidObjectId(id)) {
throw new Error('Location Id not valid')
}
const location = await Location.findById(id)

if (location) {
Expand Down
3 changes: 3 additions & 0 deletions api/src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,9 @@ export const sendEmail = async (req: Request, res: Response) => {
export const hasPassword = async (req: Request, res: Response) => {
const { id } = req.params
try {
if (!helper.isValidObjectId(id)) {
throw new Error('User id not valid')
}
const passwordExists = await User.exists({ _id: id, password: { $ne: null } })

if (passwordExists) {
Expand Down
13 changes: 13 additions & 0 deletions api/tests/booking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,19 @@ describe('POST /api/cancel-booking/:id', () => {
booking = await Booking.findById(BOOKING_ID)
expect(booking?.cancelRequest).toBeTruthy()

// test failure (supplier not found)
booking = await Booking.findById(BOOKING_ID)
booking!.cancelRequest = false
const supplierId = booking!.supplier
booking!.supplier = testHelper.GetRandromObjectId()
await booking!.save()
res = await request(app)
.post(`/api/cancel-booking/${BOOKING_ID}`)
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(204)
booking!.supplier = supplierId
await booking!.save()

res = await request(app)
.post(`/api/cancel-booking/${testHelper.GetRandromObjectIdAsString()}`)
.set(env.X_ACCESS_TOKEN, token)
Expand Down
10 changes: 5 additions & 5 deletions api/tests/car.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('POST /api/create-car', () => {
available: false,
type: bookcarsTypes.CarType.Diesel,
gearbox: bookcarsTypes.GearboxType.Automatic,
aircon: true,
aircon: false,
image: IMAGE1,
seats: 5,
doors: 4,
Expand Down Expand Up @@ -153,7 +153,7 @@ describe('PUT /api/update-car', () => {
available: true,
type: bookcarsTypes.CarType.Gasoline,
gearbox: bookcarsTypes.GearboxType.Manual,
aircon: false,
aircon: true,
seats: 6,
doors: 5,
fuelPolicy: bookcarsTypes.FuelPolicy.LikeForLike,
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('PUT /api/update-car', () => {
expect(car.available).toBeTruthy()
expect(car.type).toBe(bookcarsTypes.CarType.Gasoline)
expect(car.gearbox).toBe(bookcarsTypes.GearboxType.Manual)
expect(car.aircon).toBe(false)
expect(car.aircon).toBeTruthy()
expect(car.seats).toBe(6)
expect(car.doors).toBe(5)
expect(car.fuelPolicy).toBe(bookcarsTypes.FuelPolicy.LikeForLike)
Expand Down Expand Up @@ -398,7 +398,7 @@ describe('POST /api/cars/:page/:size', () => {
rating: 4,
seats: 6,
carSpecs: {
aircon: false,
aircon: true,
moreThanFiveSeats: true,
moreThanFourDoors: true,
},
Expand Down Expand Up @@ -544,7 +544,7 @@ describe('POST /api/frontend-cars/:page/:size', () => {
rating: 4,
seats: 6,
carSpecs: {
aircon: false,
aircon: true,
moreThanFiveSeats: true,
moreThanFourDoors: true,
},
Expand Down
13 changes: 13 additions & 0 deletions api/tests/country.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ describe('GET /api/countries-with-locations/:language/:imageRequired/:minLocatio
.get(`/api/countries-with-locations/${language}/false/1`)
expect(res.statusCode).toBe(200)
expect(res.body.find((country: bookcarsTypes.Country) => country._id === COUNTRY_ID)).toBeUndefined()

// test failure
await databaseHelper.close()
res = await request(app)
.get(`/api/countries-with-locations/${language}/false/1`)
expect(res.statusCode).toBe(400)
const connRes = await databaseHelper.connect(env.DB_URI, false, false)
expect(connRes).toBeTruthy()
})
})

Expand All @@ -268,6 +276,11 @@ describe('GET /api/country-id/:name/:language', () => {
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(204)

res = await request(app)
.get('/api/country-id/unknown/english')
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(400)

await testHelper.signout(token)
})
})
Expand Down
6 changes: 3 additions & 3 deletions api/tests/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Test database initialization', () => {

const lv1 = new LocationValue({ language: 'en', value: 'location' })
await lv1.save()
const lv2 = new LocationValue({ language: 'es', value: 'localización' })
const lv2 = new LocationValue({ language: 'pt', value: 'localização' })
await lv2.save()
const l1 = new Location({ country: testHelper.GetRandromObjectIdAsString(), values: [lv1.id, lv2.id] })
await l1.save()
Expand All @@ -42,7 +42,7 @@ describe('Test database initialization', () => {

const cv1 = new LocationValue({ language: 'en', value: 'country' })
await cv1.save()
const cv2 = new LocationValue({ language: 'es', value: 'país' })
const cv2 = new LocationValue({ language: 'pt', value: 'país' })
await cv2.save()
const c1 = new Country({ values: [cv1.id, cv2.id] })
await c1.save()
Expand All @@ -51,7 +51,7 @@ describe('Test database initialization', () => {

const pv1 = new LocationValue({ language: 'en', value: 'parking' })
await pv1.save()
const pv2 = new LocationValue({ language: 'es', value: 'aparcamiento' })
const pv2 = new LocationValue({ language: 'pt', value: 'estacionamento' })
await pv2.save()
const ps1 = new ParkingSpot({ latitude: 1, longitude: 1, values: [pv1.id, pv2.id] })
await ps1.save()
Expand Down
7 changes: 7 additions & 0 deletions api/tests/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ describe('Test trim', () => {
expect(helper.trim(' xxxxxxxx ', ' ')).toBe('xxxxxxxx')
})
})

describe('Test getStripeLocale', () => {
it('should test getStripeLocale', () => {
expect(helper.getStripeLocale('en')).toBe('en')
expect(helper.getStripeLocale('')).toBe('auto')
})
})
18 changes: 17 additions & 1 deletion api/tests/location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ describe('PUT /api/update-location/:id', () => {
expect(res.body?.longitude).toBe(payload.longitude)
expect(res.body?.parkingSpots.length).toBe(0)

const loc = await Location.findById(LOCATION_ID)
loc!.parkingSpots = null
await loc!.save()
payload.parkingSpots = [
{
latitude: 28.1268755,
Expand Down Expand Up @@ -300,6 +303,10 @@ describe('GET /api/location-id/:name/:language', () => {
res = await request(app)
.get(`/api/location-id/unknown/${language}`)
expect(res.statusCode).toBe(204)

res = await request(app)
.get('/api/location-id/unknown/english')
expect(res.statusCode).toBe(400)
})
})

Expand Down Expand Up @@ -405,6 +412,11 @@ describe('POST /api/delete-location-image/:id', () => {
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(204)

res = await request(app)
.post('/api/delete-location-image/invalid-id')
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(400)

await testHelper.signout(token)
})
})
Expand Down Expand Up @@ -477,9 +489,13 @@ describe('GET /api/locations-with-position/:language', () => {
expect(res.body.length).toBeGreaterThanOrEqual(1)

res = await request(app)
.get('/api/locations-with-position/unknown')
.get('/api/locations-with-position/pt')
expect(res.statusCode).toBe(200)
expect(res.body.length).toBe(0)

res = await request(app)
.get('/api/locations-with-position/english')
expect(res.statusCode).toBe(400)
})
})

Expand Down
2 changes: 1 addition & 1 deletion api/tests/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dotenv/config'
import * as logger from '../src/common/logger'

describe('Tes logging', () => {
describe('Test logging', () => {
it('should test logging', () => {
let res = true
try {
Expand Down
Loading

0 comments on commit 5807997

Please sign in to comment.