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.
Modified logic registerUser, registerUser.test and registerUser.spec …
…from cor, added new registerPetsitter logic and its respective tests and modified registerUserHandler, add registerPetsitterHandler y its respectives sh tests b00tc4mp#99
- Loading branch information
Showing
16 changed files
with
379 additions
and
176 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import registerUserHandler from './registerUserHandler.js' | ||
import authenticateUserHandler from './authenticateUserHandler.js' | ||
import registerPetsitterHandler from './registerPetsitterHandler.js' | ||
|
||
|
||
export { | ||
registerUserHandler, | ||
authenticateUserHandler | ||
authenticateUserHandler, | ||
registerPetsitterHandler | ||
} |
13 changes: 13 additions & 0 deletions
13
staff/tatiana-garcia/project/api/handlers/registerPetsitterHandler.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,13 @@ | ||
import { logic } from '../../cor/index.js' | ||
|
||
export default (req, res, next) => { | ||
const { image, name, surname, username, cif, city, email, password, passwordRepeat } = req.body | ||
|
||
try { | ||
logic.registerPetsitter(image, name, surname, username, cif, city, email, password, passwordRepeat) | ||
.then(() => res.status(201).send()) | ||
.catch(error => next(error)) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
staff/tatiana-garcia/project/api/handlers/registerUserHandler.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
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 @@ | ||
curl -v http://localhost:8080/users -X POST -d '{"image":"https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg","name":"Alberto","surname":"Garcia","username":"abtg", "cif":"B12345678", "city":"Barcelona","email":"[email protected]","password":"123123123","passwordRepeat":"123123123"}' -H "Content-Type: application/json"} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
curl -v http://localhost:8080/users -X POST -d '{"image":"https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg","name":"Tati","surname":"Garcia","email":"[email protected]","username":"tatig","password":"123123123","passwordRepeat":"123123123", "role":"petsitter", "petsitterName":"vetpoint","city":"Barcelona","description":"funciona de una santa vez", "pets":"conejos, hamsters, cobayas" }' -H "Content-Type: application/json" | ||
curl -v http://localhost:8080/users -X POST -d '{"image":"https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg","name":"Tati","surname":"Garcia","email":"[email protected]","username":"tatig","password":"123123123","passwordRepeat":"123123123"}' -H "Content-Type: application/json" |
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
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 |
---|---|---|
|
@@ -8,20 +8,21 @@ import { User } from '../data/models.js' | |
|
||
import { errors } from '../../com/index.js' | ||
|
||
const { NotFoundError, CredentialsError, ValidationError } = errors | ||
const { NotFoundError, ValidationError } = errors | ||
|
||
describe('authenticateUser', () => { | ||
before(() => mongoose.connect(process.env.MONGODB_URI)) | ||
|
||
beforeEach(() => User.deleteMany()) | ||
|
||
it('succeds on username and password is correct', () => { | ||
it('succeds on username and password is correct', () => | ||
bcrypt.hash('123123123', 8) | ||
.then(hash => User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', username: 'tatig', password: '123123123', passwordRepeat: '123123123', role: 'user', petsitterName: 'elsaltres', city: 'Barcelona', description: 'estoy hasta las pelotas', pets: ['conejo, cobaya'] })) | ||
.then(() => authenticateUser('tatig', '123123123')) | ||
.then(value => expect(value).to.be.string) | ||
|
||
}) | ||
.then(hash => { | ||
User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', username: 'tatig', password: hash }) | ||
.then(() => authenticateUser('tatig', '123123123')) | ||
.then(value => expect(value).to.be.string) | ||
}) | ||
) | ||
|
||
it('fails on non-existing user', () => { | ||
let _error | ||
|
@@ -37,12 +38,15 @@ describe('authenticateUser', () => { | |
it('fails on passwords do not match', () => { | ||
let _error | ||
|
||
return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', username: 'tatig', password: '123123123', passwordRepeat: '123123123', role: 'user', petsitterName: 'elsaltres', city: 'Barcelona', description: 'estoy hasta las pelotas', pets: ['conejo, cobaya'] }) | ||
.then(user => authenticateUser(user.username, '123123223')) | ||
return bcrypt.hash('123123123', 8) | ||
.then(hash => | ||
User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', username: 'tatig', password: hash }) | ||
) | ||
.then(() => authenticateUser('tatig', '111111111')) | ||
.catch(error => _error = error) | ||
.finally(() => { | ||
expect(_error).to.be.instanceOf(CredentialsError) | ||
expect(_error.message).to.equal('wrong password') | ||
expect(_error).to.be.instanceOf(ValidationError) | ||
expect(_error.message).to.equal('passwords do not match') | ||
}) | ||
}) | ||
|
||
|
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
49 changes: 49 additions & 0 deletions
49
staff/tatiana-garcia/project/cor/logic/registerPetsitter.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,49 @@ | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { User } from '../data/models.js' | ||
import { validate, errors } from '../../com/index.js' | ||
|
||
const { ValidationError, DuplicityError, SystemError } = errors | ||
|
||
export default (image, name, surname, username, cif, city, email, password, passwordRepeat) => { | ||
validate.url(image, 'image') | ||
validate.name(name, 'name') | ||
validate.surname(surname, 'surname') | ||
validate.username(username) | ||
validate.cif(cif, 'cif') | ||
validate.city(city, 'city') | ||
validate.email(email) | ||
validate.password(password) | ||
|
||
if (password !== passwordRepeat) throw new ValidationError('passwords do not match') | ||
|
||
return User.findOne({ email }).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (user) throw new DuplicityError('user already exists') | ||
|
||
return User.findOne({ username }).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
}) | ||
.then(user => { | ||
if (user) throw new DuplicityError('user already exists') | ||
|
||
return bcrypt.hash(password, 8) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
}) | ||
.then(hash => | ||
User.create({ | ||
image, | ||
name, | ||
surname, | ||
username, | ||
city, | ||
cif, | ||
email, | ||
username, | ||
password: hash | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
) | ||
.then(() => { }) | ||
} |
Oops, something went wrong.