From cfbf5846ef3759ed1f6d457cf75628d6a770c2a3 Mon Sep 17 00:00:00 2001 From: Tatiana_Garcia <151873298+TatiGV@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:27:30 +0200 Subject: [PATCH] Add registerUser.spec.js #99 --- staff/tatiana-garcia/project/app/index.jsx | 2 +- .../project/app/view/register/index.jsx | 22 +-- staff/tatiana-garcia/project/com/validate.js | 3 +- .../tatiana-garcia/project/cor/data/models.js | 4 + .../project/cor/logic/registerUser.spec.js | 163 +++++++++++++++--- 5 files changed, 155 insertions(+), 39 deletions(-) diff --git a/staff/tatiana-garcia/project/app/index.jsx b/staff/tatiana-garcia/project/app/index.jsx index 801a91a93..e1e9dc1a1 100644 --- a/staff/tatiana-garcia/project/app/index.jsx +++ b/staff/tatiana-garcia/project/app/index.jsx @@ -3,7 +3,7 @@ import { BrowserRouter as Router } from 'react-router-dom' import App from './view/App' -import './index.css' +//import './index.css' const root = ReactDOM.createRoot(document.getElementById('root')) root.render() \ No newline at end of file diff --git a/staff/tatiana-garcia/project/app/view/register/index.jsx b/staff/tatiana-garcia/project/app/view/register/index.jsx index 743996c51..09224df7f 100644 --- a/staff/tatiana-garcia/project/app/view/register/index.jsx +++ b/staff/tatiana-garcia/project/app/view/register/index.jsx @@ -195,47 +195,47 @@ export default function Register({ onRegister }) { {role === 'petsitter' && ( -
+

-
+ /> +
-
+ /> +
-
+ /> +
-
+ /> +
-
+ /> +

)} diff --git a/staff/tatiana-garcia/project/com/validate.js b/staff/tatiana-garcia/project/com/validate.js index 653549cea..6d551d404 100644 --- a/staff/tatiana-garcia/project/com/validate.js +++ b/staff/tatiana-garcia/project/com/validate.js @@ -48,12 +48,11 @@ function validateCity(city) { function validateDescription(description, explain = 'description') { validateString(description, 'description') - if (description < 1 && description > 200) throw new ValidationError(`the ${explain} must have more than 5 characters and less than 50 characters`) + if (description < 1 && description > 200) throw new ValidationError(`the ${explain} must have more than 1 characters and less than 200 characters`) if (typeof description !== 'string') throw new ValidationError(`${explain} is not a string`) } function validateUrl(url, explain = 'url') { - // console.log(url) // validateString(url, explain) if (!url.startsWith('https')) throw new ValidationError(`invalid ${explain}`) } diff --git a/staff/tatiana-garcia/project/cor/data/models.js b/staff/tatiana-garcia/project/cor/data/models.js index 0330e54b2..13190636d 100644 --- a/staff/tatiana-garcia/project/cor/data/models.js +++ b/staff/tatiana-garcia/project/cor/data/models.js @@ -23,6 +23,10 @@ const user = new Schema({ type: String, required: true }, + role: { + type: String, + required: true + }, image: { type: String, default: 'https://www.ngenespanol.com/wp-content/uploads/2024/03/estos-son-los-animales-que-no-deberias-tener-como-mascotas.jpg' diff --git a/staff/tatiana-garcia/project/cor/logic/registerUser.spec.js b/staff/tatiana-garcia/project/cor/logic/registerUser.spec.js index 10c0debf3..b9f19deb4 100644 --- a/staff/tatiana-garcia/project/cor/logic/registerUser.spec.js +++ b/staff/tatiana-garcia/project/cor/logic/registerUser.spec.js @@ -6,7 +6,7 @@ import bcrypt from 'bcryptjs' import registerUser from './registerUser.js' import { User } from '../data/models.js' -import { errors } from 'com' +import { errors } from '../../com/index.js' const { DuplicityError, ValidationError } = errors @@ -16,23 +16,46 @@ describe('registerUser', () => { beforeEach(() => User.deleteMany()) it('succeeds on new user', () => - registerUser('Tati', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') .then(() => User.findOne({ username: 'tatig' }).lean()) .then(user => { - expect(user.name).to.equal('Tati') + 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('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) }) - .then(match => expect(match).to.be.true) ) it('fails on existing user with same email', () => { let _error - return User.create({ name: 'Tati', surname: 'Garcia', email: 'tati@garcia.com', username: 'tatig', password: '123123123' }) - .then(() => registerUser('Tati', 'Garcia', 'tati@garcia.com', 'tatig2', '123123123', '123123123')) + 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) @@ -43,8 +66,20 @@ describe('registerUser', () => { it('fails on existing user with same username', () => { let _error - return User.create({ name: 'Tati', surname: 'Garcia', email: 'tati@garcia.com', username: 'tatig', password: '123123123' }) - .then(() => registerUser('Tati', 'Garcia', 'tati@garcia2.com', 'tatig', '123123123', '123123123')) + 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'])) .catch(error => _error = error) .finally(() => { expect(_error).to.be.instanceOf(DuplicityError) @@ -56,7 +91,7 @@ describe('registerUser', () => { let error try { - registerUser(123, 'Tati', 'tati@garcia2.com', 'tatig', '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', 123, 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -65,11 +100,23 @@ 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('P', 'Garcia', 'tati@garcia2.com', 'tatig', '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', '', 'Garcia', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -78,11 +125,11 @@ describe('registerUser', () => { } }) - it('fails on non-string surname', () => { + it('fails on non string surname', () => { let error try { - registerUser('Tati', 123, 'tati@garcia2.com', 'tatig', '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 123, 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -95,7 +142,7 @@ describe('registerUser', () => { let error try { - registerUser('Tati', 'p', 'tati@garcia2.com', 'tatig', '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', '', 'tati@garcia.com', 'tatig', '123123123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -104,11 +151,11 @@ describe('registerUser', () => { } }) - it('fails on non-string email', () => { + it('fails on non string email', () => { let error try { - registerUser('Tati', 'Garcia', 123, 'tatig', '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 123, 'tatig', '123123123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -121,7 +168,7 @@ describe('registerUser', () => { let error try { - registerUser('Tati', 'Garcia', 'h', 'tatig', '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '', 'tatig', '123123123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -130,11 +177,11 @@ describe('registerUser', () => { } }) - it('fails on non-string username', () => { + it('fails on non string username', () => { let error try { - registerUser('Tati', 'Garcia', 'tati@garcia2.com', 123, '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 123, '123123123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -147,7 +194,7 @@ describe('registerUser', () => { let error try { - registerUser('Tati', 'Garcia', 'tati@garcia.com', 'a', '123123123', '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', '', '123123123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -156,11 +203,11 @@ describe('registerUser', () => { } }) - it('fails on non-string password', () => { + it('fails on non string password', () => { let error try { - registerUser('Tati', 'Garcia', 'tati@garcia2.com', 'tatig', 123123123, '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', 123123123, '123123123', 'user') } catch (_error) { error = _error } finally { @@ -169,11 +216,11 @@ describe('registerUser', () => { } }) - it('fails on password short', () => { + it('fails on non password short', () => { let error try { - registerUser('Tati', 'Garcia', 'tati@garcia2.com', 'tatig', '1231231', '123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '1231', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -182,11 +229,24 @@ describe('registerUser', () => { } }) - it('fails on non-matching passwords', () => { + it('fails on non password have spaces', () => { let error try { - registerUser('Tati', 'Garcia', 'tati@garcia2.com', 'tatig', '123123123', '_123123123') + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123123 123', '123123123', 'user') + } 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 { + registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', 'tati@garcia.com', 'tatig', '123456123', '123123123', 'user') } catch (_error) { error = _error } finally { @@ -195,6 +255,59 @@ 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())