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.
Add registerUser.spec.js b00tc4mp#99
- Loading branch information
Showing
5 changed files
with
155 additions
and
39 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
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 |
---|---|---|
|
@@ -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', '[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) | ||
|
@@ -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) | ||
|
@@ -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', '[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 { | ||
|
@@ -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', '[email protected]', 'a', '123123123', '123123123') | ||
registerUser('https://www.ngenespanol.com/', 'Tatiana', 'Garcia', '[email protected]', '', '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', '[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 { | ||
|
@@ -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()) | ||
|