Skip to content

Commit

Permalink
Add registerUser.spec.js b00tc4mp#99
Browse files Browse the repository at this point in the history
  • Loading branch information
TatiGV committed Aug 12, 2024
1 parent 0a463e6 commit cfbf584
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 39 deletions.
2 changes: 1 addition & 1 deletion staff/tatiana-garcia/project/app/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Router><App /></Router>)
22 changes: 11 additions & 11 deletions staff/tatiana-garcia/project/app/view/register/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,47 +195,47 @@ export default function Register({ onRegister }) {

{role === 'petsitter' && (
<Container>
<label htmlFor="pets-input">¿Qué animales cuidas?</label><br />
<label htmlFor="pets-input">¿Qué animales cuidas?</label><br /><br />

<label htmlFor="rabbit-input">Conejos</label>
<input
type="checkbox"
id="rabbit-input"
value="Rabbit"
onChange={handlePetChange}
/><br />
/>
<label htmlFor="rabbit-input">Conejos</label><br />

<label htmlFor="guinea-pig-input">Cobayas</label>
<input
type="checkbox"
id="guinea-pig-input"
value="GuineaPig"
onChange={handlePetChange}
/><br />
/>
<label htmlFor="guinea-pig-input">Cobayas</label><br />

<label htmlFor="hamsters-input">Hamsters</label>
<input
type="checkbox"
id="hamsters-input"
value="Hamsters"
onChange={handlePetChange}
/><br />
/>
<label htmlFor="hamsters-input">Hamsters</label><br />

<label htmlFor="birds-input">Aves</label>
<input
type="checkbox"
id="birds-input"
value="Birds"
onChange={handlePetChange}
/><br />
/>
<label htmlFor="birds-input">Aves</label><br />

<label htmlFor="reptiles-input">Reptiles</label>
<input
type="checkbox"
id="reptiles-input"
value="Reptiles"
onChange={handlePetChange}
/><br />
/>
<label htmlFor="reptiles-input">Reptiles</label><br /><br />
</Container>
)}
<button type="submit">{'Register'}</button>
Expand Down
3 changes: 1 addition & 2 deletions staff/tatiana-garcia/project/com/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
Expand Down
4 changes: 4 additions & 0 deletions staff/tatiana-garcia/project/cor/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
163 changes: 138 additions & 25 deletions staff/tatiana-garcia/project/cor/logic/registerUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -16,23 +16,46 @@ describe('registerUser', () => {
beforeEach(() => User.deleteMany())

it('succeeds on new user', () =>
registerUser('Tati', 'Garcia', '[email protected]', 'tatig', '123123123', '123123123')
registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '[email protected]', '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('[email protected]')
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', '[email protected]', '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('[email protected]')
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: '[email protected]', username: 'tatig', password: '123123123' })
.then(() => registerUser('Tati', 'Garcia', '[email protected]', 'tatig2', '123123123', '123123123'))
return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', username: 'tatig', password: '123123123', passwordRepeat: '123123123', role: 'user' })
.then(() => registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '[email protected]', 'tatig', '123123123', '123123123', 'user'))
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(DuplicityError)
Expand All @@ -43,8 +66,20 @@ describe('registerUser', () => {
it('fails on existing user with same username', () => {
let _error

return User.create({ name: 'Tati', surname: 'Garcia', email: '[email protected]', username: 'tatig', password: '123123123' })
.then(() => registerUser('Tati', 'Garcia', '[email protected]', 'tatig', '123123123', '123123123'))
return User.create({ image: 'https://www.ngenespanol.com/', name: 'Tatiana', surname: 'Garcia', email: '[email protected]', username: 'tatig', password: '123123123', passwordRepeat: '123123123', role: 'user' })
.then(() => registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '[email protected]', '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: '[email protected]', 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', '[email protected]', 'tatig', '123123123', '123123123', 'petsitter', 'elsAltres', 'Barcelona', 'estoy hasta las pelotas', ['conejo, cobaya']))
.catch(error => _error = error)
.finally(() => {
expect(_error).to.be.instanceOf(DuplicityError)
Expand All @@ -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 {
Expand All @@ -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', '[email protected]', '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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -147,7 +194,7 @@ describe('registerUser', () => {
let error

try {
registerUser('Tati', 'Garcia', '[email protected]', 'a', '123123123', '123123123')
registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '[email protected]', '', '123123123', '123123123', 'user')
} catch (_error) {
error = _error
} finally {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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', '[email protected]', 'tatig', '123123123', '_123123123')
registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '[email protected]', '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', '[email protected]', 'tatig', '123456123', '123123123', 'user')
} catch (_error) {
error = _error
} finally {
Expand All @@ -195,6 +255,59 @@ describe('registerUser', () => {
}
})

it('fails on role non matching', () => {
let error

try {
registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '[email protected]', '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', '[email protected]', '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', '[email protected]', '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', '[email protected]', '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())
Expand Down

0 comments on commit cfbf584

Please sign in to comment.