Skip to content

Commit

Permalink
Update location.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Feb 21, 2024
1 parent d998f3e commit 82e434f
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 213 deletions.
380 changes: 196 additions & 184 deletions api/coverage/cobertura-coverage.xml

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions api/src/common/Helper.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion api/src/controllers/bookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down
6 changes: 4 additions & 2 deletions api/tests/TestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -54,6 +54,8 @@ export async function initializeDatabase() {
await user.save()
expect(user.id).toBeDefined()
USER_ID = user.id

console.error = () => { }
}

export function getAdminUserId() {
Expand All @@ -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] } })
Expand Down
4 changes: 2 additions & 2 deletions api/tests/agency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions api/tests/booking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
53 changes: 37 additions & 16 deletions api/tests/location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ let LOCATION_NAMES: movininTypes.LocationName[] = [
//
beforeAll(async () => {
if (await DatabaseHelper.Connect(false)) {
await TestHelper.initializeDatabase()
await TestHelper.initialize()
}
})

//
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
await TestHelper.clearDatabase()
await TestHelper.close()
await DatabaseHelper.Close(false)
})

Expand All @@ -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)
})
})
Expand All @@ -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)
})
})
Expand All @@ -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)
Expand All @@ -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)
})
})
Expand All @@ -148,18 +153,25 @@ 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)
})
})

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)
})
})

Expand Down Expand Up @@ -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)
})
})
Expand All @@ -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)
})
})
4 changes: 2 additions & 2 deletions api/tests/notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions api/tests/property.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions api/tests/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ const ADMIN_EMAIL = `${TestHelper.getName('admin')}@test.movinin.io`
//
beforeAll(async () => {
if (await DatabaseHelper.Connect(false)) {
await TestHelper.initializeDatabase()
await TestHelper.initialize()
}
})

//
// 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] } })

Expand Down

0 comments on commit 82e434f

Please sign in to comment.