diff --git a/api/coverage/cobertura-coverage.xml b/api/coverage/cobertura-coverage.xml index bd40d915..f5dd75d6 100644 --- a/api/coverage/cobertura-coverage.xml +++ b/api/coverage/cobertura-coverage.xml @@ -1,6 +1,6 @@ - + C:\dev\movinin\src\api @@ -49,7 +49,7 @@ - + @@ -79,87 +79,93 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + - - - + + - - - - - - - - + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + + + @@ -289,7 +295,7 @@ - + @@ -318,9 +324,9 @@ - + - + @@ -328,9 +334,9 @@ - + - + @@ -406,8 +412,8 @@ - - + + @@ -415,8 +421,8 @@ - - + + @@ -734,21 +740,21 @@ - + - + - + - + - + - + - + @@ -756,61 +762,61 @@ - + - + - + - + - + - + - + - + - + - + - - - - + + + + - - - - - - - + + + + + + + - - - - - - - - + + + + + + + + @@ -822,11 +828,11 @@ - - - - - + + + + + @@ -834,38 +840,38 @@ - - - - - - - + + + + + + + - - - - - - - - - + + + + + + + + + - - - - - + + + + + - - + + @@ -1896,44 +1902,44 @@ - + - + - + - + - + - + - + - + - + - + - - - - + + + + - + @@ -2249,84 +2255,89 @@ - + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -2352,63 +2363,64 @@ - - - - + + + - - - + + + - - - - - + + + + + - - - - + + + + + - - - + + - - + + - - + + + - - - + + + - + - - - - + + + + - - + + - - - - + + + + + diff --git a/api/src/common/Helper.ts b/api/src/common/Helper.ts index 80c5a525..66a2691e 100644 --- a/api/src/common/Helper.ts +++ b/api/src/common/Helper.ts @@ -1,6 +1,7 @@ import fs from 'node:fs/promises' import path from 'node:path' import { Request } from 'express' +import mongoose from 'mongoose' import * as env from '../config/env.config' /** @@ -144,3 +145,13 @@ export const getAuthCookieName = (req: Request): string => { // Mobile app and unit tests auth header name return env.X_ACCESS_TOKEN } + +/** + * Check ObjectId + * + * @param {string} id + * @returns {boolean} + */ +export function isValidObjectId(id?: string) { + return mongoose.isValidObjectId(id) +} diff --git a/api/src/controllers/bookingController.ts b/api/src/controllers/bookingController.ts index 23f0e617..d90754d5 100644 --- a/api/src/controllers/bookingController.ts +++ b/api/src/controllers/bookingController.ts @@ -545,7 +545,7 @@ export async function getBookings(req: Request, res: Response) { $match.$and.push({ to: { $lte: to } }) } // $to < to if (keyword) { - const isObjectId = mongoose.isValidObjectId(keyword) + const isObjectId = Helper.isValidObjectId(keyword) if (isObjectId) { $match.$and.push({ _id: { $eq: new mongoose.Types.ObjectId(keyword) }, diff --git a/api/tests/TestHelper.ts b/api/tests/TestHelper.ts index 0f20deee..5b924127 100644 --- a/api/tests/TestHelper.ts +++ b/api/tests/TestHelper.ts @@ -32,7 +32,7 @@ export const SIZE = 30 let ADMIN_USER_ID: string let USER_ID: string -export async function initializeDatabase() { +export async function initialize() { const salt = await bcrypt.genSalt(10) const passwordHash = await bcrypt.hash(PASSWORD, salt) const body = { @@ -54,6 +54,8 @@ export async function initializeDatabase() { await user.save() expect(user.id).toBeDefined() USER_ID = user.id + + console.error = () => { } } export function getAdminUserId() { @@ -64,7 +66,7 @@ export function getUserId() { return USER_ID } -export async function clearDatabase() { +export async function close() { const res = await User.deleteMany({ email: { $in: [ADMIN_EMAIL, USER_EMAIL] } }) expect(res.deletedCount).toBe(2) await Notification.deleteMany({ user: { $in: [ADMIN_USER_ID, USER_ID] } }) diff --git a/api/tests/agency.test.ts b/api/tests/agency.test.ts index 757861c1..62eb1fed 100644 --- a/api/tests/agency.test.ts +++ b/api/tests/agency.test.ts @@ -24,7 +24,7 @@ let AGENCY1_NAME: string // beforeAll(async () => { if (await DatabaseHelper.Connect(false)) { - await TestHelper.initializeDatabase() + await TestHelper.initialize() // create two agencies AGENCY1_NAME = TestHelper.getAgencyName() @@ -38,7 +38,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - await TestHelper.clearDatabase() + await TestHelper.close() // delete agencies await TestHelper.deleteAgency(AGENCY1_ID) diff --git a/api/tests/booking.test.ts b/api/tests/booking.test.ts index 09a429e6..044467e8 100644 --- a/api/tests/booking.test.ts +++ b/api/tests/booking.test.ts @@ -24,7 +24,7 @@ let BOOKING_ID: string // beforeAll(async () => { if (await DatabaseHelper.Connect(false)) { - await TestHelper.initializeDatabase() + await TestHelper.initialize() // create a supplier const supplierName = TestHelper.getAgencyName() @@ -78,7 +78,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - await TestHelper.clearDatabase() + await TestHelper.close() // delete the supplier await TestHelper.deleteAgency(AGENCY_ID) diff --git a/api/tests/location.test.ts b/api/tests/location.test.ts index 8c044837..4e0df7a9 100644 --- a/api/tests/location.test.ts +++ b/api/tests/location.test.ts @@ -28,7 +28,7 @@ let LOCATION_NAMES: movininTypes.LocationName[] = [ // beforeAll(async () => { if (await DatabaseHelper.Connect(false)) { - await TestHelper.initializeDatabase() + await TestHelper.initialize() } }) @@ -36,7 +36,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - await TestHelper.clearDatabase() + await TestHelper.close() await DatabaseHelper.Close(false) }) @@ -50,33 +50,31 @@ describe('POST /api/validate-location', () => { const language = TestHelper.LANGUAGE const name = uuid() - const locationValue = new LocationValue({ language, value: name }) await locationValue.save() - const payload: movininTypes.ValidateLocationPayload = { language, name, } - let res = await request(app) .post('/api/validate-location') .set(env.X_ACCESS_TOKEN, token) .send(payload) - expect(res.statusCode).toBe(204) payload.name = uuid() - res = await request(app) .post('/api/validate-location') .set(env.X_ACCESS_TOKEN, token) .send(payload) - expect(res.statusCode).toBe(200) - await LocationValue.deleteOne({ _id: locationValue._id }) + res = await request(app) + .post('/api/validate-location') + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(400) + await TestHelper.signout(token) }) }) @@ -86,16 +84,19 @@ describe('POST /api/create-location', () => { const token = await TestHelper.signinAsAdmin() const payload: movininTypes.LocationName[] = LOCATION_NAMES - - const res = await request(app) + let res = await request(app) .post('/api/create-location') .set(env.X_ACCESS_TOKEN, token) .send(payload) - expect(res.statusCode).toBe(200) expect(res.body?.values?.length).toBe(2) LOCATION_ID = res.body?._id + res = await request(app) + .post('/api/create-location') + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(400) + await TestHelper.signout(token) }) }) @@ -118,7 +119,6 @@ describe('PUT /api/update-location/:id', () => { name: uuid(), }, ] - let res = await request(app) .put(`/api/update-location/${LOCATION_ID}`) .set(env.X_ACCESS_TOKEN, token) @@ -132,6 +132,11 @@ describe('PUT /api/update-location/:id', () => { .send(LOCATION_NAMES) expect(res.statusCode).toBe(204) + res = await request(app) + .put(`/api/update-location/${LOCATION_ID}`) + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(400) + await TestHelper.signout(token) }) }) @@ -148,6 +153,10 @@ describe('GET /api/location/:id/:language', () => { res = await request(app) .get(`/api/location/${TestHelper.GetRandromObjectIdAsString()}/${language}`) expect(res.statusCode).toBe(204) + + res = await request(app) + .get(`/api/location/${LOCATION_ID}/zh`) + expect(res.statusCode).toBe(400) }) }) @@ -155,11 +164,14 @@ describe('GET /api/locations/:page/:size/:language', () => { it('should get locations', async () => { const language = 'en' - const res = await request(app) + let res = await request(app) .get(`/api/locations/${TestHelper.PAGE}/${TestHelper.SIZE}/${language}?s=${LOCATION_NAMES[0].name}`) - expect(res.statusCode).toBe(200) expect(res.body.length).toBe(1) + + res = await request(app) + .get(`/api/locations/unknown/${TestHelper.SIZE}/${language}`) + expect(res.statusCode).toBe(400) }) }) @@ -204,9 +216,13 @@ describe('GET /api/check-location/:id', () => { res = await request(app) .get(`/api/check-location/${LOCATION_ID}`) .set(env.X_ACCESS_TOKEN, token) - expect(res.statusCode).toBe(204) + res = await request(app) + .get(`/api/check-location/${uuid()}`) + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(400) + await TestHelper.signout(token) }) }) @@ -230,6 +246,11 @@ describe('DELETE /api/delete-location/:id', () => { .set(env.X_ACCESS_TOKEN, token) expect(res.statusCode).toBe(204) + res = await request(app) + .delete('/api/delete-location/0') + .set(env.X_ACCESS_TOKEN, token) + expect(res.statusCode).toBe(400) + await TestHelper.signout(token) }) }) diff --git a/api/tests/notification.test.ts b/api/tests/notification.test.ts index 6ef17d3c..9db6c859 100644 --- a/api/tests/notification.test.ts +++ b/api/tests/notification.test.ts @@ -17,7 +17,7 @@ let NOTIFICATION2_ID: string // beforeAll(async () => { if (await DatabaseHelper.Connect(false)) { - await TestHelper.initializeDatabase() + await TestHelper.initialize() ADMIN_USER_ID = TestHelper.getAdminUserId() const agencyName = TestHelper.getAgencyName() AGENCY_ID = await TestHelper.createAgency(`${agencyName}@test.bookcars.ma`, agencyName) @@ -38,7 +38,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - await TestHelper.clearDatabase() + await TestHelper.close() await TestHelper.deleteAgency(AGENCY_ID) diff --git a/api/tests/property.test.ts b/api/tests/property.test.ts index ce6b9d90..6402bf3c 100644 --- a/api/tests/property.test.ts +++ b/api/tests/property.test.ts @@ -39,7 +39,7 @@ let PROPERTY_ID: string // beforeAll(async () => { if (await DatabaseHelper.Connect(false)) { - await TestHelper.initializeDatabase() + await TestHelper.initialize() // create two agencies const agencyName1 = TestHelper.getAgencyName() @@ -57,7 +57,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - await TestHelper.clearDatabase() + await TestHelper.close() // delete agencies await TestHelper.deleteAgency(AGENCY1_ID) diff --git a/api/tests/user.test.ts b/api/tests/user.test.ts index dfd87e66..8f43ad37 100644 --- a/api/tests/user.test.ts +++ b/api/tests/user.test.ts @@ -38,7 +38,7 @@ const ADMIN_EMAIL = `${TestHelper.getName('admin')}@test.movinin.io` // beforeAll(async () => { if (await DatabaseHelper.Connect(false)) { - await TestHelper.initializeDatabase() + await TestHelper.initialize() } }) @@ -46,7 +46,7 @@ beforeAll(async () => { // Closing and cleaning the database connection after running the test suite // afterAll(async () => { - await TestHelper.clearDatabase() + await TestHelper.close() await Token.deleteMany({ user: { $in: [ADMIN_ID] } })