forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spec test and test of addReview, authenticateUser, deletePetsitte…
…rReview, getAllPetsitters, getPetsitterDetails, getPetsitterReview, getUser, registerUser and registerPetsitterUser b00tc4mp#99
- Loading branch information
Showing
21 changed files
with
531 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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) | ||
|
@@ -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 { | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
staff/tatiana-garcia/project/cor/logic/deletePetsitterReview.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) |
10 changes: 10 additions & 0 deletions
10
staff/tatiana-garcia/project/cor/logic/deletePetsitterReview.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.