diff --git a/staff/tatiana-garcia/project/api/handlers/index.js b/staff/tatiana-garcia/project/api/handlers/index.js index 870603749..bf1b38439 100644 --- a/staff/tatiana-garcia/project/api/handlers/index.js +++ b/staff/tatiana-garcia/project/api/handlers/index.js @@ -1,8 +1,10 @@ import registerUserHandler from './registerUserHandler.js' import authenticateUserHandler from './authenticateUserHandler.js' +import registerPetsitterHandler from './registerPetsitterHandler.js' export { registerUserHandler, - authenticateUserHandler + authenticateUserHandler, + registerPetsitterHandler } \ No newline at end of file diff --git a/staff/tatiana-garcia/project/api/handlers/registerPetsitterHandler.js b/staff/tatiana-garcia/project/api/handlers/registerPetsitterHandler.js new file mode 100644 index 000000000..288a9ea72 --- /dev/null +++ b/staff/tatiana-garcia/project/api/handlers/registerPetsitterHandler.js @@ -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) + } +} \ No newline at end of file diff --git a/staff/tatiana-garcia/project/api/handlers/registerUserHandler.js b/staff/tatiana-garcia/project/api/handlers/registerUserHandler.js index 995f2c61f..f24240990 100644 --- a/staff/tatiana-garcia/project/api/handlers/registerUserHandler.js +++ b/staff/tatiana-garcia/project/api/handlers/registerUserHandler.js @@ -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) { diff --git a/staff/tatiana-garcia/project/api/test/register-petsitter.sh b/staff/tatiana-garcia/project/api/test/register-petsitter.sh new file mode 100644 index 000000000..795e29bbc --- /dev/null +++ b/staff/tatiana-garcia/project/api/test/register-petsitter.sh @@ -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":"abt@garcia.com","password":"123123123","passwordRepeat":"123123123"}' -H "Content-Type: application/json"} \ No newline at end of file diff --git a/staff/tatiana-garcia/project/api/test/register-user.sh b/staff/tatiana-garcia/project/api/test/register-user.sh index 22bb13ef1..19098917c 100644 --- a/staff/tatiana-garcia/project/api/test/register-user.sh +++ b/staff/tatiana-garcia/project/api/test/register-user.sh @@ -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":"tati@garcia.com","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" \ No newline at end of file +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":"tati@garcia.com","username":"tatig","password":"123123123","passwordRepeat":"123123123"}' -H "Content-Type: application/json" \ No newline at end of file diff --git a/staff/tatiana-garcia/project/com/validate.js b/staff/tatiana-garcia/project/com/validate.js index 6d551d404..cd82b96e5 100644 --- a/staff/tatiana-garcia/project/com/validate.js +++ b/staff/tatiana-garcia/project/com/validate.js @@ -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') @@ -69,7 +75,8 @@ const validate = { email: validateEmail, username: validateUsername, password: validatePassword, - role: validateRole, + cif: validateCif, + // role: validateRole, city: validateCity, description: validateDescription, url: validateUrl, diff --git a/staff/tatiana-garcia/project/cor/data/models.js b/staff/tatiana-garcia/project/cor/data/models.js index 13190636d..6ab4e3c41 100644 --- a/staff/tatiana-garcia/project/cor/data/models.js +++ b/staff/tatiana-garcia/project/cor/data/models.js @@ -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, @@ -40,9 +43,9 @@ const user = new Schema({ petsitterName: { type: String }, - pets: { - type: [String] - } + // pets: { + // type: [String] + // } }) const User = model('User', user) diff --git a/staff/tatiana-garcia/project/cor/logic/authenticateUser.js b/staff/tatiana-garcia/project/cor/logic/authenticateUser.js index 341cbe594..132440b7e 100644 --- a/staff/tatiana-garcia/project/cor/logic/authenticateUser.js +++ b/staff/tatiana-garcia/project/cor/logic/authenticateUser.js @@ -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) @@ -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() }) diff --git a/staff/tatiana-garcia/project/cor/logic/authenticateUser.spec.js b/staff/tatiana-garcia/project/cor/logic/authenticateUser.spec.js index 75ce1c6ac..f0d61234c 100644 --- a/staff/tatiana-garcia/project/cor/logic/authenticateUser.spec.js +++ b/staff/tatiana-garcia/project/cor/logic/authenticateUser.spec.js @@ -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: 'tati@garcia.com', 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: 'tati@garcia.com', 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: 'tati@garcia.com', 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: 'tati@garcia.com', 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') }) }) diff --git a/staff/tatiana-garcia/project/cor/logic/index.js b/staff/tatiana-garcia/project/cor/logic/index.js index 0533967aa..f95c94fce 100644 --- a/staff/tatiana-garcia/project/cor/logic/index.js +++ b/staff/tatiana-garcia/project/cor/logic/index.js @@ -1,8 +1,10 @@ import registerUser from './registerUser.js' import authenticateUser from './authenticateUser.js' +import registerPetsitter from './registerPetsitter.js' const logic = { registerUser, + registerPetsitter, authenticateUser } diff --git a/staff/tatiana-garcia/project/cor/logic/registerPetsitter.js b/staff/tatiana-garcia/project/cor/logic/registerPetsitter.js new file mode 100644 index 000000000..fd25764df --- /dev/null +++ b/staff/tatiana-garcia/project/cor/logic/registerPetsitter.js @@ -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(() => { }) +} \ No newline at end of file diff --git a/staff/tatiana-garcia/project/cor/logic/registerPetsitter.spec.js b/staff/tatiana-garcia/project/cor/logic/registerPetsitter.spec.js new file mode 100644 index 000000000..747bb7184 --- /dev/null +++ b/staff/tatiana-garcia/project/cor/logic/registerPetsitter.spec.js @@ -0,0 +1,232 @@ +import 'dotenv/config' +import mongoose from 'mongoose' +import { expect } from 'chai' +import bcrypt from 'bcryptjs' + +import registerPetsitter from './registerPetsitter.js' +import { User } from '../data/models.js' + +import { errors } from '../../com/index.js' + +const { DuplicityError, ValidationError } = errors + +describe('registerPetsitter', () => { + before(() => mongoose.connect(process.env.MONGODB_URI)) + + beforeEach(() => User.deleteMany()) + + it('succeeds on new petsitter', () => + registerPetsitter('https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tat@garcia.com', '123123123', '123123123') + .then(() => User.findOne({ username: 'tatig' }).lean()) + .then(user => { + expect(user.image).to.equal('https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg') + expect(user.name).to.equal('Tatiana') + expect(user.surname).to.equal('Garcia') + expect(user.username).to.equal('tatig') + expect(user.cif).to.equal('B12345678') + expect(user.city).to.equal('Barcelona') + expect(user.email).to.equal('tat@garcia.com') + + return bcrypt.compare('123123123', user.password) + .then(match => expect(match).to.be.true) + }) + ) + + it('fails on existing user with same email', () => { + let _error + + return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', username: 'tatig', cif: 'B12345678', city: 'Barcelona', email: 'tati@garcia.com', password: '123123123', passwordRepeat: '123123123' }) + .then(() => registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '123123123', '123123123')) + .catch(error => _error = error) + .finally(() => { + expect(_error).to.be.instanceOf(DuplicityError) + expect(_error.message).to.equal('user already exists') + }) + }) + + it('fails on existing user with same username', () => { + let _error + + return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', username: 'tatig', cif: 'B12345678', city: 'Barcelona', email: 'tati@garcia.com', password: '123123123', passwordRepeat: '123123123' }) + .then(() => registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '123123123', '123123123')) + .catch(error => _error = error) + .finally(() => { + expect(_error).to.be.instanceOf(DuplicityError) + expect(_error.message).to.equal('user already exists') + }) + }) + + it('fails on non-string name', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 123, 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('name is not a string') + } + }) + + it('fails on invalid name', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', '', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid name') + } + }) + + it('fails on non string surname', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 123, 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('surname is not a string') + } + }) + + it('fails on invalid surname', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', '', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid surname') + } + }) + + it('fails on non string email', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 123, '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('email is not a string') + } + }) + + it('fails on invalid email', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', '', '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid email') + } + }) + + it('fails on non string username', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 123, 'B12345678', 'Barcelona', 'tati@garcia.com', '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('username is not a string') + } + }) + + it('fails on invalid username', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '', 'B12345678', 'Barcelona', '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid username') + } + }) + + it('fails on non string password', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', 123123123, '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('password is not a string') + } + }) + + it('fails on non password short', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '1231', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('password length is lower than 8 characters') + } + }) + + it('fails on non password have spaces', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '123123 123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('password has empty spaces') + } + }) + + it('fails on non matching passwords', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tati@garcia.com', '123456123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('passwords do not match') + } + }) + + it('fails on the field of the city can not be ampty', () => { + let error + + try { + registerPetsitter('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tatig', 'B12345678', '', 'tati@garcia.com', '123123123', '123123123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('the field can not be empty') + } + }) + + + afterEach(() => User.deleteMany()) + + after(() => mongoose.disconnect()) +}) \ No newline at end of file diff --git a/staff/tatiana-garcia/project/cor/logic/registerPetsitter.test.js b/staff/tatiana-garcia/project/cor/logic/registerPetsitter.test.js new file mode 100644 index 000000000..07a418faa --- /dev/null +++ b/staff/tatiana-garcia/project/cor/logic/registerPetsitter.test.js @@ -0,0 +1,9 @@ +import 'dotenv/config' +import registerPetsitter from './registerPetsitter.js' +import mongoose from 'mongoose' + +mongoose.connect(process.env.MONGODB_URI) + .then(() => registerPetsitter('https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg', 'Tatiana', 'Garcia', 'tatig', 'B12345678', 'Barcelona', 'tat@garcia.com', '123123123', '123123123')) + .then(() => console.log('petsitter registered')) + .catch(error => console.error(error)) + .finally(() => mongoose.disconnect()) \ No newline at end of file diff --git a/staff/tatiana-garcia/project/cor/logic/registerUser.js b/staff/tatiana-garcia/project/cor/logic/registerUser.js index 36ea941cc..313b7990e 100644 --- a/staff/tatiana-garcia/project/cor/logic/registerUser.js +++ b/staff/tatiana-garcia/project/cor/logic/registerUser.js @@ -5,60 +5,39 @@ import { validate, errors } from '../../com/index.js' const { ValidationError, DuplicityError, SystemError } = errors -export default (image, name, surname, email, username, password, passwordRepeat, role, petsitterName, city, description, pets) => { +export default (image, name, surname, email, username, password, passwordRepeat) => { validate.url(image, 'image') validate.name(name, 'name') validate.surname(surname, 'surname') validate.email(email) validate.username(username) validate.password(password) - validate.role(role, 'role') - - if (role === 'petsitter') { - validate.name(petsitterName, 'petsitterName') - validate.city(city, 'city') - validate.description(description, 'description') - validate.pets(pets, 'pets') - } 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 => { - const newUser = { - image, + .then(hash => + User.create({ name, surname, email, username, - password: hash, - role, - } - - if (role === 'petsitter') { - newUser.petsitterName = petsitterName - newUser.city = city - newUser.description = description - newUser.pets = pets - } - - return User.create(newUser) - }) - .catch(error => { - if (error instanceof SystemError || error instanceof ValidationError || error instanceof DuplicityError) { - throw error - } else { - throw new SystemError(error.message) - } - }) + password: hash + }) + .catch(error => { throw new SystemError(error.message) }) + ) + .then(() => { }) } \ No newline at end of file diff --git a/staff/tatiana-garcia/project/cor/logic/registerUser.spec.js b/staff/tatiana-garcia/project/cor/logic/registerUser.spec.js index b9f19deb4..b0b4f3295 100644 --- a/staff/tatiana-garcia/project/cor/logic/registerUser.spec.js +++ b/staff/tatiana-garcia/project/cor/logic/registerUser.spec.js @@ -15,36 +15,15 @@ describe('registerUser', () => { beforeEach(() => User.deleteMany()) - it('succeeds on new user', () => - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') + it('succeeds on new User', () => + registerUser('https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123') .then(() => User.findOne({ username: 'tatig' }).lean()) .then(user => { - expect(user.image).to.equal('https://www.ngenespanol.com/') + expect(user.image).to.equal('https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg') expect(user.name).to.equal('Tatiana') expect(user.surname).to.equal('Garcia') expect(user.email).to.equal('tati@garcia.com') expect(user.username).to.equal('tatig') - expect(user.role).to.equal('user') - - return bcrypt.compare('123123123', user.password) - .then(match => expect(match).to.be.true) - }) - ) - - it('succeeds on new petsitterUser', () => - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'petsitter', 'elsAltres', 'Barcelona', 'estoy hasta las pelotas', ['conejo, cobaya']) - .then(() => User.findOne({ username: 'tatig' }).lean()) - .then(user => { - expect(user.image).to.equal('https://www.ngenespanol.com/') - expect(user.name).to.equal('Tatiana') - expect(user.surname).to.equal('Garcia') - expect(user.email).to.equal('tati@garcia.com') - expect(user.username).to.equal('tatig') - expect(user.role).to.equal('petsitter') - expect(user.petsitterName).to.equal('elsAltres') - expect(user.city).to.equal('Barcelona') - expect(user.description).to.equal('estoy hasta las pelotas') - expect(user.pets).to.deep.equal(['conejo, cobaya']) return bcrypt.compare('123123123', user.password) .then(match => expect(match).to.be.true) @@ -54,8 +33,8 @@ describe('registerUser', () => { it('fails on existing user with same email', () => { let _error - return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: 'tati@garcia.com', username: 'tatig', password: '123123123', passwordRepeat: '123123123', role: 'user' }) - .then(() => registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user')) + return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: 'tati@garcia.com', username: 'tatig', password: '123123123', passwordRepeat: '123123123' }) + .then(() => registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123')) .catch(error => _error = error) .finally(() => { expect(_error).to.be.instanceOf(DuplicityError) @@ -66,20 +45,8 @@ describe('registerUser', () => { it('fails on existing user with same username', () => { let _error - return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: 'tati@garcia.com', username: 'tatig', password: '123123123', passwordRepeat: '123123123', role: 'user' }) - .then(() => registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user')) - .catch(error => _error = error) - .finally(() => { - expect(_error).to.be.instanceOf(DuplicityError) - expect(_error.message).to.equal('user already exists') - }) - }) - - it('fails on existing user with same petsitterName', () => { - let _error - - return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: 'tati@garcia.com', username: 'tatig', password: '123123123', passwordRepeat: '123123123', role: 'user', petsitterName: 'elsaltres', city: 'Barcelona', description: 'estoy hasta las pelotas', pets: ['conejo, cobaya'] }) - .then(() => registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'petsitter', 'elsAltres', 'Barcelona', 'estoy hasta las pelotas', ['conejo, cobaya'])) + return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: 'tati@garcia.com', username: 'tatig', password: '123123123', passwordRepeat: '123123123' }) + .then(() => registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123')) .catch(error => _error = error) .finally(() => { expect(_error).to.be.instanceOf(DuplicityError) @@ -91,7 +58,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 123, 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 123, 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123') } catch (_error) { error = _error } finally { @@ -100,23 +67,11 @@ describe('registerUser', () => { } }) - it('fails when the number of animals is less than one', () => { - let error - try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'petsitter', 'elsAltres', 'Barcelona', 'estoy hasta las pelotas', ['']) - } catch (_error) { - error = _error - } finally { - expect(error).to.be.instanceOf(ValidationError) - expect(error.message).to.equal('At least one pet must be selected') - } - }) - it('fails on invalid name', () => { let error try { - registerUser('https://www.ngenespanol.com/', '', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', '', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123') } catch (_error) { error = _error } finally { @@ -129,7 +84,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 123, 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 123, 'tati@garcia.com', 'tatig', '123123123', '123123123') } catch (_error) { error = _error } finally { @@ -142,7 +97,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', '', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', '', 'tati@garcia.com', 'tatig', '123123123', '123123123') } catch (_error) { error = _error } finally { @@ -155,7 +110,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 123, 'tatig', '123123123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 123, 'tatig', '123123123', '123123123') } catch (_error) { error = _error } finally { @@ -168,7 +123,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '', 'tatig', '123123123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '', 'tatig', '123123123', '123123123') } catch (_error) { error = _error } finally { @@ -181,7 +136,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 123, '123123123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 123, '123123123', '123123123') } catch (_error) { error = _error } finally { @@ -194,7 +149,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', '', '123123123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', '', '123123123', '123123123') } catch (_error) { error = _error } finally { @@ -207,7 +162,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', 123123123, '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', 123123123, '123123123') } catch (_error) { error = _error } finally { @@ -220,7 +175,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '1231', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '1231', '123123123') } catch (_error) { error = _error } finally { @@ -233,7 +188,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123 123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123 123', '123123123') } catch (_error) { error = _error } finally { @@ -246,7 +201,7 @@ describe('registerUser', () => { let error try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123456123', '123123123', 'user') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123456123', '123123123') } catch (_error) { error = _error } finally { @@ -255,59 +210,6 @@ describe('registerUser', () => { } }) - it('fails on role non matching', () => { - let error - - try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'guarderia') - } catch (_error) { - error = _error - } finally { - expect(error).to.be.instanceOf(ValidationError) - expect(error.message).to.equal('invalid role') - } - }) - - it('fails on non string petsitterName', () => { - let error - - try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'petsitter', 123, 'Barcelona', 'estoy hasta las pelotas', ['conejo, cobaya']) - } catch (_error) { - error = _error - } finally { - expect(error).to.be.instanceOf(ValidationError) - expect(error.message).to.equal('petsitterName is not a string') - } - }) - - it('fails on the field of the city can not be ampty', () => { - let error - - try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'petsitter', 'elsatres', '', 'estoy hasta las pelotas', ['conejo, cobaya']) - } catch (_error) { - error = _error - } finally { - expect(error).to.be.instanceOf(ValidationError) - expect(error.message).to.equal('the field can not be empty') - } - }) - - it('fails on non string description', () => { - let error - - try { - registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'petsitter', 'elsatres', 'Barcelona', ['conejo, cobaya']) - } catch (_error) { - error = _error - } finally { - expect(error).to.be.instanceOf(ValidationError) - expect(error.message).to.equal('description is not a string') - } - }) - - afterEach(() => User.deleteMany()) after(() => mongoose.disconnect()) diff --git a/staff/tatiana-garcia/project/cor/logic/registerUser.test.js b/staff/tatiana-garcia/project/cor/logic/registerUser.test.js index 345deda21..12d10d556 100644 --- a/staff/tatiana-garcia/project/cor/logic/registerUser.test.js +++ b/staff/tatiana-garcia/project/cor/logic/registerUser.test.js @@ -3,7 +3,7 @@ import registerUser from './registerUser.js' import mongoose from 'mongoose' mongoose.connect(process.env.MONGODB_URI) - .then(() => registerUser('https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user', 'elsAltres', 'Barcelona', 'estoy hasta las pelotas', 'conejo, cobaya')) + .then(() => registerUser('https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123')) .then(() => console.log('user registered')) .catch(error => console.error(error)) .finally(() => mongoose.disconnect()) \ No newline at end of file