-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(user profile settings): view and edit user profile
- add view user profile endpoint - add edit user profile endpoint - add mocha test for both view and edit user profile endpoints - Solve Hound/Eslint erros [Starts #169817545]
- Loading branch information
1 parent
4a201ea
commit 8b2ffc4
Showing
11 changed files
with
348 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
eslint: | ||
enabled: true | ||
config_file: .eslintrc | ||
config_file: .eslintrc.json | ||
ignore_file: .eslintignore | ||
|
||
fail_on_violations: true |
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 ResponseService from '../services/response.service'; | ||
import UserService from '../services/user.service'; | ||
import JwtService from '../services/jwt.service'; | ||
/** | ||
* | ||
* | ||
* @class ProfileSettingsController | ||
*/ | ||
class ProfileSettingsController { | ||
/** | ||
* users can create an account | ||
* @static | ||
* @description POST /api/view-profile | ||
* @param {object} req request object | ||
* @param {object} res response object | ||
* @memberof ProfileSettingsController | ||
* @returns {object} ResponseService | ||
*/ | ||
static async viewProfile(req, res) { | ||
const userToken = JwtService.verifyToken(req.token); | ||
const userRegistered = await UserService.findUserByProperty({ email: userToken.email }); | ||
|
||
delete userRegistered.dataValues.password; | ||
delete userRegistered.dataValues.token; | ||
delete userRegistered.dataValues.isVerified; | ||
delete userRegistered.dataValues.createdAt; | ||
delete userRegistered.dataValues.updatedAt; | ||
|
||
ResponseService.setSuccess(200, 'User Profile Found', userRegistered); | ||
ResponseService.send(res); | ||
} | ||
|
||
|
||
/** | ||
* users can create an account | ||
* @static | ||
* @description POST /api/edit-profile | ||
* @param {object} req request object | ||
* @param {object} res response object | ||
* @memberof ProfileSettingsController | ||
* @returns {object} ResponseService | ||
*/ | ||
static async editProfile(req, res) { | ||
const userToken = JwtService.verifyToken(req.token); | ||
await Promise.all(Object.keys(req.body).map(async (key, index) => { | ||
await UserService.updateUser( | ||
{ email: userToken.email }, | ||
{ [key]: (Object.values(req.body)[index]).trim() } | ||
); | ||
})); | ||
const userData = await UserService.findUserByProperty({ email: userToken.email }); | ||
|
||
delete userData.dataValues.password; | ||
delete userData.dataValues.token; | ||
delete userData.dataValues.isVerified; | ||
delete userData.dataValues.createdAt; | ||
delete userData.dataValues.updatedAt; | ||
|
||
ResponseService.setSuccess(200, 'Profile Updated successfully', userData); | ||
ResponseService.send(res); | ||
} | ||
} | ||
|
||
export default ProfileSettingsController; |
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import express from 'express'; | ||
import authRoute from './auth.route'; | ||
import tripRoute from './trip.route'; | ||
import profileSettingsRoute from './profile-settings.route'; | ||
|
||
const app = express(); | ||
|
||
app.use('/api/auth', authRoute); | ||
app.use('/api/', tripRoute); | ||
app.use('/api/', profileSettingsRoute); | ||
|
||
export default app; |
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,13 @@ | ||
import express from 'express'; | ||
import ProfileSettingsController from '../controllers/profile-settings.controller'; | ||
import RouteAccessMiddleware from '../middlewares/route-access.middleware'; | ||
import { validateToken } from '../validations/auth.validation'; | ||
import { | ||
validateAccountProfile | ||
} from '../validations/account-profile.validation'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/view-profile', RouteAccessMiddleware.checkRouteAccess, validateToken, ProfileSettingsController.viewProfile); | ||
router.patch('/edit-profile', RouteAccessMiddleware.checkRouteAccess, validateToken, validateAccountProfile, ProfileSettingsController.editProfile); | ||
export default router; |
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../../app'; | ||
import { | ||
signedUpUserToken, | ||
wrongToken, | ||
userData | ||
} from '../fixtures/users.fixture'; | ||
|
||
chai.use(chaiHttp); | ||
chai.should(); | ||
|
||
describe('Test for User Account Profile:', () => { | ||
it('It should allow user to view profile', (done) => { | ||
chai.request(app) | ||
.get('/api/view-profile') | ||
.set('Authorization', signedUpUserToken) | ||
.end((err, res) => { | ||
res.body.should.be.an('object'); | ||
res.should.have.status(200); | ||
res.body.should.have.property('message').equal('User Profile Found'); | ||
res.body.should.have.property('data'); | ||
res.body.data.should.have.property('firstName'); | ||
res.body.data.should.have.property('lastName'); | ||
res.body.data.should.have.property('gender'); | ||
res.body.data.should.have.property('birthDate'); | ||
res.body.data.should.have.property('preferredLanguage'); | ||
res.body.data.should.have.property('preferredCurrency'); | ||
res.body.data.should.have.property('residence'); | ||
res.body.data.should.have.property('department'); | ||
res.body.data.should.have.property('lineManager'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('It should NOT allow user to view profile: Wrong Token Provided', (done) => { | ||
chai.request(app) | ||
.get('/api/view-profile') | ||
.set('Authorization', wrongToken) | ||
.end((err, res) => { | ||
res.body.should.be.an('object'); | ||
res.should.have.status(403); | ||
res.body.should.have.property('message').equal('Wrong Token Provided'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('It should allow user to edit profile', (done) => { | ||
chai.request(app) | ||
.patch('/api/edit-profile') | ||
.set('Authorization', signedUpUserToken, 'Accept', 'application/json') | ||
.send(userData) | ||
.end((err, res) => { | ||
res.body.should.be.an('object'); | ||
res.should.have.status(200); | ||
res.body.should.have.property('message').equal('Profile Updated successfully'); | ||
res.body.should.have.property('data'); | ||
res.body.data.should.have.property('id'); | ||
res.body.data.should.have.property('firstName'); | ||
res.body.data.should.have.property('lastName'); | ||
res.body.data.should.have.property('email'); | ||
res.body.data.should.have.property('gender'); | ||
res.body.data.should.have.property('birthDate'); | ||
res.body.data.should.have.property('preferredLanguage'); | ||
res.body.data.should.have.property('preferredCurrency'); | ||
res.body.data.should.have.property('residence'); | ||
res.body.data.should.have.property('department'); | ||
res.body.data.should.have.property('lineManager'); | ||
res.body.data.should.have.property('role'); | ||
done(); | ||
}); | ||
}); | ||
it('It should NOT allow user to edit profile: Wrong input value', (done) => { | ||
const userGender = ''; | ||
userData.gender = userGender; | ||
chai.request(app) | ||
.patch('/api/edit-profile') | ||
.set('Authorization', signedUpUserToken, 'Accept', 'application/json') | ||
.send(userData) | ||
.end((err, res) => { | ||
res.body.should.be.an('object'); | ||
res.should.have.status(400); | ||
res.body.should.have.property('message'); | ||
res.body.message.should.be.an('array'); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.