Skip to content

Commit

Permalink
Add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Feb 19, 2024
1 parent 7bc0654 commit 3749ce5
Show file tree
Hide file tree
Showing 7 changed files with 2,620 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ jobs:
- run: npm install
- run: npm run lint
- run: npm run build
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2,404 changes: 2,404 additions & 0 deletions api/coverage/cobertura-coverage.xml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const config = {
roots: [
'./tests/',
],
collectCoverage: true,
coverageReporters: ['cobertura'],
}

export default config
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dev": "nodemon",
"build": "rimraf dist && tsc && babel dist -d dist",
"start": "npm run build && node dist/src",
"test": "cross-env NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest",
"test": "rimraf coverage && cross-env NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest --coverage",
"lint": "eslint --ext .ts .",
"ncu": "ncu -u"
},
Expand Down
4 changes: 2 additions & 2 deletions api/src/controllers/locationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function create(req: Request, res: Response) {

const location = new Location({ values })
await location.save()
return res.sendStatus(200)
return res.json(location)
} catch (err) {
console.error(`[location.create] ${strings.DB_ERROR} ${req.body}`, err)
return res.status(400).send(strings.DB_ERROR + err)
Expand Down Expand Up @@ -102,7 +102,7 @@ export async function update(req: Request, res: Response) {
await location.save()
}
}
return res.sendStatus(200)
return res.json(location)
}

console.error('[location.update] Location not found:', id)
Expand Down
65 changes: 61 additions & 4 deletions api/tests/agency.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import 'dotenv/config'
import request from 'supertest'
import url from 'url'
import path from 'path'
import fs from 'node:fs/promises'
import * as movininTypes from 'movinin-types'
import * as DatabaseHelper from '../src/common/DatabaseHelper'
import * as TestHelper from './TestHelper'
import app from '../src/app'
import * as env from '../src/config/env.config'
import * as Helper from '../src/common/Helper'
import User from '../src/models/User'
import Property from '../src/models/Property'

const __filename = url.fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

let AGENCY1_ID: string
let AGENCY2_ID: string
Expand Down Expand Up @@ -149,20 +157,69 @@ describe('DELETE /api/delete-agency/:id', () => {
const token = await TestHelper.signinAsAdmin()

const agencyName = TestHelper.getAgencyName()
const _id = await TestHelper.createAgency(`${agencyName}@test.movinin.io`, agencyName)
const agencyId = await TestHelper.createAgency(`${agencyName}@test.movinin.io`, agencyName)

let agency = await User.findById(_id)
let agency = await User.findById(agencyId)
expect(agency).not.toBeNull()

const avatarName = 'avatar1.jpg'
const avatarPath = path.resolve(__dirname, `./img/${avatarName}`)

const avatar = path.join(env.CDN_USERS, avatarName)
if (!await Helper.exists(avatar)) {
fs.copyFile(avatarPath, avatar)
}
agency!.avatar = avatarName
await agency?.save()

const locationId = await TestHelper.createLocation('Location 1 EN', 'Location 1 FR')

const propertyImageName = 'main1.jpg'
const propertyImagePath = path.resolve(__dirname, `./img/${propertyImageName}`)

const property = new Property({
name: 'Beautiful House in Detroit',
agency: agencyId,
type: movininTypes.PropertyType.House,
description: '<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium rem aperiam, veritatis et quasi.</p>',
image: propertyImageName,
images: [],
bedrooms: 3,
bathrooms: 2,
kitchens: 1,
parkingSpaces: 1,
size: 200,
petsAllowed: false,
furnished: true,
aircon: true,
minimumAge: 21,
location: locationId,
address: '',
price: 4000,
hidden: true,
cancellation: 0,
available: false,
rentalTerm: movininTypes.RentalTerm.Monthly,
})

const propertyImage = path.join(env.CDN_PROPERTIES, propertyImageName)
if (!await Helper.exists(propertyImage)) {
fs.copyFile(propertyImagePath, propertyImage)
}

await property.save()

const res = await request(app)
.delete(`/api/delete-agency/${_id}`)
.delete(`/api/delete-agency/${agencyId}`)
.set(env.X_ACCESS_TOKEN, token)

expect(res.statusCode).toBe(200)

agency = await User.findById(_id)
agency = await User.findById(agencyId)
expect(agency).toBeNull()

await TestHelper.deleteLocation(locationId)

await TestHelper.signout(token)
})
})
154 changes: 146 additions & 8 deletions api/tests/location.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import 'dotenv/config'
import request from 'supertest'
import { v1 as uuid } from 'uuid'
import * as movininTypes from 'movinin-types'
import app from '../src/app'
import * as DatabaseHelper from '../src/common/DatabaseHelper'
import * as TestHelper from './TestHelper'
import * as env from '../src/config/env.config'
import LocationValue from '../src/models/LocationValue'
import Location from '../src/models/Location'
import Property from '../src/models/Property'

let LOCATION_ID: string

let LOCATION_NAMES: movininTypes.LocationName[] = [
{
language: 'en',
name: uuid(),
},
{
language: 'fr',
name: uuid(),
},
]

//
// Connecting and initializing the database before running the test suite
Expand All @@ -27,7 +48,34 @@ describe('POST /api/validate-location', () => {
it('should validate a location', async () => {
const token = await TestHelper.signinAsAdmin()

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

await TestHelper.signout(token)
})
Expand All @@ -37,43 +85,123 @@ describe('POST /api/create-location', () => {
it('should create a location', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO
const payload: movininTypes.LocationName[] = LOCATION_NAMES

const 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

await TestHelper.signout(token)
})
})

describe('POST /api/update-location/:id', () => {
describe('PUT /api/update-location/:id', () => {
it('should update a location', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO
LOCATION_NAMES = [
{
language: 'en',
name: uuid(),
},
{
language: 'fr',
name: uuid(),
},
{
language: 'es',
name: uuid(),
},
]

const res = await request(app)
.put(`/api/update-location/${LOCATION_ID}`)
.set(env.X_ACCESS_TOKEN, token)
.send(LOCATION_NAMES)

expect(res.statusCode).toBe(200)
expect(res.body.values?.length).toBe(3)

await TestHelper.signout(token)
})
})

describe('GET /api/location/:id/:language', () => {
it('should get a location', async () => {
const language = 'en'

// TODO
const res = await request(app)
.get(`/api/location/${LOCATION_ID}/${language}`)

expect(res.statusCode).toBe(200)
expect(res.body?.name).toBe(LOCATION_NAMES.filter((v) => v.language === language)[0].name)
})
})

describe('GET /api/locations/:page/:size/:language', () => {
it('should get locations', async () => {
const language = 'en'

// TODO
const 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)
})
})

describe('GET /api/check-location/:id', () => {
it('should check a location', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO
const agencyName = TestHelper.getAgencyName()
const agencyId = await TestHelper.createAgency(`${agencyName}@test.movinin.io`, agencyName)

const property = new Property({
name: 'Beautiful House in Detroit',
agency: agencyId,
type: movininTypes.PropertyType.House,
description: '<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium rem aperiam, veritatis et quasi.</p>',
image: 'main1.jpg',
images: [],
bedrooms: 3,
bathrooms: 2,
kitchens: 1,
parkingSpaces: 1,
size: 200,
petsAllowed: false,
furnished: true,
aircon: true,
minimumAge: 21,
location: LOCATION_ID,
address: '',
price: 4000,
hidden: true,
cancellation: 0,
available: false,
rentalTerm: movininTypes.RentalTerm.Monthly,
})
await property.save()

let res = await request(app)
.get(`/api/check-location/${LOCATION_ID}`)
.set(env.X_ACCESS_TOKEN, token)

expect(res.statusCode).toBe(200)

await Property.deleteOne({ _id: property._id })
await TestHelper.deleteAgency(agencyId)

res = await request(app)
.get(`/api/check-location/${LOCATION_ID}`)
.set(env.X_ACCESS_TOKEN, token)

expect(res.statusCode).toBe(204)

await TestHelper.signout(token)
})
Expand All @@ -83,7 +211,17 @@ describe('DELETE /api/delete-location/:id', () => {
it('should delete a location', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO
let location = await Location.findById(LOCATION_ID)
expect(location).not.toBeNull()

const res = await request(app)
.delete(`/api/delete-location/${LOCATION_ID}`)
.set(env.X_ACCESS_TOKEN, token)

expect(res.statusCode).toBe(200)

location = await Location.findById(LOCATION_ID)
expect(location).toBeNull()

await TestHelper.signout(token)
})
Expand Down

0 comments on commit 3749ce5

Please sign in to comment.