forked from b00tc4mp/isdi-bootcamp-202409
-
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.
populate data model and testing of register/authenticate user b00tc4m…
- Loading branch information
Showing
32 changed files
with
345 additions
and
130 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
37 changes: 37 additions & 0 deletions
37
staff/rafael-infante/lovingHands/api/logic/authenticateUser.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,37 @@ | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { User } from 'dat' | ||
import { validate, errors } from 'com' | ||
const { CredentialsError, SystemError } = errors | ||
|
||
export default (email, password) => { | ||
validate.email(email) | ||
validate.password(password) | ||
|
||
return (async () => { | ||
let user | ||
|
||
try { | ||
user = await User.findOne({ email }) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!user) throw new CredentialsError('wrong credentials') | ||
|
||
let match | ||
|
||
try { | ||
match = await bcrypt.compare(password, user.password) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!match) throw new CredentialsError('wrong credentials') | ||
|
||
return { | ||
id: user._id.toString(), | ||
role: user.role, | ||
} | ||
})() | ||
} |
42 changes: 42 additions & 0 deletions
42
staff/rafael-infante/lovingHands/api/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,42 @@ | ||
import 'dotenv/config' | ||
import * as chai from 'chai' | ||
import chaiAsPromised from 'chai-as-promised' | ||
import bcrypt from 'bcryptjs' | ||
|
||
chai.use(chaiAsPromised) | ||
const { expect } = chai | ||
import db, { User } from 'dat' | ||
import { errors } from 'com' | ||
|
||
const { CredentialsError } = errors | ||
|
||
import authenticateUser from './authenticateUser.js' | ||
|
||
describe('authenticateUser', () => { | ||
before(() => db.connect(process.env.MONGO_URL_TEST)) | ||
|
||
beforeEach(() => User.deleteMany()) | ||
|
||
it('succeeds on existing user', async () => { | ||
await User.create({ | ||
name: 'Alba Cete', | ||
email: '[email protected]', | ||
password: bcrypt.hashSync('123123123', 10), | ||
}) | ||
|
||
const user = await authenticateUser('[email protected]', '123123123') | ||
expect(user).to.exist | ||
expect(user.id).to.be.a.string | ||
expect(user.role).to.equal('caregiver') | ||
expect(user.id).to.have.lengthOf(24) | ||
}) | ||
|
||
it('fails on non-existing user', () => | ||
expect( | ||
(async () => { | ||
await authenticateUser('[email protected]', '123123123') | ||
})() | ||
).to.be.rejectedWith(CredentialsError, /^wrong credentials$/)) | ||
|
||
after(() => db.disconnect()) | ||
}) |
15 changes: 15 additions & 0 deletions
15
staff/rafael-infante/lovingHands/api/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,15 @@ | ||
import 'dotenv/config' | ||
import db from 'dat' | ||
|
||
import authenticateUser from './authenticateUser.js' | ||
|
||
await db.connect(process.env.MONGO_URL_TEST) | ||
|
||
try { | ||
const result = await authenticateUser('[email protected]', '123123123') | ||
console.log(result) | ||
} catch (error) { | ||
console.error(error) | ||
} finally { | ||
await db.disconnect() | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import registerUser from "./registerUser.js"; | ||
import authenticateUser from './authenticateUser.js' | ||
import registerUser from './registerUser.js' | ||
|
||
const logic = { | ||
registerUser | ||
registerUser, | ||
authenticateUser, | ||
} | ||
|
||
export default logic | ||
export default logic |
26 changes: 14 additions & 12 deletions
26
staff/rafael-infante/lovingHands/api/logic/registerUser.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 |
---|---|---|
@@ -1,26 +1,28 @@ | ||
import bcrypt from 'bcryptjs' | ||
|
||
import {User} from "../../dat/index.js" | ||
import { User } from '../../dat/index.js' | ||
import { validate, errors } from 'com' | ||
|
||
const {DuplicityError, SystemError} = errors | ||
const { DuplicityError, SystemError } = errors | ||
|
||
export default (name, email, username, password, passwordRepeat) => { | ||
export default (name, email, password, passwordRepeat) => { | ||
validate.name(name) | ||
validate.email(email) | ||
validate.username(username) | ||
validate.password(password) | ||
validate.passwordsMatch(password, passwordRepeat) | ||
|
||
return bcrypt.hash(password, 10) | ||
.catch(error => {throw new SystemError(error.message)}) | ||
.then(hash => | ||
User.create({name, email, username, password: hash}) | ||
.then(_ => {}) | ||
.catch(error => { | ||
return bcrypt | ||
.hash(password, 10) | ||
.catch((error) => { | ||
throw new SystemError(error.message) | ||
}) | ||
.then((hash) => | ||
User.create({ name, email, password: hash }) | ||
.then((_) => {}) | ||
.catch((error) => { | ||
if (error.code === 11000) throw new DuplicityError('user already exists') | ||
|
||
throw new SystemError(error.message) | ||
}) | ||
) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
staff/rafael-infante/lovingHands/api/logic/registerUser.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,64 @@ | ||
import 'dotenv/config' | ||
import * as chai from 'chai' | ||
import chaiAsPromised from 'chai-as-promised' | ||
import bcrypt from 'bcryptjs' | ||
|
||
chai.use(chaiAsPromised) | ||
const { expect } = chai | ||
|
||
import db, { User } from 'dat' | ||
import { errors } from 'com' | ||
|
||
const { DuplicityError, ValidationError } = errors | ||
|
||
import registerUser from './registerUser.js' | ||
|
||
describe('registerUser', () => { | ||
before(() => db.connect(process.env.MONGO_URL_TEST)) | ||
|
||
beforeEach(() => User.deleteMany()) | ||
afterEach(() => User.deleteMany()) | ||
|
||
it('succeds on new user', async () => { | ||
await registerUser('Rive Lino', '[email protected]', '123123123', '123123123') | ||
|
||
const user = await User.findOne({ email: '[email protected]' }) | ||
|
||
expect(user).to.exist //.not.to.be.null | ||
expect(user.name).to.equal('Rive Lino') | ||
expect(user.email).to.equal('[email protected]') | ||
expect(bcrypt.compareSync('123123123', user.password)).to.be.true | ||
}) | ||
|
||
it('fails on existing user', () => | ||
expect( | ||
(async () => { | ||
await User.create({ name: 'Casi Miro', email: '[email protected]', password: bcrypt.hashSync('123123123', 10) }) | ||
|
||
await registerUser('Casi Miro', '[email protected]', '123123123', '123123123') | ||
})() | ||
).to.be.rejectedWith(DuplicityError, 'user already exists')) | ||
|
||
it('fails on existing user', () => | ||
expect( | ||
(async () => { | ||
await registerUser(undefined, '[email protected]', '123123123', '123123123') | ||
})() | ||
).to.be.rejectedWith(ValidationError, 'Invalid name')) | ||
|
||
it('fails on existing email', () => | ||
expect( | ||
(async () => { | ||
await registerUser('Seño Rito', undefined, '123123123', '123123123') | ||
})() | ||
).to.be.rejectedWith(ValidationError, 'Invalid email')) | ||
|
||
it('fails when passwords do not match', () => | ||
expect( | ||
(async () => { | ||
await registerUser('Seño Rito', 'señ[email protected]', '123123124', '123123123') | ||
})() | ||
).to.be.rejectedWith(ValidationError, 'passwords do not match')) | ||
|
||
after(() => db.disconnect()) | ||
}) |
35 changes: 22 additions & 13 deletions
35
staff/rafael-infante/lovingHands/api/logic/registerUser.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 |
---|---|---|
@@ -1,17 +1,26 @@ | ||
import 'dotenv/config' | ||
import db from '../../dat/index.js' | ||
import db from 'dat' | ||
|
||
import registerUser from './registerUser.js' | ||
|
||
db.connect(process.env.MONGO_URL_TEST) | ||
.then(() => { | ||
try { | ||
return registerUser('Rive Lino', '[email protected]', 'rivelino', '123123123', '123123123') | ||
.then(console.log) | ||
.catch(console.error) | ||
} catch (error) { | ||
console.error() | ||
} | ||
}) | ||
.catch(console.error) | ||
.finally(() => db.disconnect()) | ||
await db.connect(process.env.MONGO_URL_TEST) | ||
|
||
try { | ||
const result = await registerUser('Marga Rita', '[email protected]', '123123123', '123123123') | ||
console.log(result) | ||
} catch (error) { | ||
console.error(error) | ||
} finally { | ||
await db.disconnect() | ||
} | ||
|
||
// db.connect(process.env.MONGO_URL_TEST) | ||
// .then(() => { | ||
// try { | ||
// return registerUser('Rive Lino', '[email protected]', '123123123', '123123123').then(console.log).catch(console.error) | ||
// } catch (error) { | ||
// console.error() | ||
// } | ||
// }) | ||
// .catch(console.error) | ||
// .finally(() => db.disconnect()) |
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
35 changes: 35 additions & 0 deletions
35
staff/rafael-infante/lovingHands/api/test/authenticate-user.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,35 @@ | ||
import { errors } from 'com' | ||
|
||
const { SystemError } = errors | ||
|
||
fetch('http://localhost:8080/users/auth', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
email: '[email protected]', | ||
password: '123123123', | ||
}), | ||
}) | ||
.catch((error) => { | ||
throw new SystemError(error.message) | ||
}) | ||
.then((res) => { | ||
if (res.ok) | ||
return res | ||
.json() | ||
.catch((error) => { | ||
throw new SystemError(error.message) | ||
}) | ||
.then((token) => { | ||
console.log(token) | ||
}) | ||
|
||
return res | ||
.json() | ||
.catch((error) => { | ||
throw new SystemError(error.message) | ||
}) | ||
.then(({ error, message }) => { | ||
throw new errors[error](message) | ||
}) | ||
}) |
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 @@ | ||
curl -H 'Content-Type: application/json' -d '{"email":"[email protected]", "password":"123123123"}' http://localhost:8080/users/auth -v |
Oops, something went wrong.