Skip to content

Commit

Permalink
add change email and password logic | changeEmail spec b00tc4mp#361
Browse files Browse the repository at this point in the history
  • Loading branch information
Abel Prieto committed Mar 2, 2024
1 parent 0544305 commit e629cda
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 11 deletions.
18 changes: 14 additions & 4 deletions staff/abel-prieto/PROYECT/API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

`Retrieve User`

- Request: GET /users Authorization: Bearer ${session.sessionUserId}
- Request: GET /users Authorization: Bearer ${session.token}
- Response: 200 "Content-Type": application/json { username, [ group ], [ role ] }
- Response (error) : 500|404|406 "Content-Type": application/json { error, message }

Expand All @@ -26,8 +26,18 @@
- Response: 200 "Content-Type": application/json { username, [ group ], [ role ] }
- Response (error) : 500|404|406 "Content-Type": application/json { error, message }

`Upload Files` ⚠️
`Upload Files`

- Request: POST /upload Authorization: Bearer ${session.sessionUserId}
- Request: POST /upload Authorization: Bearer ${session.token}
- Response: 201
- Response (error) : 500 "Content-Type": multipart/form-data { error, message }
- Response (error) : 500|404|406|409 "Content-Type": multipart/form-data { error, message }

`Change Email`
- Request: PATCH /users/email
- Response: 200
- Response (error) : 500|404|406|409 "Content-Type": application/json { error, message }

`Change Password`
- Request: PATCH /users/password
- Response: 200
- Response (error) : 500|404|406|409 "Content-Type": application/json { error, message }
2 changes: 1 addition & 1 deletion staff/abel-prieto/PROYECT/API/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const file = new Schema({
}
})

// ASSING GROUP & USER TYPE
// ASIGN GROUP & USER TYPE
user.pre('save', function (next) {
if (!this.group || this.group.length === 0) {
this.group.push('localhost');
Expand Down
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions staff/abel-prieto/PROYECT/API/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ mongoose.connect(process.env.URL_MONGODB_HIINIT_API)
// RETRIEVE GUEST
server.get('/users', retrieveGuestHandler)

// CHANGE USER EMAIL
server.patch('users/email', jsonBodyParser, changeUserEmailHandler)

// CHANGE USER PASSWORD
server.patch('users/password', jsonBodyParser, changeUserPasswordHandler)

// UPLOAD FILE
server.post('/upload', upload.single('file'), uploadFileHandler)

Expand Down
37 changes: 37 additions & 0 deletions staff/abel-prieto/PROYECT/API/logic/changeUserEmail.js
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
46 changes: 46 additions & 0 deletions staff/abel-prieto/PROYECT/API/logic/changeUserPassword.js
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
2 changes: 1 addition & 1 deletion staff/abel-prieto/PROYECT/API/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"jsonwebtoken": "^9.0.2",
"mocha": "^10.3.0",
"mongoose": "^8.1.2",
"multer": "1.4.5-lts.1",
"multer": "^1.4.5-lts.1",
"pnpm": "^8.15.2"
},
"type": "module"
Expand Down
6 changes: 3 additions & 3 deletions staff/abel-prieto/PROYECT/API/test/authenticateUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions staff/abel-prieto/PROYECT/API/test/changeUserEmail.spec.js
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 staff/abel-prieto/PROYECT/API/test/changeUserEmail.test.js
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.
2 changes: 1 addition & 1 deletion staff/abel-prieto/PROYECT/API/test/registerUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion staff/abel-prieto/PROYECT/API/test/uploadFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Binary file not shown.
Binary file removed staff/abel-prieto/PROYECT/API/uploads/coding.webp
Binary file not shown.
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 removed staff/abel-prieto/PROYECT/API/uploads/mern.png
Binary file not shown.
Binary file not shown.
Binary file removed staff/abel-prieto/PROYECT/API/uploads/pikachu.jpg
Binary file not shown.
Binary file not shown.

0 comments on commit e629cda

Please sign in to comment.