From f5fe073721f74e077a9a9fc0413b999793942ef2 Mon Sep 17 00:00:00 2001 From: aelassas Date: Sun, 17 Nov 2024 22:02:37 +0100 Subject: [PATCH] Update locationController.ts and location.test.ts --- api/src/controllers/locationController.ts | 22 +++--- api/tests/location.test.ts | 96 +++++++++++++++++++---- 2 files changed, 91 insertions(+), 27 deletions(-) diff --git a/api/src/controllers/locationController.ts b/api/src/controllers/locationController.ts index 4464895f0..03f29084b 100644 --- a/api/src/controllers/locationController.ts +++ b/api/src/controllers/locationController.ts @@ -89,8 +89,7 @@ export const create = async (req: Request, res: Response) => { const _image = path.join(env.CDN_TEMP_LOCATIONS, image) if (!await helper.exists(_image)) { - logger.error(i18n.t('LOCATION_IMAGE_NOT_FOUND'), body) - return res.status(400).send(i18n.t('LOCATION_IMAGE_NOT_FOUND')) + throw new Error(`Location image not found: ${_image}`) } } @@ -124,14 +123,12 @@ export const create = async (req: Request, res: Response) => { if (image) { const _image = path.join(env.CDN_TEMP_LOCATIONS, image) - if (await helper.exists(_image)) { - const filename = `${location._id}_${Date.now()}${path.extname(image)}` - const newPath = path.join(env.CDN_LOCATIONS, filename) + const filename = `${location._id}_${Date.now()}${path.extname(image)}` + const newPath = path.join(env.CDN_LOCATIONS, filename) - await fs.rename(_image, newPath) - location.image = filename - await location.save() - } + await fs.rename(_image, newPath) + location.image = filename + await location.save() } return res.send(location) @@ -284,7 +281,7 @@ export const deleteLocation = async (req: Request, res: Response) => { await LocationValue.deleteMany({ _id: { $in: location.values } }) await Location.deleteOne({ _id: id }) - if (location.parkingSpots) { + if (location.parkingSpots && location.parkingSpots.length > 0) { const values = location.parkingSpots.map((ps) => ps.values).flat() await LocationValue.deleteMany({ _id: { $in: values } }) const parkingSpots = location.parkingSpots.map((ps) => ps._id) @@ -342,7 +339,7 @@ export const getLocation = async (req: Request, res: Response) => { const countryName = ((location.country as env.CountryInfo).values as env.LocationValue[]).filter((value) => value.language === req.params.language)[0].value location.country.name = countryName } - if (location.parkingSpots) { + if (location.parkingSpots && location.parkingSpots.length > 0) { for (const parkingSpot of location.parkingSpots) { parkingSpot.name = (parkingSpot.values as env.LocationValue[]).filter((value) => value.language === req.params.language)[0].value } @@ -706,6 +703,9 @@ export const deleteTempImage = async (req: Request, res: Response) => { const { image } = req.params try { + if (!image.includes('.')) { + throw new Error('Filename not valid') + } const imageFile = path.join(env.CDN_TEMP_LOCATIONS, image) if (await helper.exists(imageFile)) { await fs.unlink(imageFile) diff --git a/api/tests/location.test.ts b/api/tests/location.test.ts index 919f48f18..64b153776 100644 --- a/api/tests/location.test.ts +++ b/api/tests/location.test.ts @@ -126,18 +126,6 @@ describe('POST /api/create-location', () => { latitude: 28.0268755, longitude: 1.6528399999999976, image: 'unknown.jpg', - parkingSpots: [ - { - latitude: 28.1268755, - longitude: 1.752839999999997, - values: [{ language: 'en', value: 'Parking spot 1' }, { language: 'fr', value: 'Parking 1' }], - }, - { - latitude: 28.2268755, - longitude: 1.8528399999999976, - values: [{ language: 'en', value: 'Parking spot 2' }, { language: 'fr', value: 'Parking 2' }], - }, - ], } // image not found @@ -147,7 +135,33 @@ describe('POST /api/create-location', () => { .send(payload) expect(res.statusCode).toBe(400) - // image found + // no image + payload.image = undefined + res = await request(app) + .post('/api/create-location') + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + expect(res.statusCode).toBe(200) + expect(res.body._id).toBeTruthy() + await Location.deleteOne({ _id: res.body._id }) + + // image found and parkingspots + payload.parkingSpots = [ + { + latitude: 28.1268755, + longitude: 1.752839999999997, + values: [{ language: 'en', value: 'Parking spot 1' }, { language: 'fr', value: 'Parking 1' }], + }, + { + latitude: 28.2268755, + longitude: 1.8528399999999976, + values: [{ language: 'en', value: 'Parking spot 2' }, { language: 'fr', value: 'Parking 2' }], + }, + { + latitude: 27.2268755, + longitude: 12.852839999999997, + }, + ] const tempImage = path.join(env.CDN_TEMP_LOCATIONS, IMAGE0) if (!await helper.exists(tempImage)) { await fs.copyFile(IMAGE0_PATH, tempImage) @@ -162,7 +176,7 @@ describe('POST /api/create-location', () => { expect(res.body?.values?.length).toBe(2) expect(res.body?.latitude).toBe(payload.latitude) expect(res.body?.longitude).toBe(payload.longitude) - expect(res.body?.parkingSpots?.length).toBe(2) + expect(res.body?.parkingSpots?.length).toBe(3) LOCATION_ID = res.body?._id // no payload @@ -204,7 +218,7 @@ describe('PUT /api/update-location/:id', () => { }, }) .lean() - expect(location?.parkingSpots.length).toBe(2) + expect(location?.parkingSpots.length).toBe(3) const parkingSpot2 = (location!.parkingSpots[1]) as unknown as FlattenMaps expect(parkingSpot2.values!.length).toBe(2) @@ -344,12 +358,22 @@ describe('POST /api/update-location-image/:id', () => { .attach('image', IMAGE2_PATH) expect(res.statusCode).toBe(200) const filename = res.body as string - const imageExists = await helper.exists(path.join(env.CDN_LOCATIONS, filename)) + const imageFile = path.join(env.CDN_LOCATIONS, filename) + const imageExists = await helper.exists(imageFile) expect(imageExists).toBeTruthy() const location = await Location.findById(LOCATION_ID) expect(location).not.toBeNull() expect(location?.image).toBe(filename) + await fs.unlink(imageFile) + location!.image = undefined + await location?.save() + res = await request(app) + .post(`/api/update-location-image/${LOCATION_ID}`) + .set(env.X_ACCESS_TOKEN, token) + .attach('image', IMAGE2_PATH) + expect(res.statusCode).toBe(200) + location!.image = `${nanoid()}.jpg` await location?.save() res = await request(app) @@ -407,6 +431,19 @@ describe('POST /api/delete-location-image/:id', () => { location = await Location.findById(LOCATION_ID) expect(location?.image).toBeNull() + res = await request(app) + .post(`/api/delete-location-image/${LOCATION_ID}`) + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(200) + + location = await Location.findById(LOCATION_ID) + location!.image = `${nanoid()}.jpg` + await location!.save() + res = await request(app) + .post(`/api/delete-location-image/${LOCATION_ID}`) + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(200) + res = await request(app) .post(`/api/delete-location-image/${testHelper.GetRandromObjectIdAsString()}`) .set(env.X_ACCESS_TOKEN, token) @@ -441,6 +478,11 @@ describe('POST /api/delete-temp-location-image/:image', () => { .set(env.X_ACCESS_TOKEN, token) expect(res.statusCode).toBe(200) + res = await request(app) + .post('/api/delete-temp-location-image/unknown') + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(400) + await testHelper.signout(token) }) }) @@ -454,6 +496,13 @@ describe('GET /api/location/:id/:language', () => { expect(res.statusCode).toBe(200) expect(res.body?.name).toBe(LOCATION_NAMES.filter((v) => v.language === language)[0].name) + const locationId = await testHelper.createLocation('loc1-en', 'loc1-fr') + res = await request(app) + .get(`/api/location/${locationId}/${language}`) + expect(res.statusCode).toBe(200) + expect(res.body?.name).toBe('loc1-en') + await Location.deleteOne({ _id: locationId }) + res = await request(app) .get(`/api/location/${testHelper.GetRandromObjectIdAsString()}/${language}`) expect(res.statusCode).toBe(204) @@ -573,6 +622,21 @@ describe('DELETE /api/delete-location/:id', () => { location = await Location.findById(LOCATION_ID) expect(location).toBeNull() + let locationId = await testHelper.createLocation('loc1-en', 'loc1-fr') + res = await request(app) + .delete(`/api/delete-location/${locationId}`) + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(200) + + locationId = await testHelper.createLocation('loc2-en', 'loc2-fr') + location = await Location.findById(locationId) + location!.image = `${nanoid()}.jpg` + await location!.save() + res = await request(app) + .delete(`/api/delete-location/${locationId}`) + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(200) + res = await request(app) .delete(`/api/delete-location/${LOCATION_ID}`) .set(env.X_ACCESS_TOKEN, token)