Skip to content

Commit

Permalink
Add spec test and test of addReview, authenticateUser, deletePetsitte…
Browse files Browse the repository at this point in the history
…rReview, getAllPetsitters, getPetsitterDetails, getPetsitterReview, getUser, registerUser and registerPetsitterUser b00tc4mp#99
  • Loading branch information
TatiGV committed Sep 5, 2024
1 parent cf17d06 commit d165a06
Show file tree
Hide file tree
Showing 21 changed files with 531 additions and 60 deletions.
6 changes: 3 additions & 3 deletions staff/tatiana-garcia/project/cor/logic/addReview.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { validate, errors } from '../../com/index.js'

const { SystemError, NotFoundError } = errors

export default (userId, petsitterId, comment, rate = 0) => {
validate.string(userId, 'userId')
validate.string(petsitterId, 'petsitterId')
export default (petsitterId, userId, comment, rate = 0) => {
validate.id(userId, 'userId')
validate.id(petsitterId, 'petsitterId')
validate.string(comment, 'comment')
validate.number(rate, 'rate')

Expand Down
36 changes: 33 additions & 3 deletions staff/tatiana-garcia/project/cor/logic/addReview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ describe('addReviews', () => {

})

it('succeeds on valid input and creates a review', () => {
return User.create({ image: 'https://www.ngenespanol.com/', name: 'Alberto', surname: 'Garcia', email: '[email protected]', password: '123123123', passwordRepeat: '123123123', role: 'regular' })
.then(user =>
User.create({ image: 'https://hospitalveterinariodonostia.com/', name: 'Tatiana', city: 'Barcelona', description: 'Por favor, funciona de una santa vez', email: '[email protected]', phoneNumber: '655454545', password: '123123123', passwordRepeat: '123123123', role: 'petsitter', pets: ['conejos', 'cobayas'] })
.then(petsitter => {
return addReview(petsitter._id.toString(), user._id.toString(), 'me encanta esta guarderia', 5)
.then(review => {
expect(review).to.exist
expect(review.comment).to.equal('me encanta esta guarderia')
expect(review.rate).to.equal(5)
expect(review.petsitter._id.toString()).to.equal(petsitter._id.toString())
expect(review.author._id.toString()).to.equal(user._id.toString())
})
})
)
})

it('fails on non existing author', () => {
let error

Expand All @@ -58,7 +75,7 @@ describe('addReviews', () => {
let error

return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', password: '123123123', passwordRepeat: '123123123', role: 'regular' })
.then(user => addReview(user.id, '66cc32b55e0e1ff3003b3efa', 'me encanta esta guarderia', 5))
.then(user => addReview('66cc32b55e0e1ff3003b3efa', user.id, 'me encanta esta guarderia', 5))
.catch(_error => error = _error)
.finally(() => {
expect(error).to.be.instanceOf(NotFoundError)
Expand All @@ -70,7 +87,7 @@ describe('addReviews', () => {
let error

try {
addReview(123, '66cc32b55e0e1ff3003b3efa', 'me encanta esta guarderia', 5)
addReview('66cc32b55e0e1ff3003b3efa', 123, 'me encanta esta guarderia', 5)
} catch (_error) {
error = _error
} finally {
Expand All @@ -83,7 +100,7 @@ describe('addReviews', () => {
let error

try {
addReview('66cc32b55e0e1ff3003b3efa', 123, 'me encanta esta guarderia', 5)
addReview(123, '66cc32b55e0e1ff3003b3efa', 'me encanta esta guarderia', 5)
} catch (_error) {
error = _error
} finally {
Expand All @@ -92,6 +109,19 @@ describe('addReviews', () => {
}
})

it('fails on invalid petsitterId', () => {
let error

try {
addReview('', '66cc32b55e0e1ff3003b3efa', 'me encanta esta guarderia', 5)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('invalid petsitterId')
}
})

it('fails on non-string comment', () => {
let error

Expand Down
2 changes: 1 addition & 1 deletion staff/tatiana-garcia/project/cor/logic/addReview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mongoose from 'mongoose'
import addReview from './addReview.js'

mongoose.connect(process.env.MONGODB_URI)
.then(() => addReview('66cc30605e0e1ff3003b3ef6', '66cc32b55e0e1ff3003b3efa', 'son los mejores', 5))
.then(() => addReview('66d988b9e935d3037007f4be', '66d988a804fc191d11113029', 'son los mejores', 5))
.then(review => console.log('review made', review))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())
71 changes: 65 additions & 6 deletions staff/tatiana-garcia/project/cor/logic/authenticateUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,32 @@ describe('authenticateUser', () => {

beforeEach(() => User.deleteMany())

it('succeds on email and password is correct', () =>
bcrypt.hash('123123123', 8)
it('succeeds when email and password are correct, transforming _id to id and returning role', () => {
const email = '[email protected]'
const password = '123123123'

return bcrypt.hash(password, 8)
.then(hash => {
User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', password: hash })
.then(() => authenticateUser('[email protected]', '123123123'))
.then(value => expect(value).to.be.string)
return User.create({
image: 'https://www.ngenespanol.com/',
name: 'Tatiana',
surname: 'Garcia',
email,
password: hash,
role: 'regular'
})
})
)
.then(user => {
return authenticateUser(email, password)
.then(result => {
expect(result).to.be.an('object')
expect(result).to.have.property('id')
expect(result).to.have.property('role')
expect(result.id).to.equal(user._id.toString())
expect(result.role).to.equal('regular')
})
})
})

it('fails on existing user with same email', () => {
let _error
Expand All @@ -39,6 +57,17 @@ describe('authenticateUser', () => {
})
})

it('fails on non existing user', () => {
let _error

authenticateUser('[email protected]', '123123123')
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(NotFoundError)
expect(_error.message).to.equal('user not found')
})
})

it('fails on wrong password', () => {
let error

Expand All @@ -54,6 +83,21 @@ describe('authenticateUser', () => {
})
})

it('fails on wrong email', () => {
let error

bcrypt.hash('123123123', 8)
.then(hash =>
User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', password: hash })
)
.then(() => authenticateUser('[email protected]', '123123123'))
.catch(_error => error = _error)
.finally(() => {
expect(error).to.be.instanceOf(CredentialsError)
expect(error.message).to.equal('wrong email')
})
})

it('fails on non string email', () => {
let error

Expand Down Expand Up @@ -106,6 +150,21 @@ describe('authenticateUser', () => {
}
})

it('fails on password do not match', () => {
let _error

bcrypt.hash('123123123', 8)
.then(hash =>
User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', password: hash })
)
.then(user => authenticateUser(user.email, '147895235'))
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(CredentialsError)
expect(_error.message).to.equal('wrong password')
})
})

it('fails on password with spaces', () => {
let error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import authenticateUser from './authenticateUser.js'
import mongoose from 'mongoose'

mongoose.connect(process.env.MONGODB_URI)
.then(() => authenticateUser('carmen@valdivia.com', '123123123'))
.then((user) => console.log('user authenticated', user))
.then(() => authenticateUser('samuele@spinetti.com', '123123123'))
.then((petsitter) => console.log('user authenticated', petsitter))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { validate, errors } from '../../com/index.js'
const { SystemError, NotFoundError } = errors

export default (userId, reviewId) => {
validate.string(userId, 'userId')
validate.string(reviewId, 'reviewId')
validate.id(userId, 'userId')
validate.id(reviewId, 'reviewId')

return User.findById(userId).lean()
.catch(error => { throw new SystemError(error.message) })
Expand Down
122 changes: 122 additions & 0 deletions staff/tatiana-garcia/project/cor/logic/deletePetsitterReview.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import 'dotenv/config'
import deletePetsitterReview from './deletePetsitterReview.js'
import mongoose, { Types } from 'mongoose'

const { ObjectId } = Types

import { expect } from 'chai'
import { User, Review } from '../data/models.js'

import { errors } from '../../com/index.js'

const { NotFoundError, ValidationError } = errors

describe('deletePetsitterReview', () => {
before(() => mongoose.connect(process.env.MONGODB_URI))

beforeEach(() => Promise.all([User.deleteMany(), Review.deleteMany()]))



it('succeeds on deleting review', () => {
return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', password: '123123123', passwordRepeat: '123123123', role: 'regular' })
.then(petsitter => {
return User.create({ image: 'https://www.vetpointclinicaveterinaria.com/', name: 'Vetpoint', city: 'Barcelona', description: 'Somos veterinarios, pero cuidamos animales en vacaciones', email: '[email protected]', linkPage: '', contactEmail: '', phoneNumber: '935555555', password: '123123123', pets: ['conejos'] })
.then(user => {
return Review.create({
author: user.id,
petsitter: petsitter.id,
comment: 'Me ha encantado la guarderia',
rate: 5
})
.then(review => deletePetsitterReview(user.id, review.id))
})
})
.then(() => {
return Review.find()
.then(reviews => {
expect(reviews).to.be.an('array')
expect(reviews.length).to.equal(0)
})
})
})

it('fails on non string userId', () => {
let error

try {
deletePetsitterReview(123, '66cc32b55e0e1ff3003b3efa')
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('userId is not a string')
}
})

it('fails on non-existing user', () => {
let _error

return User.create({ image: 'https://www.vetpointclinicaveterinaria.com/', name: 'Vetpoint', city: 'Barcelona', description: 'Somos veterinarios, pero cuidamos animales en vacaciones', email: '[email protected]', linkPage: '', contactEmail: '', phoneNumber: '935555555', password: '123123123', pets: ['conejos'] })
.then(petsitter => {
return Review.create({
author: new ObjectId(),
petsitter: petsitter.id,
comment: 'Me ha encantado la guarderia',
rate: 5
})
})
.then(review => deletePetsitterReview('66cc32b55e0e1ff3003b3efa', review.id))
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(NotFoundError)
expect(_error.message).to.equal('user not found')
})
})

it('fails on invalid userId', () => {
let error

try {
deletePetsitterReview('', '66cc32b55e0e1ff3003b3efa')
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('invalid userId')
}
})

it('fails on non string reviewId', () => {
let error

try {
deletePetsitterReview('66cc32b55e0e1ff3003b3efa', 123)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('reviewId is not a string')
}
})


it('fails on invalid reviewId', () => {
let error

try {
deletePetsitterReview('66cc32b55e0e1ff3003b3efa', '')
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('invalid reviewId')
}
})



afterEach(() => Promise.all([User.deleteMany(), Review.deleteMany()]))

after(() => mongoose.disconnect())
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dotenv/config'
import deletePetsitterReview from './deletePetsitterReview.js'

import mongoose from 'mongoose'

mongoose.connect(process.env.MONGODB_URI)
.then(() => deletePetsitterReview('66d988a804fc191d11113029', '66d989d27d3c64a43a97d317'))
.then(() => console.log('review deleted'))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())
7 changes: 6 additions & 1 deletion staff/tatiana-garcia/project/cor/logic/getAllPetsitters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ import { User } from '../data/models.js'

import { errors } from '../../com/index.js'

const { SystemError } = errors
const { SystemError, NotFoundError } = errors

export default () => {

return User.find({ role: 'petsitter' }, { __v: 0 }).sort({ name: 1 }).lean()
.catch(error => { throw new SystemError(error.message) })
.then(petsitters => {
if (!petsitters || petsitters.length === 0) throw new NotFoundError('petsitters not found')

return petsitters.map(petsitter => ({
id: petsitter._id.toString(),
image: petsitter.image,
name: petsitter.name,
city: petsitter.city,
description: petsitter.description,
email: petsitter.email,
linkPage: petsitter.linkPage,
contactEmail: petsitter.contactEmail,
phoneNumber: petsitter.phoneNumber,
pets: petsitter.pets
}))
})

}
Loading

0 comments on commit d165a06

Please sign in to comment.