forked from b00tc4mp/isdi-parttime-202309
-
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 change email and password logic | changeEmail spec b00tc4mp#361
- Loading branch information
Abel Prieto
committed
Mar 2, 2024
1 parent
0544305
commit e629cda
Showing
24 changed files
with
161 additions
and
11 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
Empty file.
Empty file.
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,37 @@ | ||
import bcrypt from 'bcrypt' | ||
import { User } from '../data/models.js' | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError, CredentialsError } = errors | ||
|
||
function changeUserEmail(userId, newEmail, password, againPassword) { | ||
validate.email(newEmail, 'New email') | ||
validate.password(password, 'Password') | ||
validate.password(againPassword, 'Repeat password') | ||
|
||
return User.findById(userId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) { | ||
throw new NotFoundError('User not found. Try again') | ||
} | ||
|
||
return bcrypt.compare(password, user.password) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(match => { | ||
if (!match) { | ||
throw new CredentialsError('Wrong credentials. Try again') | ||
} | ||
|
||
if (password !== againPassword) { | ||
throw new CredentialsError('Passwords do not match. Try again') | ||
} | ||
|
||
user.email = newEmail | ||
|
||
return user.save() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
}) | ||
}) | ||
} | ||
|
||
export default changeUserEmail |
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,46 @@ | ||
import bcrypt from 'bcrypt' | ||
import { User } from '../data/models.js' | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError, CredentialsError } = errors | ||
|
||
function changeUserPassword(userId, password, newPassword, againNewPassword) { | ||
validate.password(password, 'Password') | ||
validate.password(newPassword, 'New password') | ||
validate.password(againNewPassword, 'Repeat new password') | ||
|
||
return User.findById(userId) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) { | ||
throw new NotFoundError('User not found. Try again') | ||
} | ||
|
||
return bcrypt.compare(password, user.password) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(match => { | ||
if (!match) { | ||
throw new CredentialsError('Wrong credentials. Try again') | ||
} | ||
|
||
if (newPassword !== againNewPassword) { | ||
throw new CredentialsError('Wrong credentials with new password') | ||
} | ||
|
||
return bcrypt.hash(newPassword, 5) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(hash => { | ||
|
||
user.password = hash | ||
|
||
return user.save() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
}) | ||
|
||
|
||
}) | ||
|
||
}) | ||
|
||
} | ||
|
||
export default changeUserPassword |
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ describe('authenticateUser', () => { | |
// POSITIVE CASE | ||
it('success on authenticate user', async () => { | ||
const username = new ObjectId().toString() | ||
const email = new ObjectId().toString() | ||
const email = '[email protected]' | ||
const password = new ObjectId().toString() | ||
|
||
const hash = await bcrypt.hash(password, 5) | ||
|
@@ -30,7 +30,7 @@ describe('authenticateUser', () => { | |
|
||
// NEGATIVE CASE - User not found | ||
it('fails on user not found', async () => { | ||
const email = new ObjectId().toString() | ||
const email = '[email protected]' | ||
const password = new ObjectId().toString() | ||
|
||
try { | ||
|
@@ -45,7 +45,7 @@ describe('authenticateUser', () => { | |
// NEGATIVE CASE - Wrong credentials | ||
it('fails on wrong credentials with password', async () => { | ||
const username = new ObjectId().toString() | ||
const email = new ObjectId().toString() | ||
const email = '[email protected]' | ||
const password = new ObjectId().toString() | ||
|
||
const hash = await bcrypt.hash(password, 5) | ||
|
37 changes: 37 additions & 0 deletions
37
staff/abel-prieto/PROYECT/API/test/changeUserEmail.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,37 @@ | ||
import mongoose, { Types } from 'mongoose' | ||
import bcrypt from 'bcrypt' | ||
import dotenv from 'dotenv' | ||
import expect from 'mocha' | ||
import changeUserEmail from '../logic/changeUserEmail.js' | ||
import { errors } from 'com' | ||
import { User } from '../data/models.js' | ||
const { ObjectId } = Types | ||
const { NotFoundError, CredentialsError } = errors | ||
|
||
dotenv.config() | ||
|
||
describe('changeUserEmail', () => { | ||
before(() => mongoose.connect(process.env.URL_MONGODB_TEST)) | ||
|
||
beforeEach(() => User.deleteMany()) | ||
|
||
// POSTIVE CASE | ||
it('success with changing email user', async () => { | ||
const username = new ObjectId().toString() | ||
const email = '[email protected]' | ||
const password = new ObjectId().toString() | ||
const againNewPassword = password | ||
|
||
const newEmail = '[email protected]' | ||
|
||
const hash = await bcrypt.hash(password, 5) | ||
|
||
const user = await User.create({ username, email, password: hash, group: 'localhost', role: 'user' }) | ||
const userChanged = await changeUserEmail(user.id, newEmail, password, againNewPassword) | ||
|
||
expect(userChanged).to.be.undefined | ||
|
||
}) | ||
|
||
after(() => mongoose.disconnect()) | ||
}) |
14 changes: 14 additions & 0 deletions
14
staff/abel-prieto/PROYECT/API/test/changeUserEmail.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,14 @@ | ||
import mongoose from 'mongoose' | ||
import changeUserEmail from '../logic/changeUserEmail.js' | ||
|
||
mongoose.connect('mongodb://127.0.0.1:27017/hiinit') | ||
.then(() => { | ||
try { | ||
changeUserEmail('65d7ad5a0477fa56b47795bc', '[email protected]', '123123123', '123123123') | ||
.then(() => console.log('Email changed successfully!')) | ||
.catch(error => console.error(error)) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
}) | ||
.catch(error => console.error(error)) |
Empty file.
Empty file.
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ describe('registerUser', () => { | |
// POSITIVE CASE | ||
it('success on register a new user type', async () => { | ||
const username = new ObjectId().toString() | ||
const email = new ObjectId().toString() | ||
const email = '[email protected]' | ||
const password = new ObjectId().toString() | ||
|
||
await registerUser(username, email, password) | ||
|
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ describe('uploadFiles', () => { | |
// POSITIVE CASE | ||
it('success with uploading user file', async () => { | ||
const username = new ObjectId().toString() | ||
const email = new ObjectId().toString() | ||
const email = '[email protected]' | ||
const password = new ObjectId().toString() | ||
|
||
const fileName = new ObjectId().toString() | ||
|
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+11.1 KB
...80x1800-dark-apple-mac-pro-macbook-pro-retina-backgrounds-and-macbook-black.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.