Skip to content

Commit

Permalink
Modified logic registerUser, registerUser.test and registerUser.spec …
Browse files Browse the repository at this point in the history
…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
TatiGV committed Aug 16, 2024
1 parent e2f0de3 commit 1f0a346
Show file tree
Hide file tree
Showing 16 changed files with 379 additions and 176 deletions.
4 changes: 3 additions & 1 deletion staff/tatiana-garcia/project/api/handlers/index.js
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
}
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { image, name, surname, email, username, password, passwordRepeat, role, petsitterName, city, description, pets } = req.body
const { image, name, surname, email, username, password, passwordRepeat } = req.body

try {
logic.registerUser(image, name, surname, email, username, password, passwordRepeat, role, petsitterName, city, description, pets)
logic.registerUser(image, name, surname, email, username, password, passwordRepeat)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
Expand Down
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"}
2 changes: 1 addition & 1 deletion staff/tatiana-garcia/project/api/test/register-user.sh
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"
15 changes: 11 additions & 4 deletions staff/tatiana-garcia/project/com/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ function validatePassword(password, explain = 'password') {
if (password.includes(' ')) throw new ValidationError(`${explain} has empty spaces`)
}

function validateRole(role, explain = 'role') {
validateString(role, 'role')
if (role !== 'user' && role !== 'petsitter') throw new ValidationError(`invalid ${explain}`)
function validateCif(cif, explain = 'cif') {
validateString(cif, 'cif')
if (cif.trim().length < 9) throw new ValidationError(`${explain} length is lower than 9 characters`)
if (cif.includes(' ')) throw new ValidationError(`${explain} has empty spaces`)
}

// function validateRole(role, explain = 'role') {
// validateString(role, 'role')
// if (role !== 'user' && role !== 'petsitter') throw new ValidationError(`invalid ${explain}`)
// }

function validateCity(city) {
validateString(city, 'city')
if (city === '') throw new ValidationError('the field can not be empty')
Expand Down Expand Up @@ -69,7 +75,8 @@ const validate = {
email: validateEmail,
username: validateUsername,
password: validatePassword,
role: validateRole,
cif: validateCif,
// role: validateRole,
city: validateCity,
description: validateDescription,
url: validateUrl,
Expand Down
13 changes: 8 additions & 5 deletions staff/tatiana-garcia/project/cor/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ const user = new Schema({
type: String,
required: true
},
role: {
// role: {
// type: String,
// required: true
// },
cif: {
type: String,
required: true
},
image: {
type: String,
Expand All @@ -40,9 +43,9 @@ const user = new Schema({
petsitterName: {
type: String
},
pets: {
type: [String]
}
// pets: {
// type: [String]
// }
})

const User = model('User', user)
Expand Down
4 changes: 2 additions & 2 deletions staff/tatiana-garcia/project/cor/logic/authenticateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { User } from '../data/models.js'

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

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

export default (username, password) => {
validate.username(username)
Expand All @@ -20,7 +20,7 @@ export default (username, password) => {
.catch(error => { throw new SystemError(error.message) })
.then(match => {
if (!match)
throw new CredentialsError('wrong password')
throw new ValidationError('passwords do not match')

return user._id.toString()
})
Expand Down
26 changes: 15 additions & 11 deletions staff/tatiana-garcia/project/cor/logic/authenticateUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
})
})

Expand Down
2 changes: 2 additions & 0 deletions staff/tatiana-garcia/project/cor/logic/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import registerUser from './registerUser.js'
import authenticateUser from './authenticateUser.js'
import registerPetsitter from './registerPetsitter.js'

const logic = {
registerUser,
registerPetsitter,
authenticateUser
}

Expand Down
49 changes: 49 additions & 0 deletions staff/tatiana-garcia/project/cor/logic/registerPetsitter.js
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(() => { })
}
Loading

0 comments on commit 1f0a346

Please sign in to comment.