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.
implement and test authenticateUser in core b00tc4mp#84
- Loading branch information
Showing
4 changed files
with
186 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { User } from '../data/models.js' | ||
|
||
import { validate, errors } from 'com' | ||
|
||
const { NotFoundError, CredentialsError, SystemError } = errors | ||
|
||
export default (username, password) => { | ||
validate.username(username) | ||
validate.password(password) | ||
|
||
return User.findOne({ username }).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new NotFoundError('user not found') | ||
|
||
return bcrypt.compare(password, user.password) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(match => { | ||
if (!match) throw new CredentialsError('wrong password') | ||
|
||
return user._id.toString() | ||
}) | ||
}) | ||
} |
125 changes: 125 additions & 0 deletions
125
staff/marti-herms/project/V-HUB/core/logic/authenticateUser.spec.js
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import 'dotenv/config' | ||
import mongoose from 'mongoose' | ||
import { expect } from 'chai' | ||
import bcrypt from 'bcryptjs' | ||
|
||
import authenticateUser from './authenticateUser.js' | ||
import { User } from '../data/models.js' | ||
|
||
import { errors } from 'com' | ||
|
||
const { NotFoundError, CredentialsError, ValidationError } = errors | ||
|
||
describe('authenticateUser', () => { | ||
before(() => mongoose.connect(process.env.MONGODB_URI)) | ||
|
||
beforeEach(() => User.deleteMany()) | ||
|
||
it('succeeds on existing user and correct password', () => | ||
bcrypt.hash('123123123', 8) | ||
.then(hash => { | ||
return User.create({ username: 'monoloco', email: '[email protected]', password: hash, role: false }) | ||
.then(user => { | ||
return authenticateUser('monoloco', '123123123') | ||
.then(id => expect(id).to.equal(user.id)) | ||
}) | ||
}) | ||
) | ||
|
||
it('fails on non-existing user', () => { | ||
let error | ||
|
||
authenticateUser('monoloco', '123123123') | ||
.catch(_error => error = _error) | ||
.finally(() => { | ||
expect(error).to.be.instanceOf(NotFoundError) | ||
expect(error.message).to.equal('user not found') | ||
}) | ||
}) | ||
|
||
|
||
it('fails on wrong password', () => { | ||
let error | ||
|
||
return bcrypt.hash('123123123', 8) | ||
.then(hash => | ||
User.create({ name: 'Mono', surname: 'Loco', email: '[email protected]', username: 'monoloco', password: hash }) | ||
) | ||
.then(() => authenticateUser('monoloco', '111111111')) | ||
.catch(_error => error = _error) | ||
.finally(() => { | ||
expect(error).to.be.instanceOf(CredentialsError) | ||
expect(error.message).to.equal('wrong password') | ||
}) | ||
|
||
}) | ||
|
||
it('fails on non-string username', () => { | ||
let error | ||
|
||
try { | ||
authenticateUser(123, '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 { | ||
authenticateUser('', '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 { | ||
authenticateUser('monoloco', 123) | ||
} catch (_error) { | ||
error = _error | ||
} finally { | ||
expect(error).to.be.instanceOf(ValidationError) | ||
expect(error.message).to.equal('password is not a string') | ||
} | ||
}) | ||
|
||
it('fails on password short', () => { | ||
let error | ||
|
||
try { | ||
authenticateUser('monoloco', '132') | ||
} 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 password with spaces', () => { | ||
let error | ||
|
||
try { | ||
authenticateUser('monoloco', '123 123132') | ||
} catch (_error) { | ||
error = _error | ||
} finally { | ||
expect(error).to.be.instanceOf(ValidationError) | ||
expect(error.message).to.equal('password has empty spaces') | ||
} | ||
}) | ||
|
||
afterEach(() => User.deleteMany()) | ||
|
||
after(() => mongoose.disconnect()) | ||
}) |
16 changes: 16 additions & 0 deletions
16
staff/marti-herms/project/V-HUB/core/logic/authenticateUser.test.js
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'dotenv/config' | ||
import bcrypt from 'bcryptjs' | ||
|
||
import authenticateUser from './authenticateUser.js' | ||
|
||
import { User } from '../data/models.js' | ||
|
||
import mongoose from 'mongoose' | ||
|
||
mongoose.connect(process.env.MONGODB_URI) | ||
.then(() => bcrypt.hash('123123123', 8)) | ||
.then(hash => User.create({ username: 'eden', email: '[email protected]', password: hash })) | ||
.then(() => authenticateUser('eden', '123123123')) | ||
.then(userId => console.log('user authenticated', userId)) | ||
.catch(error => console.error(error)) | ||
.finally(() => mongoose.disconnect()) |